// misc functions for itf

$(document).ready(function() {
	// keep track of clicking on an entry to override hash check in url, see below
	var load_entry_on_click = false;
	
	// shareable link for current entry
	var share_link = "";
	
	// search bar functionality
	$("#search").click(function() {
		$("#searchbar").toggle();
		$("#search").toggleClass("active");
		$("#searchbar .input").focus();
		return false;
	});
	$("#searchbar .input").keyup(function() {
		if ($(this).val()) {
			$(this).addClass("with-value");
		}
		else {
			$(this).removeClass("with-value");
		}
	});
	
	// subnav dropdowns
	$(".subnav").parent().mouseover(function() {
		$(this).find(".subnav").show();
		$(this).children("a").addClass("over");
	});
	$(".subnav").parent().mouseout(function() {
		$(this).find(".subnav").hide();
		$(this).children("a").removeClass("over");
	});
	
	// leaderboard div
	$("#lb-closed").click(function() {
		$(".view").slideUp("slow");  // slide up any open view panels
		$(".hilighted").hide();  // hide current hilight
		$("#lb-closed").animate({width:'0'}, "fast", "linear", function() {
			$("#lb-open").show();
			$("#lb-open .expandable").animate({width:'400px'}, "normal", "swing");
		});
		return false;
	});
	$("#lb-open .close a").click(function() {
		$("#lb-open .expandable").animate({width:'0'}, "normal", "swing", function() {
			$("#lb-open").hide();
			$("#lb-closed").animate({width:'20px'}, "fast", "linear");
		});
		return false;
	});
	
	// load page with leaderboard presented
	// if ($("#lb-open.present").length) {
	// 	$("#lb-closed").width("0");
	// 	$("#lb-open").show();
	// 	$("#lb-open .expandable").width("400px");
	// 	setTimeout(function () {$("#lb-open .close a").click()}, 2000);
	// }
	
	// avatar selector (register & update profile pages)
	$(".avatar-thumb").click(function() {
		$("#avatar-current").attr("src", $(this).attr("src"));
		$("#avatar-selected").val($(this).attr("src"));
	});
	
	// how did you hear, other box (submit & update profile pages)
	$("#referral").change(function () {
		if ($("#referral option:selected").val() == "Other") {
			$("#referral-other").val("");
			$("#referral-other").slideDown("slow");
			$("#referral-other").focus();
		}
		else {
			$("#referral-other").slideUp("slow");
		}
	});
	
	// entries page close an entry
	$(".view .close").click(function() {
		$(this).parent().parent().slideUp("slow");
		$(".hilighted").hide();
		return false;
	});
	
	// close an entry if it's already open
	$(".hilighted").click(function () {
		var id = $(this).parent().parent().attr("id").replace("row", "");
		$("#view0").slideUp("slow");   // slide up first view panel
		$("#view" + id).slideUp("slow");   // slide up associated view panel
		$(this).hide();
		return false;
	});
	
	// entries page open an entry
	$(".row .open").click(function() {
		var id = $(this).parent().parent().attr("id").replace("row", "");
		var entry = $(this).parent();
		var type = $(entry).find(".type").html();
		load_entry_on_click = true;
		
		$(".view:not(#view" + id + ")").slideUp("slow");  // slide up all other view panels
		$(".hilighted").hide();  // hide current hilight
		
		if (type == "video") {
			$(".view .youtube").html("");  // stop any video currently playing
			video_view_populate(id, entry);  // populate view box with details
		}
		else if (type == "essay") {
			essay_view_populate(id, entry);  // populate view box with details
		}
		
		$("#view" + id).slideDown("slow");  // slide down if not already down
		$(entry).find(".hilighted").show();  // hilight thumb
	});
	
	// submit vote
	$(".buttons .vote").click(function() {
		var param = $(this).attr("href").split("/");
		$.get("vote.php", {type:param[1], id:param[2]}, function() {
			$(".buttons .vote").addClass("vote-thanks");
		});
		return false;
	});
	
	// entries page filter
	$("#age-filter").mouseover(function() {
		$("#age-list").show();
	});
	$("#age-filter").mouseout(function() {
		$("#age-list").hide();
	});
	
	// follow/share in top nav
	$("#followshare-dropdown").mouseover(function() {
		$("#followshare").addClass("active");
		$("ul.followshare").show();
	});
	$("#followshare-dropdown").mouseout(function() {
		$("#followshare").removeClass("active");
		$("ul.followshare").hide();
	});
	
	// load page with hash code for an entry? (ie. browser page load, not back/forward button see below)
	if (location.hash) {
		load_from_hash();
	}
	
	// page's hash code changes (ie. browser back/forward button)
	if (jQuery.isFunction($(window).hashchange)) {
		$(window).hashchange(function () {
			$("#lb-open .close a").click();  // close leaderboard if open
			if (load_entry_on_click) {  // if already loading an entry on click
				load_entry_on_click = false;
			}
			else {  // if not already loading an entry
				load_from_hash();
			}
		});
	}
	
	// checkboxes with confirm for deleting entries
	$(".remove-entries .confirm").click(function() {
		if (!$(this).is(":checked")) {
			return true;
		}
		else if (confirm("Are you sure you want to select this entry to delete it?")) {
			return true;
		}
		else {
			return false;
		}
	});
	
	// HOME PAGE VIDEO PLACEMENT - In JS cuz ie 6 initiation sux
	$("#home-youtube").css('display','none');
	
});

