var langCode = ["ptb", "eng"];
var langCode2 = ["br", "en"];
var overColor = ["#dceec5", "#dbc7cd"];
var overColor2 = ["dceec5", "dbc7cd"];
var section = ["home", "gallery", "artists", "exhibitions", "news", "contact"];
var roomInfo = ["", "Rua Jerônimo da Veiga, 131", "Rua Jerônimo da Veiga, 62"];
var artistItemsText = [
						["Currículo","Bibliografia","Imagens","Vídeos","Textos Selecionados","Exposições","Edições","Publicações", "PDF's"],
						["CV","Bibliography","Images","Videos","Selected Texts","Exhibitions","Editions","Publications", "PDF's"]
						];

var exhibitionItemsText = [
						["Imagens","Vídeos","Textos","PDF's", "Próximas Exposições", "Exposições Atuais"],
						["Images","Videos","Texts","PDF's", "Next Exhibitions", "Current Exhibitions"]
						];

var contactText = [
						["Dados enviados. \nObrigado!",'O campo "E-mail" é obrigatório. \nPor favor preecha-o com um e-mail válido.'],
						["Data sent. \nThank you!",'Field "E-mail" is mandatory. \nPlease fill it with a valid email.']
						];

var contactInputText = [
						["","Nome:","Sobrenome:","Telefone:","Cidade:","País:","E-mail:"],
						["","Name:","Last name:","Phone:","City:","Country:","E-mail:"]
						];

var lang = 0;
var xmlLoaded = 0;
var fullLoaded = false;
var currentSection = 0;
var countArtist = 0;
var selectedExhibition = '';

var arExhibitionPhotos = new Array();
var arExhibitionPhotosTags = new Array();
var arExhibitionVideos = new Array();
var arExhibitionTexts = new Array();
var arExhibitionExpos = new Array();

var arArtistPhotos = new Array();
var arArtistPhotosTags = new Array();
var arArtistPubs = new Array();
var arArtistPubsTags = new Array();
var arArtistVideos = new Array();
var arArtistTexts = new Array();
var arArtistExpos = new Array();
var timerArtistCover;

var textField1 = 'Nome:';
var textField2 = 'Sobrenome:';
var textField3 = 'Telefone:';
var textField4 = 'Cidade:';
var textField5 = 'País:';
var textField6 = 'E-mail:';

var tmpEvent = "";

function init(){
	arExhibitionPhotos = new Array();
	arExhibitionPhotosTags = new Array();
	arExhibitionVideos = new Array();
	arExhibitionTexts = new Array();
	arExhibitionExpos = new Array();
	
	arArtistPhotos = new Array();
	arArtistPhotosTags = new Array();
	arArtistVideos = new Array();
	arArtistTexts = new Array();
	arArtistExpos = new Array();
	
	loadHome();
	loadExhibition();
	loadArtist();
	loadGallery();
	loadNews();
	
	//set first position and hide the 'overs'
	$("#mainMenuOver").animate({ opacity: 0.00, top: -4 }, 10).show();
	$("#exhibitionsItemOver").animate({ opacity: 0.00, top: -4 }, 10).show();
	$("#artistsItemOver").animate({ opacity: 0.00, top: -4 }, 10).show();
	
	//set opacity for the 'ons'
	$("#mainMenuOn").animate({ opacity: 1}, 10);

	$("#exhibitionsListItemOn").animate({ opacity: 1 }, 10);
	$("#exhibitionsListYearOn").animate({ opacity: 1 }, 10);
	$("#exhibitionsItemOn").animate({ opacity: 1 }, 10);
	$("#exhibitionsSubItemOn").animate({ opacity: 1 }, 10);
	
	$("#artistsListItemOn").animate({ opacity: 1 }, 10);
	$("#artistsItemOn").animate({ opacity: 1 }, 10);
	$("#artistsSubItemOn").animate({ opacity: 1 }, 10);

	updateColors();

	overLang(0); overLang(1);
	outLang(0); outLang(1);
	
	//changeSection(0);
}

function calcLoad(){
	xmlLoaded++;
	if(xmlLoaded >= 5){
		fullLoaded = true;
		checkPath(tmpEvent);
	}
}

$.address.change(checkPath);

function checkPath(event) {

	tmpEvent = event;
	if(fullLoaded){
		xLang = "";
		xPage = "";
		xPath = event.value;
		if(xPath != undefined){
			//alert(xPath);
			xPath = xPath.split("/");
			xLang = xPath[1];
			xPage = xPath[2];
		}
		
		if(xLang == "en"){ if(lang != 1) changeLang(1); }
		else{ if(lang != 0) changeLang(0); }
		
		if(xPage == "home"){
			changeSection(0);
		}else if(xPage == "gallery"){
			changeSection(1);
		}else if(xPage == "artist"){
			changeSection(2);
		}else if(xPage == "exhibition"){
			changeSection(3);
		}else if(xPage == "news"){
			changeSection(4);
		}else if(xPage == "contact"){
			changeSection(5);
		}else{
			goTo("/home");
		}
	}
}

