function setContentBackground() {
	if ($("#Home").attr("id") != undefined) {
		$("div.content").css("background", "#FFFFFF");
	}
}

function resetSmallBanner() {
	$('img.smallbannerimg').each(function (index) {
		newsrc = $(this).attr("src").replace("_over", "_normal");
		$(this).attr("src", newsrc);
	});
}

function changeBanner() {
    $("img.smallbannerimg").hover(function (e) {

        resetSmallBanner();
        var position = $(this).attr("src").lastIndexOf("/");
        var img = $(this).attr("src").substring(position + 1);
        var path = $(this).attr("src").substring(0, position + 1);

        newsrc = path + img.toString().replace("small", "big")
        newsrc = newsrc.replace(".png", ".jpg");
        newsrc = newsrc.replace("_normal", "");
        newsrc = newsrc.replace("_over", "");

        $("#imgBigBanner").attr("src", newsrc);

        newsmallimg = path + img.toString().replace("_normal", "_over");

        $(this).attr("src", newsmallimg);

        //Fix to put a usemap to battlenova in banner, this hack must be removed when this functionality is no longer use
        var battlenovaPat = /big_battlenova/;
        if (battlenovaPat.test(newsrc)) {
            document.getElementById("area1").coords = "65,112,265,162";
            document.getElementById("area2").coords = "65,166,265,218";
            document.getElementById("urlBigBanner").style.cursor="default";
        }
        else {
            document.getElementById("area1").coords = "0, 0, 0, 0";
            document.getElementById("area2").coords = "0, 0, 0, 0";
            document.getElementById("urlBigBanner").style.cursor = "pointer";
        }
        //end of js hack
    });

	$("a.smallbannerimg").hover(function (e) {
		newhref = $(this).attr("href");
		$("#urlBigBanner").attr("href", newhref);
	});
}

function setupNavigation() {
	$("#menu li.submenu").each(function () {
		var button = $(this), submenu = $("ul", button);

		button.data("currentController", button.hasClass("active"));
		button.add(submenu).hover(showSubmenu, hideSubmenu);

		function showSubmenu() {
			submenu.data("hide", false);

			if (!submenu.visible() || !submenu.data("hide")) {
				button.addClass("active");
				submenu.fadeIn("fast");
			}
		}

		function hideSubmenu() {
			submenu.data("hide", true);

			window.setTimeout(function () {
				if (submenu.visible() && submenu.data("hide")) {
					submenu.fadeOut("fast");

					if (!button.data("currentController"))
						button.removeClass("active");
				}
			}, 1000);
		}
	});
}

function setupIEActiveButton() {
	$("#menu .bgwrapper > ul > li")
		.append($("<div/>").addClass("tl"))
		.append($("<div/>").addClass("tr"));
}

function getParameterFromQueryString(name) {
	name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
	var regexS = "[\\?&]" + name + "=([^&#]*)";
	var regex = new RegExp(regexS);
	var results = regex.exec(window.location.href);
	if (results == null)
		return "";
	else
		return results[1];
}


jQuery.fn.setPlaceholder = function () {
	if (typeof document.createElement("input").placeholder != "undefined")
		return this;

	return this.each(function () {
		var $this = $(this);

		var placeholder = $this.attr("placeholder");

		this.prevType = $this.attr("type").toLowerCase();
		this.prevMaxLength = this.maxLength;

		if (this.prevType == "password") {
			$this.after("<input type='text' name='pseudo_" + this.id + "' id='" + this.id + "' />");
			$("[name=pseudo_" + this.id + "]").attr("class", $this.attr("class")).width($this.width()).height($this.height()).hide().val(text)[0].campo = this;
		}

		$this.focus(function () {
			if (this.value == placeholder) {
				this.value = "";

				if (this.prevMaxLength > 0)
					this.maxLength = this.prevMaxLength;

				if (this.prevType == "password") {
					$("[name=pseudo_" + this.id + "]").hide();
				}
			}
		});

		$("[name=pseudo_" + this.id + "]").focus(function () {
			$(this).hide();
			$(this.campo).show().trigger("focus")[0].focus();
		});

		$this.blur(function () {
			if (this.value == "") {
				if (this.prevMaxLength > 0)
					this.maxLength = placeholder.length;
				this.value = placeholder;

				if (this.prevType == "password") {
					$("[name=pseudo_" + this.id + "]").show();
					$(this).hide();
				}
			}
		});

		$this.trigger("blur");
	});
};

jQuery.fn.enable = function () {
	return this.removeAttr("disabled");
};

jQuery.fn.disable = function () {
	return this.attr("disabled", "disabled");
};

jQuery.fn.visible = function () {
	var invisible = this.css("display") == "none";
	return !invisible;
}

jQuery.fn.addLoader = function (callback) {
	$('*', this).disable();

	var position = this.position();
	var centerX = position.left + (this.width() / 2);
	var centerY = position.top + (this.height() / 2);

	return this.fadeTo("fast", 0.5, function () {
		var loader = $('<div class="loader"></div>');

		loader.insertBefore(this);
		loader.css({
			left: centerX - (loader.width() / 2),
			top: centerY - (loader.height() / 2)
		});

		if (callback) callback(this);
	});
};

jQuery.fn.removeLoader = function (callback) {
	this.prev('div.loader').remove();

	return this.fadeTo("fast", 1, function () {
		$('*', this).enable();
		if (callback) callback(this);
	});
};

// starting the script on page load
$(document).ready(function () {
	setContentBackground();
	changeBanner();
	setupNavigation();

	$("input[placeholder]").setPlaceholder();

	var preLoader = $('<img src="/Content/images/loader.gif" />').hide();
	$("body").append(preLoader);
});
