var ps = {
	params: params,
	xmlsrc: 'scroller_loader.php',
	loading: 'images/az_template/ajax_loader.gif',
	noimage: 'images/noimage.gif',
	width: '100',
	height: '80',
	bgcolor: '',
	path: '',
	params: params
};

function gotoPage(url){ window.location = url; }

$(function(){
	$.ajax({
		url: ps.xmlsrc,
		type: "get",
		dataType: "xml",
		data: ps.params,
		beforeSend: function(){
		},
		error: function(){
			$("#slider_container").html('<br>Error loading XML document');
		},
		success: function(response){
			parseData(response);
		}
	});
	
	parseData = function(xml) {
		var ps_data = new Array();
		var gallery = $(xml).find("slidergallery");
		
		ps.path = gallery.attr("path");
		ps.width = !gallery.attr("width") ? ps.width : gallery.attr("width");
		ps.height = !gallery.attr("height") ? ps.height : gallery.attr("height");
		ps.duration = gallery.attr("duration");
		ps.bgcolor = gallery.attr("bgcolor");
		
		$("slide", xml).each(function(i){
			ps_data[i] = new Array();
			ps_data[i]['title'] = $("title", this).text() ? $("title", this).text() : "";
			ps_data[i]['url'] = $("url", this).text()? $("url", this).text() : "";
			ps_data[i]['image'] = $("image", this).text() ? $("image", this).text() : ps.noimage;
			ps_data[i]['price'] = $("price", this).text() ? $("price", this).text() : 0;
		});
		
		var bgcolor = ps.bgcolor=='none' ? '' : ps.bgcolor;
		$("#slider_container").css({'background-color' : bgcolor});
		showPScontent(ps_data);
	};
	
	showPScontent = function(ps_data) {
		var _html = '';
		_html += 
		'<div class="sliderGallery">' + 
	      '<table id="slider_items" cellpadding="0" cellspacing="0" border="0"><tr>';
		$(ps_data).each(function(i, data){
			imgpath = ps.path + data['image'];
			price = data['price'].split("|");
			pprice = (price.length > 1 ? '<s>'+price[0]+'</s> '+price[1] : data['price']);
			ptitle = data['title'].length > 10 ? data['title'].substr(0,10)+'...' : data['title'];
			
			_html += '<td><img onDblClick="gotoPage(\''+data['url']+'\');" src="'+imgpath+'" width="'+ps.width+'" height="'+ps.height+'" alt="'+data['title']+'" />' + 
			'<br><span class="prod_name">' + 
			'<a href="'+data['url']+'" title="'+data['title']+'" target="_parent">'+(ptitle ? ptitle : '')+'</a>' + 
			'</span>' + 
			'<span class="prod_price">'+(pprice ? '<br>'+pprice : '')+'</span>' + 
			'</td>';
		});
		_html += '</tr></table>' + 
  	  	'</div>' + 
	  	'<div id="slider_nav">' + 
	  	  '<div class="slider_prev"></div>' + 
      	  '<div class="slider">' + 
	        '<div id="left_end"></div>' + 
            '<div class="handle"></div>' + 
		    '<div id="right_end"></div>' + 
      	  '</div>' + 
	      '<div class="slider_next"></div>' + 
	    '</div>';
		
		$("#slider_container").html(_html);
		initPS_slider();
	};
	
	initPS_slider = function() {
		var container = $("div.sliderGallery");
		var ul = $("#slider_items", container);
		var itemsWidth = ul.innerWidth() - container.outerWidth();
		var _startX = 0;
		
		$(".slider").slider({
			min: 0, max: itemsWidth, handle: ".handle",
			stop: function (event, ui) {
			if(_startX > 0){ ul.css({"left" : ui.value * -1}); }
			else{ ul.animate({"left" : ui.value * -1}, 500); }
			},
			slide: function (event, ui) { ul.css("left", ui.value * -1); }
		});
	
		if(ul.innerWidth() < container.outerWidth()){
			$(".slider").slider("disable");
		}else{
			$(".slider_prev").click(function(){ $(".slider").slider("moveTo", "-=600"); });
			$(".slider_next").click(function(){ $(".slider").slider("moveTo", "+=600"); });
			
			$("#slider_items").draggable({
				axis: "x", cursor: "move",
				start: function(event, ui){ _startX = event.clientX; },
				drag: function(event, ui){ $(".slider").slider("moveTo", (ui.position.left*-1)+"px"); },
				stop: function(event, ui){ _startX = 0; }
			});
		}
	};
});