	/************************************************************************************************************
	(C) www.dhtmlgoodies.com, March 2006
	
	This is a script from www.dhtmlgoodies.com. You will find this and a lot of other scripts at our website.	
	
	Terms of use:
	You are free to use this script as long as the copyright message is kept intact. However, you may not
	redistribute, sell or repost it without our permission.
	
	Version:
		1.0	Released	March. 3rd 2006
	
	Thank you!
	
	www.dhtmlgoodies.com
	Alf Magne Kalleland
	
	************************************************************************************************************/
	url_domain = 'images.costasur.com';
	var flyingSpeed = 25;
	var url_addProductToBasket = 'http://' + url_domain + '/images/gfx/addProduct.php';
	var url_removeProductFromBasket = 'http://' + url_domain + '/images/gfx/removeProduct.php';
	var txt_totalPrice = 'Total: ';
	
	var shopping_cart_div = false;
	var flyingDiv = false;
	var currentProductDiv = false;
	
	var shopping_cart_x = false;
	var shopping_cart_y = false;
	
	var slide_xFactor = false;
	var slide_yFactor = false;
	
	var diffX = false;
	var diffY = false;
	
	var currentXPos = false;
	var currentYPos = false;
	
	var ajaxObjects = new Array();
	
	
	function shoppingCart_getTopPos(inputObj)
	{		
	  var returnValue = inputObj.offsetTop;
	  while((inputObj = inputObj.offsetParent) != null){
	  	if(inputObj.tagName!='HTML')returnValue += inputObj.offsetTop;
	  }
	  return returnValue;
	}
	
	function shoppingCart_getLeftPos(inputObj)
	{
	  var returnValue = inputObj.offsetLeft;
	  while((inputObj = inputObj.offsetParent) != null){
	  	if(inputObj.tagName!='HTML')returnValue += inputObj.offsetLeft;
	  }
	  return returnValue;
	}
		
	
	function addToBasket(productId)
	{
		if(!document.getElementById('shopping_cart_items_product' + productId))
		{	// A product with this id is allready in the basket - just add number items
			
			
		if(!shopping_cart_div)shopping_cart_div = document.getElementById('shopping_cart');
		if(!flyingDiv){
			flyingDiv = document.createElement('DIV');
			flyingDiv.style.position = 'absolute';
			document.body.appendChild(flyingDiv);
		}
		
		shopping_cart_x = shoppingCart_getLeftPos(shopping_cart_div);
		shopping_cart_y = shoppingCart_getTopPos(shopping_cart_div);
	
		currentProductDiv = document.getElementById('slidingProduct' + productId);
		
		currentXPos = shoppingCart_getLeftPos(currentProductDiv);
		currentYPos = shoppingCart_getTopPos(currentProductDiv);
		
		diffX = shopping_cart_x - currentXPos;
		diffY = shopping_cart_y - currentYPos;
		
	
		
		var shoppingContentCopy = currentProductDiv.cloneNode(true);
		shoppingContentCopy.id='';
		flyingDiv.innerHTML = '';
		flyingDiv.style.left = currentXPos + 'px';
		flyingDiv.style.top = currentYPos + 'px';
		flyingDiv.appendChild(shoppingContentCopy);
		flyingDiv.style.display='block';
		flyingDiv.style.width = currentProductDiv.offsetWidth + 'px';
		flyToBasket(productId);
		}
		else alert(textoAlojamientoExiste);
		
	}
	
	
	function flyToBasket(productId)
	{
		var maxDiff = Math.max(Math.abs(diffX),Math.abs(diffY));
		var moveX = (diffX / maxDiff) * flyingSpeed;;
		var moveY = (diffY / maxDiff) * flyingSpeed;	
		
		currentXPos = currentXPos + moveX;
		currentYPos = currentYPos + moveY;
		
		flyingDiv.style.left = Math.round(currentXPos) + 'px';
		flyingDiv.style.top = Math.round(currentYPos) + 'px';	
		
		
		if(moveX > 0 && currentXPos > shopping_cart_x)
		{
			flyingDiv.style.display='none';		
		}
		if(moveX < 0 && currentXPos < shopping_cart_x)
		{
			flyingDiv.style.display='none';		
		}
			
		if(flyingDiv.style.display=='block')setTimeout('flyToBasket("' + productId + '")',60); else ajaxAddProduct(productId);	
	}
	
	function showAjaxBasketContent(ajaxIndex)
	{
		// Getting a reference to the shopping cart items table
		var itemBox = document.getElementById('shopping_cart_items');
		
		var productItems = ajaxObjects[ajaxIndex].response.split('|||');	// Breaking response from Ajax into tokens
		
		if(document.getElementById('shopping_cart_items_product' + productItems[0]))
		{	
			// A product with this id is allready in the basket - just add number items
			var row = document.getElementById('shopping_cart_items_product' + productItems[0]);
			var items = row.cells[0].innerHTML /1;
			items = items + 1;
			row.cells[0].innerHTML = items;
		}
		else
		{	
			var tr = itemBox.insertRow(-1);
			tr.id = 'shopping_cart_items_product' + productItems[0];

			var td = tr.insertCell(-1);
		
			/*var y;
			y = document.createElement('a');
			y.innerHTML = 'test2';
			y.href = 'test2';
			
			var c;
			c = document.createElement('td');
			
			c.appendChild(y);	
			
			var r;
			r = document.createElement('tr');
		
			r.appendChild(c);
        
			var t;
			t = document.createElement('table');
			t.setAttribute('border', '1');
			t.appendChild(r);

			td.innerHTML = t.innerHTML;*/
	
			
			
			
			
			/*var tb_1 = document.createElement("table");
        	tb_1.setAttribute("cellpadding", "1");
        	tb_1.setAttribute("cellspacing", "1");
        	tb_1.setAttribute("width", "100%");   
        	tb_1.setAttribute("class", "TeaserTitle");
        	tb_1.style.border = '1px solid #E5E5E5';
        	
			td.appendChild(tb_1);
        	
        	
        	 var row_1 = document.createElement("tr");
 			 tb_1.appendChild(row_1);
        	 
        	 var cell_11 = document.createElement("td");
        	 cell_11.setAttribute("width", "80%");
        	 cell_11.setAttribute("colspan", "2");
        	 cell_11.setAttribute("class", "TeaserTitle");
 			 row_1.appendChild(cell_11);
        	 
        	 
 			 var a_11 = document.createElement('A');
			 a_11.href = 'http://' + productItems[2] + '/sites/' + productItems[1] + '/' + productItems[6];
			 a_11.target = '_infobox';
			 cell_11.appendChild(a_11);
			 
			 var aText_11 = document.createTextNode(productItems[3]);
			 a_11.appendChild(aText_11);

        	 var cell_12 = document.createElement("td");
        	 cell_12.setAttribute("width", "20%");
        	 cell_12.setAttribute("align", "right");
        	 cell_12.setAttribute("valign", "top");
 			 row_1.appendChild(cell_12);
        	 
        	 
        	var a_12 = document.createElement('A');
       	 	a_12.setAttribute("href", "#");
			a_12.onclick = function(){ removeProductFromBasket(productItems[0]); };
			cell_12.appendChild(a_12);
			
			var img_12 = document.createElement('IMG');
			img_12.src = 'http://' + productItems[2] + '/images/gfx/remove.gif';
			img_12.border = '0';
			img_12.width = '13';
			img_12.height = '13';
			a_12.appendChild(img_12);


        	var row_2 = document.createElement("tr");
 			tb_1.appendChild(row_2);
        	 
        	var cell_21 = document.createElement("td");
 			
 			cell_21.style.fontSize = '9px';
 			cell_21.style.fontWeight = 'normal';
 			cell_21.style.marginLeft = '1px';
 			cell_21.valign = 'top';
 		  	cell_21.setAttribute("colspan", "3");
 			row_2.appendChild(cell_21);
 		  	
      	 
 			var tb_2   = document.createElement("table");
			cell_21.appendChild(tb_2);

         	var row_21 = document.createElement("tr");
 			tb_2.appendChild(row_21);
        	 
        	var cell_211 = document.createElement("td");
 			row_21.appendChild(cell_211);

 			var a_211 = document.createElement('A');
			a_211.href = 'http://' + productItems[2] + '/sites/' + productItems[1] + '/' + productItems[6];
			a_211.target = '_infobox';
			cell_211.appendChild(a_211);
			
			
			var img_211 = document.createElement('IMG');
			img_211.src = 'http://' + productItems[2] + '/images/categories/thumbnails/' + productItems[1] + '0.jpg';
			img_211.border = '0';
			img_211.width = '40';
			img_211.height = '40';
			img_211.style.border = '1px solid #C4C4C4';
			img_211.style.marginRight = '0px';
			a_211.appendChild(img_211);
			 
        	var cell_212 = document.createElement("td");
 			row_21.appendChild(cell_212);

 			var a_212 = document.createElement('A');
			a_212.href = 'http://' + productItems[2] + '/sites/' + productItems[1] + '/' + productItems[6];
			a_212.target = '_infobox';
			a_212.style.color = '#3366AA';
			a_212.style.fontSize = '9px';
			cell_212.appendChild(a_212);
			
			
   			var aText_212 = document.createTextNode(productItems[5]);
            a_212.appendChild(aText_212);
            
           	 var row_3 = document.createElement("tr");
 			 tb_1.appendChild(row_3);
        	 
        	 var cell_13 = document.createElement("td");
        	 cell_13.setAttribute("colspan", "2");
        	 cell_13.style.fontSize = '9px';
			 cell_13.style.color = '#3366AA';
			 cell_13.style.fontWeight = 'bold';
 			 row_3.appendChild(cell_13);
        	 
        	 
			 var cellText_13 = document.createTextNode(productItems[7] + ' - ' + productItems[8]);
			 cell_13.appendChild(cellText_13);

        	 var cell_23 = document.createElement("td");
        	 cell_23.setAttribute("align", "right");
        	 cell_23.style.fontSize = '9px';
        	 cell_23.style.color = '#000000';
			 cell_23.style.fontWeight = 'bold';
 			 row_3.appendChild(cell_23);
        	 
        	 
			 var cellText_23 = document.createTextNode(productItems[9]);
			 cell_23.appendChild(cellText_23);*/
			
			var span_1 = document.createElement("span");
			var tb_1 = document.createElement("table");
        	tb_1.setAttribute("cellpadding", "1");
        	tb_1.setAttribute("cellspacing", "1");
        	tb_1.setAttribute("width", "100%");
        	tb_1.setAttribute("class", "TeaserTitle");
        	
        	 if(productItems[9] > 0){tb_1.style.backgroundColor = "#FFFFE6";}else{tb_1.style.backgroundColor = "#F0FFFF";}
        	
        	 var row_1 = document.createElement("tr");
 			 tb_1.appendChild(row_1);
 			 span_1.appendChild(tb_1);
        	 
        	 var cell_11 = document.createElement("td");
        	 cell_11.setAttribute("width", "80%");
        	 cell_11.setAttribute("colspan", "2");
        	 cell_11.setAttribute("class", "TeaserTitle");
         	 
			if(productItems[9] > 0){cell_11.style.backgroundColor = "#FFFFE6";}else{cell_11.style.backgroundColor = "#F0FFFF";}
        
 			 row_1.appendChild(cell_11);
        	 
        	 
 			 var a_11 = document.createElement('A');
			 a_11.href = 'http://' + productItems[2] + '/sites/' + productItems[1] + '/' + productItems[6]+'/';
			 a_11.target = '_infobox';
			 a_11.style.fontSize = '10px';
			 a_11.style.fontWeight = 'bold';
			 cell_11.appendChild(a_11);
			 
			 var aText_11 = document.createTextNode(productItems[3]);
			 a_11.appendChild(aText_11);

        	 var cell_12 = document.createElement("td");
        	 cell_12.setAttribute("width", "20%");
        	 cell_12.setAttribute("align", "right");
        	 cell_12.setAttribute("valign", "top");
 			 row_1.appendChild(cell_12);
        	 
        	 
        	var a_12 = document.createElement('A');
			var img_12 = document.createElement('IMG');
			img_12.src = 'http://' + productItems[2] + '/images/gfx/remove.gif';
			img_12.border = '0';
			img_12.width = '13';
			img_12.height = '13';

       	 	a_12.href = "#";
			//a_12.onclick = function(){removeProductFromBasket(productItems[0]);}; 
			a_12.setAttribute("onclick", "javascript:removeProductFromBasket('" + productItems[0] + "');");

			a_12.appendChild(img_12);
			cell_12.appendChild(a_12);
			


        	var row_2 = document.createElement("tr");
 			tb_1.appendChild(row_2);
        	 
        	var cell_21 = document.createElement("td");
 			
 			cell_21.style.fontSize = '9px';
 			cell_21.style.fontWeight = 'normal';
 			cell_21.style.marginLeft = '1px';
 			cell_21.valign = 'top';
 		  	cell_21.setAttribute("colspan", "3");
 			row_2.appendChild(cell_21);
 		  	
      	 
 			var tb_2   = document.createElement("table");
			cell_21.appendChild(tb_2);

         	var row_21 = document.createElement("tr");
 			tb_2.appendChild(row_21);
        	 
        	var cell_211 = document.createElement("td");
 			row_21.appendChild(cell_211);

 			var a_211 = document.createElement('A');
			a_211.href = 'http://' + productItems[2] + '/sites/' + productItems[1] + '/' + productItems[6]+'/';
			a_211.target = '_infobox';
			cell_211.appendChild(a_211);
			
			
			var img_211 = document.createElement('IMG');
			img_211.src = 'http://' + productItems[2] + '/images/categories/thumbnails/' + productItems[1] + '0.jpg';
			img_211.border = '0';
			img_211.width = '40';
			img_211.height = '40';
			img_211.style.border = '1px solid #C4C4C4';
			img_211.style.marginRight = '0px';
			a_211.appendChild(img_211);
			 
        	var cell_212 = document.createElement("td");
 			row_21.appendChild(cell_212);

 			var a_212 = document.createElement('A');
			a_212.href = 'http://' + productItems[2] + '/sites/' + productItems[1] + '/' + productItems[6]+'/';
			a_212.target = '_infobox';
			a_212.style.color = '#3366AA';
			a_212.style.fontSize = '9px';
			cell_212.appendChild(a_212);
			
						
   			var aText_212 = document.createTextNode(productItems[5]);
            a_212.appendChild(aText_212);
            
           	 var row_3 = document.createElement("tr");
 			 tb_1.appendChild(row_3);
        	 
        	 var cell_13 = document.createElement("td");
        	 var span_13 = document.createElement("span");
        	 
        	 cell_13.colSpan = "2";
        	 span_13.style.fontSize = '9px';
			 span_13.style.color = '#3366AA';
			 span_13.style.fontWeight = 'bold';
        	 
			 if(productItems[8] == "no")
			 {
			 	var cellText_13 = document.createTextNode(productItems[7]);
			 }
			 else
			 {
			 	var cellText_13 = document.createTextNode(productItems[7] + ' - ' + productItems[8]);
			 }
			 
			 span_13.appendChild(cellText_13);
			 cell_13.appendChild(span_13);
 			 row_3.appendChild(cell_13);


        	 var cell_23 = document.createElement("td");
        	 var span_23 = document.createElement("span");
			 
        	 cell_23.style.textAlign = 'right';
        	 cell_23.style.whiteSpace="nowrap";
        	 
        	 span_23.style.fontSize = '9px';
        	 span_23.style.color = '#000000';
			 span_23.style.fontWeight = 'bold';
			 span_23.id = 'FramePriceId_'+ (productItems[13]);
			 

			 if(productItems[9] > 0)
			 {
				 var cellText_23 = document.createTextNode(productItems[9] + ' ' + productItems[15]);
				 var img_23 = document.createElement('IMG');
				 img_23.src = 'http://' + productItems[2] + '/images/gfx/euro_5.png'; 
				 img_23.border = '0';

				 span_23.appendChild(cellText_23);			
				 cell_23.appendChild(span_23);			
				 //cell_23.appendChild(img_23);			
 				 row_3.appendChild(cell_23);
			 }
 			 
 			 if(productItems[10] == 1)
 			 {
 		 	 	var row_4 = document.createElement("tr");
		 	 	tb_1.appendChild(row_4);

		 	 	
		 		var cell_14 = document.createElement("td");
        	 	cell_14.colSpan = "3";
        	 	cell_14.align = "center";
        	 	row_4.appendChild(cell_14);
        	 	
        	 	var a_14 = document.createElement('A');
				a_14.href = 'http://' + productItems[2] + '/sites/' + productItems[1] + '/' + productItems[6]+'/';
				a_14.target = '_infobox';
				a_14.style.color = '#008800';
				a_14.style.fontSize = '9px';
				cell_14.appendChild(a_14);
						
   				var aText_14 = document.createTextNode(productItems[11]);
            	a_14.appendChild(aText_14);
 			 }
        	 
        	 
			 
			td.innerHTML = span_1.innerHTML;
			td.style.border = '1px solid #E5E5E5';
			td.style.backgroundColor = '#fff8ee';
			td.colSpan = "3";
		} 
		updateTotalPrice();
		ajaxObjects[ajaxIndex] = false;		
		
		FrameArrayCurrency[productItems[13]] = new Array('FramePriceId_'+productItems[13],'4',productItems[9] + '_' + productItems[14]);
	}
	
	
