var configXML;
var lang;
var langId;
var page;
var langNames = ["catala", "castellano", "english", "magyar"];
var menuId = ["slideshow", "projects", "about_us", "contact"];
var menu = new Array();
var stageHeight = 0;
var content;

menu[langNames[0]] = ["Vista rápida", "Projectes", "Qui som", "Contacte"];
menu[langNames[1]] = ["Vista rápida", "Proyectos", "Quién somos", "Contacto"];
menu[langNames[2]] = ["Slideshow", "Projects", "About us", "Contact"];
menu[langNames[3]] = ["Slideshow", "Munkák", "Rólunk", "Kapcsolat"];


$(document).ready(function() {
	langId = 2;
	lang = langNames[langId];
	page = menuId[0];
	
	//createBg();
	createFlashBg();
	
	$(window).resize(resize);
	resize();
	
	checkHash();
	initButtons();
	setMenu();
	loadContent();
	
});

function createBg() {
	var colors = ["#FF0000", "#FF00FF", "#0000FF", "#00FFFF", "#00ff00", "#ffff00"];
	
	var screenWidth = screen.width;
	
	var rowNum = Math.ceil(screenWidth / 135) * 2;
	
	$('#bgContainer div').append('<table cellspacing="0"></table>');
	
	$('#bgContainer div').width(Math.ceil(screenWidth / 135) * 135);
	$('#bgContainer div').height(100);
	
	var appendTxt = '';
	for (var i = 0; i < 4; i++) {
		appendTxt+='<tr>';
		for (var a = 0; a < rowNum; a++) {
			appendTxt+='<td></td>';
		}
		appendTxt+='</tr>';
	}
	$('#bgContainer table').append(appendTxt);
	$('#bgContainer div td:odd').each(function(i){
		$(this).css("background-color", colors[Math.floor(colors.length * Math.random())]);
	});
	
	$('#bgContainer #bgTop tr:odd td').css("background-color", "#000000");
	$('#bgContainer #bgBottom tr:even td').css("background-color", "#000000");
} 

function createFlashBg() {
	var flashTop = new SWFObject("media/swf/background.swf", "background", "100%", "100%", "9", "#FFFFFF");
	flashTop.addParam("scale", "noscale");
	flashTop.addParam('menu', 'false');
	flashTop.addParam('salign', 'lt');
	flashTop.addParam('wmode', 'opaque');
	flashTop.addVariable("position", "0");
	flashTop.write("bgTop");
	
	var flashBottom = new SWFObject("media/swf/background.swf", "background", "100%", "100%", "9", "#FFFFFF");
	flashBottom.addParam("scale", "noscale");
	flashBottom.addParam('menu', 'false');
	flashBottom.addParam('salign', 'lt');
	flashBottom.addParam('wmode', 'opaque');
	flashBottom.addVariable("position", "1");
	flashBottom.write("bgBottom");
}

function checkHash() {
	if (document.location.hash) {
		var hash = document.location.hash.split("/");
		
		if (langNames.indexOf(hash[1]) != -1) {
			lang = hash[1];
			langId = langNames.indexOf(hash[1]);
		}
		if (menuId.indexOf(hash[2]) != -1) {
			page = hash[2];
		}
	}
}

function resize() {
	stageHeight = $('body').height();
	
	if (stageHeight < 500) stageHeight = 500;
	
	$('#content').height(stageHeight - 190);
	$('#contentScrollable').height(stageHeight - 255);
	$('#bgBottom').css('margin-top', (stageHeight - 190) + 'px');
	$('#footer').css('top', (stageHeight - 45) + 'px');
	
	
	var h = stageHeight - $('#galleryInfo .container[type="text"]').height() - 240;
	$('#galleryInfo .container[type="text"]').css('top', h + 'px');
}

function initButtons() {
	//MENU BUTTONS ROLLOVER
	$('.menuItem').hover(function () {
	    $(this).css("background-color", "#00FFFF");
	  }, function () {
		  $(this).css("background-color", "");
	  }
	);
	
	//MENU CLICK
	$('#menu .menuItem').click(function(event) {
		$('#menuNav div[selected="1"]').attr('selected', 0);
		page = $(this).attr('menuid');
		loadContent();
		event.preventDefault();
	});
	
	//LANG CLICK
	$('#menuLang .menuItem').click(function(event) {
		lang = $(this).attr('menuId');
		langId = langNames.indexOf(lang);
		setMenu();
		loadContent();
		event.preventDefault();		
	});
	
	//FB SHARE CLICK
	$('#footer #socialIcons #fb ').click(function(event) {
		window.open('http://www.facebook.com/sharer.php?u=http%3A%2F%2Fwww.torrents.info', 'fb');
		event.preventDefault();
	});
}

function setMenu() {
	for (var i = 0; i < 4; i++) {
		$('#menuNav .menuItem div').eq(i).html(menu[lang][i]);
	}
}

function loadContent() {
	document.location.hash = '#/' + lang + '/' + page;
	$('#menuNav div[menuId="' + page + '"]').attr('selected', 1);
	
	$('#content').empty();
	
	switch (page) {
	case "slideshow":
		content = new Slideshow();
		break;
	case "contact":
		content = new Contact();
		break;
	case "about_us":
		content = new AboutUs();
		break;
	case "projects":
		content = new Projects();
		break;
	}
	
	resize();
	
}

function log (msg) {
	try{
		console.log(msg, this);
	} catch(err) {}
	return this;
};

