var azs04 = {
	params: params,
	coreimg: 'images/azscroll04/',
	xmlsrc: 'azscroll04_loader.php',
	loading1: 'images/azscroll04/ajax_loader.gif',
	imgpath: null,
	noimage: 'noimage.gif',
	view: 0, // 0:new_products, 1:specials, 2:featured
	dimension: ['',100], // width | height
	dimension2: new Array(), // temp var (just leave unset)
	scaleby: 10,
	sortby: 'latest',
	sort_order: 'desc',
	limitstart: 0,
	limit: 5,
	total: 0
};

var azs04_desc = false;
var azs04_data = new Array();

function azs04_loadItem(gallery, state) {
	azs04.limitstart = gallery.first-1;
	var data_params = azs04.params+"&task=showproducts&view="+azs04.view+"&sort="+azs04.sortby+"&sortorder="+azs04.sort_order+"&limitstart="+azs04.limitstart+"&limit="+azs04.limit;

    // Check if the requested items already exist
    if (gallery.has(gallery.first, gallery.last)) {
        return;
    }
	
	jQuery.ajax({
		url: azs04.xmlsrc,
		type: "get",
		dataType: "xml",
		data: data_params,
		error: function(){
			$(".azscroll04-container").prepend($azs04_label['ERROR1']);
		},
		success: function(xml){
			azs04_addItem(gallery, gallery.first, gallery.last, xml);
		}
	});
};

function azs04_addItem(gallery, first, last, xml) {
	var gal = $(xml).find("gallery");
	azs04_data = new Array();
	
	azs04.limitstart = gal.attr("limitstart");
	azs04.total = gal.attr("total");
	azs04.imgpath = gal.attr("path");
		
    gallery.size(parseInt(azs04.total));
	
	$("product", xml).each(function(i){
		azs04_data[i] = new Array();
		azs04_data[i]['id'] = $("id", this).text() ? $("id", this).text() : 0;
		azs04_data[i]['title'] = $("title", this).text() ? $("title", this).text() : null;
		azs04_data[i]['image'] = $("image", this).text() ? $("image", this).text() : azs04.noimage;
		azs04_data[i]['price'] = $("price", this).text() ? $("price", this).text() : 0;
		azs04_data[i]['sprice'] = $("sprice", this).text() ? $("sprice", this).text() : 0;
		gallery.add(first + i, azs04_getItemHTML(azs04_data[i], i));
	});
	
	jQuery('div.azs04_pbox').hover(
		function () {
			$(this).addClass('selected')
			$('.azs04_button2',this).css({'display':'inline'})
			$('.azs04_button',this).css({'display':'none'})
			if(azs04.dimension[0]){
				$('img', this).width(parseInt(azs04.dimension[0]+azs04.scaleby)+'px').height('auto')
			}else{
				$('img', this).width('auto').height(parseInt(azs04.dimension[1]+azs04.scaleby)+'px')
			}
		},
		function () {
			$(this).removeClass('selected')
			$('.azs04_button',this).css({'display':'inline'})
			$('.azs04_button2',this).css({'display':'none'})
			if(azs04.dimension[0]){
				$('img', this).width(azs04.dimension[0]+'px').height('auto')
			}else{
				$('img', this).width('auto').height(azs04.dimension[1]+'px')
			}
		}
	);
	
	if(azs04.total < azs04.limit){
		$('.azscroll04-item-temp').css({'background' : 'none'})
	}
};

function azs04_getItemHTML(data, id) {
	var imgpath = azs04.imgpath + data['image'];
	imgDimension = azs04_desc ? 
			(azs04.dimension[0] ? 'width="'+parseInt(azs04.dimension[0]+azs04.scaleby)+'" ' : '') + (azs04.dimension[1] ? 'height="'+parseInt(azs04.dimension[1]+azs04.scaleby)+'" ' : '') 
			: 
			(azs04.dimension[0] ? 'width="'+azs04.dimension[0]+'" ' : '') + (azs04.dimension[1] ? 'height="'+azs04.dimension[1]+'" ' : '');
			
	ptitle = data['title'].length > 16 ? data['title'].substr(0,15)+'...' : data['title'];
	buttonText = azs04_desc ? $azs04_label['BACK'] : $azs04_label['MORE'] ;

	var html = '<div id="azs04_pbox' + id + '" class="azs04_pbox' + (azs04_desc ? ' selected' : '' ) + '">' + 
			   '<div class="azscroll04_title" title="' + data['title'] + '">' + ptitle + '</div>' + 
			   '<a href="'+pl_filename["product_info"]+'&products_id='+data['id']+'" title="' + data['title'] + '">' + 
			   '<img id="' + data['id'] + '" src="' + imgpath + '" ' + imgDimension + ' alt="" border="0"></a>' + 
			   '<div class="azscroll04_price">' + getPrice(data['price'], data['sprice']) + '</div>' + 
			   '<a onclick="showDescription(' + id + ');" href="javascript:void(0);" class="azs04_button">' + 
			   azButton(buttonText, 4, 85) + '</a>' + 
			   '<a onclick="showDescription(' + id + ');" href="javascript:void(0);" class="azs04_button2">' + 
			   azButton(buttonText, 5, 105) + '</a>' + 
			   '</div>';
	return html;
};

