var oldHash = "";


function MostraInfo(newHash, $info, $pageWrap){
		
		$pageWrap.animate({ top: "-420px"}, 300, 'easeInBack', function(){
					$info.hide().load(newHash + " #guts", function() {
						//document.recalc();
						$("#info").css({visibility:"visible"});
						$info.fadeIn(200, function() {
							
							$pageWrap.animate({ top: "20px"}, 300, 'easeOutBack', function(){
								//animation
								document.title = $("#info").find("h1").html()+" - kiwity";
							});
						}); 
					});
		});
}

function MostraInfoNadal(newHash, $info, $pageWrap){
		
		$("#nadal").css({visibility:"visible"});
		$pageWrap.animate({ top: "-420px"}, 300, 'easeInBack', function(){
					setNight();
					$info.hide().load(newHash + " #nadal", function() {
						//document.recalc();
						
						$("#nadal").css({visibility:"visible"});
						$info.fadeIn(200, function() {
							
							$pageWrap.animate({ top: "20px"}, 300, 'easeOutBack', function(){
								//animation
								$("#nadal").css({visibility:"visible"});
								document.title = "Bon Nadal, Feliz Navidad, Merry Xmas - kiwity";
							});
						}); 
					});
		});
}

function MostraInfoIE(newHash, $info, $pageWrap){
		
		$pageWrap.animate({ top: "-420px"}, 300, 'easeInBack', function(){
					$info.hide().load(newHash + " #guts2", function() {
						
						$info.fadeIn(200, function() {
							
							$pageWrap.animate({ top: "20px"}, 300, 'easeOutBack', function(){
								//animation
								document.title = $("#info").find("h1").html()+" - kiwity";
							});
						}); 
					});
		});
}
function AmagaInfo($pageWrap){
		$pageWrap.animate({ top: "-450px"}, 300, 'easeInBack', function(){
			//
		});
}
function PujaCortina(){
	$('#cortina').animate({top:-560},2000, 'linear', function(){ 
		//alert("cortina");
	});	
}

function MouCordaDown(){
	$("#back").animate({top: '0px'}, 300, 'easeInBack', function(){
		
	});
}
function MouCordaUp(){
	$("#back").animate({top: '-12px'}, 300, 'easeOutBack', function(){
		
	});
}



function MostraPortfolio(newHash){ 
		$("#portfolioContainer").animate({top: '0px'}, 500, 'easeInBack', function (){
			$("#portfolioContainer").load(newHash + " #info_portfolio", function() {
						//document.recalc();
							
						$("#portfolioContainer").fadeIn(200, function() {
							document.title = "Portfolio - kiwity";
							/*$pageWrap.animate({ top: "0px"}, 300, 'easeOutBack', function(){
								//animation
							});*/
						});
					});
		});
	
}

function setOldHash(old){
	oldHash = old;	
}
function getOldHash(){
	return oldHash;	
}

function AmagaPortfolio(){
	
	old = getOldHash();
	
	if( $("#portfolioContainer").position().top == '0' ){
		
		$("#portfolioContainer").animate({top: '-550px'}, 600, 'easeOutBack', function (){
				//animation
				window.location.hash = "#"+old; 
				//alert(old);
		});
	}
}

function MostraPeople(newHash, $people){
		
		/*$people.load(newHash + " #ghost, #jordi, #merce, #laia", function() {	
			//load
		});*/
		TornaPeople();
		AnimaPerson("jordi", 2, 'jordi');
		AnimaPerson("merce", 2, 'merce');
		AnimaPerson("laia", 2, 'laia');
}

function MostraPerson(newHash, $people, person, person1, person2, posleft){
		TornaPeople();
		AnimaPerson(person1, 0, 'inactiu1');
		AnimaPerson(person2, 0, 'inactiu2');
		AnimaPerson(person, 1, 'actiu');
}


function AnimaPerson(person, prota, classe){
	
	$person = $("#"+person);
	var diferencia = 0;
	var posReal = 0;
	var posDefault = 0;
	var sizeDefault = 0;
		
		posReal = $person.find("img").position().left;
		$("#ghost").removeClass();
		$("#ghost").addClass(classe);
		posDefault = $("#ghost").css("left").replace("px", "");
		
		$("#ghost").removeClass();
		$("#ghost").addClass(person);
		sizeDefault = $("#ghost").css("height").replace("px", "");
		
		
		if(prota == 1){
			size = 450;
			bott = -20;	
			
			//hem de moure el background
			
			if(person == 'laia'){
				pixels = '-440';
			}else if(person == 'jordi'){
				pixels = '-1450';
			}else{
				pixels = '-1070';	
			}
			BackAnimate(pixels);
		
		
		}else if(prota == 0){
			//size = sizeDefault*0.88;
			size = 210;
			bott = +35;
			$person.find("a").attr("disabled","disabled");
		}else{
			$person.find("a").removeAttr("disabled");

			size = 390;
			bott = 0;	
		}
		if(Math.abs(posDefault - posReal) > 1){
			$person.find("img").animate({left : posDefault, height: size+'px', bottom: bott+'px'}, 400, 'linear', function(){
						//
			});
		}else if( Math.abs(posDefault - posReal) < 1){
			if(prota == 1){
				$person.find("img").animate({left : posDefault, height: size+'px', bottom: bott+'px'}, 400, 'linear', function(){
							//alert("hi");
				});
			}else{
				$person.find("img").animate({height: size+'px', bottom: bott+'px'}, 400, 'linear', function(){
						//alert("hi");
			});
			}
		}
		
		
		
	/*}*/
	
	
}

