var firstHASH = window.location.hash;
var linksLocked = 0;

$(function() {
	if (location.hash.length == 0) {
		//loadContent('News', 'p', 'news');
		loadContent('Home', 'p', 'home');
	}
	else {
		var hash = location.hash.split("/");
		loadContent(hash[0], hash[1], hash[2]);
	}
	
	jsReady = true;
});

function loadContent(title, type, id) {
	if (id == 'hotblog') {
		window.location.href = './blog/';
	}
	
	if ($('#overlay').length) {
		toogleLights();
	}
	
	stopSlidePhotoTimer(true);
	lockLinks();
	
	setTitle(title);
	setLocation(title, type, id);
	
	if (type == "news") {
		$("#content").hide();
		showLoading();
		$("#content").load("news.php", {'type': type, 'id': id}, onContentLoaded);
	}
	else {
		if (type == 'sid' && (id == '3' || id == '3')) {
			$("#content").hide();
			showLoading();
			$("#content").load("content.php", {'type': type, 'id': id, 'listType': 2}, onContentLoaded);
		}
		else {
			$("#content").hide();
			showLoading();
			$("#content").load("content.php", {'type': type, 'id': id}, onContentLoaded);
		}
	}
}

function loadSubcontent(title, type, id) {
	if ($('#overlay').length) {
		toogleLights();
	}
	
	stopSlidePhotoTimer(true);
	lockLinks();
	
	setTitle(title);
	setLocation(title, type, id);
	
	if (title == "Special Jobs" || title == "Voltar") {
		$("#subContent").hide();
	}
	else {
		if (type == 'sid' && (id == '3' || id == '3')) {
			$("#subContent").hide();
			showLoading();
			$("#subContent").load("thumbnail_grid.php", {'type': type, 'id': id, 'listType': 2}, onSubcontentLoaded);
		}
		else if (type == "sid") {
			$("#subContent").hide();
			showLoading();
			$("#subContent").load("thumbnail_grid.php", {'type': type, 'id': id}, onSubcontentLoaded);
		}
		else if (type == "id") {
			$("#subContent").hide();
			showLoading();
			$("#subContent").load("post.php", {'type': type, 'id': id}, onSubcontentLoaded);
		}
	}
}

function setLocation(title, type, id) {
	location.hash = title + '/' + type + '/' + id;
}

function getFlashMovieObject(movieName){
	if (window.document[movieName]){
		return window.document[movieName];
	}
	if (navigator.appName.indexOf("Microsoft Internet")==-1){
		if (document.embeds && document.embeds[movieName])
			return document.embeds[movieName];
	}
	else{
		return document.getElementById(movieName);
	}
}

jsReady = false;
function isJSReady() {
	return jsReady;
}

function setMenuSelection(id) {
	if (id == 'news') var activeButton = 'btNews';
	else if (id == 'ftp') var activeButton = 'btFTP';
	else if (id == 'contato') var activeButton = 'btContato';
	else if (id == 'hotplanet') var activeButton = 'btHotPlanet';
	else if (id == 'portfolio') var activeButton = 'btPortfolio';
	else if (id == 'theHouse') var activeButton = 'btTheHouse';
	else if (id == 'classicos') var activeButton = 'btClassicos';
	else if (id == 'supergaleria') var activeButton = 'btSuperGaleria';
	
	//var flashObject = getFlashMovieObject("menuPrincipal")
	//flashObject.selectButton(activeButton);
	setTimeout(setMenuSelectionTimer, 500, activeButton);
}

function setMenuSelectionTimer(id) {
	var swf = getFlashMovieObject("menuPrincipal");
	if (swf && swf.selectButton) {
		swf.selectButton(id);
	}
}

function setTitle(str) {
	document.title = "Planet House Propaganda :: " + str.replace('#', '');
}

function lockLinks() {
	linksLocked = 1;
}

function unlockLinks() {
	linksLocked = 0;
}

function reloadByHash() {
	var hash = location.hash.split("/");
	
	if (hash.length > 1) {
		loadContent(hash[0], hash[1], hash[2]);
	}
}

function enlargeImage(url, title) {
	/*if (Shadowbox) {
		Shadowbox.open({
			content:    url,
			player:     "img",
			title:      title
		});
	}*/
}

var lastSlidePhoto = 1;
var slideTimer = null;

function loadSlidePhoto(id) {
	$("#slidephoto" + lastSlidePhoto).fadeOut(500);
	$("#btSlideshow" + lastSlidePhoto).removeClass("selected");
	
	$("#slidephoto" + id).fadeIn(500);
	$("#btSlideshow" + id).addClass("selected");
	
	lastSlidePhoto = id;
	
	stopSlidePhotoTimer();
}

function nextSlidePhoto() {
	if (lastSlidePhoto == maxSlidePhotos) {
		loadSlidePhoto(1);
	}
	else {
		loadSlidePhoto(lastSlidePhoto + 1);
	}
	startSlidePhotoTimer();
}

function startSlidePhotoTimer() {
	slideTimer = setTimeout(nextSlidePhoto, 4000);
}

function stopSlidePhotoTimer(resetIndex) {
	if (slideTimer) {
		clearInterval(slideTimer);
	}
	
	if (resetIndex) {
		lastSlidePhoto = 1;
	}
}

function openFTP(user, password) {
	url = "ftp://" + user + ":" + password + "@ftp.planetpropaganda.com.br";
	window.open(url, "ftp");
}

function toogleLights() {
	if ($('.btLightbulbOff').length) {
		$('#btLights').removeClass('btLightbulbOff').addClass('btLightbulbOn');
		$('#btLights').attr('title', 'Escurecer a Sala');
		$('#overlay').fadeTo(250, 0, function() {
			$("#overlay").remove();
		});
	}
	else {
		$('<div id="overlay"></div>').appendTo('body');
		$('#btLights').removeClass('btLightbulbOn').addClass('btLightbulbOff');
		$('#btLights').attr('title', 'Clarear a Sala');
		$('#overlay').fadeTo(250, 0.85);
	}
}

function showLoading() {
	$("#boxLoading").html("aguarde");
	$("#boxLoading").fadeTo(33, 0.75);
}

function hideLoading() {
	$("#boxLoading").fadeTo(33, 0);
	$("#boxLoading").html("");
}

/* EVENTS */
function onContentLoaded() {
	hideLoading();
	$("#content").show();
	unlockLinks();
}

function onSubcontentLoaded() {
	hideLoading();
	$("#subContent").show();
	unlockLinks();
}

/* ON HASH CHANGE */
var onHashChange = function(event) {
	//get hash function
	var getHashValue = function() {
		var arr = window.location.hash.split("#");
		var hasValue = arr[1];
		
		//sets default
		if (typeof hasValue == "undefined") {
			return false;
		}
		
		var hashLen = hasValue.indexOf("?");
		if (hashLen > 0) {
			hasValue = hasValue.substring(0, hashLen);
		}
		
		return hasValue;
	}
	
	//last hash
	var lastHash = getHashValue();

	//checker
	(function watchHash() {
		var hash = getHashValue();

		if (hash !== lastHash) {
			event();
			lastHash = hash;
		}

		var t = setTimeout(watchHash, 10);
	})();
}

onHashChange(function() {
	if (!linksLocked) {
		reloadByHash();
	}
});
