var view = {
	//	[per page, per row, width, height]
	small: [24, 4, 90, ''],
	med:   [9, 3, 110, ''],
	large: [6, 1, 180, '']
};

//dcrosbie - march 16, 2011
//changed var "view" below to be "med" - other options are small/med/large
var pl = {
	xmlsrc: pl_config['fetch'],
	pl_imgpath: pl_config['img_path'],
	imgpath: '',
	loading1: pl_config['img_path'] + 'ajax_loader.gif',
	noimage: pl_config['noimage'],
	sortby: 'none',
	sort_dir: 'desc',
	limitstart: 0,
	max_count: 6,
	view: 'med',
	result: 0,
	total: 0,
	debug_mode: pl_config['debug']
};

$(function(){
	if(pl.debug_mode == 1) {
		$('#pl_container').append('<div id="pl_debug"></div>');
	}
	
	retrieveContent = function(pl_reload){
		var data_params = "task=showproducts&view="+pl.view+"&sort="+pl.sortby+"&sortdir="+pl.sort_dir+"&limitstart="+pl.limitstart+"&limit="+view[pl.view][0];
		
		if(pl_reload==true){
			$("#pl_menu").html(showPLmenu());
		}
		$.ajax({
			url: pl.xmlsrc,
			type: "get",
			dataType: "xml",
			data: data_params,
			beforeSend: function(){				
				$("#pl_content").html('<div id="pl_loading"></div>');
			},
			error: function(){
				$("#pl_content").html('&nbsp;'+pl_label['ERROR1']);
			},
			success: function(response){
				parseData(response, pl_reload);
			}
		});
		
		if(pl.debug_mode == 1) {
			$('#pl_debug').html('Request URl: <a href="' + pl.xmlsrc + '&' + data_params + '" target="_blank">' + pl.xmlsrc + '&' + data_params + '</a>');
		}
	};
	
	parseData = function(xml, pl_reload) {
		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.view = gallery.attr("view");
		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]['imagelrg'] = $("imagelrg", this).text() ? $("imagelrg", this).text() : pl_data[i]['image'];
			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;
		});
		
		setPagenav();
		getPageValue();
		if(pl_reload==true){
			setPLmenuData();
		}
		showPLcontent(pl_data);
	};
	
	setPLmenuData = function(){
		var steps = Math.ceil(parseInt(pl.total)/parseInt(view[pl.view][0]))-1;
		
		$('td#pl_viewbuttons a').click(function() {
			if(this.title != pl.view){
				pl.view = this.title;
				pl.limitstart = 0;
				retrieveContent(true);
			}
			$(this).blur();
		});
		
		$("#pl_pagenav a.pl_nav").click(function(){
			var num = parseInt($(this).text())-1;
			var limit = view[pl.view][0];
			pl.limitstart = parseInt(limit*num);
			animateSlide();
			$(this).blur();
		});
		
		$("#pl_last, #pl_first").click(function(){
			var num = parseInt($(this).attr('title'))-1;
			var limit = view[pl.view][0];
			pl.limitstart = parseInt(limit*num);
			animateSlide();
			$(this).blur();
		});
	};
	
	addtocart = function(id, cart_params) {
		var data_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_shoppingcart").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: <a href="' + pl.xmlsrc + '&' + data_params + '" target="_blank">' + pl.xmlsrc + '&' + data_params + '</a>');
		}
	}
	
	createImage = function(obj) {
		var newimg = new Image();
		for(var i in obj){
			$(newimg).attr(i, obj[i]);
		}
		$(newimg).load();
		
		return newimg;
	};
	
	setPagenav = function() {
		var total = pl.total;
		var limit = view[pl.view][0];
		var steps = Math.ceil(parseInt(total)/parseInt(limit));
		var last = '<li><a href="javascript:void(0);" id="pl_last" title="'+steps+'">'+pl_label['LAST']+'</a></li>';
		var first = '<li><a href="javascript:void(0);" id="pl_first" title="1">'+pl_label['FIRST']+'</a></li>';
		var pagenav = '';	
		
		if(total > 0 && steps > 1){
			for(var x=((parseInt(pl.limitstart/limit)-(parseInt(pl.max_count/2)-1)) > 0 && steps > (pl.max_count-1) ? (parseInt(pl.limitstart/limit)-(parseInt(pl.max_count/2)-1)) : 0), y =0;x < steps && y < pl.max_count-1; x++,y++){
				pagenav += '<li><a href="javascript:void(0)" '+(x==parseInt(pl.limitstart/limit)? 'id="pl_nav_sel"' : '')+' class="pl_nav" title="'+(x+1)+'">' + (x+1) + '</a></li>';
				if(steps < pl.max_count-1){
					pagenav += y < steps-1 ? '<li class="pl_nav_sep"></li>' : '';
				}else{
					pagenav += (y < (pl.max_count-2) && x < (steps-1)) ? '<li class="pl_nav_sep"></li>' : '';
				}
			}
			if(steps > (pl.max_count-1)){
				pagenav = first + '<li class="pl_nav_sep"></li>' + pagenav + '<li class="pl_nav_sep"></li>' + last;
			}
			$("#pl_pagenav").html(pagenav);
		}
	}
	
	getPageValue = function() {
		var pagenav = '<span id="pl_pages_txt">' + pl_label['PAGES'] + '</span>&nbsp; ';
		var steps = Math.ceil(parseInt(pl.total)/parseInt(view[pl.view][0]));
		var curpage = steps==1 ? '1' : $('#pl_nav_sel').text();
		pagenav = pagenav + '<span id="pl_curpage_txt">' + curpage + '</span> ' + pl_label['FROM'] + ' <span id="pl_totalpage_txt">' + steps + '</span>';
		$("#page_result").html(pagenav);
	};
	
	animateSlide = function() {
		$("#pl_content").animate({opacity: 0
		  }, 500, '', function(){
			  $("#pl_content").css({opacity:1});
			  if(jQuery.browser.msie) this.style.removeAttribute('filter');
			  retrieveContent(true);
		  }
		);
	}
		
	
	retrieveContent(true);
});
