var view = {
	//	[per page, per row, width, height]
	large: [12, 2, 210, ''],
	med:   [18, 3, 120, ''],
	small: [24, 4, 110, '']
};

var pl = {
	params: params,
	pl_imgpath: baseDir+'themes/prestashop/img/box/',
	xmlsrc: pl_label['xmlsrc'],
	imgpath: '',
	loading1: baseDir+'themes/prestashop/img/box/ajax_loader.gif',
	loading2: baseDir+'themes/prestashop/img/box/ajax_loader2.gif',
	noimage: 'noimage.gif',
	sortby: 'id_product',
	sort_dir: 'desc',
	limitstart: 0,
	view: 'med',
	result: 0,
	total: 0,
	debug_mode: pl_debug
};

$(function(){
	if(pl.debug_mode == 1) {
		$('#pl_container').append('<div id="pl_debug"></div>');
	}
	retrieveContent = function(pl_reload){
		setSelectedCookie('read',pl_reload);
		var data_params = pl.params+"&task=showproducts&view="+pl.view+"&sort="+pl.sortby+"&sortdir="+pl.sort_dir+"&limitstart="+pl.limitstart+"&limit="+view[pl.view][0]+'&token=' + static_token;
		if(pl_reload==true){
			$(".pl_menu").html(showPLmenu());
		}
		pl.xmlsrc = pl_label['xmlsrc'];
		$.ajax({
			type: 'GET',
			url: pl.xmlsrc,
			async: true,
			cache: false,
			dataType : "json",
			data: data_params,
			beforeSend: function(){
				$("#pl_content").html('<div id="pl_loading"></div>');
				$("#pl_loading").html('<img src="'+pl.loading2+'" border="0" alt="Loading">');
			},
			error: function(x){
				$("#pl_content").html('&nbsp;'+'<img src="./img/warning.jpg"/>Alert!<br/>Failed to get data,This page will reload after you refresh.<br/>if not,please contact us,Thank!');
			},
			success: function(jsonData)
			{
				parseData(jsonData, pl_reload);
			}
		});

		//		if(pl.debug_mode == 1) {
		//			$('#pl_debug').html('Request URl: ' + pl.xmlsrc + '?' + data_params);
		//		}
	};
	parseData = function(jsonData, pl_reload){

		if(jsonData.noproduct)
		{
			$("#pl_content").html('no product here!');
			return false;
		}
		//showPLcontent(jsonData.products);
		$(jsonData.products).each(function(){
			if (this.id != undefined)
			{
			}
		});

		pl.sortby = jsonData.sort;
		pl.sort_dir = jsonData.sortdir;
		pl.limitstart = jsonData.limitstart;
		pl.view = jsonData.view;
		pl.result = jsonData.result;
		pl.total = jsonData.total;
		setSelectedCookie('set',pl_reload);
		//pl.xmlsrc =jsonData.xmlsrc;
		if(pl_reload==true){

			initPL_slider();
			setPLmenuData();
		}

		setPagenav();
		setSelectedView();
		setSortValue();
		showPLcontent(jsonData.products);
	};

	initPL_slider = function() {
		var steps = Math.ceil(parseInt(pl.total)/parseInt(view[pl.view][0]))-1;
		var contentwidth = (parseInt($("#pl_content").innerWidth())+100);
		if(!steps)
		steps=1;
		$(".pl_slider").slider({
			min: 0,
			max: (steps*parseInt(view[pl.view][0])),
			handle: ".pl_handle",
			step: steps,
			startValue:parseInt(pl.limitstart),
			change: function(event, ui){
				var dir = "";
				if(ui.value > pl.limitstart) dir = "-";
				pl.limitstart = ui.value;

				$("#pl_content").animate({
					left: dir+contentwidth+"px",
					opacity: 0
				}, 500, 'linear', function(){
					$("#pl_content").css({left:"0px", opacity:1});
					retrieveContent(false);
				}
				);
			}
		});
		if(steps < 1){
			$(".pl_slider").slider("disable");
		}
	};

	setPLmenuData = function(){
		var steps = Math.ceil(parseInt(pl.total)/parseInt(view[pl.view][0]))-1;

		$(".pl_prev").click(function(){
			if(pl.limitstart > 0){
				$(".pl_slider").slider("moveTo", "-="+view[pl.view][0]);
			}
		});
		$(".pl_next").click(function(){
			if(steps > 0){
				if(pl.limitstart < (parseInt(view[pl.view][0])*steps)){
					$(".pl_slider").slider("moveTo", "+="+view[pl.view][0]);
				}
			}
		});

		$('td.pl_viewnum a').click(function() {
			if(this.title != pl.view){
				$('td.pl_viewnum a').removeClass("pl_viewnum_sel");
				$(this).addClass("pl_viewnum_sel");
				pl.view = this.title;
				pl.limitstart = 0;
				setSelectedCookie('set',false);
				retrieveContent(true);
			}
			$(this).blur();
		});
		$('td.pl_viewbuttons a').click(function() {
			if(this.title != pl.view){
				pl.view = this.title;
				pl.limitstart = 0;
				setSelectedCookie('set',false);
				retrieveContent(true);
			}
			$(this).blur();
		});

		$(".pl_sort").change(function(){
			pl.sortby = this.value;
			pl.limitstart = 0;
			setSelectedCookie('set',false);
			retrieveContent(true);
		});

		$(".pl_sortdir").click(function(){
			if(this.title == 'asc'){
				pl.sort_dir = "desc";
				$(this).attr({title: "desc"});
				$("img", this).attr({src: pl.pl_imgpath+"desc.gif"});
			}else{
				pl.sort_dir = "asc";
				$(this).attr({title: "asc"});
				$("img", this).attr({src: pl.pl_imgpath+"asc.gif"});
			}
			pl.limitstart = 0;
			setSelectedCookie('set',false);
			retrieveContent(true);
			$(this).blur();
		});
	};
	createImage = function(obj) {
		var newimg = new Image();
		for(var i in obj){
			$(newimg).attr(i, obj[i]);
		}
		$(newimg).load();

		return newimg;
	};

	setPagenav = function() {
		var pagenav = ' ' + pl_label['OF'] + ' ' + pl.total;
		var result = pl.result;
		var total = pl.total;
		var limit = view[pl.view][0];
		var limitstart = pl.limitstart;
		//pagenav = (total > 0 ? (parseInt(limitstart)+1) : '0') + ' - ' + (parseInt(result)+parseInt(limitstart)) + pagenav;
		pagenav = parseInt(parseInt(limitstart/limit)+1)+'/'+parseInt(parseInt(total/limit)+1)+'  pages';
		$(".page_result").html(pagenav);
	}

	setSortValue = function() {
		$(".pl_sort").val(pl.sortby);
	};

	setSelectedView = function() {
		var display = view[pl.view][0];

		$('td.pl_viewnum').find('a').each(function(){
			if($(this).text() == display){
				$(this).addClass("pl_viewnum_sel");
			}else{
				$(this).removeClass("pl_viewnum_sel");
			}
		});
	}
	setSelectedCookie = function(type,boolvalue){
		var arr;
		if(type=="set"){
			var date=new Date();
			date.setTime(date.getTime()+3600*1000);
			arr=[pl.limitstart,pl.view,pl.xmlsrc,pl.sortby,pl.sort_dir];
			document.cookie="boxstatus=" + arr+";expire="+date.toGMTString();
		}else{
			if(arrs=getCookie("boxstatus"))
			{
				arr=arrs.split(",");
				if(pl.xmlsrc==arr[2]){
					if(arr[0]!=0&&boolvalue){
						pl.limitstart = arr[0];
					}
					if(arr[1]!='med')
					{
						pl.view = arr[1];
					}
					if(arr[3]!='name')
					{
						pl.sortby = arr[3];
					}
					if(arr[4]!='asc')
					{
						pl.sort_dir = arr[4];
					}
				}
			}
		}
	}
	getCookie = function(str){
		var tmp,reg=new RegExp("(^| )"+str+"=([^;]*)(;|$)","gi");
		if(tmp=reg.exec(document.cookie))return(tmp[2]);
		return false;
	}

	retrieveContent(true);
});