var lang="it";
var l="home";

$(document).ready(function() {
	l=$('#location').attr('value');
	lang=$('#language').attr('value');
	
	loadPage($("#section-content"),l);
	$(".menu-element").bind('click',function(event){
		loadPage($("#section-content"),$(this).attr('id'));
	});
	
	$(".bread-element").live('click',function(event){
		loadPage($("#section-content"),$(this).attr('id'));
	});
	
	$(".menu-element").bind('mouseover',function(event){
		$("span[class='white-arrow']").hide();
		$(this).prev('span').show();
	});
	
	$(".flag").bind('click',function(event){
		if (l==21){
			l=201;
		}
		if (l==22){
			l=202;
		}
		switch($(this).attr('id')){
			case "it":
				window.open("index.php?a="+l,"_self");
			break;
			case "eng":
				window.open("index.php?a="+l+"&lang="+$(this).attr('id'),"_self");
			break;
			case "fr":
				//alert("sorry, not implemented yet");
				window.open("index.php?a="+l+"&lang="+$(this).attr('id'),"_self");
			break;
			case "es":
				//alert("sorry, not implemented yet");
				window.open("index.php?a="+l+"&lang="+$(this).attr('id'),"_self");
			break;
		}
	});
	
	$("a[class='video']").live('click',function(event){
		event.preventDefault();
		$("body").append("<div class=\"ui-widget-shadow modal\"></div>");
		$("body").append("<div id=\"video-container\" class=\"ui-corner-all\"></div>");
		var n= $(this).attr('href');
		var w=$(document).width();
		$("#video-container").css("left",((w/2)-330));
		$('#video-container').flash({
			swf:"js/inc/mediaplayer/player.swf",
			height: 400,
			width: 600,
			flashvars: {
				file: 'http://www.tekmak.it/'+n,
				name: 'Video',
				autostart:true
			}
		});
		$("#video-container").append("<span id=\"chiudi-video\">Chiudi</span>");

		
	});
	
	$("#chiudi-video").live('click',function(event){
		event.preventDefault();
		$("#video-container").fadeOut().remove();
		$("div[class='ui-widget-shadow modal']").fadeOut().remove();
		
	});
});

function loadPage(target, kind, anchor){
	
	l=kind;
	var page="modules/";
	var parent=undefined;
	var btext;
	
	switch(kind){
		case "0":
			btext=getText(lang,0,kind);
			page+="home.php";
		break;
		case "1":
			btext=getText(lang,1,kind);	
			page+="storia-mission.php";
		break;
		case "2":
			btext=getText(lang,2,kind);
			page+="settori.php";
		break;
		case "3":
			btext=getText(lang,3,kind);
			page+="news.php";
		break;
		case "4":
			btext=getText(lang,4,kind);
			page+="lavoro.php";
		break;
		case "5":
			btext=getText(lang,5,kind);
			page+="modulo-contatti.php";
		break;
		case "22":
			page+="prodotto.php";
			btext=getText(lang,6,"202");
			parent=new Array();
			parent[0]=getText(lang,2,"2");
		break;
		case "21":
			page+="prodotto.php";
			btext=getText(lang,7,"201");
			parent=new Array();
			parent[0]=getText(lang,2,"2");
		break;
		case "23":
			page+="prodotto.php";
			btext=getText(lang,8,"203");
			parent=new Array();
			parent[0]=getText(lang,2,"2");
		break;
		case "202":
			page+="ceramica.php";
			btext=getText(lang,6,kind);
			parent=new Array();
			parent[0]=getText(lang,2,"2");
		break;
		case "201":
			page+="agglomerato-e-pietre.php";
			btext=getText(lang,7,kind);
			parent=new Array();
			parent[0]=getText(lang,2,"2");
		break;
		case "203":
			page+="imballaggio.php";
			btext=getText(lang,8,kind);
			parent=new Array();
			parent[0]=getText(lang,2,"2");
		break;
		case "10":
			btext=getText(lang,1,kind);	
			page+="storia-mission.php";
		break;
		case "11":	
			btext=getText(lang,9,kind);	
			page+="dove-siamo.php";
			parent=new Array();
			parent[0]=getText(lang,1,"1");
		break;
		case "30":
			btext=getText(lang,3,kind);
			page+="news.php";
		break;
		case "31":
			page+="fiere.php";
			btext=getText(lang,10,kind);
			parent=new Array();
			parent[0]=getText(lang,3,"3");
		break;
		case "50":
			btext=getText(lang,5,kind);
			page+="modulo-contatti.php";
		break;
		case "51":
			btext=getText(lang,11,kind);
			page+="riferimenti.php";
			parent=new Array();
			parent[0]=getText(lang,5,"5");
		break;
		default:
			page+="home.php";
			kind="Home";
		break;
	}
	
	switch(lang){
		case "eng":
			page="eng/"+page;
		break;
		case "fr":
			page="fr/"+page;
		break;
		case "es":
			page="es/"+page;
		break;
	}
	//window.location="/aa.php?page="+page+"&anchor="+anchor+"&btext="+btext+"&parent="+parent+"&antimone=vero";
	$.post(page,{cod: anchor}, function(data) {
		target.fadeOut(function(){
			updateBreadcrumbs(btext,parent);
			target.html(data).fadeIn('slow',function(){
					loadImage();
					/*if(anchor){
						var k="#section-content tr[id='"+anchor+"']";
						var offset=$(k).offset();
						$(document).scrollTop(offset.top);
					}*/
				});
		});
	});
}

function updateBreadcrumbs(kind,parent){
		var text="";
		if(parent!=undefined){
			var x=0;
			for (x in parent){
				text+=parent[x]+" <span class=\"white-arrow bread\"></span> ";
			}
		}
		text+=kind
		$("#breadcrumbs-text").html(text);
		
}

function loadImage(){
  $("div[class='image']").each(function(i,e){
    $(this).css("background","url(\"css/images/section/prod_"+(i+1)+".jpg\") no-repeat scroll 50% 50% transparent");
  });
}

function getText(lang,index,extra){
	var it = new Array("Home","Azienda","Settori","News","Lavora con noi","Contatti","Ceramica","Agglomerato &amp; Pietre naturali","Imballaggio","Come arrivare","Fiere","Riferimenti in azienda");
	var eng = new Array("Home","About us","Fields","News","Careers","Contacts","Ceramic","Composit &amp; natural stones","Packaging","How to reach us","Trade Shows","Contacts inside the Company");
	var fr = new Array("Home","Soci&eacute;t&eacute;","Secteurs","Nouvelles","Carri&egrave;res","Contacts","C&eacute;ramique","Agglom&eacute;r&eacute;s et pierres naturelles","Emballage","Comment arriver","Foires","Contacts dans l'Entreprise");
	var es = new Array("Home","Empresa","Sectores","Noticias","Carreras"," Contacto","Ceramica","Aglomerado y Piedra natural","Emballaje","Como llegar","Ferias","Contactos en la Empresa");
	switch(lang){
		case "it":
			return "<a class=\"bread-element\" href=\"#\" id=\""+extra+"\">"+it[index]+"</a>";
		break;
		case "eng":
			return "<a class=\"bread-element\" href=\"#\" id=\""+extra+"\">"+eng[index]+"</a>";
		break;
		case "fr":
			return "<a class=\"bread-element\" href=\"#\" id=\""+extra+"\">"+fr[index]+"</a>";
		break;
		case "es":
			return "<a class=\"bread-element\" href=\"#\" id=\""+extra+"\">"+es[index]+"</a>";
		break;
	}
}