function PersonHover(person){
	$("#"+person).find("img").animate({height: '+=10px', left: '-=3', bottom:'-=5px'}, 200, 'linear', function(){
						//alert("hi");
			});
	
}
function PersonOut(person){
	$("#"+person).find("img").animate({height: '-=10px', left: '+=3', bottom: '+=5px'}, 200, 'linear', function(){
						//alert("hi");
			});
	
}
function TornaPeople(){
	$("#people").animate({left:'570px'}, 100, 'linear', function(){
		//	
	});
}

function AmagaPeople(){
	$("#people").animate({left:'1100px'}, 100, 'linear', function(){
		//	
	});
}

function BackDefault(){
	
	$("#buildings").animate({left:'-100px'}, 400, 'linear', function(){
		//alert("mm");
		});
}
function BackAnimate(pixels){
	$("#buildings").animate({left:pixels}, 400, 'linear', function(){
		//
		});	
}

function ClearMenu(){
	$("#productes li").removeClass("selected");	
}

function SenyalaMenu(capa){
	$("#"+capa).addClass("selected");
	
}


function formValidaPle(valor) {
	var j;
	for ( j = 0; j < valor.length; j++ ) {
		if ( valor.charAt(j) != " " ) {
			return true
		}
	}
	return false
}

function formValidaEmail(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   return false
		}
		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    return false
		}
		 if (str.indexOf(at,(lat+1))!=-1){
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    return false
		 }

 		 return true					
	}
	
	/** FORM **/
	
	function validaCamps(){
		if(formValidaPle(document.getElementById('name').value)){
			ok1 = 1;
			$('#name').removeClass('error');
		}else{
			ok1 = 0;
			$('#name').addClass('error');
		}
		if(formValidaPle(document.getElementById('email').value) && formValidaEmail(document.getElementById('email').value)){
			ok2 = 1;
			$('#email').removeClass('error');
		}else{
			ok2 = 0;
			$('#email').addClass('error');
		}
		if(formValidaPle(document.getElementById('message').value)){
			ok3 = 1;
			$('#message').removeClass('error');
		}else{
			ok3 = 0;
			$('#message').addClass('error');
		}

		if(document.getElementById('agreement').checked==true){
			ok4 = 1;
			$('#agreement').removeClass('error');
		}else{
			ok4 = 0;
			$('#agreement').addClass('error');
		}		
		
		return (ok1*ok2*ok3*ok4);
	}
	
	function get_radio_value(){
		var rad_val = '';
		for (var i=0; i < document.contact.referent.length; i++){
		   	if (document.contact.referent[i].checked){
			  var rad_val = document.contact.referent[i].value;
			}
		}
		return(rad_val);
	}

	
    function submitForm(){
    	//funcio per validar els camps
    	ok = validaCamps();
    	
    	if(ok){
			//referent = get_radio_value();
			var sel = document.getElementById("referent");
			var referent = sel.options[sel.selectedIndex].value;
			var agreement = document.getElementById('agreement').checked;	
			
    		if(document.getElementById('telf').value != ''){
				if(referent != ''){
					
    				params = "name="+encodeURI(document.getElementById('name').value)+"&email="+encodeURI(document.getElementById('email').value)+"&telf="+encodeURI(document.getElementById('telf').value)+"&message="+encodeURI(document.getElementById('message').value)+"&referent="+encodeURI(referent)+"&agreement="+encodeURI(agreement);
				}else{
					params = "name="+encodeURI(document.getElementById('name').value)+"&email="+encodeURI(document.getElementById('email').value)+"&telf="+encodeURI(document.getElementById('telf').value)+"&message="+encodeURI(document.getElementById('message').value)+"&agreement="+encodeURI(agreement);
				}
    		}else{
				if(referent != ''){
    				params = "name="+encodeURI(document.getElementById('name').value)+"&email="+encodeURI(document.getElementById('email').value)+"&message="+encodeURI(document.getElementById('message').value)+"&referent="+encodeURI(referent)+"&agreement="+encodeURI(agreement);
				}else{
					params = "name="+encodeURI(document.getElementById('name').value)+"&email="+encodeURI(document.getElementById('email').value)+"&message="+encodeURI(document.getElementById('message').value)+"&agreement="+encodeURI(agreement);
				}
    		}
			formSend(params, 'send.php');
			
			//netejem el form:
			document.getElementById('name').value = '';
			document.getElementById('email').value = '';
			document.getElementById('telf').value = '';
			document.getElementById('message').value = '';
		}
    }
    
    /** BALLOONS **/
    
    function balloonSwitch() {
		var active = $('#balloons li.active');
		if ( active.length == 0 ) active = $('#balloons li:last');

		var next =  active.next().length ? active.next()
			: $('#balloons li:first');

		active.animate({opacity: 0.0}, 1000, function() {
			active.removeClass('active');
			next.css({opacity: 0.0})
				.addClass('active')
				.show()
				.animate({opacity: 1.0}, 1000);
		});
	}
	function borraGlobus(){
			//alert(balloons);
			//clearInterval(balloons);
			//$('#balloons li.active').fadeOut("slow");
			$('#balloons').fadeOut("slow");
	}
	function resetBalloons(){
		//clearInterval(balloons);
		//balloonSwitch();
		//balloons = setInterval( "balloonSwitch()", 5000 );
		$('#balloons').fadeIn("slow");
		/*var active = $('#balloons li.active');
		if ( active.length == 0 ) active = $('#balloons li:last');
		active.show();	*/
	}
	
