/************************************************************************************************************
(C) www.dhtmlgoodies.com, March 2006

Este script es adaptado para la pagina web de la diputación de CÁCERES.
Akash Daswani.
Puntos importantes:
	- EL parámetro tipo recibe alo (alojamientos) o res (restaurantes).
	- Sigue un orden secuencial -> addToBasket ,flyToBasket, ajaxAddProduct. 
	- EL ultimo redirecciona a la pagina correspondiente.

************************************************************************************************************/

var flyingSpeed = 4;
var url_addProductToBasket = 'http://demos.inerza.com/caceres/templates/caceres/addProduct.php';
var url_removeProductFromBasket = 'http://demos.inerza.com/caceres/templates/caceres/removeProduct.php';
var txt_totalPrice = 'Total: ';


var shopping_cart_div = false;
var flyingDiv = false;
var currentProductDiv = false;

var shopping_cart_x =500;
var shopping_cart_y = 600;

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 productoinsertado(id) {
	alert("El artículo ya se encuentra en tu colección.");
	window.location="index.php";

}

function addToBasket(productId, tipo)
{

	if(!shopping_cart_div)shopping_cart_div = document.getElementById('contenidos');
	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, tipo);

}


function flyToBasket(productId, tipo)
{
	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+'","'+tipo+'")', 10); 
	else ajaxAddProduct(productId, tipo);
}

function URLEncode(text)
{
	var texto = text + "";
	// The Javascript escape and unescape functions do not correspond
	// with what browsers actually do...
	var SAFECHARS = "0123456789" +					// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";					// RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";

	var encoded = "";
	for (var i = 0; i < texto.length; i++ ) {
		var ch = texto.charAt(i);
	    if (ch == " ") {
		    encoded += "+";				// x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
			    alert( "Unicode Character '" 
                        + ch 
                        + "' cannot be encoded using standard URL encoding.\n" +
				          "(URL encoding only supports 8-bit characters.)\n" +
						  "A space (+) will be substituted." );
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} // for
	return (encoded);
};

function ajaxAddProduct(productId, tipo)
{
	var ajaxIndex = ajaxObjects.length;
	ajaxObjects[ajaxIndex] = new sack();
	var s = null;
	if (tipo!='') {
		s = "index.php?option=com_cesta&task=ins&id="+productId+"&tipo="+tipo; // AQui se insertaría en nuestra CESTA!
	}
	else {
		s = "index.php?option=com_cesta&task=ins&id="+productId; // AQui se insertaría en nuestra CESTA!
	}
	
	s = s + "&redirectTo=" + URLEncode(window.location);
	window.location = s;
}