var azcs = {
	mod_imgpath: az_alphaselect_imgpath,
	xmlsrc: az_alphaselect_fetch,
	dimension: [null,30], // width | height
	images: '',
	loading: az_alphaselect_imgpath + 'az_catscroll_loader.gif',
	noimage: 'no_picture.gif',
	sort_order: 'asc',
	keyword: '',
	limitstart: 0,
	limit: 5,
	result: 0,
	total: 0
};

$(function(){
	
	$('#alpha_list a').click(
		function() { 
			if(!$(this).hasClass("alpha_sel")) {
				$('#alpha_list a').removeClass('alpha_sel')
				$(this).addClass('alpha_sel')
				$(this).blur()
				//azcs.keyword = escape($(this).text())
				azcs.keyword = this.id
				azcs.limitstart = 0
				retrieveContent()
			}
		}
	);
	
	$('.az_catScroll_up').click(function() {
	  if(azcs.limitstart > 0){
		$(this).blur()
		//azcs.keyword = $('.alpha_sel').text()
		azcs.keyword = $('.alpha_sel').attr('id') ? $('.alpha_sel').attr('id') : ''
		azcs.limitstart = azcs.limitstart - azcs.limit
		retrieveContent()
	  }
	});
	$('.az_catScroll_down').click(function() {
	  var $steps = Math.ceil(parseInt(azcs.total)/parseInt(azcs.limit))-1
	  if($steps > 0 && azcs.limitstart < parseInt(azcs.limit*$steps)){
		$(this).blur()
		//azcs.keyword = $('.alpha_sel').text()
		azcs.keyword = $('.alpha_sel').attr('id') ? $('.alpha_sel').attr('id') : ''
		azcs.limitstart = (parseInt(azcs.limitstart) + parseInt(azcs.limit))
		retrieveContent()
	  }
	});
	
	retrieveContent = function(){
		var data_params = 'scroll_key='+azcs.keyword+'&sort_order='+azcs.sort_order+'&limitstart='+azcs.limitstart+'&limit='+azcs.limit;

		$.ajax({
			url: azcs.xmlsrc,
			type: "get",
			dataType: "xml",
			data: data_params,
			beforeSend: function(){
				$("#az_catScroll").html('<div class="az_catscroll_loading">&nbsp;</div>')
			},
			error: function(){
				$("#az_catScroll").html('<p>Error!</p>')
			},
			success: function(response){
				parseData(response);
			}
		});
	};
	
	parseData = function(xml) {
		var data = new Array();
		var gallery = $(xml).find("gallery");
		
		azcs.sort_order = gallery.attr("sortorder") ? gallery.attr("sortorder") : 'asc';
		azcs.limitstart = gallery.attr("limitstart") ? gallery.attr("limitstart") : 0;
		azcs.result = gallery.attr("result") ? gallery.attr("result") : 0;
		azcs.total = gallery.attr("total") ? gallery.attr("total") : 0;
		azcs.imgpath = gallery.attr("path") ? gallery.attr("path") : '';
		
		$("product", xml).each(function(i){
			data[i] = new Array();
			data[i]['id'] = $("id", this).text() ? $("id", this).text() : 0;
			data[i]['title'] = $("title", this).text() ? $("title", this).text() : null;
			data[i]['desc'] = $("description", this).text()? $("description", this).text() : null;
			data[i]['image'] = $("image", this).text() ? $("image", this).text() : azcs.noimage;
			data[i]['price'] = $("price", this).text() ? $("price", this).text() : 0;
			data[i]['link'] = $("link", this).text() ? $("link", this).text() : '';
		});
		
		displayData(data);
	};
	
	displayData = function(data) {
		var html = '';
		html += '<div class="azcs_list"><ul>';
		$(data).each(function(i, prod){
			imgpath = azcs.imgpath + prod['image'];
			imgDimension = (azcs.dimension[0] ? 'width="'+azcs.dimension[0]+'" ' : '') + (azcs.dimension[1] ? 'height="'+azcs.dimension[1]+'" ' : '');
			
			html += '<li><div style="position:relative">' + 
					'<a href="'+prod['link']+'"><img src="'+imgpath+'" border="0" ' + imgDimension + ' /></a>' + 
					'<div class="azcs_text">' + 
					'<span class="azcs_title"><a href="'+prod['link']+'">' + prod['title'] + '</a></span>' + 
					'<span class="azcs_price">' + prod['price'] + '</span>' + 
					'</div>' + 
			 		'</div></li>';
		});
		html += '</ul></div>';
		if(azcs.result < 1) html = '<p>0 Result</p>';
		
		$("#az_catScroll").html(html);
		
		var $steps = Math.ceil(parseInt(azcs.total)/parseInt(azcs.limit))-1;
		
		if(azcs.limitstart > 0){
			$('.az_catScroll_up').addClass('azcsUp_active')
		}else{
			$('.az_catScroll_up').removeClass('azcsUp_active')
		}
		if($steps > 0 && azcs.limitstart < parseInt(azcs.limit*$steps)){
			$('.az_catScroll_down').addClass('azcsDown_active')
		}else{
			$('.az_catScroll_down').removeClass('azcsDown_active')
		}
	}
		
	retrieveContent();
});