/* fix fixed */
if(navigator.platform == 'iPad' || navigator.platform == 'iPhone' || navigator.platform == 'iPod') {
	$("#pie").css("position", "static");
	$("#negre").css("position", "static");
	$("div.foot").css("position", "static");
}

/* submenu */
tempsm=0;
smv = 0;
menuobert=0;
//obre sub menu
function osm() {
	clearTimeout(tempsm);
	if (!smv) {
		$('#sm').fadeIn("fast");
		if(!menuobert) $('#mcomp').addClass("actiu");
		smv=1;
	}
}
//tanca sub menu
function ttsm() {
	if (smv) {
		$('#sm').fadeOut("fast");
		if(!menuobert) $('#mcomp').removeClass("actiu");
		smv=0;
	}	
}
//programa timer per tancar submenu
function tsm() {
	tempsm = setTimeout(ttsm, 200);
}

/* submenu foundation */
tempsmf=0;
smvf = 0;
menuobertf=0;
//obre sub menu
function osmf() {
	clearTimeout(tempsm);
	if (!smv) {
		$('#subfound').fadeIn("fast");
		if(!menuobertf) $('#msoc').addClass("actiu");
		smvf=1;
	}
}
//tanca sub menu
function ttsmf() {
	if (smvf) {
		$('#subfound').fadeOut("fast");
		if(!menuobertf) $('#msoc').removeClass("actiu");
		smvf=0;
	}	
}
//programa timer per tancar submenu
function tsmf() {
	tempsm = setTimeout(ttsmf, 200);
}

/* submenu */
tempp=0;
pv = 0;
pobert=0;
//obre sub menu
function op(id) {
	alert(id+"op");
	clearTimeout(tempp);
	if (!pv) {
		$('#'+id).fadeIn("fast");
		smv=1;
	}
}
//tanca sub menu
function ttp(id) {
	alert(id+"ttp");
	if (pv) {
		$('#'+id).fadeOut("fast");
		pv=0;
	}	
}
//programa timer per tancar submenu
function tp(id) {
	alert(id+"tp");
	tempp = setTimeout(function(){ttp(id); id = null}, 200);
}

/* peu */

pieo = false;
dist = 0;
norecolocar = false;

function op() {
	if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i))) {
		//ios
		norecolocar=true;
		if (!pieo) {
			$('#pie').animate({ top: margintopp-150-dist }, 300, null);
			$('#bo').fadeOut(1000, reco);
			$('#bc').fadeIn();
		}
		else {
			$('#pie').animate({ top: margintopp }, 300, null);
			$('#bo').fadeIn(1000, reco);
			$('#bc').fadeOut();
		}
		
	} else {
		if (!pieo) {
			$('#pie').animate({ bottom: dist }, 300, null);
			$('#bo').fadeOut();
			$('#bc').fadeIn();
		}
		else {
			$('#pie').animate({ bottom: -150 }, 300, null);
			$('#bo').fadeIn();
			$('#bc').fadeOut();
		}
	}
	
	pieo = !pieo;
}
function reco() {
	norecolocar = false;
}

/* 1 minute */

