/*----------------------------------------------------------------------------//
// Javascript pour le site client  -------------------------------------------//
//----------------------------------------------------------------------------//
// Client : Gaz Metro Plus
// Date   : Aout 2010
//----------------------------------------------------------------------------*/
(function($, w, undefined) {

    var G = {};
	var slidingMenu = false;

    /*----------------------------------------------------------------------------*/
    // Inclusion dynamique des fichiers externes javascripts
    /*----------------------------------------------------------------------------*/
    function importJavascript(src) { document.write("<script type=\"text/javascript\" src=\"" + src + "\"></scr" + "ipt>"); }
    function importCss(href, media) { document.write('<link rel="stylesheet" type="text/css" media="' + media + '" href="' + href + '" />'); }

    /*----------------------------------------------------------------------------*/
    // Importations de fichiers
    /*----------------------------------------------------------------------------*/

    /* Caroussel */
    importJavascript("/includes/js/plugins/abso.caroussel.js");

    /*----------------------------------------------------------------------------*/
    // Declarations jQuery
    /*----------------------------------------------------------------------------*/
    $(document).ready(function() {
		
		$("#MenuPrincipal").children("li:last").children("div").remove();
		
		$("#MenuPrincipal li").hover(
		  function () {
			$(this).children("div").stop(true, true).slideDown(250);
		  },
		  function () {
			$(this).children("div").stop(true, true).slideUp(250);
		  }
		);
		
		

        /////////////////////////////////////////////////////////////
        // Fixes Navigateurs
        $("hr").wrap("<div class=\"hr\">");
        $("p > table").unwrap();
        if (G.isEmpty($("table").prev("p").text()))
            $("table").prev("p").remove();

        /////////////////////////////////////////////////////////////
        // AbsoCaroussel
        AbsoCaroussel.init({
            id: 'CarousselAcc',
            wrap: true,
            autostart: {
                interval: 6000
            }
        });

        /////////////////////////////////////////////////////////////
        // Champ de recherche
        $('#txtKeywords').keydown(function(e) {
            if (e.keyCode == 13) {
                G.redirectToSearchPage(); return false;
            } else {
                return true;
            }
        });
        $('#SearchButton').click(G.redirectToSearchPage);

    });

    /////////////////////////////////////////////////////////////
    // Fonction Utilitaire
    G.isEmpty = function(text) {
        text = text.replace(/^\s*|\s*$/g, '');
        text = text.replace(/^\t*|\t*$/g, '');
        return (text == '') ? true : false;
    };
    G.redirectToSearchPage = function() {
        var obj = $('#txtKeywords');
        var query = obj.val();
        if (query != null && query != '' && query != obj.attr('title')) {
            document.location = GMP_SearchPage + '?q=' + query;
            return false;
        }
    }
    /////////////////////////////////////////////////////////////
    // Equalcols height
    G.EqualHeight = function(group, important) {
        tallest = 0;
        important = (important == false ? "" : " !important");
        group.each(function() {
            thisHeight = jQuery(this).height();
            if (thisHeight > tallest) {
                tallest = thisHeight;
            }
        });
        group.each(function() {
            css = (jQuery(this).attr("style") ? jQuery(this).attr("style") : "");
            jQuery(this).attr("style", css + "height:" + tallest + "px" + important + ";");
        });
    }
    G.ClearEqualHeight = function(group) {
        group.each(function() { jQuery(this).css("height", ""); });
    }

    w.GMP = G;

})(jQuery, window);


function showAlert(txt) {
    $(document).ready(function() {
        if (txt != "") { $("#ErreurPopup p").html(txt); }
        tb_show(null, '#TB_inline?height=125&amp;width=350&amp;inlineId=ErreurPopup', false);
    });
};

function CheckForm(groupName) {
    if (typeof (Page_ClientValidate) == 'function') {
        if (!Page_ClientValidate(groupName)) {
            var arrayError = new Array();
			//Ne pas oublier d'appliquer "Page_ClientValidate()" avant d'appeler cette procédure
			for (var i = 0; i < Page_Validators.length; i++) {
				if (Page_Validators[i].validationGroup == groupName && !Page_Validators[i].isvalid) {
					if(Page_Validators[i].controltovalidate != "")
						arrayError.push(Page_Validators[i].controltovalidate);
				}
			}
			// Show Style on the control
			ShowFormError(arrayError.join(","));
            return false;
        }
    }
    return true;
}
// strErrorCS : String with id comma separated
function ShowFormError(strErrorCS) {
	var classError = "Erreur";
	var arrayError = strErrorCS.split(",");
	if (arrayError.length > 0) { 
		jQuery('.' + classError).removeClass(classError);//Cleanup
		for (var i = 0; i < arrayError.length; i++) {
			jQuery('#' + arrayError[i]).addClass(classError);
		}
	}
}
// Loading function
function showLoading() {
    $(".Formulaire").block({
        message: '<img src=\"/images/Img_Loading.gif\" width=\"32\" height=\"32\" alt=\"\" />',
        overlayCSS: { opacity: 0.2 },
        css: {
            width: '32px',
            height: '32px',
            border: 'none',
            padding: '0px',
            backgroundColor: 'transparent'
        }
    }).css('overflow', 'hidden');
}
function hideLoading() {
    $(".Formulaire").unblock().css('overflow', 'visible');
}

function MaxLength(obj, mlength) {
    if (obj.getAttribute) {
        if (obj.value.length > mlength) {
            obj.value = obj.value.substring(0, mlength)
        }
    }
}
