/**
 * @author Nico Di Fiore
 */
 
var isVisible = false;
var defaultTheme = "0x4DBCE9, 0x26ADE4, 0xD1E751, 0x3F3F3F, 0x7ACCEF";
var isHidden = true;
var jsReady = false;

$(document).ready(function(){

	// Set the JSEngine ready for process
	// used by flash
	jsReady = true;
	
	// TEST
	//$.cookies.del(true);
	//$.cookies.set("_rainentry", null)
	//console.log("---> "+$.cookies.get("_rainentryx"));
	//$.cookies("themeColor", null);
	//$.cookies("themePointer", null);
	
	// Hide everything first
	$("#body_content").show();
	//$("#content_container").hide();
	$("#body_content .container_shadow").hide();
	$("#tagline").hide();
	$("#first_time").hide();	
	$("#header").hide();
	$("#flash_bg").hide();
	
	$("#explode_pixel").click(function(){
		thisMovie("flashBaseBg").explode();
	});
	
	//console.log("Theme Color Cookie: "+$.cookies("themeColor"));
	/*
	// This is the first time the user uses the site
	if($.cookies.get("_rainentry") == null) {
		
		$("#first_time").show();
		
		$("#body_content").hide();
		$("#body_content .container_shadow").hide();
		$("#tagline").hide();
		$("#first_time").hide()
		
		// Add the cookies to determine that the user has already entered the site
		$.cookies.set("is_firsttime", true);
		isVisible = false;
		$.cookies.set("_rainentry", 1);
		
		// To do necessary adjustments first
		$("#body_content").hide();
		
		$.cookies.set("themeColor", "0x0B8C8F, 0xFCF8BC, 0xCACF43, 0x2B2825, 0xD6156C");
		$.cookies.set("themePointer", 3);
		
		toggleCustomizer();
	}
	else {
	*/
		// the theme color is null or not set
		// create a temporary theme color
		if($.cookies.get("themeColor") == null && $.cookies.get("themePointer") == null) {
			$.cookies.set("themeColor", "0x4DBCE9, 0x26ADE4, 0xD1E751, 0x3F3F3F, 0x7ACCEF");
			$.cookies.set("themePointer", 3);
		}
		
		$("#custom_panel").show();
		$("#content_container").show();
		$("#flash_bg").show();
		$("#color_scheme_small").show();
		$("#header").show();
		$("#body_content").show();
		$("#body_content .container_shadow").show();
		$("#tagline").show();
		
		if(IS_HOME) {
			adjustSize();
		}
		
		// If the user has already entered the site
		// multiple times
		// Don't show the customization panel anymore.
		isVisible = true;
		
	//}

});

	function getThemePointer() {
	
		// Return default theme pointer
		if($.cookies.get("themePointer") == null)
			return 3;
		// Return the value from the cookie
		return $.cookies.get("themePointer");
	}
	
	function getThemeColors() {
		
		// Return a default color scheme if null
		if($.cookies.get("themeColor") == null)
			return "0x4DBCE9, 0x26ADE4, 0xD1E751, 0x3F3F3F, 0x7ACCEF";
			
		// Return the value from the cookies
		return $.cookies.get("themeColor");
	}
	
	////////////////////////////////////////////////////////
	// Use by flash to communicate to various layers
	// for the theme.
	function isReady() {
		return jsReady;
	}
	function pageInit() {
		jsReady = true;
	}
	function thisMovie(movieName) {
		if (navigator.appName.indexOf("Microsoft") != -1) {
			return window[movieName];
		} else {
			return document[movieName];
		}
	}
	function sendToActionScript(value) {
		thisMovie("customizerScreen").sendToActionScript(value);
	}



function hideElements() {

	if($("#flash_bg").is(":visible")) {
		thisMovie("flashBaseBg").implode();
		setTimeout(function() {$("#flash_bg").hide();}, 600);
	}
}

function toggleUiElements() {
	//$("#body_container").toggle();
}


function toggleCustomizeUi() {

	// Show the customizer dialog window	
	$("#customizer").toggle();
}


function changeGridColor(color, themePointer) {
	
	//console.log("Changing grid color");
	
	$.cookies.set("themeColor", color);
	$.cookies.set("themePointer", themePointer);
	
	//console.log("Changing color");
	//console.log("Color: "+color);
	//console.log("themePointer: "+themePointer);
	
	thisMovie("customizerScreen").changeGridColor(color); 
}

function closeCustomizer() {

	//console.log("Color: "+$.cookies("themeColor"));
	//console.log("themePointer: "+$.cookies("themePointer"));
	
	thisMovie("customizerScreen").hidePixelBoard();
	toggleCustomizeUi();
	
	
}

/**
	When the user enters the site for the first time,
	or the user clicks on the customizer panel,
	this method is called and executed.
 */
function toggleCustomizer() {
	
	hideElements();
	
	setTimeout(function() {
		
		// If the cookies is set to first time viewers
		// show them the customizer screen.
		// after setting it, automatically set it to non-firsttime user
		if($.cookies.get("is_firsttime") == "true") {
		
			$("#customizer_base_bg").show();
			$("#customizer_screen").css("width", "100%");
			$("#customizer_screen").css("height", "100%");
		
			
			// Display the pixel grid
			thisMovie("customizerScreen").sendToActionScript("grid");
			
			$("#body_content").hide();
			$("#body_container").hide();
			
			// Set the user as not-firsttime user.
			$.cookies.set("is_firsttime", "false");
		} else {

			$("#customizer_base_bg").show();
			$("#customizer_screen").css("width", "100%");
			$("#customizer_screen").css("height", "100%");
		
		
			//////////////////////////////////////
			//Set customizer screen
			if($.cookies.get("themeColor") != null)
				thisMovie("customizerScreen").setThemeColor($.cookies.get("themeColor"));
			else
				thisMovie("customizerScreen").setThemeColor("0x4DBCE9, 0x26ADE4, 0xD1E751, 0x3F3F3F, 0x7ACCEF");
			
			// Display the pixel grid
			thisMovie("customizerScreen").sendToActionScript("grid");
		}
		
	}, 700);
	
}

	// Once the pixel grid is appearing, hide all necessary elements
	function hideUiElements() {
		// hide the following elements if they are shown
		$("#tagline").hide();
		$("#header").hide();
		$("#body_container").hide();
	}
	
	function showUiElements() {
		// hide the following elements if they are shown
		$("#tagline").show();
		$("#header").show();
		$("#body_container").show();
		$("#body_content").show();
		
		// If the page set to the home page.
		if(IS_HOME) {
			adjustSize();
		}
	}
	
	// Reset the customizer screen to its original form
	// sets the customizer_screen to 1% width and height.
	function resetCustomizer() {
	
		//console.log($.cookie("themeColor"));
		
		// Reset the size of the customizer screen
		$("#customizer_screen").css("width", "1%");
		$("#customizer_screen").css("height", "1%");
		
		
		// Show the UI
		$("#flash_bg").show();
		$("#customizer_base_bg").hide();
		
	}
