
// Action controller
$(document).ready(function()
 {
    $("#link_etre").click(function(){window.location = "http://www.guidegaiduquebec.com/etremag/magazine_etre.php";});
    $("#link_2b").click(function(){window.location = "http://www.guidegaiduquebec.com/2bmag/2b_magazine.php";});
    $("#link_rg").click(function(){window.location = "http://www.guidegaiduquebec.com/rgmag/magazine_rg.php";});
//    $("#link_rg").click(function(){loadPopup(); initPopup18Controller();});
//    $("#closePopupRG").click(function(){disablePopup();});
    $("#link_guide_img").click(function(){window.location = "http://www.guidegaiduquebec.com/guidegaiduquebec/guide_gai.php";});
    $("#link_guide_fr").click(function(){window.location = "http://www.guidegaiduquebec.com/guidegaiduquebec/guide_gai.php";});
    $("#link_guide_en").click(function(){window.location = "http://www.guidegaiduquebec.com/guidegaiduquebec/gay_guide.php";});

    $("#link_etre").live("mouseover", function(){$(this).addClass('img-hover');});
    $("#link_etre").live("mouseout", function(){$(this).removeClass('img-hover');});
    $("#link_2b").live("mouseover", function(){$(this).addClass('img-hover');});
    $("#link_2b").live("mouseout", function(){$(this).removeClass('img-hover');});
    $("#link_rg").live("mouseover", function(){$(this).addClass('img-hover');});
    $("#link_rg").live("mouseout", function(){$(this).removeClass('img-hover');});
    $("#link_guide").live("mouseover", function(){$(this).addClass('img-hover');});
    $("#link_guide").live("mouseout", function(){$(this).removeClass('img-hover');});

    //$("#content").hide();
    $("#link_contact_fr").click(function() {
        loadPage("pages/content/contact.php", {lang: "fr"});
        initContactController();
//		$("html,body").animate({ scrollTop: $("html,body").attr("scrollHeight") }, 2000);
    $("html,body").animate({ scrollTop: "560" }, 2000);
        return false;
    });

    $("#link_contact_en").click(function() {
        loadPage("pages/content/contact.php", {lang: "en"});
        initContactController();
//        $("html,body").animate({ scrollTop: $("html,body").attr("scrollHeight") }, 2000);
        $("html,body").animate({ scrollTop: "560" }, 2000);
        return false;
    });

    $("#link_pub_fr").click(function() {
        loadPage("pages/content/pub.php", {lang: "fr"});
        $("html,body").animate({ scrollTop: "560" }, 2000);
        return false;
    });

    $("#link_pub_en").click(function() {
        loadPage("pages/content/pub.php", {lang: "en"});
        $("html,body").animate({ scrollTop: "560" }, 2000);
        return false;
    });

    $("#link_promo_fr").click(function() {
        loadPage("pages/content/promo.php", {lang: "fr"}); 
        initPubController();
        $("html,body").animate({ scrollTop: "560" }, 2000);
        return false;
    });
    
    $("#link_promo_en").click(function() {
        loadPage("pages/content/promo.php", {lang: "en"}); 
        initPubController();
        $("html,body").animate({ scrollTop: "560" }, 2000);
        return false;
    });
    	
    $(".buttonform").live("mouseover", function(){$(this).addClass('buttonform-hover');});
    $(".buttonform").live("mouseout", function(){$(this).removeClass('buttonform-hover');});

    $(".linktext").live("mouseover", function(){$(this).addClass('linktext-hover');});
    $(".linktext").live("mouseout", function(){$(this).removeClass('linktext-hover');});

    // popup handlers
    $("#popupContactClose").click(function(){disablePopup();});
    $("#backgroundPopup").click(function(){disablePopup();});

    $(document).keypress(function(e){
            if(e.keyCode==27 && popupStatus==1){disablePopup();}
    });
	
    jQuery.fn.centerScreen = function(loaded) {
        var obj = this;
        if(!loaded) {
                obj.css('top', $(window).height()/2-this.height()/2);
                obj.css('left', $(window).width()/2-this.width()/2);
                $(window).resize(function() { obj.centerScreen(!loaded); });
        } else {
                obj.stop();
                obj.animate({ top: $(window).height()/2-this.height()/2, left: $
                	(window).width()/2-this.width()/2}, 200, 'linear');
        }
    }
    
    var section = $(document).getUrlParam("section"); 
    var langue = $(document).getUrlParam("langue"); 
    
    if (langue == 'fr'){
        switch(section)
        {
            case 'promotion':
                $("#link_promo_fr").trigger("click"); 
            break;
            case 'tarif':
                $("#link_pub_fr").trigger("click");
            break;
            case 'contact':
                $("#link_contact_fr").trigger("click");
            break;
            default:
        }
    }
    else if (langue == 'en'){
        switch(section)
        {
            case 'promotion':
                $("#link_promo_en").trigger("click"); 
            break;
            case 'tarif':
                $("#link_pub_en").trigger("click");
            break;
            case 'contact':
                $("#link_contact_en").trigger("click");
            break;
            default:
        }           
    }          
});

// AJAX page switcher
function loadPage(page, params, dest) {
    if (dest == null) { dest = "#content"; }

    showLoading(true);

    $(dest).load(page, params, function() {
    		showLoading(false);
    	}
    );
    
    //$(dest).toggle();
	    
    return false;
}

// Loading actions
function showLoading(loading){
    if (loading) {
            //document.body.style.cursor='wait';
            //$('#loading').show(500);
    } else {
            document.body.style.cursor='default';
            //$('#loading').hide(500);
    }
}


function include(filename){
    var head = document.getElementsByTagName('head')[0];

    script = document.createElement('script');
    script.src = filename;
    script.type = 'text/javascript';

    head.appendChild(script);

    return false;
}


function loadPopup(){
    //loads popup only if it is disabled
    if(popupStatus==0){
        //$("#popupContact").centerScreen();
        centerPopup();
        $("#backgroundPopup").css({
                "opacity": "0.7"
        });
        $("#backgroundPopup").fadeIn("slow");
        $("#popupContact").fadeIn("slow");
        popupStatus = 1;
    }
}

//disabling popup with jQuery magic!
function disablePopup(){
    //disables popup only if it is enabled
    if(popupStatus==1){
            $("#backgroundPopup").fadeOut("slow");
            $("#popupContact").fadeOut("slow");
            popupStatus = 0;
    }
}

function centerPopup(){
    //request data for centering
    var windowWidth = document.documentElement.clientWidth;
    var windowHeight = document.documentElement.clientHeight;
    var popupHeight = $("#popupContact").height();
    var popupWidth = $("#popupContact").width();
    //centering
    $("#popupContact").css({
            "position": "absolute",
            "top": windowHeight/2-popupHeight/2,
            "left": windowWidth/2-popupWidth/2
    });
    //only need force for IE6
    $("#backgroundPopup").css({
            "height": windowHeight
    });
}