var view = {
	//	[col, img width, img height]
	grid: [2, 140, ''],
	list: [1, 140, '']
};

var display = [4, 6, 10];
var default_display = display[1];
var default_view = 'grid';
		
var pl = {
	params: params,
	pl_imgpath: 'images/pl_images/',
	xmlsrc: 'az_pl_src.php',
	loading1: 'images/pl_images/ajax_loader.gif',
	loading2: 'images/pl_images/ajax_loader2.gif',
	noimage: 'noimage.gif',
	imgpath: '',
	sortby: 'name',
	sort_dir: 'asc',
	limitstart: 0,
	limit: default_display,
	view: default_view,
	max_count: 6,
	result: 0,
	total: 0,
	debug_mode: pl_debug
};

$(function(){
	if(pl.debug_mode == 1) {
		$('#pl_container').append('<div id="pl_debug"></div>');
	}
	
	$("ul.az_tabs li").click(
		function() {			
			$("ul.az_tabs li").removeClass("az_tabs_sel");
			$(this).addClass("az_tabs_sel");
			pl.params = pl.params.replace(/page=+[a-zA-Z0-9-_\s].+\.php/, 'page=' + this.id);
			$("a", this).blur();
			pl.limit = default_display;
			pl.view = default_view;
			pl.limitstart = 0;
			retrieveContent(true);
		}
	);
	retrieveContent = function(pl_reload){
		var data_params = pl.params+"&task=showproducts&sort="+pl.sortby+"&sortdir="+pl.sort_dir+"&limitstart="+pl.limitstart+"&limit="+pl.limit;
		
		if(pl_reload==true){
			$("#pl_menu").html(showPLmenu());
			// Remove comment to show bottom menu
			//$("#pl_menuBottom").html(showPLmenu());
		}
		$.ajax({
			url: pl.xmlsrc,
			type: "get",
			dataType: "xml",
			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(){
				$("#pl_content").html('&nbsp;'+pl_label['ERROR1']);
			},
			success: function(response){
				parseData(response);
			}
		});
		
		if(pl.debug_mode == 1) {
			$('#pl_debug').html('Request URl: ' + pl.xmlsrc + '?' + data_params);
		}
	};
	
	parseData = function(xml) {
		var pl_data = new Array();
		var gallery = $(xml).find("gallery");
		
		pl.sortby = gallery.attr("sortby");
		pl.sort_dir = gallery.attr("sortdir");
		pl.limitstart = gallery.attr("limitstart");
		pl.result = gallery.attr("result");
		pl.total = gallery.attr("total");
		pl.imgpath = gallery.attr("path");
		
		$("product", xml).each(function(i){
			pl_data[i] = new Array();
			pl_data[i]['id'] = $("id", this).text() ? $("id", this).text() : 0;
			pl_data[i]['title'] = $("title", this).text() ? $("title", this).text() : "no data";
			pl_data[i]['desc'] = $("description", this).text()? $("description", this).text() : "no data";
			pl_data[i]['image'] = $("image", this).text() ? $("image", this).text() : pl.noimage;
			pl_data[i]['price'] = $("price", this).text() ? $("price", this).text() : 0;
			pl_data[i]['sprice'] = $("sprice", this).text() ? $("sprice", this).text() : 0;
			pl_data[i]['cart'] = $("cart", this).text() ? $("cart", this).text() : 0;
			pl_data[i]['attribute'] = $("attribute", this).text() ? $("attribute", this).text() : '';
			pl_data[i]['attr_status'] = $("attribute", this).attr("status") ? $("attribute", this).attr("status") : 0;
		});
		
		setPLmenuData();
		showPLcontent(pl_data);
	};
	
	setPLmenuData = function(){
		var steps = Math.ceil(parseInt(pl.total)/parseInt(pl.limit))-1;
		setPagenav();
		$(".pl_sort").val(pl.sortby);
		$(".pl_result").val(pl.limit);
		
		$('a.pl_viewbuttons').click(function() {
			if(this.title != pl.view){
				pl.view = this.title;
				pl.limitstart = 0;
				retrieveContent(true);
			}
			$(this).blur();
		});
		
		$(".pl_sort").change(function(){
			pl.sortby = this.value;
			pl.limitstart = 0;
			retrieveContent(true);
		});
		
		$(".pl_result").change(function(){
			pl.limit = this.value;
			pl.limitstart = 0;
			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;
			retrieveContent(true);
			$(this).blur();
		});
		
		$("a.pl_next, a.pl_prev", ".pl_pagenav").click(function(){
			if(this.title == 'Next'){
				if(steps > 0 && pl.limitstart < parseInt(pl.limit*steps)){
					pl.limitstart = parseInt(pl.limitstart*1 + pl.limit*1);
					animateSlide();
				}
			}else{
				if(pl.limitstart > 0){
					pl.limitstart = parseInt(pl.limitstart - pl.limit);
					animateSlide();
				}
			}			
			$(this).blur();
		});
		
		$(".pl_pagenav a.pl_nav").click(function(){
			var num = parseInt($(this).text())-1;
			pl.limitstart = parseInt(pl.limit*num);
			animateSlide();
			$(this).blur();
		});
	};
	
	setPagenav = function() {
		var next = '<a href="javascript:void(0);" class="pl_next" title="Next"><img src="'+pl.pl_imgpath+'az_pl_next.gif" border="0" title="'+pl_label['NEXT']+'"></a>';
		var prev = '<a href="javascript:void(0);" class="pl_prev" title="Previous"><img src="'+pl.pl_imgpath+'az_pl_prev.gif" border="0" title="'+pl_label['PREVIOUS']+'"></a>';
		var steps = Math.ceil(parseInt(pl.total)/parseInt(pl.limit));
		var pagenav = '';
		
		for(var x = ((parseInt(pl.limitstart/pl.limit)-(parseInt(pl.max_count/2)-1))> 0 && steps > (pl.max_count-1) ? (parseInt(pl.limitstart/pl.limit)-((pl.max_count/2)-1)) : 0), y =0;x < steps && y < pl.max_count-1; x++,y++){
			pagenav += '<a href="javascript:void(0)" '+(x==parseInt(pl.limitstart/pl.limit)? 'id="selected"' : '')+' class="pl_nav" title="'+(x+1)+'">' + (x+1) + '</a>&nbsp;';
		}
		pagenav = (steps-1) > 0 ? (pl.limitstart > 0 ? prev : '') + '&nbsp;&nbsp;' + pagenav + '&nbsp;' + 
				  (pl.limitstart < parseInt(pl.limit*(steps-1)) ? next : '') : '';
		$(".pl_pagenav").html(pagenav);
	}
	
	animateSlide = function() {
		$("#pl_content").fadeOut(500, 
		  function(){
			$("#pl_content").fadeIn(1);
			retrieveContent(false);
		  }
		);
	}
	
	addtocart = function(id, cart_params) {
		var data_params = pl.params + '&task=addtocart' + (cart_params ? cart_params : '&pid='+id);
		$("#cart"+id+", .pl_pop_cart").hide(1);
		$.ajax({
			url: pl.xmlsrc,
			type: "get",
			dataType: "xml",
			data: data_params,
			beforeSend: function(){
				$("#err"+id+", #pl_pop_cart_loading").show(1);
			},
			error: function(){
				$("#err"+id+", #pl_pop_cart_loading").show(1, function(){
					$(this).html(pl_label['ERROR1']);
				}).fadeOut(4000, function(){
					$(this).html(getLoading1());
					$("#cart"+id+", .pl_pop_cart").show(1)
				});
			},
			success: function(xml){
				var result = parseInt($(xml).find("result").text());
				var azcart_top = $(xml).find("carttext").text();
				if(result > 0){
					$("#cart"+id+", .pl_pop_cart").show(1).html('('+result + ') ' + pl_label['IN_CART']);
					$("#err"+id+", #pl_pop_cart_loading").hide(1);
					$("#az_cart_top").html(azcart_top);
				}else{
					$("#err"+id+", #pl_pop_cart_loading").html(pl_label['ERROR2']);
				}
				$("#err"+id+", #pl_pop_cart_loading").fadeOut(4000, function(){
					$(this).html(getLoading1());
					$("#cart"+id+", .pl_pop_cart").show(1)
				});
			}
		});
		
		if(pl.debug_mode == 1) {
			$('#pl_debug').html('Request URl: ' + pl.xmlsrc + '?' + data_params);
		}
	}
	
	retrieveContent(true);
});