actual=1;
temps=4000;
debug=0;
numimg=0;
automatic=false;
jQuery(document).ready(function() {
	//programar interval
	if(numimg>1 && automatic) ttime=setTimeout(seguentelem, temps);
	//programar teclat
	$(document).keydown(function(e){
		//alert(e.keyCode);
    	if (e.keyCode == 37 || e.keyCode == 38) { 
    	   prev();
    	   return false;
   		}
   		if (e.keyCode == 39 || e.keyCode == 40) { 
    	   next();
    	   return false;
   		}
   		if(e.keyCode == 48) toelem(1);
   		if(e.keyCode>=49 && e.keyCode<=55) toelem(e.keyCode-47);
	});

});
function seguentelem() {
	//netejar timeout
	if(automatic) clearTimeout(ttime);
	//id actual
	ara = '#img'+actual;
	//id seguent
	seguent = actual+1;
	if (seguent==numimg+1) seguent = 1;
	despres = '#img'+seguent;
	if(debug) console.info("act:"+actual+" / seg:"+seguent);
	//fades
	$(ara).fadeOut("slow");
	$(despres).fadeIn("slow");
	//estils textos
	$('#item'+actual).removeClass('actiu');
	$('#item'+seguent).addClass('actiu');
	//actualitzar actual
	actual = seguent;
	//programar timeout
	if(automatic) ttime=setTimeout(seguentelem, temps);
}
function next() {
	//netejar timeout
	if(automatic) clearTimeout(ttime);
	//id actual
	ara = '#img'+actual;
	//id seguent
	seguent = actual+1;
	if (seguent==numimg+1) seguent = 1;
	despres = '#img'+seguent;
	if(debug) console.info("act:"+actual+" / seg:"+seguent);
	//fades
	$(ara).fadeOut("slow");
	$(despres).fadeIn("slow");
	//estils textos
	$('#item'+actual).removeClass('actiu');
	$('#item'+seguent).addClass('actiu');
	//actualitzar actual
	actual = seguent;
	//programar timeout
	if(automatic) ttime=setTimeout(seguentelem, temps);
}
function prev() {
	//netejar timeout
	if(automatic) clearTimeout(ttime);
	//id actual
	ara = '#img'+actual;
	//id seguent
	seguent = actual-1;
	if (seguent==0) seguent = numimg;
	despres = '#img'+seguent;
	if(debug) console.info("act:"+actual+" / seg:"+seguent);
	//fades
	$(ara).fadeOut("slow");
	$(despres).fadeIn("slow");
	//estils textos
	$('#item'+actual).removeClass('actiu');
	$('#item'+seguent).addClass('actiu');
	//actualitzar actual
	actual = seguent;
	//programar timeout
	if(automatic) ttime=setTimeout(seguentelem, temps);
}
function toelem(elem) {
	//netejar timeout
	if(automatic) clearTimeout(ttime);
	//id actual
	ara = '#img'+actual;
	//id seguent
	seguent = elem;
	if (seguent==0) seguent = numimg;
	despres = '#img'+seguent;
	if(debug) console.info("act:"+actual+" / seg:"+seguent);
	//fades
	$(ara).fadeOut("slow");
	$(despres).fadeIn("slow");
	//estils textos
	$('#item'+actual).removeClass('actiu');
	$('#item'+seguent).addClass('actiu');
	//actualitzar actual
	actual = seguent;
	//programar timeout
	if(automatic) ttime=setTimeout(seguentelem, temps);
}
function stop() {
	//netejar timeout
	clearTimeout(ttime);
	if(debug) console.info("stop");
}
function play() {
	//netejar timeout
	ttime=setTimeout(seguentelem, 2000);
	if(debug) console.info("play");
}



var Carlousel = function(n, prefix, ew, em) {
	//inicialitzacio
	this.ew = ew; //todo: obtenir de css
	this.em = em; //todo: obtenir de css
	this.n = n;
	this.id = prefix;
	var ref = this;
	//variables privades
	var w = n*(ew+em)-em;
	var pos = 0;
	var actiu = 1;
	var debug = 0;
	this.step = 1;
	
	this.right = function() {
		pos+=(ew+em)*this.step;
		if(pos>w-ew) pos=0;
		this.topos();
	}	
	this.left = function() {
		pos-=(ew+em)*this.step;
		if(pos<0) pos=w-ew;
		this.topos();
	}
	this.to = function(p) {
		pos=p*(ew+em)-ew-em;
		this.topos();
	}
	this.topos = function() {
		p = parseInt((pos+ew)/ew);
		//alert(p);
		//alert('#b'+actiu+' -> #b'+p);
		$('#'+this.id+' #b'+p).addClass("actiu");
		$('#'+this.id+' #b'+actiu).removeClass("actiu");		
		actiu=p;
		$('#'+this.id+" div.witems").animate({ scrollLeft: pos}, 1000, 'easeOutExpo' );
	}
	
	//programar events
	$('#'+this.id+' .arrow-left').click(function () { ref.left(); });
	$('#'+this.id+' .arrow-right').click(function () { ref.right(); });

}



var Carlouselp = function(n,prefix,ew,em) {
	//inicialitzacio
	this.ew = ew; //todo: obtenir de css
	this.em = em; //todo: obtenir de css
	this.n = n;
	this.id = prefix;
	var ref = this;
	//variables privades
	var w = n*(ew+em)-em;
	var pos = 0;
	var actiu = 1;
	var debug = 0;
	this.step = 1;
	
	this.right = function() {
		pos+=(ew+em)*this.step;
		if(pos>w-ew) pos=0;
		this.topos();
	}	
	this.left = function() {
		pos-=(ew+em)*this.step;
		if(pos<0) pos=w-ew;
		this.topos();
	}
	this.to = function(p) {
		pos=p*(ew+em)-ew-em;
		this.topos();
	}
	this.topos = function() {
		p = parseInt((pos+ew)/ew);
		//alert(p);
		//alert('#b'+actiu+' -> #b'+p);
		$('#'+this.id+' #b'+p).addClass("actiu");
		$('#'+this.id+' #b'+actiu).removeClass("actiu");		
		actiu=p;
		$('#'+this.id+" div.witems").animate({ scrollLeft: pos}, 1000, 'easeOutExpo' );
	}
	
	//programar events
	$('#'+this.id+' .arrow-left').click(function () { ref.left(); });
	$('#'+this.id+' .arrow-right').click(function () { ref.right(); });

}
