/// <reference path="http://code.jquery.com/jquery-1.4.1-vsdoc.js" />
// Functions For all pages.

var t = setTimeout("doNothing", 1500); //the timeout variable
var tPromo = setTimeout("doNothing", 1500);
clearTimeout(t); //unsets the timeout for initial load

////////////////////////////////////// ASYNC IMAGE LOAD ///////////////////////////////////
// CREDIT: http://blog.realmofzod.com/2009/04/09/asynchronous-image-loading-with-jquery/ //
///////////////////////////////////////////////////////////////////////////////////////////
var sliderPane = 0;
gLoadSpinnerUrl = '/images/interface/ajax-loader.gif';
gFailImage = '/images/interface/image_load_error.gif';

function LoadImage(pSelector, pCallback) {
	var loader = $(pSelector);
	//loader.html('<img src="' + gLoadSpinnerUrl + '"/>');
	loader.html('');
	LoadThisImage(loader, pCallback);
}

function LoadThisImage(loader, pCallback) {
	image_src = loader.attr('src');
	image_class = loader.attr('class');
	image_style = loader.attr('style');
	img = new Image();
	$(img).hide();

	$(img).load(function () {
		cb_js = loader.get(0).getAttribute('onload');
		onload_cb = function () {
			eval(cb_js);
		}

		//loader.replaceWith(this);
		if ($(loader).parent() != undefined) {
			loader.replaceWith(this);
		}

		//$(this).show();
		if (onload_cb) {
			onload_cb($(this));
		}
		if (pCallback) {
			cb = pCallback;
			cb($(this));
		}
	})
	.error(function () { $(this).attr('src', gFailImage).show(); })
	.attr('src', image_src)
	.show();
	if (image_style != undefined)
		$(img).attr('style', image_style);
}

function LoadAllImages() {
	$('div').find('.loadable-image').each(function () {
		var loader = $(this);
		//loader.html('<img src="' + gLoadSpinnerUrl + '"/>');
		loader.html(null);
		LoadThisImage(loader);
	});
	//setTimeout('SecondLoadDelay()', 200);
}

function LoadTheseImages(div) {
	$("#" + div).find('.loadable-thumbnail').each(function () {
		var loader = $(this);
		loader.html(null);
		LoadThisImage(loader);
	});
	//setTimeout('SecondLoadDelay()', 200);
}

function SecondLoadDelay() {
	$('div').find('.loadable-image').each(function () {
		var loader = $(this);
		//loader.html('<img src="' + gLoadSpinnerUrl + '"/>');
		loader.html();
		LoadThisImage(loader);
	});
}

function ShowPromo(element, e) {
	//alert("show");
	$("#CLONED").each(function () { $(this).fadeOut(200).remove() });
	$(element).height("auto");
	if ($(element).find(".PromoText").hasClass("Hidden")) {
		$(element).find(".PromoText").hide().removeClass("Hidden");
	}
	var divPromo = $(element).find(".PromoText").clone();
	$(divPromo).css("position", "absolute").css("top", e.pageY).css("left", e.pageX).attr("id", "CLONED");
	$("body").append($(divPromo));
	var intPromoWidth = $(divPromo).width();
	var intPromoPosition = parseInt($(divPromo).css("left"));
	var intRight = intPromoWidth + intPromoPosition + 100;
	if (intRight > $(window).attr("innerWidth")) {
		$(divPromo).css("left", $(window).attr("innerWidth") - intPromoWidth - 100 + "px");
	}
	//$(divPromo).fadeIn(100);
	$("#CLONED").fadeTo('slow', 0.99);
}
function HidePromo(element) {
	$("#CLONED").each(function () { $(this).fadeOut('slow').remove() });
	//$(element).find(".PromoText").fadeOut(200);
}

function ShowMoreSubjects() {
	//alert("ShowMoreSubjects");
	if ($("#divMorePopularSubjects").css("display") == "none") {
		$("#divMorePopularSubjects").animate({ height: 'toggle' }, { "duration": 500, "easing": "easeInOutCirc" });
		$("#divMorePopularSubjects").removeClass("Hidden");
		$("#divSubject_0").html("Less <img src='/images/interface/icn_nav_arrow_up.png' />");
		t = setTimeout("CloseMoreSubjects()", 5000);
	} else {
		CloseMoreSubjects();
	}
}

function CloseMoreSubjects() {
	$("#divMorePopularSubjects").animate({ height: 'toggle' }, { "duration": 500, "easing": "easeInOutCirc" });
	$("#divSubject_0").html("More <img src='/images/interface/icn_nav_arrow_down.png' />");
	clearTimeout(t);
}

function RemoveWatermark(text) {
	$(text).filter(function () {
		return $(this).val() == "" || $(this).val() == strBaseSearch
	}).removeClass("WaterMarkOn").val("");
}

