// JavaScript Document

var flyingSpeed = 25;
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;

// *********************** ry8misi kinisis kai periexomenou flying div *************************
//function gia Top
function shoppingCart_getTopPos(inputObj)
{		
  var returnValue = inputObj.offsetTop;
  while((inputObj = inputObj.offsetParent) != null){
  	if(inputObj.tagName!='HTML')returnValue += inputObj.offsetTop;
  }
  return returnValue;
}

//function gia Left
function shoppingCart_getLeftPos(inputObj)
{
  var returnValue = inputObj.offsetLeft;
  while((inputObj = inputObj.offsetParent) != null){
  	if(inputObj.tagName!='HTML')returnValue += inputObj.offsetLeft;
  }
  return returnValue;
}
//ekkinisi script
function addToBasket(position,productId)
{
	if(!shopping_cart_div)shopping_cart_div = document.getElementById('shopping_cart');
	if(!flyingDiv){ 
		flyingDiv = document.createElement('DIV'); //dimiourgo ena flying div
		flyingDiv.style.position = 'absolute';
		document.body.appendChild(flyingDiv);
	}
	
	shopping_cart_x = shoppingCart_getLeftPos(shopping_cart_div); //briskw teliko seimio cart
	shopping_cart_y = shoppingCart_getTopPos(shopping_cart_div);

	currentProductDiv = document.getElementById('slidingProduct' + position); //trexon product
	document.getElementById('slidingProduct' + position).innerHTML = "<span style=\"color:#ccc;\">Lodging added to wish list. <br />See top left corner!</span>";
	
	currentXPos = shoppingCart_getLeftPos(currentProductDiv); //briskw seimeia ekinnisis gia ka8e product
	currentYPos = shoppingCart_getTopPos(currentProductDiv);
	
	diffX = shopping_cart_x - currentXPos; //i diafora arxikis meion telikis mas kanei tin apostasi pou dianeii
	diffY = shopping_cart_y - currentYPos;
	

	//dimiourgo periexomeno div
	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); //kalw flytobasket na paei sto kala8i
	
}
function removeFromBasket(productId)
{
	ajaxRemoveProduct(productId);
}

//function ypeu8ini gia ti kinisi - taxytita -topo8esia
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 + '")',10);
	}
	else{
		ajaxAddProduct(productId);	
	}
	
}


//**************ajax wishlist ********************
function ajaxAddProduct(productId)
{
var xmlHttp;
if (productId.length==0)
{ 
	document.getElementById("shopping_cart").innerHTML="keno";
	return;
}

xmlhttp=null;
xmlhttp = GetXmlHttpObject()

if (xmlhttp!=null)
  {
  var url="/en/_inc/addProduct.php";
  url = url + "?productId=" + productId;
  xmlhttp.onreadystatechange=state_Change;
  xmlhttp.open("GET",url,true);
  xmlhttp.send(null);
  }
else
  {
  alert("Your browser does not support XMLHTTP.");
  }
}


function ajaxRemoveProduct(productId)
{
var xmlHttp;
if (productId.length==0)
{ 
	document.getElementById("Removefromcart").innerHTML="keno";
	return;
}

xmlhttp=null;
xmlhttp = GetXmlHttpObject()

if (xmlhttp!=null)
  {
  var url="/en/_inc/removeProduct.php";
  url = url + "?productId=" + productId;
  xmlhttp.onreadystatechange=state_ChangeRemove;
  xmlhttp.open("GET",url,true);
  xmlhttp.send(null);
  }
else
  {
  alert("Your browser does not support XMLHTTP.");
  }
}

//***********************************************
function state_Change()
{
if (xmlhttp.readyState==4)
  {// 4 = "loaded"
  if (xmlhttp.status==200)
    {// 200 = "OK"
    document.getElementById('shopping_cart').innerHTML=xmlhttp.responseText;
    }
  else
    {
    alert("Problem retrieving data:" + xmlhttp.statusText);
    }
  }
}

function state_ChangeRemove()
{
if (xmlhttp.readyState==4)
  {// 4 = "loaded"
  if (xmlhttp.status==200)
    {// 200 = "OK"
    document.getElementById('Removefromcart').innerHTML=xmlhttp.responseText;
    }
  else
    {
    alert("Problem retrieving data:" + xmlhttp.statusText);
    }
  }
}


function GetXmlHttpObject()
{ 
var objXMLHttp=null
if (window.XMLHttpRequest)
{
objXMLHttp=new XMLHttpRequest()
}
else if (window.ActiveXObject)
{
objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
}
return objXMLHttp
} 

