//
// Copyright 2009 Gameslate from this line until end note --
// (see separate notices for plugins at the end of this document):
//
$(document).ready(function() {
	// Turn off "basic" style intended for non-js browsers
	if ($("link[title=basic]").get(0)) {$("link[title=basic]").get(0).disabled = true;}
	// Resize according to our window size:
	$(window).resize(function(){winResize()});
	winResize();
	// Initial tab animations to hint user:
	// In the long run we may maintain tab position via cookies and database.
	$("#sidebar_left .panel_box").css({"z-index": "40","left": "0px"}).animate({pause:1},700).animate({left: "-188px"},"slow","swing",function(){$("#sidebar_left .panel_box").css("z-index", "20")});
	$("#sidebar_right .panel_box").css({"z-index": "40","left": "18px"}).animate({pause:1},700).animate({left: "206px"},"slow","swing",function(){$("#sidebar_left .panel_box").css("z-index", "20")});
	// Tab toggles:
	$("#sidebar_left .panel_box .handle").toggle(function(){$(this).parent().css("z-index", "40");$(this).parent().animate({left: "0px"},"fast","swing");$(this).parent().parent().prev().animate({opacity: 0},"fast");},function(){$(this).parent().animate({left: "-188px"},"fast","swing", function(){$(this).css("z-index", "20")});$(this).parent().parent().prev().animate({opacity: 1},"fast");});
	$("#sidebar_right .panel_box .handle").toggle(function(){$(this).parent().css("z-index", "40");$(this).parent().animate({left: "18px"},"fast","swing");$(this).parent().parent().prev().animate({opacity: 0},"fast");},function(){$(this).parent().animate({left: "206px"},"fast","swing", function(){$(this).css("z-index", "20")});$(this).parent().parent().prev().animate({opacity: 1},"fast");});
	// Display and Fade out the alert box if has content:
	fade_box("#alert_box");
	// Touch Gameslate for special update items like checking for mail:
	$('#wait').fadeIn("slow");
	var touch_timeout;
	touch_timeout = setTimeout(function() {
		$('#wait').fadeOut("fast");
	},9000);
	$.post("/includes/js_touch.php",{
		linksend:$(this).attr('href')
	},
	function(data) {
		clearTimeout(touch_timeout);
		content_sort(data);
	},"json");
	// Login is custom:
	$("#login_submit").live("click", function(clicker){
		if (clicker.button == 0) {
			$('#wait').fadeIn("slow");$('body').css('cursor', 'wait'); 
			$("p[class=login_notice]").remove();
			$("#login_submit").attr("disabled", "disabled").attr("value", "Verifying...");
			$("#login_name").attr("disabled", "disabled");
			$("#login_password").attr("disabled", "disabled");
			var login_timeout;
			// Timeout at 9 secs and give an error:
			login_timeout = setTimeout(function() {
				$('#wait').fadeIn("slow");$('body').css('cursor', 'wait'); 
				$("#panel_login .panel_content").append('<p class="login_notice">Server timeout: try again.</p>');
				$("#login_submit").removeAttr("disabled").attr("value", "Login");
				$("#login_name").removeAttr("disabled");
				$("#login_password").removeAttr("disabled");
			},9000);
			// Make our post request:
			$.post("/includes/login.php",{
				login_name:$('#login_name').val(),
				login_password:$('#login_password').val()
			},
			function(data) {
				// Clear timeout and handle our post info:
				clearTimeout(login_timeout);
				$("#login_submit").removeAttr("disabled").attr("value", "Login");
				$("#login_name").removeAttr("disabled");
				$("#login_password").removeAttr("disabled");
				content_sort(data);
			},"json");
			return false; // Keep jQuery from following the normal link content.
		} else {
			return true; // Other mouse button clicked, let it do normal stuff.
		}
    });
	// Dropdown menus:
	$("div.droplist > ul > li > a").live("click", function(clicker){
		if (clicker.button == 0) {
			$(this).parent().find("ul").slideDown('fast');
			return false;
		}
	});
	// Handle generic js links:
	$("a.jslink").live("click", function(clicker){
		if (clicker.button == 0) {
			$('#wait').fadeIn("slow");
			// Ugly browser sniffing to fix iPhone's lack of position:fixed
			if (navigator.userAgent.match(/iPhone/i)){
				$("#alert_box").css("top",(clicker.pageY -70)+"px");
				$("#dialog_box").css("top",(clicker.pageY -70)+"px");
			}
			var link_timeout;
			link_timeout = setTimeout(function() {
				$('#wait').fadeOut("fast");
			},9000);
			$.post("/includes/action_js.php",{
				linksend:$(this).attr('href')
			},
			function(data) {
				clearTimeout(link_timeout);
				content_sort(data);
			},"json");
			return false; // Keep jQuery from following the normal link content.
		} else {
			return true; // Other mouse button clicked, let it do normal stuff.
		}
	});
	// Handle generic js forms:
	$(".button").live("click", function(clicker){
		if (clicker.button == 0) {
			$('#wait').fadeIn("slow");$('body').css('cursor', 'wait'); 
			$(".generic_notice").fadeOut("fast").html("");
			$(".generic_input").attr("disabled", "disabled");
			var submit_label = $("#generic_submit").attr("value");
			var submit_alt_label = $("#generic_submit_alt").attr("value");
			var submit_value = $(this).attr("value");
			$(this).attr("value", "Working...");
			// Ugly browser sniffing to fix iPhone's lack of position:fixed
			if (navigator.userAgent.match(/iPhone/i)){
				$("#alert_box").css("top",(clicker.pageY -70)+"px");
				$("#dialog_box").css("top",(clicker.pageY -70)+"px");
			}
			var form_timeout;
			// Timeout at 9 secs and give an error:
			form_timeout = setTimeout(function() {
				$('#wait').fadeOut("fast");$('body').css('cursor', 'default'); 
				$("#alert_box").html("<p>Server timeout: try again.</p>");
				fade_box("#alert_box");
				$(".generic_input").removeAttr("disabled");
				$("#generic_submit").attr("value", submit_label);$("#generic_submit_alt").attr("value", submit_alt_label);
			},9000);
			// Make our post request:
			$.post("/includes/action_js.php",{
				generic_1:$('#generic_1').val(),generic_2:$('#generic_2').val(),generic_3:$('#generic_3').val(),generic_4:$('#generic_4').val(),generic_5:$('#generic_5').val(),generic_6:$('#generic_6').val(),
				generic_submit:submit_value,
				form_action:$('#form_action').attr("value")
			},
			function(data) {
				// Clear timeout and handle our post info:
				clearTimeout(form_timeout);
				$(".generic_input").removeAttr("disabled");
				content_sort(data);
				$("#generic_submit").attr("value", submit_label);$("#generic_submit_alt").attr("value", submit_alt_label);
			},"json");
			return false; // Keep jQuery from following the normal link content.
		} else {
			return true; // Other mouse button clicked, let it do normal stuff.
		}
    });
	// Clicking an alert box makes it go away faster:
	$("#alert_box").live("click", function(clicker){
		$("#alert_box").stop(true,true).slideUp("fast").addClass("back").removeClass("front").html('');
	});
});
// Sort our content:
function content_sort(data){
	var updateVert = false;
	// Gameslate index panels:
	if (data.label_login){$("#panel_login .panel_label").html(data.label_login);updateVert = true;}
	if (data.panel_login){$("#panel_login .panel_content").html(data.panel_login);updateVert = true;}
	if (data.panel_bank){$("#panel_bank .panel_content").html(data.panel_bank);updateVert = true;}
	// Gates Motel panels:
	if (data.panel_weapons){$("#panel_weapons .panel_content").html(data.panel_weapons);updateVert = true;}
	if (data.panel_defenses){$("#panel_defenses .panel_content").html(data.panel_defenses);updateVert = true;}
	if (data.panel_targets){$("#panel_targets .panel_content").html(data.panel_targets);updateVert = true;}
	// Other page elements to update:
	if (data.center_content){$("#center_content").html(data.center_content);}
	if (data.breadcrumbs){$("#breadcrumbs").html(data.breadcrumbs);}
	if (data.new_mail){
		if (data.new_mail != '0' && data.new_mail != 'None') {
			$("#mail_icon").slideDown("slow","easeOutElastic");
		} else {
			$("#mail_icon").slideUp("slow");
		}
		$("#new_mail").html(data.new_mail);
	}
	if (data.preview_box) {
		if (data.preview_box == 'close') {
			static_box("#preview_box",'close');
		} else {
			$("#preview_box").html(data.preview_box)
			static_box("#preview_box",'');
		}
	}
	if (data.trivia_action) {
		if (data.trivia_box) {
			if (data.trivia_box != 'nochange') {
				$("#trivia_box").html(data.trivia_box);
			}
			game_box("#trivia_box",data.trivia_action);
		} else {
			game_box("#trivia_box",'close');
		}
	}
	if (updateVert) {winResize();updateVert = false;}
	if (data.act_alert){$("#alert_box").html(data.act_alert);fade_box("#alert_box");} else if (data.alert_box){$("#alert_box").html(data.alert_box);fade_box("#alert_box");}
	$('#wait').fadeOut("fast");
	$('body').css('cursor', 'default'); 
}
// Popup Box display with close button:
function static_box(thebox,boxaction) {
	if ($(thebox).is(':empty')) {
		$(thebox).stop(true,true).addClass("back").removeClass("front").css("display","none").html('');
	} else {
		if (boxaction == 'close') {
			$(thebox).stop(true,true).fadeOut("slow", function(){$(this).addClass("back").removeClass("front").html('');});
		} else {
			if ($(thebox).is(".back")) {
				$(thebox).stop(true,true).addClass("front_almost").removeClass("back").fadeIn("fast");
				if ($(document).width() > 801){$(thebox).draggable({cancel: '#preview_content'});}
			}
		}
	}
}
// Popup Box display and fade (mainly for alerts):
function fade_box(thebox) {
	if ($(thebox).is(':empty')) {
		$(thebox).stop(true,true).addClass("back").removeClass("front").css("display","none").html('');
	} else {
		if ($(thebox).is(".back")) {
			// Our dummy pause animation is .animate({pause:1},3000).
			$(thebox).stop(true,true).addClass("front").removeClass("back").fadeIn("fast").animate({pause:1},3000).fadeOut(1000, function(){
				$(this).addClass("back").removeClass("front").html('');
			});
		}
	}
}
// Game box, customized for our Trivia so probably not multipurpose. See static_box below for other purposes.
function game_box(thebox,boxaction) {
	if ($(thebox).is(':empty') || boxaction == 'close') {
		$(thebox).addClass("back").removeClass("front_almost").html('').fadeOut("fast");
	} else if ($(thebox).is(".back")) {
		$(thebox).addClass("front_almost").removeClass("back").fadeIn("fast");
		if ($(document).width() > 801){$(thebox).draggable({cancel: '#trivia_content'});}
	}
	if (boxaction == 'animate') {
		$(thebox).find(".anim_up").css("top","-550px").animate({pause:1},1200).animate({"top" : 0},600,"easeOutElastic");
		$(thebox).find(".anim_down").css("top","-550px").animate({pause:1},100).animate({"top" : 0},600,"easeOutElastic");
		$(thebox).find(".anim_left").css("left","-550px").animate({pause:1},300).animate({"left" : 0},1000,"easeOutElastic");
		$(thebox).find(".anim_right").css("right","-550px").animate({pause:1},300).animate({"right" : 0},1000,"easeOutElastic");
	} else if (boxaction == 'correct') {
		$("#trivia_correct").fadeIn("fast").animate({pause:1},1000).fadeOut("slow",function(){fetch_trivia(thebox);});
	} else if (boxaction == 'wrong') {
		$("#trivia_wrong").fadeIn("fast");
		$(thebox).animate({pause:1},600).fadeOut("slow",function(){$(thebox).addClass("back").removeClass("front_almost")});
	} else if (boxaction == 'end') {
		$(thebox).animate({pause:1},2000).fadeOut(2200,function(){$(thebox).addClass("back").removeClass("front_almost")});
	}
}
// Fetch more trivia:
function fetch_trivia(thebox) {
	$("#trivia_correct").animate({pause:1},600).fadeOut("slow");
	$('#wait').fadeIn("slow");
	var trivia_timeout;
	trivia_timeout = setTimeout(function() {
		$('#wait').fadeOut("fast");
		$("#alert_box").html("<p>Server timeout: Game interrupted.</p>");
		fade_box("#alert_box");
		$(thebox).animate({pause:1},600).fadeOut("slow");
	},15000);
	$.post("/includes/action_js.php",{
		linksend: "/?action=trivia&trivia_action=next"
	},
	function(data) {
		content_sort(data);
		clearTimeout(trivia_timeout);
	},"json");
}
// Resize according to window size function:
function winResize(){
	$("#width_info").html($(document).width());
	if ($(document).width() < 801) {
		styleChange("style_800");
	} else if ($(document).width() < 1025) {
		styleChange("style_1024");
	} else {
		styleChange("default");
	}
	// Center content height hack:
	if ($("#sidebar_left").height() > $("#sidebar_right").height()) {
		var minSideBarHeight = $("#sidebar_left").height() + 16;
	} else {
		var minSideBarHeight = $("#sidebar_right").height() + 16;
	}
	$("#center").css("min-height", minSideBarHeight);
	// Vertical positioning hack:
	$("div[class=handle_pull]").vAlign();
	$("div[class=panel_label]").vAlign();
}

function styleChange(style_name){
	$('link[@rel*=style][title]').each(function(i){
		if (this.getAttribute('title') && this.getAttribute('title') != 'basic') {
			this.disabled = true;
			if (this.getAttribute('title') == style_name) this.disabled = false;
		}
	});
}
// -- End Copyright 2009 Gameslate material. Any other plugin code below is used under other licenses:

// ...

// Full credits and license details for included plugins can be found at http://gameslate.com/?action=credits

// Vertically align function from http://seodenver.com/2008/09/08/simple-vertical-align-plugin-for-jquery/
(function ($) {$.fn.vAlign = function() {return this.each(function(i){var ah = $(this).outerHeight();var ph = $(this).parent().height();var mh = (ph - ah) / 2;$(this).css('margin-top', mh);});};})(jQuery);