function AddWatermark(text) {
	$(text).filter(function () {
		return $(this).val() == ""
	}).addClass("WaterMarkOn").val(strBaseSearch);
}

$(document).ready(function () {

	var images = ['/images/interface/bg_nav_hover.gif', '/images/interface/bg_nav.gif'];

	jQuery.each(images, function (i) {
		images[i] = new Image();
		images[i].src = this;
	});

	$("#txtHeaderSearch").keypress(function (e) {
		if (e.keyCode == 13 && $("#txtHeaderSearch").val().length != 0 && $("#txtHeaderSearch").val() != strBaseSearch) {
			Search();
		}
	});
	if (("#txtLibrarySearch").length > 0) {
		$("#txtLibrarySearch").keypress(function (e) {
			if (e.keyCode == 13 && $("#txtLibrarySearch").val().length != 0 && $("#txtLibrarySearch").val() != strBaseSearch)
				Search();
		});
	}
	if ($("#txtHeaderSearch").val().length != 0 && $("#txtHeaderSearch").val() != strBaseSearch) {
		$("#txtHeaderSearch").removeClass("WaterMarkOn");
	} else {
		AddWatermark($("#txtHeaderSearch"));
	}
	if (("#txtLibrarySearch").length > 0 && $("#txtLibrarySearch").val() != undefined) {
		if ($("#txtLibrarySearch").val().length != 0 && $("#txtLibrarySearch").val() != strBaseSearch) {
			$("#txtLibrarySearch").removeClass("WaterMarkOn");
		} else {
			AddWatermark($("#txtLibrarySearch"));
		}
	}

	$("li.ShowPromo").live("mouseenter", function (e) { ShowPromo($(this), e) });
	$("li.ShowPromo").live("mouseleave", function () { HidePromo($(this)) });
	$("div.ShowPromo").live("mouseenter", function (e) { ShowPromo($(this), e) });
	$("div.ShowPromo").live("mouseleave", function () { HidePromo($(this)) });

	LoadAllImages();

	//set's the nav bar accordingly
	var intWidth = 0;
	var objectCount = 0;
	var maxWidth = $("#divPopularSubjects").width();
	$("#divPopularSubjects").find(".PopularSubjectNav").each(function () {
		intWidth += $(this).width() + 1; // 1px for right hand border;
		objectCount++;
	});
	intWidth -= 1 // no border on "more" column
	intAvailable = maxWidth - intWidth;
	intPadding = Math.floor(intAvailable / objectCount / 2);
	intWidth = 0;
	$("#divPopularSubjects").find(".PopularSubjectNav").each(function () {
		if ($(this).attr("id") != "divSubject_0") {
			if (isNaN(intPadding) || intPadding < 0)
				intPadding = 4;
			$(this).css("padding", "8px " + intPadding + "px");
			intWidth += $(this).width() + 1 + (2 * intPadding);
		}
	});
	$("#divSubject_0").css("border", "0px");
	$("#divSubject_0").width(50);
	intAvailable = maxWidth - intWidth - $("#divSubject_0").width();
	intLeft = Math.floor(intAvailable / 2);
	intRight = Math.ceil(intAvailable / 2);
	if (isNaN(intLeft) || intLeft < 0)
		intLeft = 0;
	if (isNaN(intRight) || intRight < 0)
		intRight = 0;
	$("#divSubject_0").css("padding", "8px " + intLeft + "px 8px " + intRight + "px")

	$("#divMorePopularSubjects").bind("mouseenter", function () { clearTimeout(t); });
	$("#divMorePopularSubjects").bind("mouseleave", function () { t = setTimeout("CloseMoreSubjects()", 1500); });

	$("#txtLibrarySearch").focus(function () { $("#divLibrarySearch").css("background-color", "#efefef"); });
	$("#txtLibrarySearch").blur(function () { $("#divLibrarySearch").css("background-color", "#fff"); });
});

function doNothing() {
	return;
}

function UpdateIndeces(noVariable) {
	if (window.location.pathname == '/player.html') {
		UpdateIndex(MEDIA_VIDEO);
		UpdateIndex(MEDIA_AUDIO);
	}
}

function PingClick(prod, ad, loc) {
	if (typeof player != undefined)
		player.sendEvent('play', 'false');
	$.get("/dynamic/ad_click.php", { lngProductDisplay: prod, lngAdvertisement: ad, bytLocation: loc, vid: visitorID }, function () { });
}

function PingDisplay(prod, ad, loc) {
	$.get("/dynamic/ad_impression.php", { lngProductDisplay: prod, lngAdvertisement: ad, bytLocation: loc, vid: visitorID }, function () { });
}

function ShowSlider() {
	sliderPane++;
	LoadTheseImages("SliderPane" + sliderPane);
}