/**** CONTROLA BROWSER ****/
function TestBrowser(info){
	
	/*if (jQuery.browser.mozilla && jQuery.browser.version < '1.9.2') {
    	// do sth.
		$('<style type="text/css">#main:before { display: none;  visibility: hidden; } </style>').appendTo("head");
	
	}else if (jQuery.browser.msie) {
    	$("#browser").fadeIn('slow'); 
	}else if (jQuery.browser.safari) {
		if(parseInt(jQuery.browser.version) > 400){
			//nth
		}else{
			$('<style type="text/css">#main:before { display: none;  visibility: hidden; } </style>').appendTo("head");	
		}
	}*/
	if (jQuery.browser.msie && parseInt($.browser.version, 10) < 9) {
    	$("#browser").fadeIn('slow'); 
	}
	
	var propietat = getStyleProperty('pointerEvents');
	if(propietat != 'pointerEvents'){
		$('<style type="text/css">#main:before { display: none;  visibility: hidden; } </style>').appendTo("head");		
	}
		

}
function getStyleProperty(propName, element) {
	 var prefixes = ['Moz', 'Webkit', 'Khtml', 'O', 'Ms'];
  var _cache = { };

    element = element || document.documentElement;
    var style = element.style,
        prefixed,
        uPropName;
 
    // check cache only when no element is given
    if (arguments.length == 1 && typeof _cache[propName] == 'string') {
      return _cache[propName];
    }
    // test standard property first
    if (typeof style[propName] == 'string') {
      return (_cache[propName] = propName);
    }
 
    // capitalize
    uPropName = propName.charAt(0).toUpperCase() + propName.slice(1);
 
    // test vendor specific properties
    for (var i=0, l=prefixes.length; i<l; i++) {
      prefixed = prefixes[i] + uPropName;
      if (typeof style[prefixed] == 'string') {
        return (_cache[propName] = prefixed);
      }
    }
}
	
	
	
/*** PORTFOLIO ***/	
	
function ProjectePrincipal(id, imatge, titol, info, props, thumb){
	$("#principal").find("h1").html(titol);
	
	
	$capa_imatge = $("#principal");
	$imatge = $("#principal").find("img");
	
	document.getElementById("img_principal").src = "img/portfolio/"+imatge;
		
	$("#principal").find("h2").html(info);	
	$("#principal").find("ul").html(props);	
	$("#"+thumb).animate({ opacity: "1"}, 100,"linear",
						function(){
							//an compl
						});	
}

function overProject(id){
	$("#"+id).animate({ opacity: "1"}, 100,"linear",
						function(){
						});	
}
function outProject(id){
	$("#"+id).animate({ opacity: "0.6"}, 100,"linear",
						function(){
						});	
}

	
	
	
/*** HORARI ***/
function getDayPeriod(){
	//alert("period");
	var hh = (new Date()).getHours();
	if ((hh > 20) || (hh < 7)) 
		var bkgClass = 'night';
	else 
		if (hh < 9) 
			var bkgClass = 'sunrise';
		else 
			if (hh < 19) 
				var bkgClass = 'day';
			else 
				var bkgClass = 'dawn';
	$("#background").removeClass();
	$("#background").addClass(bkgClass);
}

function setNight(){
	//alert("night");
	$("#background").removeClass();
	
	$("#background").addClass('night');	
}