/*	function updateTotalPrice()
	{
		var itemBox = document.getElementById('shopping_cart_items');
		var totalPrice = 0;
		if(document.getElementById('shopping_cart_totalprice')){
			for(var no=1;no<itemBox.rows.length;no++){
				totalPrice = totalPrice + (itemBox.rows[no].cells[0].innerHTML.replace(/[^0-9]/g) * itemBox.rows[no].cells[2].innerHTML);
				
			}		
			document.getElementById('shopping_cart_totalprice').innerHTML = txt_totalPrice + totalPrice.toFixed(2);
			
		}	
		
	}*/
	
	function updateTotalPrice()
	{
		var itemBox = document.getElementById('shopping_cart_items');
		// Calculating total price and showing it below the table with basket items
		var totalPrice = 0;
		if(document.getElementById('shopping_cart_totalprice'))
		{
			totalPrice = itemBox.rows.length - 1;
			document.getElementById('shopping_cart_totalprice').innerHTML = totalPrice + ' infobox(s)';
		}	
		
	}
	
	function removeProductFromBasket(productId)
	{
		var productRow = document.getElementById('shopping_cart_items_product' + productId);
		productRow.parentNode.removeChild(productRow);	
		updateTotalPrice();
		ajaxRemoveProduct(productId);	
	}
	
	function ajaxValidateRemovedProduct(ajaxIndex)
	{
		if(ajaxObjects[ajaxIndex].response!='OK')alert('Error while removing product from the database');
		
	}
	
	function ajaxRemoveProduct(productId)
	{
		var ajaxIndex = ajaxObjects.length;
		ajaxObjects[ajaxIndex] = new sack();
		ajaxObjects[ajaxIndex].requestFile = url_removeProductFromBasket;	// Saving product in this file
		ajaxObjects[ajaxIndex].setVar('productIdToRemove',productId);
		ajaxObjects[ajaxIndex].onCompletion = function(){ ajaxValidateRemovedProduct(ajaxIndex); };	// Specify function that will be executed after file has been found
		ajaxObjects[ajaxIndex].runAJAX();		// Execute AJAX function		
	}
	
	function ajaxAddProduct(productId)
	{
		var ajaxIndex = ajaxObjects.length;
		ajaxObjects[ajaxIndex] = new sack();
		ajaxObjects[ajaxIndex].requestFile = url_addProductToBasket;	// Saving product in this file
		ajaxObjects[ajaxIndex].setVar('productId',productId);
		ajaxObjects[ajaxIndex].onCompletion = function(){ showAjaxBasketContent(ajaxIndex); };	// Specify function that will be executed after file has been found
		ajaxObjects[ajaxIndex].runAJAX();		// Execute AJAX function		
	}