var addingToCart = 0;

runOnLoad(function(){
	$('[tooltip]').each(function(){
		$(this).qtip({
				content: $(this).attr('tooltip'),
				style:{
					name:'lulifama',
					tip:true
				},
				position:{
					corner:{target:'leftMiddle',	tooltip:'rightMiddle'}
				}
			});
		});
		var full_image=document.getElementById('full_image');
		var fullPath=full_image.src;
		var len=fullPath.length;
		var rootPath=fullPath.substring(0,len-5)
		var largePath=rootPath+'l.jpg'
		var backPath=rootPath+'b.jpg'
		var zoomPath=rootPath+'z.jpg'
		$('#imgHolder').css('background',backPath);
		$('#openImgGal').attr('href',largePath);
		$('#openImgGal').colorbox();
		$('#zoomImg').attr('rel',zoomPath);
		$('#zoomImg').bezoom();
});
$(document).ready(function(){	   
	$(".testing").colorbox({width:"30%", inline:true, href:"#testing1"});
	if(current_image_count<3){$('#prod_carousel').jCarouselLite({visible:current_image_count});}else{$('#prod_carousel').jCarouselLite();};
	$.swapImage('.swapImage', true, true, 'click');
		$('.swapImage').click(function(){$('#full_image').show();$('#openImgGal').show();$('#viewBack').show();$('#viewFront').hide();});
	$('#zoomImg').bezoom();
	$('#openImgGal').colorbox();
	
	//$('#viewBack').click(function(){$('#openImgGal').hide();$('#viewFront').show();$('#viewBack').hide();return false;});
	$('#viewBack').click(function(){
		$('#openImgGal').hide();
		$('#viewFront').show();
		$('#viewBack').hide();
		var full_image=document.getElementById('full_image');
		var fullPath=full_image.src;
		var len=fullPath.length;
		var rootPath=fullPath.substring(0,len-5)
		var backPath=rootPath+'b.jpg'
		var zoomPath=rootPath+'bz.jpg'
		$('#full_image').attr('src',backPath);
		$('#zoomImg').attr('rel',zoomPath);
		$('#zoomImg').bezoom();
	});
	//$('#viewFront').click(function(){$('#full_image').show();$('#openImgGal').show();$('#viewBack').show();$('#viewFront').hide();return false;});
	$('#viewFront').click(function(){
		$('#openImgGal').show();
		$('#viewFront').hide();
		$('#viewBack').show();
		var full_image=document.getElementById('full_image');
		var backPath=full_image.src;
		var len=backPath.length;
		var rootPath=backPath.substring(0,len-5)
		var frontPath=rootPath+'f.jpg'
		var zoomPath=rootPath+'z.jpg'
		$('#full_image').attr('src',frontPath);
		$('#zoomImg').attr('rel',zoomPath);
		$('#zoomImg').bezoom();
	});
	$('#preloader').load('/preload.html?Category_Code='+cat_code);
});

function updateQty(product_id,bool,color,code){
	var idCount=product_id;
	var s=('s'+idCount);
	var size=document.getElementById(s).value;
	//var c=('c'+idCount);
	//var color=document.getElementById(c).value;
	var q=('q'+idCount);
	var qty=document.getElementById(q);
	if (size!=''){qty.value=1;}else{qty.value='';};
	if(bool == '1') updateSwatch(color,code);
}

function updateSwatch(color,code){
	document.getElementById("swatch_" + code).style.backgroundImage = "url('images/"+color.value+".jpg')";
    $("#swatch_" + code).css("border","1px solid #666");
}