function load_from_hash() {
	// load an entry from the hash code in a url
	var load_id = location.hash.replace("#", "");
	var entry = $("#entry" + load_id);
	var type = $(entry).find(".type").html();
	if (type == "video") {
		video_view_populate(0, entry);
	}
	else if (type == "essay") {
		essay_view_populate(0, entry);
	}
	else {
		notfound_view_populate(0);   // if an entry isn't found
	}
	$(".view:not(#view0)").slideUp("slow");  // slide up all other view panels
	$("#view0").slideDown("slow");  // slide down first view panel
	$(".hilighted").hide();  // hide current hilight if any
	$(entry).find(".hilighted").show();  // hilight thumb
}

function video_view_populate(id, entry) {
	var entryid = $(entry).find(".id").html();
	share_link = "http://www.inventthefuture.ca/video" + entryid;
	// populate fields in view
	$("#view" + id).find("h2").html($(entry).find("h2").html());
	$("#view" + id).find(".youtube").html('<object width="640" height="344"><param name="movie" value="' + $(entry).find(".youtube").html() + '"></param><param name="allowFullScreen" value="true"></param><embed src="' + $(entry).find(".youtube").html() + '" type="application/x-shockwave-flash" allowfullscreen="true" width="640" height="344"></embed></object>');
	$("#view" + id).find(".essay").html('');
	$("#view" + id).find(".avatar").attr("src", $(entry).find(".avatar").html());
	$("#view" + id).find(".posted-by").html($(entry).find(".posted-by").html());
	$("#view" + id).find(".posted-date").html($(entry).find(".posted-date").html());
	$("#view" + id).find(".description").html($(entry).find(".description").html());
	$("#view" + id).find(".views").html($(entry).find(".views").html());
	$("#view" + id).find(".votes").html($(entry).find(".votes").html());
	$("#view" + id).find(".rank").html($(entry).find(".rank").html());
	$("#view" + id).find(".facebook-app").attr("href", "http://apps.facebook.com/inventthefuture/?type=video&add=" + entryid);
	$("#view" + id).find(".vote").attr("href", "#vote/video/" + entryid);
	// check for existing vote today
	$.get("vote.php", {check:1, type:"video", id:entryid}, function(data) {
		if (data == "vote-found") {
			$("#view" + id).find(".vote").addClass("vote-thanks");
		}
		else {
			$("#view" + id).find(".vote").removeClass("vote-thanks");
		}
	});
}

function essay_view_populate(id, entry) {
	var entryid = $(entry).find(".id").html();
	share_link = "http://www.inventthefuture.ca/essay" + entryid;
	// populate fields in view
	$("#view" + id).find("h2").html($(entry).find("h2").html());
	$("#view" + id).find(".youtube").html('');
	$("#view" + id).find(".essay").load("get-essay.php", "id=" + entryid);
	$("#view" + id).find(".avatar").attr("src", $(entry).find(".avatar").html());
	$("#view" + id).find(".posted-by").html($(entry).find(".posted-by").html());
	$("#view" + id).find(".posted-date").html($(entry).find(".posted-date").html());
	$("#view" + id).find(".description").html($(entry).find(".description").html());
	$("#view" + id).find(".views").html($(entry).find(".views").html());
	$("#view" + id).find(".votes").html($(entry).find(".votes").html());
	$("#view" + id).find(".rank").html($(entry).find(".rank").html());
	$("#view" + id).find(".facebook-app").attr("href", "http://apps.facebook.com/inventthefuture/?type=essay&add=" + entryid);
	$("#view" + id).find(".vote").attr("href", "#vote/essay/" + entryid);
	// check for existing vote today
	$.get("vote.php", {check:1, type:"essay", id:entryid}, function(data) {
		if (data == "vote-found") {
			$("#view" + id).find(".vote").addClass("vote-thanks");
		}
		else {
			$("#view" + id).find(".vote").removeClass("vote-thanks");
		}
	});
}

function notfound_view_populate(id) {
	// if an entry isn't found
	$("#view" + id).find("h2").html("Not found");
}

function fbs_click() {
	u = share_link;
	window.open('http://www.facebook.com/sharer.php?u=' + encodeURIComponent(u), 'sharer', 'toolbar=0,status=0,width=626,height=436');
	return false;
}

// ----------------------------------------------------------------
// HOME PAGE - REMOVE THE FLASH AD DIV
function home_unload_ad_video() {
	$("#home-ad-video-overlay").html("");
	$("#home-ad-video-overlay").css('display','none');
	$("#home-youtube").css("display","block");
}
