var userAds = {
	sort_field: "publish_date", //price
	sort_order: "desc", //desc|asc
	start: 0,
	limit: 10,
	page: 1,
	current_page: 1,
	last_page: 1,
	return_type: "json", // json only,
	url: baseURL+"ads/ajax_get_ads/category/"+category+"/",
	ads:{},
	html_string:[],
	total_rows: 0,
	set_sort_field: function(field){
		this.sort_field = field;
	},
	set_sort_order: function(order){
		if(order == "asc" || order=="desc"){
			this.sort_order = order;
		}
	},
	set_start: function(nr){
		if(!isNaN(nr)){
			this.start = nr;
		}
	},
	set_limit: function(nr){
		if(!isNaN(nr)){
			this.limit = nr;
		}
	},
	set_page: function(nr){
		if(!isNaN(nr)){
			this.page = nr;
		}
	},
	set_return_type: function(type){
		this.return_type = type;
	},
	set_url: function(url){
		this.url = url;
	},
	init: function(params){
		for(i in params){
			if(eval("this.set_"+i)){
				eval("this.set_"+i+"('"+params[i]+"')");
			}
		}
	},
	set_get_params: function(){
		return "type/"+this.return_type+"/sort_field/"+this.sort_field+"/sort_order/"+this.sort_order+"/page/"+this.page+"/limit/"+this.limit+"/randomnr/"+Math.random();
	},
	show_ads: function(){
		var x = 0;
		var z = 1;
		userAds.html_string = new Array();
		$.each(userAds.ads,function(i,current_ad){
			var ad = current_ad;
			if(ad.title){
				z++;
				userAds.html_string[x++] = '<tr>';
				if(!ad.image_url){
					userAds.html_string[x++] = '<td class="col2"><div class="center_thumbs"><div class="cropped_list_thumbs"><img src="'+baseURL+'images/no_picture.jpg" width="50" height="37" alt="" /></div></div></td>';
				}else{
					ad.image_url_resized = '';
					if (ad.image_url.indexOf('cache') != -1) {
						ad.image_url_resized = ad.image_url + '/operation=resize&width=50';
					} else {
						ad.image_url_resized = ad.image_url + '/width/50';
					}
					userAds.html_string[x++] = '<td class="col2"><div class="center_thumbs"><div class="cropped_list_thumbs"><img src="'+ad.image_url_resized+'" alt="" /></div></div></td>';
				}
				userAds.html_string[x++] = '<td class="col3"><a href="'+ad.item_url+'">'+ad.title+"</a></td>";
				userAds.html_string[x++] = '<td class="col4">'+ad.category+'</td>';
				userAds.html_string[x++] = '<td class="col5">'+ad.price+' '+ad.currency.toUpperCase()+'</td>';
				userAds.html_string[x++] = '<td class="col6">'+ad.publish_date+'</td>';
				userAds.html_string[x++] = '</tr>';
			}
		});

		$("#ads_holder").html(userAds.html_string.join(""));
		userAds.color_rows();
	},
	color_rows: function(){
		$("#ads_holder tr").removeClass("odd");
		$("#ads_holder tr:even").addClass("odd");
	},
	recreate_pagination: function(){
		$.get(baseURL+'ads/redo_pagination/'+this.page+'/'+this.total_rows,function(data){
			$(".pagination").remove();
			$("#ads_holder").after(data);
			userAds.set_pagination_actions();
		})
	},
	select_user_ads: function(){
		my_obj = this;
		$.get(this.url+this.set_get_params(),function(data){
			if(data != ""){
				my_obj.ads= eval("("+data+")");
				if(my_obj.ads){
					my_obj.show_ads();
					my_obj.recreate_pagination();
				}
			}
		})
	},
	get_instance: function(){
		return this;
	},
	set_sort_actions: function(){
		$("#ads_header a").click(function(){
			var el = $(this);
			var sort_field = el.parent("th").attr("id").replace("th_","");
			var sort_order = "asc";
			if(el.attr("title") == "descrescator"){
				sort_order = "desc";
			}
			userAds.set_sort_field(sort_field);
			userAds.set_sort_order(sort_order);
			userAds.select_user_ads();
			var img_el = el.children("img");
			userAds.reset_arrows();
			if(sort_order == "asc"){
				img_el.attr("src",baseURL+"images/sortUp_active.png");
			} else {
				img_el.attr("src",baseURL+"images/sortDown_active.png");
			}
		});
	},
	reset_arrows: function(){
		var elements = $("#ads_header a").each(function(){
			sort_order = $(this).attr("title");
			if(sort_order == "descrescator"){
				$(this).children("img").attr("src",baseURL+"images/sortDown.png");
			} else {
				$(this).children("img").attr("src",baseURL+"images/sortUp.png");
			}
		});
	},
	set_pagination_actions: function(){
		$(".pagination a").click(function(event){
			userAds.current_page = parseInt($(".pagination a.curent").html());
			userAds.last_page = parseInt($(".pagination > a:last").html());
			$(".pagination a").removeClass("curent");
			var pag = $(this).html();
			if(pag == "»"){
				if(userAds.current_page < userAds.last_page){
					pag = parseInt(userAds.current_page)+1;
				}else{
					pag = userAds.last_page;
				}
			}
			if(pag == "«"){
				if(userAds.current_page>1){
					pag = parseInt(userAds.current_page)-1;
				}else{
					pag = 1;
				}
			}
			userAds.set_page(pag);
			$(".pagination a").each(function(){
				if($(this).html() == userAds.page){
					$(this).addClass("curent");
					return false;
				}
			});
			userAds.select_user_ads();
			event.preventDefault();
		})
	}
};
$().ready(function(){
	userAds.total_rows = totalRows;
	userAds.select_user_ads();
	userAds.set_sort_actions();
	//userAds.set_pagination_actions();
});

