var rootURL;
rootURL="/";

var aryPhotoAnimStatus= new Array();



function randomString() {
	var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
	var string_length = 10;
	var randomstring = '';
	for (var i=0; i<string_length; i++) {
		var rnum = Math.floor(Math.random() * chars.length);
		randomstring += chars.substring(rnum,rnum+1);
	}
	return randomstring;
}


function findPos(idOfObj) {

	var obj;
	obj = document.getElementById(idOfObj);
	
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}



function ajaxItemToCart(IMG,intItemID){

		//IMG.style.display = "none";
		
		if (aryPhotoAnimStatus[intItemID] == null){
			//alert('setting null value');
			aryPhotoAnimStatus[intItemID] = "0";
		}
				
				
		if (aryPhotoAnimStatus[intItemID] == "0"){
		
			
			//alert('value not 1');
			aryPhotoAnimStatus[intItemID] = "1";
			var url;
			url = rootURL + "ajax/AddItemToCart.aspx?product=" + intItemID;
			
		
			var http_request = false;

			if (window.XMLHttpRequest) { // Mozilla, Safari,...
					http_request = new XMLHttpRequest();
					if (http_request.overrideMimeType) {
							http_request.overrideMimeType('text/xml');
							// See note below about this line
					}
			} else if (window.ActiveXObject) { // IE
					try {
							http_request = new ActiveXObject("Msxml2.XMLHTTP");
					} catch (e) {
							try {
									http_request = new ActiveXObject("Microsoft.XMLHTTP");
							} catch (e) {}
					}
			}

			if (!http_request) {
					alert('Giving up :( Cannot create an XMLHTTP instance');
					return false;
			}

		
			http_request.onreadystatechange = function() { alertContentsAddCart(http_request,intItemID,IMG); };
			http_request.open('GET', url, true);
			http_request.send(null);
	        
        
        }

}
 
function alertContentsAddCart(http_request,intItemID,IMG) {
        //alert('Ready State: '+http_request.readyState+'\nHTTP Status: '+http_request.status);
        if ((http_request.readyState == 4) && (http_request.status == 200)) {
              //alert(http_request.responseText);    
              
              if (http_request.responseText != "1"){
				 	//alert('Error adding product to cart.');
					IMG.src = rootURL + 'custom%2Fimages%2Fbuttons%2FbtnAddToCart.jpg';
					
					var div;
					div = document.getElementById('divTextOverlap'+intItemID);				
					div.style.display = "none";
					
					setTimeout("UnlockButton("+intItemID+")",1000);					
					setTimeout("UpdateViewCartText()",1000);
					
              }
              else{
					//alert('Product Added');
					//change button image
					IMG.src = rootURL + 'custom%2Fimages%2Fbuttons%2FbtnRemoveFromCart.jpg';
					//IMG.style.display = 'none';
				
					
					//find cart coordinates				
					var aryCartPosition;
					aryCartPosition = findPos('imgViewCart');	
					
					var aryProductPosition;
					aryProductPosition = findPos('imgA' + intItemID);	
					
					var aryTDPosition;
					aryTDPosition = findPos('tdProductPhoto' + intItemID);	
										
					
					var div;
					div = document.getElementById('divTextOverlap'+intItemID);
					div.style.top = aryTDPosition[1];
					div.style.left = aryTDPosition[0] + 5;
					div.style.display = "inline";
				
					
					var intHeightOfRectangle;
					var intWidthOfRectangle;					
					intHeightOfRectangle = Math.abs(aryCartPosition[1] - aryProductPosition[1]);
					intWidthOfRectangle = Math.abs(aryCartPosition[0] - aryProductPosition[0]);		
					
								
					intHeightOfRectangle = intHeightOfRectangle;//adjust	
					intWidthOfRectangle = intWidthOfRectangle;//adjust		
					
						
					//requires 1 sec
					MoveImageToCart('imgA' + intItemID,intHeightOfRectangle,intWidthOfRectangle);
					
					//requires 0 sec			
					setTimeout("hideImage('imgA"+intItemID+"')",1000);
						
					//requires 0 sec
					setTimeout("MoveImageBackFromCart('imgA"+intItemID+"',"+intHeightOfRectangle+","+intWidthOfRectangle+")", 1000);
				
					//requires 0 sec
					setTimeout("showImage("+intItemID+")",1000);
					
					setTimeout("UnlockButton("+intItemID+")",1000);						
					
					setTimeout("UpdateViewCartText()",1000);					
					
              }                
        }
}





function UnlockButton(intItemID){
	//alert('unlocking button ' + intItemID); 
	aryPhotoAnimStatus[intItemID] = "0";
}

function MoveImageToCart(imgID,intHeightOfRectangle,intWidthOfRectangle){	
	Effect.MoveBy(imgID,(-1 * intHeightOfRectangle),intWidthOfRectangle,{duration: 1});	
	new Effect.Scale(imgID, 10);					
}

function MoveImageBackFromCart(imgID,intHeightOfRectangle,intWidthOfRectangle){
	Effect.MoveBy(imgID,intHeightOfRectangle,(-1 * intWidthOfRectangle),{duration: 0});	
	new Effect.Scale(imgID, 1000);		
}

function hideImage(imgID){
	var img;
	img = document.getElementById(imgID);
	img.style.display = "none";
}

function showImage(intItemID){
	var img;
	img = document.getElementById('imgA'+intItemID);
	img.style.display = "inline";
	aryPhotoAnimStatus[intItemID]="0";
}




function UpdateViewCartText(){

	var url;
	url = rootURL + "ajax/getCartProductCountAndTotal.aspx?r="+randomString();

	var http_request = false;

        if (window.XMLHttpRequest) { // Mozilla, Safari,...
                http_request = new XMLHttpRequest();
                if (http_request.overrideMimeType) {
                        http_request.overrideMimeType('text/xml');
                        // See note below about this line
                }
        } else if (window.ActiveXObject) { // IE
                try {
                        http_request = new ActiveXObject("Msxml2.XMLHTTP");
                } catch (e) {
                        try {
                                http_request = new ActiveXObject("Microsoft.XMLHTTP");
                        } catch (e) {}
                }
        }

        if (!http_request) {
                alert('Giving up :( Cannot create an XMLHTTP instance');
                return false;
        }

        http_request.onreadystatechange = function() { alertContentsCountTotal(http_request); };
        http_request.open('GET', url, true);       
        http_request.send(null);
        
}


function alertContentsCountTotal(http_request) {
        //alert('Ready State: '+http_request.readyState+'\nHTTP Status: '+http_request.status);
        if ((http_request.readyState == 4) && (http_request.status == 200)) {
              //alert(http_request.responseText);    
                           
              var spanCart;
				spanCart = document.getElementById('CustomerCart');
					
              
				if (http_request.responseText != "0"){				
					var aryParts=http_request.responseText.split("|");			
					spanCart.innerHTML = '<a href=\''+rootURL+'ViewCart.aspx\'><small>Your Cart has '+aryParts[0]+' Items<br>Total of $'+aryParts[1]+'</small></a>';
				}
				else{
					spanCart.innerHTML = '<small>Your Cart is Empty</small>';
				}
				   
        }
}
		
	