/*==CONSTANTS==*/
var galleryarray = new Array();
var shoutbox_loop_time = 5000;
var COUNTER_NUMBERS_FADE_TIME = 400;
var LOOP_SHOUTBOX = false;
var COUNTER_CURRENT_DAYS = 0;
var COUNTER_CURRENT_HOURS1 = 0;
var COUNTER_CURRENT_HOURS2 = 0;
var COUNTER_CURRENT_MINUTES1 = 0;
var COUNTER_CURRENT_MINUTES2 = 0;
var COUNTER_CURRENT_SECONDS1 = 0;
var COUNTER_CURRENT_SECONDS2 = 0;

/*==========================*/
/*==ON PAGE LOAD FUNCTIONS==*/
/*==========================*/
jQuery(function( $ ) {
});

// Page has loaded
$(document).ready(function() {
	//initCounter();

	// Setup for shadowbox
	Shadowbox.init({
		// skip the automatic setup again, we do this later manually
		skipSetup: true
	});

	// Setup for jQuery dialog
	$( "#dialog" ).dialog({
		minHeight: "500px",
		width: "800px",
		modal: true,
		draggable: false,
		resizable: false,
		autoOpen: false,
		close: function() {document.getElementById("dialog").innerHTML = "";}
	});
	
	// Call for menu buttons display
	setTimeout(function() {
		getShoutboxData();
		loopShoutbox();
		showMenuButtons(1);
	}, 700);
});

/*==========================*/
/*=====COUNTER FUNCTIONS====*/
/*==========================*/
function initCounter(date) {
	document.getElementById('counter_days').innerHTML = COUNTER_CURRENT_DAYS;
	document.getElementById('counter_hours1').innerHTML = COUNTER_CURRENT_HOURS1;
	document.getElementById('counter_hours2').innerHTML = COUNTER_CURRENT_HOURS2;
	document.getElementById('counter_minutes1').innerHTML = COUNTER_CURRENT_MINUTES1;
	document.getElementById('counter_minutes2').innerHTML = COUNTER_CURRENT_MINUTES2;
	document.getElementById('counter_seconds1').innerHTML = COUNTER_CURRENT_SECONDS1;
	document.getElementById('counter_seconds2').innerHTML = COUNTER_CURRENT_SECONDS2;
	$.ajax({
		type: "POST",
		url: "homepage/shoutbox.php",
		data: "action=get_counter_data",
		dataType: "json",
		success: function(res){
			calculate_time(res.cur, res.finish);
			
			setTimeout(function() {
				$( "#counter_box" ).fadeIn(300);
			}, 1000);
		}
	});
}

function calculate_time(cur, finish) {
	var diff = 1;
	
		diff = finish - cur;
		if(diff < 0) {
			return;
		}
		setTimeout(function() {
			var date = new Date(diff*1000);
			// Days
			var days = parseInt(diff/86400);
			// Hours
			var hours = date.getHours();
			if(hours <= 9) {
				var hours1 = 0;
				var hours2 = hours;
			} else {
				var hours1 = hours.toString()[0];
				var hours2 = hours.toString()[1];
			}
			//Minutes
			var minutes = date.getMinutes();
			if(minutes <= 9) {
				var minutes1 = 0;
				var minutes2 = minutes;
			} else {
				var minutes1 = minutes.toString()[0];
				var minutes2 = minutes.toString()[1];
			}
			//Seconds
			var seconds = date.getSeconds();
			if(seconds <= 9) {
				var seconds1 = 0;
				var seconds2 = seconds;
			} else {
				var seconds1 = seconds.toString()[0];
				var seconds2 = seconds.toString()[1];
			}
			
			populateCounter(days, hours1, hours2, minutes1, minutes2, seconds1, seconds2);
			cur++;
			calculate_time(cur, finish);
		}, 1000);
}

