/*
 * Strutture Preferite - Plugin
 * Copyright (c) 2011 Francesco Zacchino
 * Version: 1.5 (15-APR-2011)
 */


function getEl(name)
{
	return document.getElementById(name); 
}

function shiftAnimStatus(houseId)
{
	showAddAnim(houseId);
}
   

var aniDestX; 
var aniDestY; 
var aniObj = null; 
var aniStepX; 
var aniStepY; 
var aniStepWidth; 
var aniStepHeight; 
var aniCount; 
function showAddAnim(id)
{
	aniCount = 12; 
	var startWidth = 0; 
	var startHeight = 0; 
	var endWidth = 11; 
	var endHeight = 11; 
	var po = getEl("p" + id); 
	if(po == null){
	po = getEl("mainpic"); 
	}
	var so = getEl("Anim"); 
	if(po != null && so != null){
	var px = findPosX(po); 
	var py = findPosY(po); 
	aniDestX = findPosX(so); 
	aniDestY = findPosY(so); 
	startWidth = po.width; 
	startHeight = po.height; 
	if(aniObj == null){
	var wi = document.createElement("div"); 
	wi.style.borderStyle = "solid"; 
	wi.style.borderWidth = "3px"; 
	wi.style.borderColor = "#CC0000"; 
	wi.style.position = "absolute"; 
	wi.style.zIndex = 300; 
	document.body.appendChild(wi); 
	aniObj = wi; 
	}
	else {
	aniObj.style.display = "block"; 
	}
	aniObj.style.width = startWidth + 'px'; 
	aniObj.style.height = startHeight + 'px'; 
	aniObj.style.left = px + 'px'; 
	aniObj.style.top = py + 'px'; 
	var centerX = px + startWidth / 2; 
	var centerY = py + startHeight / 2; 
	aniStepX = Math.round((aniDestX - centerX) / aniCount);
	aniStepY = Math.round((aniDestY - centerY) / aniCount);
	aniStepWidth = Math.round((startWidth - endWidth) / aniCount);
	aniStepHeight = Math.round((startHeight - endHeight) / aniCount);
	animateAnim(); 
	}
}


function animateAnim(){
   var x = 0; 
   var y = 0; 
   if(aniCount == 0){
      if(aniObj != null){
         aniObj.style.display = "none"; 
         }
      return; 
      }
   else if(aniCount == 1){
      x = aniDestX; 
      y = aniDestY; 
      }
   else {
      var curWidth = getNumNoPx(aniObj.style.width); 
      var curHeight = getNumNoPx(aniObj.style.height); 
      var newWidth = curWidth - aniStepWidth + 1; 
      var newHeight = curHeight - aniStepHeight + 1; 
      var curX = findPosX(aniObj); 
      var curY = findPosY(aniObj); 
      var curCenterX = curX + curWidth / 2; 
      var curCenterY = curY + curHeight / 2; 
      var newCenterX = curCenterX + aniStepX; 
      var newCenterY = curCenterY + aniStepY; 
      aniObj.style.width = newWidth + 'px'; 
      aniObj.style.height = newHeight + 'px'; 
      x = newCenterX - newWidth / 2; 
      y = newCenterY - newHeight / 2; 
      }
   aniObj.style.left = x + 'px'; 
   aniObj.style.top = y + 'px'; 
   --aniCount; 
   setTimeout("animateAnim()", 29); 
   }

function getNumNoPx(num)
{
if(num.indexOf("px") !=- 1)return num.substring(0, num.length - 2); 
return num; 
}
function findPosX(obj){
var curleft = 0; 
if(obj.offsetParent){
while (obj.offsetParent){
curleft += obj.offsetLeft; 
obj = obj.offsetParent; 
}
}
else if(obj.x){
curleft += obj.x; 
}
return curleft; 
}
function findPosY(obj){
var curtop = 0; 
if(obj.offsetParent){
while (obj.offsetParent){
curtop += obj.offsetTop; 
obj = obj.offsetParent; 
}
}
else if(obj.y){
curtop += obj.y; 
}
return curtop; 
}


function getCookie(check_name) {

	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	
	if (cookie_name == null)
	{
		var cookie_name = '';
	}
	var cookie_value = '';
	var b_cookie_found = false; 

	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		a_temp_cookie = a_all_cookies[i].split( '=' );
		
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}

var cont = 0;

var idCont = 0;



function checkPrefer() {

	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false;

	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		a_temp_cookie = a_all_cookies[i].split( '=' );
		
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
		
		nome = "tastoPr" + a_temp_cookie[1];
		
		if (a_temp_cookie[0].match("pr") && !a_temp_cookie[0].match("idCont") && !a_temp_cookie[0].match("Contatore") && parseInt(a_temp_cookie[1]) != -1)
		{			
			if (document.getElementById(nome))
			{
				document.getElementById(nome).innerHTML = "<img src='/immagini/cestino.png' border='0' alt='Preferiti' title='Preferiti' width='24px' height='24px' align='absmiddle' /> RIMUOVI";
				document.getElementById(nome).href = "javascript:deleteInput("+a_temp_cookie[1]+")";
			}
		}
	}
}