function goTo(str){ $.address.value("" + langCode2[lang] + str); }

function goLang(num){
	xPath = $.address.path();
	xPath = xPath.split("/");
	xLang = xPath[1];
	newPath = langCode2[num];
	for(i = 2; i < xPath.length; i++){
		newPath += "/" + xPath[i];
	}
	$.address.value(newPath);
}

function updateColors(){
	//set colors
	$("#topNewsTitle").css("background", overColor[lang]);
	$("#mainMenuOver").css("background", overColor[lang]);
	$("#mainMenuOn").css("background", overColor[lang]);

	$("#exhibitionsListItemOn").css("background", overColor[lang]);
	$("#exhibitionsListYearOn").css("background", overColor[lang]);
	$("#exhibitionsItemOver").css("background", overColor[lang]);
	$("#exhibitionsItemOn").css("background", overColor[lang]);
	$("#exhibitionsSubItemOn").css("background", overColor[lang]);

	$("#artistsListItemOn").css("background", overColor[lang]);
	$("#artistsItemOver").css("background", overColor[lang]);
	$("#artistsItemOn").css("background", overColor[lang]);
	$("#artistsSubItemOn").css("background", overColor[lang]);

	$(".allTitleBg").each(function(){ $(this).css("background", overColor[lang]); });
	$(".newsLine ").each(function(){ $(this).css("background", overColor[lang]); });
}

function changeLang(num){
	for (i = 0; i < translations.length; i++){
		txt = (num == 0 ? translations[i].ptb : translations[i].eng);
		$("#" + translations[i].id).html(txt);
	}
	if(trim($("input#firstname").val())	==	textField1)	$("input#firstname").val('');
	if(trim($("input#lastname").val())	==	textField2)	$("input#lastname").val('');
	if(trim($("input#phone").val())		==	textField3) $("input#phone").val('');
	if(trim($("input#city").val())		==	textField4) $("input#city").val('');
	if(trim($("input#country").val())	==	textField5) $("input#country").val('');
	if(trim($("input#email").val())		==	textField6) $("input#email").val('');

	lang = num;

	textField1 = contactInputText[lang][1];
	textField2 = contactInputText[lang][2];
	textField3 = contactInputText[lang][3];
	textField4 = contactInputText[lang][4];
	textField5 = contactInputText[lang][5];
	textField6 = contactInputText[lang][6];
	
	if($("input#firstname").val()	==	'')	$("input#firstname").val(textField1);
	if($("input#lastname").val()	==	'')	$("input#lastname").val(textField2);
	if($("input#phone").val()		==	'') $("input#phone").val(textField3);
	if($("input#city").val()		==	'') $("input#city").val(textField4);
	if($("input#country").val()		==	'') $("input#country").val(textField5);
	if($("input#email").val()		==	'') $("input#email").val(textField6);
	

	
	outLang(0); outLang(1);
	init();
}

function resizeHomeInfo(){
	$("#home").show();
	height1 = $("#homeSala1Info").height();
	height2 = $("#homeSala2Info").height();
	if(currentSection != 0) $("#home").hide();
	if(height1 > height2)	$("#homeSala2Info").height(height1 + ($.browser.msie ? 6 : 0));
	else					$("#homeSala1Info").height(height2 + ($.browser.msie ? 6 : 0));
}

function goPhotoExhibition(id,photo){
	var tempArray = new Array();
	for(i=0;i < arExhibitionPhotos.length; i++){
		if(arExhibitionPhotos[i].id == id){
			tempArray.push(arExhibitionPhotos[i].photo);
		}
	}
	p = photo + 1;
	if(p > tempArray.length) p = 1;
	$("#exhibitionPhotoNums" + id).html(p + "/" + tempArray.length + " &gt;&nbsp;");
	$("#exhibitionsImage"+id+"-"+photo).hide();
	$("#exhibitionsImage"+id+"-"+p).show();
}

function goPhotoArtist(id,photo){
	var tempArray = new Array();
	for(i=0;i < arArtistPhotos.length; i++){
		if(arArtistPhotos[i].id == id){
			tempArray.push(arArtistPhotos[i].photo);
		}
	}
	p = photo + 1;
	if(p > tempArray.length) p = 1;
	$("#artistPhotoNums" + id).html(p + "/" + tempArray.length + " &gt;&nbsp;");
	$("#artistsImage"+id+"-"+photo).hide();
	$("#artistsImage"+id+"-"+p).show();
}

