// JavaScript Document

loadCartPreview = function()
{
	if($('#cartpreview').length > 0)
	{
		var date = new Date().getTime();
		$('#cartpreview').load("cart/cartPreview/"+date);	
	}		
}

cartFunctions = function()
{
	var product_id = $('#product_id').val();
	var product_name = $('#product_name').val();
	var qty = $('#qty').val();
	var size = $('input[name=size]:checked').val();
	var price = $('#price').val();
	
	if(!size) size = "-";
	
	$.post("cart/addToCart",{product_id:product_id,product_name:product_name,price:price,qty:qty,size:size},loadCartPreview);	
	
}

zoomImage = function(){
	
	var zoom_options = {
	    zoomWidth: 400,
	    zoomHeight: 320,
        xOffset: 5,
        yOffset: 0,
        position: "right" //and MORE OPTIONS
	};
	$('.zoomimage').jqzoom(zoom_options);	
}

loadProductImage = function()
{
	$('.loadImage').click(function(){
		
		var src = $(this).attr('href');
		var ttl = $(this).attr('title');
		var img = new Image();
		
		$('#productImage').html('Loading...');
		
		$(img).load(function()
		{		 
		  $(this).hide(); // hide image				  
		  $('#productImage').html('').removeClass('loading').append(this);
		  $(this).wrap('<a href="'+src+'" class="zoomimage" title="'+ttl+'"></a>');		  
		  $(this).show();	
		  $(this).attr('width',265);
		  $(this).attr('height',265);
		  $(this).attr('style','width:256px; height:256px');
		  zoomImage();
		})
		.error(function(){ 
		  $('#productImage').html('There was an error loading the image');
		})
		.attr('src', src)
		.attr('width', 265)
		.attr('height', 265);
		
		return false;
	});	
}

loadFrontImages = function(lang)
{
	$.getJSON("./home/getimages",
        function(data){
			if(data.success == 'false')
			{				
				$('#frontimgs_wrapper').css({background:'url(./system/application/assets/images/shop-1.jpg)'});
				$('.frontimgs_text').css({background:'url(./system/application/assets/images/shop-2.jpg)'});
				return;
			}
			else
			{				
				i = 0;
				$.each(data.items, function(i,item){
					i++;
					title = (lang == 'en') ? item.title_en : item.title_es;
					desc = (lang == 'en') ? item.text_en : item.text_es;
					cursor = (item.url != '') ? 'cursor:pointer;' : '';
					url = (item.url != '') ? 'onclick=window.location.href="'+item.url+'"' : '';
					$("#frontimgs_holder").append('<div class="frontimgs_img" style="background:url(\'./system/application/assets/uploads/'+item.file+'\') no-repeat; '+cursor+'" '+url+'><div class="frontimgs_title">'+title+'</div><div id="text_'+i+'">'+desc+'</div></div>');
				});				
				setTimeout('animateFrontImages('+data.items.length+')',6000);
				setFrontImagesText(1);
			}
        });
}
currslide = 1;
animateFrontImages = function(slides)
{
	currslide++;
	if(currslide <= slides)
	{
		$('#frontimgs_holder').animate({marginLeft:'-=390px'},1500,setFrontImagesText(currslide));		
	}
	else
	{
		$('#frontimgs_holder').animate({marginLeft:0},1500);
		currslide = 1;
	}	
	setTimeout('animateFrontImages('+slides+')',6000,setFrontImagesText(currslide));
}
setFrontImagesText = function(id)
{
	var text = $('#text_'+id).html();
	if(text != '')
	{
		$('.frontimgs_text').css({background:'none'}).html(text);
	}
	else
	{
		$('.frontimgs_text').css({background:'url(./system/application/assets/images/shop-2.jpg)'}).html('');
	}
}

$(document).ready(function(){
	
	
	var lang = $('meta[http-equiv=content-language]').attr("content");
	if($('#frontimgs_wrapper').length > 0) loadFrontImages(lang);
	
	zoomImage();
	loadCartPreview();
	loadProductImage();	
	
	$('#add2cart').click(function(){
		cartFunctions();		
	});
	
	$("#cpyfields").click(function(){	  
	
		var checked_status = this.checked;				
		
		if(checked_status == true)
		{
			$('#ship_name').val($('#bill_name').val());
			$('#ship_company').val($('#bill_company').val());
			$('#ship_address').val($('#bill_address').val());
			$('#ship_city').val($('#bill_city').val());
			$('#ship_zip').val($('#bill_zip').val());
			$('#ship_country')[0].selectedIndex = ($('#bill_country')[0].selectedIndex);
		}
		else
		{
			$('#ship_name').val('');
			$('#ship_company').val('');
			$('#ship_address').val('');
			$('#ship_city').val('');
			$('#ship_zip').val('');
			$('#ship_country')[0].selectedIndex = 0;
		}
				
	});
});



