
function swopMainImage(url){
	var mainIMG = document.getElementById("mainIMG");
	mainIMG.setAttribute("src",url);
};
  
function loadingCart(){ 
	$(document.body).prepend("<div class='overlayDiv'><div class='overlayPopup'></div></div>");  
	$("div.overlayDiv").css({ 
			height:$(document).height()
	});  
	$("div.overlayPopup").animate({  
		marginTop:250  
	},500).html("Loading...");  
}
  
function isNumeric(t,even){
	if (even.keyCode == 13)
		return false;
	var numb = "0123456789";
	var w = "";
	for (i=0; i < t.value.length; i++) {
		x = t.value.charAt(i);
		if (numb.indexOf(x,0) != -1)
			w += x;
	}
	t.value = w;
	
	return true;
}
function addCartAction() {
	if(!isCartAvailable){
		return;
	}
	
	//Scroll to top of page for custome to see adding cart event
	$('html, body').animate({scrollTop:0}, 'slow');
	
	isCartAvailable = false;
	//TODO change image for add button		
	var productX 		= $("#mainIMGDiv").offset().left;
	var productY 		= $("#mainIMGDiv").offset().top;
	
	var basketX 		= $("#cartIcon").offset().left;
	var basketY 		= $("#cartIcon").offset().top;
	
	var gotoX 			= basketX - productX;
	var gotoY 			= basketY - productY;
	
	var newImageWidth 	= $("#mainIMGDiv").width() / 6;
	var newImageHeight	= $("#mainIMGDiv").height() / 6;		
	
	$("#mainIMGDiv img")
	.clone()
	.prependTo("#mainIMGDiv")
	.css({'position' : 'absolute'})
	.animate({opacity: 0.5}, 100 )
	.animate({opacity: 0.1, marginLeft: gotoX, marginTop: gotoY, width: newImageWidth, height: newImageHeight}, 1000, addCartRequest);
}

var isCartAvailable = true;
function addCartRequest(){
	$(this).remove();
	
	$("#cartIcon").attr('src', 'theme/images/loading.gif');				
	var asinx = $('#asinHidden').val();
	var offerID = $('#offerIdHidden').val();	
	var quantity = $('#quantity').val();	
	var jdate = new Date(); //append with time to prevent caching data
	$.ajax({  
		type: "GET",  
		url: "index.php",  
		data: "type=cart&action=add&quantity="+quantity+"&asin="+asinx+"&jtime="+jdate.getTime()+"&offerID="+offerID, 
		dataType: "json",
		cache: false,
		success: function(res) {
			if(res.success){
				//change cart text
				$("#cartInfo").html(res.cart);
				//show message	
				cartNotification(res.message);
			
			} else {
				//show error message
				cartNotification(res.message);
			}
			isCartAvailable = true;
			$("#cartIcon").attr('src', 'theme/images/yourbag3.png');
		}  
	});
};

function cartNotification(message){	
	$("#cart-result").html(message);				
	var $alertx = $('#cart-alert');
	if($alertx.length){
		var atimer = window.setTimeout(function () {
			$alertx.trigger('click');
		}, 3000);
		$alertx.animate({width: '300px'}, 250)
		.click(function () {
			window.clearTimeout(atimer);
			$alertx.animate({width: '00'}, 250);
		});
	}
};

//---------------------------------

var setFloatInterval = false;
var setFloatTimeOut = false;
function stopFloat(){
	clearInterval(setFloatInterval);
}
function floatDiv(id, startY, maxHeight) {
	var element = $("#" + id);
	window[id + "_object"] = element;
	element.currentY = element.startY = startY;
	element.setPosition = function(newY){
		this.css('top', newY);
	};
	element.float = function(){
		var pageY = $(document).scrollTop();
		this.currentY += (pageY + this.startY - this.currentY)/8;	
		if(this.currentY > maxHeight){
			this.currentY = maxHeight;
		}
		this.setPosition(this.currentY);
		$(window).scroll(function(){
			if(setFloatInterval != false){
				clearInterval(setFloatInterval);
				clearTimeout(setFloatTimeOut);
			}
			setFloatInterval = setInterval(id + "_object.float()", 40);
			setFloatTimeOut = setTimeout("stopFloat()", 4000);
		});		
	};
	return element;
}




//Add event listenner
$(document).ready(function() {
	
	//Tabbed float cart
	var maxHeight = $("div.container").height() - 220;		
	floatDiv("cartTab", 220, maxHeight).float();	
	$("div#cartTab").hover(function(){
		$(this).toggleClass('hoverActive').find("img#cartTabImage").attr('src', 'theme/images/cart2.gif');
	}, function(){
		$(this).toggleClass('hoverActive').find("img#cartTabImage").attr('src', 'theme/images/cart1.gif');
	});
	
	
	//Add cart button action
	$("#addcart").click(addCartAction);
	
	//Offer add cart action
	$(".addOfferCart").click(function(){
		$('#offerIdHidden').attr('value', $(this).attr('offerID'));									  
		addCartAction();
	});
	
	
		
	//Set action to variant selection box			
	$("#variantBox tr").click(function() {
		$("#variantBox tr").removeClass("click");
		$(this).toggleClass("click");
		
		$(this).find('input').attr('checked', 'checked');
		$('#asinHidden').attr('value', $(this).attr('asin'));
		$('#offerIdHidden').attr('value', $(this).attr('offerID'));
		
		$('#mainIMG').attr('src', $(this).attr('large'));			
		$('#merchantName').html($(this).attr('merchantname'));			
		
		var available = '';
		if($(this).attr('availability'))
			available = '<img src="theme/images/instock.gif" width="52" height="20" alt="Jewelry in stock" align="absmiddle"/><span>'+$(this).attr('availability')+'</span>';
		if($(this).attr('shipping'))
			available += ' - <strong>FREE</strong> shipping';
		
		$('#info_availability').html(available);
		
		var infoprice = '';
		if($(this).attr('color'))
			infoprice += $(this).attr('color');
		if($(this).attr('length'))
			infoprice += ' : ' + $(this).attr('length');
		if($(this).attr('metal'))
			infoprice += ' : ' + $(this).attr('metal');
		if($(this).attr('size'))
			infoprice += ' : ' + $(this).attr('size');
		if($(this).attr('price'))
			infoprice += ' : Price ' + $(this).attr('price');
		$('#info_activeoption').html( '[' + infoprice + ']');		
	})
	
	$("#variantBox tr").mouseover(function(){ $(this).css({'cursor':'pointer'}); });
	
	$("#variantBox tr:even").addClass("row1");
				
		
	//Amazin information link
	$(".fancy_link, a.iframe, #account>a").fancybox({'width':980, 'height':640, 'centerOnScroll':true });
	
	
});