function populateCounter(days, hours1, hours2, minutes1, minutes2, seconds1, seconds2) {
	if(days != COUNTER_CURRENT_DAYS) {
		$( "#counter_days" ).fadeOut(COUNTER_NUMBERS_FADE_TIME/2, function() {
			document.getElementById('counter_days').innerHTML = days;
			COUNTER_CURRENT_DAYS = days;
			$( "#counter_days" ).fadeIn(COUNTER_NUMBERS_FADE_TIME/2);
		});
	}
	if(hours1 != COUNTER_CURRENT_HOURS1) {
		$( "#counter_hours1" ).fadeOut(COUNTER_NUMBERS_FADE_TIME/2, function() {
			document.getElementById('counter_hours1').innerHTML = hours1;
			COUNTER_CURRENT_HOURS1 = hours1;
			$( "#counter_hours1" ).fadeIn(COUNTER_NUMBERS_FADE_TIME/2);
		});
	}
	if(hours2 != COUNTER_CURRENT_HOURS2) {
		$( "#counter_hours2" ).fadeOut(COUNTER_NUMBERS_FADE_TIME/2, function() {
			document.getElementById('counter_hours2').innerHTML = hours2;
			COUNTER_CURRENT_HOURS2 = hours2;
			$( "#counter_hours2" ).fadeIn(COUNTER_NUMBERS_FADE_TIME/2);
		});
	}
	if(minutes1 != COUNTER_CURRENT_MINUTES1) {
		$( "#counter_minutes1" ).fadeOut(COUNTER_NUMBERS_FADE_TIME/2, function() {
			document.getElementById('counter_minutes1').innerHTML = minutes1;
			COUNTER_CURRENT_MINUTES1 = minutes1;
			$( "#counter_minutes1" ).fadeIn(COUNTER_NUMBERS_FADE_TIME/2);
		});
	}
	if(minutes2 != COUNTER_CURRENT_MINUTES2) {
		$( "#counter_minutes2" ).fadeOut(COUNTER_NUMBERS_FADE_TIME/2, function() {
			document.getElementById('counter_minutes2').innerHTML = minutes2;
			COUNTER_CURRENT_MINUTES2 = minutes2;
			$( "#counter_minutes2" ).fadeIn(COUNTER_NUMBERS_FADE_TIME/2);
		});
	}
	if(seconds1 != COUNTER_CURRENT_SECONDS1) {
		$( "#counter_seconds1" ).fadeOut(COUNTER_NUMBERS_FADE_TIME/2, function() {
			document.getElementById('counter_seconds1').innerHTML = seconds1;
			COUNTER_CURRENT_SECONDS1 = seconds1;
			$( "#counter_seconds1" ).fadeIn(COUNTER_NUMBERS_FADE_TIME/2);
		});
	}
	if(seconds2 != COUNTER_CURRENT_SECONDS2) {
		$( "#counter_seconds2" ).fadeOut(COUNTER_NUMBERS_FADE_TIME/2, function() {
			document.getElementById('counter_seconds2').innerHTML = seconds2;
			COUNTER_CURRENT_SECONDS2 = seconds2;
			$( "#counter_seconds2" ).fadeIn(COUNTER_NUMBERS_FADE_TIME/2);
		});
	}
}

/*==========================*/
/*====SHOUTBOX FUNCTIONS====*/
/*==========================*/
// Loop shoutbox
function loopShoutbox() {
	if(LOOP_SHOUTBOX) {
		setTimeout(function() {
			getShoutboxData();
			loopShoutbox();
		}, shoutbox_loop_time);
	}
}

// Show shoutbox
function displayShoutbox(el) {
	document.getElementById("shoutbox_input").value = "";
	$("#shoutbox_div").stop().animate({ "margin-left": "-200px", width: "200px"}, 700, function() {
		$("#shoutbox_content").fadeIn(300);
	});
	el.find("span").removeClass("ui-icon-circle-arrow-w").addClass("ui-icon-circle-arrow-e");
}

// Hide shoutbox
function hideShoutbox(el) {
	$("#shoutbox_content").fadeOut(300, function() {
		document.getElementById("shoutbox_input").value = "";
		$("#shoutbox_div").stop().animate({ "margin-left": "0px", width: "0px"}, 700);
		el.find("span").removeClass("ui-icon-circle-arrow-e").addClass("ui-icon-circle-arrow-w");
	});
}

function sendNewMessage() {
	var send_text = document.getElementById("shoutbox_input").value;
	if(send_text == "" || send_text == null) {
		return;
	}
	var data = {action: "add_message", text: send_text};
	$.post("homepage/shoutbox.php", data,
		function(res) {
			if(res == '"success"') {
				document.getElementById("shoutbox_input").value = "";
				getShoutboxData();
			}
		});
}

function getShoutboxData() {
	$.ajax({
		type: "POST",
		url: "homepage/shoutbox.php",
		data: "action=get_messages",
		dataType: "json",
		success: function(res){
			//console.log(res);
			populateShoutbox(res);
		}
	});
}

function populateShoutbox(res) {
	document.getElementById("shoutbox_messages").innerHTML = "";
	for(var i = 0; i < res.length; i++) {
		$('#shoutbox_messages').append('<fieldset class="ui-corner-tr ui-corner-bl"><legend>' + res[i].time + '</legend>' + res[i].text + '</fieldset>');
	}
}

/*==========================*/
/*==MENU BUTTONS FUNCTIONS==*/
/*==========================*/
// Displays all the menu buttons using animation
function showMenuButtons(index) {
	// Last menubutton has loaded and we can attach hover effects
	if(!document.getElementById('menu_button_' + index)) {
		addButtonEvents();
		displayShoutbox($("#shoutbox_toggle"))
		return;
	}
	$("#menu_button_" + index).show("blind", 1000);
	setTimeout(function() {showMenuButtons(index+1)}, 200);
}

