var fixMinMaxwidth = function () {
    if (typeof document.body.style.maxHeight !== "undefined" &&
               typeof document.body.style.minHeight !== "undefined") return false;

    $('.fixMinMaxwidth').each(function () {
        var maxWidth = parseInt($(this).css("max-width"));
        var minWidth = parseInt($(this).css("min-width"));

        if (maxWidth > 0 && $(this).width() > maxWidth) {
            $(this).width(maxWidth);
        } else if (minWidth > 0 && $(this).width() < minWidth) {
            $(this).width(minWidth);
        }
    });
}

function CheckWindowSize() {
    var width = $(window).width();
    var bodyClass = 'smallWindow';
    var formsClass = 'smallForms';
    if (width < 990) {
        $('body').removeClass(bodyClass).addClass(bodyClass);
    } else {
        $('body').removeClass(bodyClass);
    }

    if (width < 1200) {
        $('.form-holder-wrapper').addClass(formsClass);
    } else {
        $('.form-holder-wrapper').removeClass(formsClass);
    }

    ApplyFillers();
}

function ApplyFillers() {

	// alert($(window).height() - $("#content").height() + 140);
	// $("#content").append('<div id="main-content-filler"></div>');
	// $('#content #main-content-filler').css("height", $(window).height() - $("#content").height());
	/*
    $('#main-content-filler').remove();
    if ($("#content").height() < $("#body-wrapper").height()) {
        $("#content").append('<div id="main-content-filler">&nbsp;</div>');
        $('#content #main-content-filler').css("height", $("#body-wrapper").height() - $("#content").height() + 30);
    } else {
        if ($("#content").height() < $("#right-side-bar").height()) {
            $("#content").append('<div id="main-content-filler">&nbsp;</div>');
            $('#content #main-content-filler').css("height", $("#right-side-bar").height() - $("#content").height() + 30);
        }
    }
    */
}

function ResizeForm() {
    if ($(".form-holder-hover").length != 0) {
        $('.form-holder-hover').width($("#content").width());
        $('.form-holder-hover').height(document.documentElement.scrollHeight);
    }
}

function showSubMenu() {
    $(this).find(".submenu-wrapper").height($(window).height());
    $(this).find(".submenu-wrapper").fadeIn(200);
    $(this).addClass("hovered");
}

function hideSubMenu() {
    $(this).find(".submenu-wrapper").fadeOut(200);
    $(this).removeClass("hovered");
}

$(document).ready(function () {

    /* Use to fix minWidth and maxWidth in older browsers */
    $("#main-box").addClass("fixMinMaxwidth");
    $("#footer").addClass("fixMinMaxwidth");
    fixMinMaxwidth();
    /* Use to fix minWidth and maxWidth in older browsers */

	/* Profile Menu */

	if($("#logged-in-menu").length != 0){
		//$("#logged-in-menu").hide();
	}

	$("#logged-in-btn").click(function(){
		if($("#logged-in-menu").length != 0){
			$("#logged-in-menu").slideToggle();
			$(this).toggleClass("open");
		}
	});

	/* Profile Menu */

    /* Add class to first or last element to help with the display */
    $("#sharing-icons li:first").addClass("first");
    $("#login a.lnk-btn:first").addClass("first");
    $("#form-steps .form-step:first").addClass("first");

    $("#right-side-bar div.list-box").each(function () {
        $(this).find("a.lnk-btn:first").addClass("first");
    });

    $("#right-side-bar div.survey-box").each(function () {
        $(this).find("a.lnk-btn:first").addClass("first");
    });

    $("#right-side-bar div.refine-box").each(function () {
        $(this).find("a.lnk-btn:first").addClass("first");
    });

    $("#right-side-bar div.refine-box").each(function () {
        $(this).find(".refine-fields:last").addClass("last");
    });

    /* Add class to first or last element to help with the display */

    /* Default text for input text fields */
    $('#newsletter-email').watermark('Entrez votre courriel', { className: 'watermark' });
    $('#recherche').watermark('Rechercher dans le site', { className: 'watermark' });
    $(".recherche").watermark("Rechercher...", { className: "watermark" });
    $("#txtEmploisCritere").watermark("Entrez vos critère de recherche...", { className: "watermark" });
    $("#txtEvenementsCritere").watermark("Entrez vos critère de recherche...", { className: "watermark" });
    $("#txtSearchCriteria").watermark("Entrez vos critère de recherche...", { className: "watermark" });
    $("#utilisateurSearchCriteria").watermark("Entrez vos critère de recherche...", { className: "watermark" });
    
    /* Default text for input text fields */

    /* Custom Select Boxes */

	 /*$("select").addClass("custom-sb fixed-width medium");
    $("select.custom-sb").sb({ fixedWidth: true });*/

    /* Custom Select Boxes */

    /* Add span to titles with author */

    $('.title-w-author').each(function () {
        var titleContent = $(this).html()
        $(this).empty().append("<span>" + titleContent + "</span>");
    });

    /* Add span to titles with author */

    $("#login-btn").click(function () {
        if ($("#login-form").length != 0) {
            if ($("#login-form").hasClass('login-shown')) {
                $("#login-form").fadeOut();
                $('#login-form').removeClass('login-shown');
            } else {
                $('body').prepend($('#login-form'));
                ResizeForm();
                $('#login-form').fadeIn('slow');
                $('#login-form').addClass('login-shown');
            }
        }
        return false;
    });

    /* Submenu */

    var config = {
        over: showSubMenu, // function = onMouseOver callback (REQUIRED)
        timeout: 150, // number = milliseconds delay before onMouseOut
        out: hideSubMenu // function = onMouseOut callback (REQUIRED)
    };

    $(".has-submenu").hoverIntent(config);

    /* Submenu */

    /* Footer bottom */

    if ($("#body-wrapper").height() + $("#footer").height() < $(window).height()) {
        $("#body-wrapper").height($(window).height() - $("#footer").height() - 2);
    }

    $(window).resize(function () {
        if ($("#body-wrapper").height() + $("#footer").height() < $(window).height()) {
            $("#body-wrapper").height($(window).height() - $("#footer").height() - 2);
        }
    })

    /* Footer bottom */

	/* Items List */
/*
		$("table.items-list-table tr").each(function(i){
			$(this).after('<tr class="spacer"><td colspan="' + $(this).find("td").length + '">&nbsp;</td></tr>');
		});
	*/
	/* Items List */

    CheckWindowSize();
	// ApplyFillers();

});

$(window).bind("resize", function () {
    fixMinMaxwidth();
    CheckWindowSize();
    ResizeForm();
});