/****  POPUP avis legal ***/

function openWindow(){
	myWindow=window.open('avis_legal.php','','width=600,height=600,scrollbars=1');
	myWindow.focus();

}



/**** CARREGAR IMATGES ****/

function preload(imatges) {
	//preload.arguments.length-1
	//alert("load");
	for (i = 0; i < imatges.length-1; i++) {
		images[i] = new Image();
		images[i].src = imatges[i];
	}
	//fem la imatge dels buildings depenent de la classe del background
	var classe = $("#background").attr('class');
	switch(classe){
		case 'day':
					imatge = new Image();
					imatge.src = "img/struct/buildings_0001_day.png";
					break;
		case 'night':
					imatge = new Image();
					imatge.src = "img/struct/buildings_0000_night.png";
					break;
		case 'sunrise':
					imatge = new Image();
					imatge.src = "img/struct/buildings_0002_sunrise.png";
						break;
		case 'dawn': 
					imatge = new Image();
					imatge.src = "img/struct/buildings_0003_dawn.png";
					break;
		default:
				break;														
	}
	
	//pujar la cortina
	PujaCortina();
}

//calcular pressu Rural

function submitPressu(){
    	//funcio per validar els camps
    		
			var disponibilitat = document.getElementById('disponibilitat').checked;	
			var reserva = document.getElementById('reserva').checked;
			var cases = document.getElementById('reserva').value;
			var opinio = document.getElementById('opinio').checked;
			var videos = document.getElementById('videos').checked;
			var pagines = document.getElementById('pagines').value;
			var idiomes = document.getElementById('idiomes').value;
			var gestor = document.getElementById('gestor').checked;
			var preu = document.getElementById('preu').firstChild.nodeValue;
			
		
    		params = "cases="+encodeURI(document.getElementById('cases').value)+"&pagines="+encodeURI(document.getElementById('pagines').value)+"&idiomes="+encodeURI(document.getElementById('idiomes').value)+"&preu="+encodeURI(preu)+"&disponibilitat="+encodeURI(disponibilitat)+"&reserva="+encodeURI(reserva)+"&opinio="+encodeURI(opinio)+"&videos="+encodeURI(videos)+"&gestor="+encodeURI(gestor);
			
			guardaPressu(params, 'guarda_pressupost.php');
			
	}
    
	

function preuPressu(){
	//calcul del pressu
	
	var disponibilitat=0;
	var reserva=0;
	var cases =0;
	var videos = 0;
	var opinio = 0;
	var idiomes = 0;
	var pagines = 0;
	var gestor = 0;	
	var consulta = "";
	
	var total = parseInt(document.getElementById("form_pressu").base.value);
	
	if(document.getElementById("form_pressu").disponibilitat.checked == true){
		total = total+400;
		disponibilitat = 1;
	}else{
		disponibilitat = 0;
		total = total;	
	}
	
	if(document.getElementById("form_pressu").reserva.checked == true){
		reserva=1;
		if(disponibilitat == 1){
			total = total+200;
		}else{
			document.getElementById("form_pressu").disponibilitat.checked = true;
			total = total +600	
		}
	}else{
		reserva=0;
		total = total;	
	}
	
	cases = document.getElementById("form_pressu").cases.value;
	if(cases > 1){
		if(cases < 8){
			total = total + total*0.3;
		}else{
			consulta = "CONSULTA";	
		}
	}
	
	if(document.getElementById("form_pressu").opinio.checked == true){
		total = total+300;
		opinio = 1;
	}else{
		opinio = 0;
		total = total;	
	}
	
	if(document.getElementById("form_pressu").videos.checked == true){
		total = total+50;
		videos = 1;
	}else{
		videos = 0;
		total = total;	
	}
	
	idiomes = document.getElementById("form_pressu").idiomes.value;
	if(idiomes > 0){
		total = (total + total*0.15)*idiomes;
	}
	
	pagines = document.getElementById("form_pressu").pagines.value;
	if(pagines > 0){
		total = total + pagines*150;
	}
	
	if(document.getElementById("form_pressu").gestor.checked == true){
		consulta = "CONSULTA";
		gestor = 1;
	}
	
	if(consulta == ""){	
		document.getElementById("preu").innerHTML = Math.round(total)+' &euro;';
		document.getElementById("iva").innerHTML = '+ IVA';
	}else{
		document.getElementById("preu").innerHTML = document.getElementById("form_pressu").consultar.value;
		document.getElementById("iva").innerHTML = '';
	}
}


//Tooltips:

function showTooltip(tooltip){
	//alert("tool");
	document.getElementById('tooltip').innerHTML = "<span>"+tooltip+"</span>";
			
}
function hideTooltip(){
	document.getElementById('tooltip').innerHTML = "";	
}