// Adds mouse events to menu buttons
function addButtonEvents() {

	$("#content_div").load("homepage/pages/home.html");
	setTimeout(function() {
		contentFadeIn();
	}, 300);
	
	$("#shoutbox_toggle").click(function() {
		if($(this).parent().width() == 0) { // Show shoutbox
			displayShoutbox($(this));
		} else if($(this).parent().width() == 200) { // Hide shoutbox
			hideShoutbox($(this));
		}
	});

	$(".menu_button").hover(function() {
		$(this).stop().addClass("ui-state-hover");
		if(!$(this).hasClass('ui-state-active')) {
			$(this).stop().animate({'padding-top': "13px"}, 300);
		}
	},function(){
		$(this).stop().removeClass("ui-state-hover");
		if(!$(this).hasClass('ui-state-active')) {
			$(this).stop().animate({'padding-top': "7px"}, 300);
		}
	});
	
	$(".menu_button").click(function() {
		if(!$(this).hasClass('ui-state-active')) {
			changeActiveButton($(this));
		
			var button_id = $(this).attr("id");
			
			contentFadeOut();

			setTimeout(function() {
				if(button_id == "menu_button_1") {
					$("#content_div").load("homepage/pages/home.html?r=1", function() {contentFadeIn();});
				} else if(button_id == "menu_button_2") {
					$("#content_div").load("homepage/pages/gallery.html?r=1");
					$("#content_div").stop().animate({ opacity: "1"}, 150);
				} else if(button_id == "menu_button_3") {
					$("#content_div").load("homepage/pages/files.html?r=1", function() {getAllFiles(); contentFadeIn();});
				} else if(button_id == "menu_button_4") {
					$("#content_div").load("homepage/pages/projects.html?r=1", function() {contentFadeIn();});
				}
			}, 300);
		}
	});
}

// Removes active style from previus button and adds it to new active button
function changeActiveButton(el) {
	$(".ui-state-active").removeClass("ui-state-active").stop().animate({'padding-top': "7px"}, 300).removeClass("menu_button_active");
	el.addClass("ui-state-active menu_button_active");
}

// Main page content fade out
function contentFadeOut() {
	$("#content_div").stop().animate({ opacity: "0"}, 300);
}

// Main page content fade in
function contentFadeIn() {
	$("#content_div").stop().animate({ opacity: "1"}, 300);
}

// Fills the dialog containter with images
function populateDialogWithImages(el) {
	path = el.attr("name");
	//Clear gallery div from old pictures
	document.getElementById("gallery_div").innerHTML = "";
	
	if(path == undefined) {
		setUpShadowBox();
		el.find("a").click();
		return;
	}

	$.ajax({
		type: "POST",
		url: "homepage/getFiles.php",
		data: "action=get_all_media_files&path=" + path,
		dataType: "json",
		success: function(res){
			if(res.length == 0) {
				alert("Folder is empty");
			} else {
				populateGalleryPage(res);
			}
			
		}
	});
}

// creates dom elements in gallery page
function populateGalleryPage(res) {
	for(var i = 0; i < res.length; i++) {
		var nomargin = "";
		if((i+1)%5 == 0) {
			nomargin = "nomargin";
		}
		$("#gallery_div").append("<a rel='shadowbox[" + path + "]' href='homepage/images/gallery/" + path + "/" + res[i] + "' title='" + res[i] + "'>" + res[i] + "</a></div>");
	}
	setUpShadowBox();
	$('#gallery_div a:first').click();
}

// gets all the files from files folder
function getAllFiles() {
	$.ajax({
		type: "POST",
		url: "homepage/getFiles.php",
		data: "action=get_all_files",
		dataType: "json",
		success: function(res){
			if(res.length == 0) {
				$('<p class="ui-corner-br ui-corner-tl ui-state-error" style="position: absolute; padding: 10px; border: 1px black solid; margin-left: 280px; margin-top: 150px;"><span class="ui-icon ui-icon-alert" style="float: left; margin-right: .3em;"></span>No Files To Display</p>').insertAfter('#file_table');
			} else {
				populateFilesPage(res);
			}
		}
	});
}

// creates dom elements in files page
function populateFilesPage(res) {
	var file_type = "";

	for(var i = 0; i < res.length; i++) {	
		if(((i + 1) % 2) == 0) {
			$('#file_table > tbody:last').append('<tr style="background-color: #EAF2D3;"><td><span class="ui-icon ui-icon-' + res[i].type + ' thumb_icon"></td><td><a target="_blank" href="homepage/files/' + res[i].name + '">' + res[i].name + '</a></td><td>' + res[i].size + '</td></tr>');
		} else {
			$('#file_table > tbody:last').append('<tr><td><span class="ui-icon ui-icon-' + res[i].type + ' thumb_icon"></td><td><a target="_blank" href="homepage/files/' + res[i].name + '">' + res[i].name + '</a></td><td>' + res[i].size + '</td></tr>');
		}
	}
}

// Sets up shadowbox and clears the old one on close
function setUpShadowBox() {
	Shadowbox.setup(".gallery a", {
		modal: true,
		continuous: true,
		counterType: "skip",
		onClose: function() {
			//Clear gallery div from old pictures
			document.getElementById("gallery_div").innerHTML = "";
			Shadowbox.teardown(".gallery a");
			Shadowbox.clearCache();
		}
	});
}