var today = new Date();
var oggi = today.getTime();

function setCookie(name, value, expires, path, domain, secure)
{
	var today = new Date();
	today.setTime( oggi );
	
	if ( expires )
	{
	expires = expires * 1000 * 60 * 60 * 3600;
	}
	
	var expires_date = new Date( oggi + (expires) );
	
	var expires = new Date ( 2020, 01, 01 );
	
	document.cookie = name + "=" +escape( value ) +
	( ( expires ) ? ";expires=" + expires.toGMTString() : "" ) +
	";path=/" +
	( ( domain ) ? ";domain=" + domain : "" ) +
	( ( secure ) ? ";secure" : "" );	

}

function delCookie(id) {

	var asCookies = document.cookie.split(";");
	
	for (var iCnt = 0; iCnt < asCookies.length; iCnt++)
	{
		var asCookie = asCookies[iCnt].split("=");

		if (parseInt(id) == parseInt(asCookie[1]))
		{
			newS = asCookie[0].split("pr")
			
			setCookie("pr"+newS[1], '-1', '', '', '', '');	
			
			iCnt = asCookies.length;
		}
	}
	
	cont = parseInt(cont) - 1;
	
	
	setCookie('prContatore', cont, '', '', '', '');		
	
	document.getElementById('numeroPref').innerHTML = "" + cont;   
	
	if (document.location.href.match("preferiti.asp"))
	{
		window.location.href = "preferiti.asp";
	}
	else
	{
		nome = "tastoPr" + id;
		document.getElementById(nome).innerHTML = "<img src='/immagini/star.png' border='0' alt='Preferiti' title='Preferiti' width='24px' height='24px' align='absmiddle' style='text-decoration:none' /> AGGIUNGI";
		document.getElementById(nome).href = "javascript:addInput("+id+")";	
	}
}

var arrInput = new Array(0);
  var arrInputValue = new Array(0);
 

function updateNum()
{
	document.getElementById('numeroPref').innerHTML = "" + cont;
}


function addInput(id) {
	
	shiftAnimStatus(id);
		
	setCookie(("pr"+idCont),id, '', '', '', '');
	
	cont = parseInt(cont) + 1;
	
	idCont = parseInt(idCont) + 1;
	
	setCookie('pridCont', parseInt(idCont), '', '', '', '');	
	
	setCookie('prContatore', parseInt(cont), '', '', '', '');
	
	nome = "tastoPr" + id;
	
	document.getElementById(nome).innerHTML = "<img src='/immagini/cestino.png' border='0' alt='Preferiti' title='Preferiti' width='24px' height='24px' align='absmiddle' /> RIMUOVI";
	document.getElementById(nome).href = "javascript:deleteInput("+id+")";	
	
	setTimeout("updateNum()",350);  
}



function saveValue(intId,strValue) {
  arrInputValue[intId]=strValue;
}  

function createInput(id,value) {
  return "<input type='text' id='test "+ id +"' onChange='javascript:saveValue("+ id +",this.value)' value='"+ value +"'><br>";
}

function deleteInput(id) {
  if (cont > 0) { 
	delCookie(id);

  }
   
}


function verificaCookie()
{
  document.cookie = 'verifica_cookie';
  var testcookie = (document.cookie.indexOf('verifica_cookie') != -1) ? true : false;
  return testcookie;
}

/*var test = verificaCookie();
if (test == false) alert('Il tuo browser non accetta i cookie!');*/


function contatore()
{
	if (getCookie('prContatore') != null)
	{
		setCookie('prContatore', getCookie('prContatore'), '', '', '', '');
		cont = parseInt(getCookie('prContatore'));
		
		document.getElementById('numeroPref').innerHTML = "" + cont;    
   }
	else
	{
		setCookie('prContatore', 0, '', '', '', '');
		
		document.getElementById('numeroPref').innerHTML = "" + cont; 
	}
	
	if (getCookie('pridCont') != null)
	{
		setCookie('pridCont', getCookie('prContatore'), '', '', '', '');
		idCont = parseInt(getCookie('pridCont'));
	}
	else
	{
		setCookie('pridCont', 0, '', '', '', '');
	}
}



function resetCookie() {

	var asCookies = document.cookie.split(";");
	
	for (var iCnt = 0; iCnt < asCookies.length; iCnt++)
	{
		var asCookie = asCookies[iCnt].split("=");

		if (asCookie[0].match("pr"))
		{
			newS = asCookie[0].split("pr")
			
			setCookie("pr"+newS[1], '-1', '', '', '', '');	
		}
	}
	
	idCont = 0
	
	cont = 0
	
	setCookie('prContatore', 0, '', '', '', '');
	
	setCookie('pridCont', 0, '', '', '', '');	
	
	document.getElementById('numeroPref').innerHTML = "0";   
	
	window.location.reload();
}



// -->