function azs04_getDescriptionHTML(id) {
	var html = '<div class="azscroll04_descBox">' + 
			   '<ul class="azscroll04-list">' + 
			   '<li class="azscroll04-item">' + azs04_getItemHTML(azs04_data[id], id) + '</li>' + 
			   '<li class="azs04_descRight"><div class="azs04_descBorder"><div id="azs04_descWrapper"></div></div></li>' + 
			   '</ul>' + 
			   '</div>';
	return html;
};

function showDescription(id) {
	if(azs04_desc == false) {
		var data_params = azs04.params+"&task=viewproductinfo&pid="+azs04_data[id]['id'];
		azs04_desc = true
		jQuery('.azscroll04_descContainer').show(1).html(azs04_getDescriptionHTML(id))
		jQuery('.azscroll04-clip').hide(1)
		jQuery('.azs04_button2','.azscroll04_descContainer').css({'display':'inline'})
		jQuery('.azs04_button','.azscroll04_descContainer').css({'display':'none'})
	
		jQuery.ajax({
			url: azs04.xmlsrc,
			type: "get",
			dataType: "html",
			data: data_params,
			error: function(){
				$('#azs04_descWrapper').html($azs04_label['ERROR1'])
			},
			success: function(response){
				$('#azs04_descWrapper').html(response)
			}
		});
	}else{
		azs04_desc = false
		jQuery('.azscroll04-clip').show(1)
		jQuery('.azscroll04_descContainer').hide(1).empty()
		$('img', '.azs04_pbox').height(azs04.dimension2[1]+'px').width(azs04.dimension2[0]+'px')
	}
};

function azs04AddtoCart(obj, pid) {
	var id = '&' + $("form#az_popForm").serialize()
	var data_params = azs04.params + '&task=addtocart' + (id ? id : '&pid='+pid);
	obj.blur()
	
	$(".azs04_cartItems").hide(1);
	$.ajax({
		url: azs04.xmlsrc,
		type: "get",
		dataType: "xml",
		data: data_params,
		beforeSend: function(){
			$("#azs04_cart_loading").show(1);
		},
		error: function(){
			$("#azs04_cart_loading").show(1, function(){
				$(this).html($azs04_label['ERROR1']);
			}).fadeOut(4000, function(){
				$(this).html('<img src="'+azs04.loading1+'" border="0" width="16" height="16" alt="">');
				$(".azs04_cartItems").show(1)
			});
		},
		success: function(xml){
			var result = parseInt($(xml).find("result").text());
			var azcart_top = $(xml).find("carttext").text();
			if(result > 0){
				$(".azs04_cartItems").show(1).html('('+result + ') ' + $azs04_label['IN_CART']);
				$("#azs04_cart_loading").hide(1);
				$("#az_cart_top").html(azcart_top);
			}else{
				$("#azs04_cart_loading").html($azs04_label['ERROR2']);
			}
			$("#azs04_cart_loading").fadeOut(4000, function(){
				$(this).html('<img src="'+azs04.loading1+'" border="0" width="16" height="16" alt="">');
				$(".azs04_cartItems").show(1)
			});
		}
	});
};

function azButton(alt, bnum, size) {
	var buttonW = size ? ' style="width:'+size+'px;"' : '';
	var image_button = '<span class="az-template-button"><span class="az-button-left'+bnum+'" title="' + alt + '">&nbsp;</span><span class="az-button-middle'+bnum+'" title="' + alt + '"' + buttonW + '>&nbsp;' + alt +'&nbsp;</span><span class="az-button-right'+bnum+'" title="' + alt + '">&nbsp;</span></span>';
	return image_button;
};

function getPrice(price, sprice){
	if(!sprice == 0){
		price = '<s>' + price + '</s> ' + sprice;
	}
	return price;
};

jQuery(document).ready(function() {
    jQuery('#myscroll').azscroll04({
		scroll: azs04.limit,
        itemVisibleOutCallback: {onAfterAnimation: function(gallery, item, i, state, evt) { gallery.remove(i); }},
        itemLoadCallback: azs04_loadItem
    });
});