function goPubArtist(id,photo){
	var tempArray = new Array();
	for(i=0;i < arArtistPubs.length; i++){
		if(arArtistPubs[i].id == id){
			tempArray.push(arArtistPubs[i].photo);
		}
	}
	p = photo + 1;
	if(p > tempArray.length) p = 1;
	$("#artistPubNums" + id).html(p + "/" + tempArray.length + " &gt;&nbsp;");
	$("#artistsImage"+id+"-"+photo).hide();
	$("#artistsImage"+id+"-"+p).show();
}

function getVideo(url){
	
	vWidth = 535;
	vHeight = 380;

	txtEmbTag = "VIDEO URL ERROR!";
	
	if(url.indexOf("youtube.com") > -1){ //YOUTUBE
		arURL = url.split("=");
		arURL = arURL[1].split("&");
		videoID = arURL[0];

		txtEmbTag  = '<object width="'+vWidth+'" height="'+vHeight+'"><param name="movie" value="http://www.youtube.com/v/' + videoID + '"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param>';
		txtEmbTag += '<embed src="http://www.youtube.com/v/' + videoID + '" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="'+vWidth+'" height="'+vHeight+'"></embed></object>';
	}
	if(url.indexOf("vimeo.com") > -1){ //VIMEO
		arURL = url.split("/");
		videoID = arURL[3];
		
		txtEmbTag = '<iframe src="http://player.vimeo.com/video/' + videoID + '?portrait=0&amp;color='+overColor2[lang]+'" width="'+vWidth+'" height="'+vHeight+'" frameborder="0"></iframe>';
	}

	return txtEmbTag;
}

function initContactForm(){
	$("input#saveNewsletter").click(function() {
		if(validateEmail($("input#email").val())){
			if(trim($("input#firstname").val())	==	textField1)	$("input#firstname").val('');
			if(trim($("input#lastname").val())	==	textField2)	$("input#lastname").val('');
			if(trim($("input#phone").val())		==	textField3) $("input#phone").val('');
			if(trim($("input#city").val())		==	textField4) $("input#city").val('');
			if(trim($("input#country").val())	==	textField5) $("input#country").val('');
			$.ajax({
				type: 'POST',
				data: $("#formNewsletter").serialize(),
				dataType: "json",
				url: 'inc/saveNewsletter.php',
				success: function(json) {
					if(json.status == "ok"){
						/* $('#msgErrorForm').hide();
						$('#msgSucessForm').show(); */
						alert(contactText[lang][0]);
						$("input#firstname").val(textField1);
						$("input#lastname").val(textField2);
						$("input#phone").val(textField3);
						$("input#city").val(textField4);
						$("input#country").val(textField5);
						$("input#email").val(textField6);
					} else {
						/* $('#msgSucessForm').hide();
						$('#msgErrorForm').show(); */
						alert(contactText[lang][1]);
					}
				}
			});
		}
		else{
			/* $('#msgSucessForm').hide();
			$('#msgErrorForm').show(); */
			alert(contactText[lang][1]);
		}
		return false;
	});
}

function fieldFocus(obj, txt){ if(trim(obj.value) == txt) obj.value = ''; }
function fieldBlur(obj, txt){ if(trim(obj.value) == '') obj.value = txt; }

	
function removeMSOTags(h){
	h = h.replace(/<[/]?(font|st1|shape|path|lock|imagedata|stroke|formulas|span|xml|del|ins|[ovwxp]:\w+)[^>]*?>/gi, '');

//	h = h.replace(/<([^>]*)(?:class|lang|style|size|face|[ovwxp]:\w+)=(?:'[^']*'|""[^""]*""|[^\s>]+)([^>]*)>/gi, '<$1$3>');
	
	h = h.replace(/<([^>]*)style="([^>"]*)"([^>]*)>/gi, '<$1$3>')
	h = h.replace(/<([^>]*)style='([^>']*)'([^>]*)>/gi, '<$1$3>')
	h = h.replace(/<([^>]*)style=([^> ]*) ([^>]*)>/gi, '<$1$3>')
	h = h.replace(/<([^>]*)style=([^>]*)>/gi, '<$1>')

	h = h.replace(/<([^>]*)class="([^>"]*)"([^>]*)>/gi, '<$1$3>')
	h = h.replace(/<([^>]*)class='([^>']*)'([^>]*)>/gi, '<$1$3>')
	h = h.replace(/<([^>]*)class=([^> ]*) ([^>]*)>/gi, '<$1$3>')
	h = h.replace(/<([^>]*)class=([^>]*)>/gi, '<$1>')
	
	//h = h.replace(/<(p|em|strong)(>| [^>]*>)<\/\1>/gi, '');
	
	return h;
}

function trim(str){return str.replace(/^\s+|\s+$/g,"");}

function validateEmail(elementValue){
   var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
   return emailPattern.test(elementValue);
}