function addToCart(prodcode,catcode,pictured)
{
	$('#result_' + prodcode).html('<div class="notice"><img src="images/preload.gif" alt="loading..."/></div>');
	if(addingToCart == 0)
	{
		addingToCart = 1;
		if(pictured)
		{
			var selected_size = $(".pictured_size_"+prodcode + " option:selected").val();
			var selected_color = $(".pictured_color_"+prodcode + " option:selected").val();
			var selected_size_attr_code = $("#pictured_size_attr_code_"+prodcode).val();
			var selected_color_attr_code = $("#pictured_color_attr_code_"+prodcode).val();
		}
		else{	
			var selected_size = $(".size_"+prodcode + " option:selected").val();
			var selected_color = $(".color_"+prodcode + " option:selected").val();
			var selected_size_attr_code = $("#size_attr_code_"+prodcode).val();
			var selected_color_attr_code = $("#color_attr_code_"+prodcode).val();
		}
		
		//alert(selected_size_attr_code+'-'+selected_size);
		
		
		if(selected_size != '' && selected_color != '')
		{
			var url = "http://www.lulifama.com/miva/merchant.mvc?Screen=BASK-AJAX&Store_Code=CES&Product_Code=" + prodcode + "&Category_Code="+catcode+"&Quantity=1&Action=ADPR&Product_Attributes[1]:code="+selected_size_attr_code+"&Product_Attributes[1]:value="+selected_size+"&Product_Attributes[2]:code="+selected_color_attr_code+"&Product_Attributes[2]:value=" + selected_color;
			//alert(url);
			if($('#js-response').length >= 0) $('#js-response').html('<p>Javascript Response</p>' + url);
			$.get(url, function(data) {
				if(data.search(/success/i) != -1) {
					addingToCart = 0;
					if(pictured) $('#result_' + prodcode).html('<div class="confirm"><img src="images/img_approved.png" alt="Success" class="notice_img"/>The product was successfully added to your cart!<div id="popup_btns"><a onclick="popupclose(\''+prodcode+'\',\'pictured\')" class="input_btn notice">Close</a><a href="http://www.lulifama.com/bask.html" class="input_btn notice">Checkout Now</a></div>');
					else $('#result_' + prodcode).html('<div class="confirm"><img src="images/img_approved.png" alt="Success" class="notice_img"/>The product was successfully added to your cart!<div id="popup_btns"><a onclick="popupclose(\''+prodcode+'\')" class="input_btn notice">Close</a><a href="/bask.html" class="input_btn notice">Checkout Now</a></div>');
					$('#product_basket_response').html(data);
					var headerUrl = 'http://www.lulifama.com/miva/merchant.mvc?Screen=BASK-AJAX-2';
					$.get(headerUrl, function(data) {
						$('#header_basket_response').html(data);																	   
					});
				}
				else if(data.search(/LULI FAMA: Sold Out!/i) != -1) {
					addingToCart = 0;
					$('#result_' + prodcode).html('<div class="error"><img src="images/img_error.png" alt="Error" class="notice_img"/>The product you have selected is out of stock. Please make another selection.</div>');					
				} else if(data.search(/:: Missing Product Attributes/i) != -1) {
					addingToCart = 0;
					$('#result_' + prodcode).html('<div class="error"><img src="images/img_error.png" alt="Error" class="notice_img"/>All of the attributes were not specified properly. Please make sure you specify a size and color.</div>');
					
				} else if(data.search(/: Limited Stock!/i) != -1) {
					addingToCart = 0;
					$('#result_' + prodcode).html('<div class="error"><img src="images/img_error.png" alt="Error" class="notice_img"/>The product you have chosen has a limited stock supply. Please make another selection.</div>');
					
				} else {
					addingToCart = 0;
					$('#result_' + prodcode).html('<div class="error"><img src="images/img_error.png" alt="Error" class="notice_img"/>There was an error adding your product to the cart. Please make another selection.</div>');
					//alert(data);
				}
			});
		}
		else{
			addingToCart = 0;
			$('#result_' + prodcode).html('<div class="error"><img src="images/img_error.png" alt="Error" class="notice_img"/>Both fields are required before continuing. Please make a selection.</div>');
		}
	}
}

$('.prod_preview .addToCart_btn').click(function(e) {
	e.preventDefault();
	var current_id = $(this).attr("id");
	var current_type = $(this).attr("class");
	if(current_type == 'pictured')
	{
		$('#popup_'+current_id+'_pictured').lightbox_me({
			centered: true,
			overlayCSS:	{background: 'white', 
opacity: .65}
	});
	}
	else
	{
		$('#popup_'+current_id).lightbox_me({
			centered: true 
		});
	}
});

function popupclose(product,pictured){
	document.productForm.reset();
	document.getElementById("result_" + product).innerHTML = '';
	document.getElementById("swatch_" + product).style.backgroundImage = '';
	if(pictured)
	{
		$('#popup_'+product+'_pictured').trigger('close');
	}
	else $('#popup_'+product).trigger('close');
}

$('.prodPic').click(function(e) {
	$("#picturedProdWrapper > .prod_preview").each(function() {
	    this.style.display = 'none';
	});
	
	var current_id = $(this).attr("id");	
	var idSplitArray = current_id.split(':',2);
	var picturedProd = idSplitArray[1].split('.');
	
	for(var i=0;i<picturedProd.length;i++)
	{
		if(document.getElementById("prodPicItem:" + picturedProd[i])) document.getElementById("prodPicItem:" + picturedProd[i]).style.display = 'block';
	}
	$('#accordion-steps').mvAccordion();
});



