/*----------------------------------------------------------------------------//
// Javascript pour le site client  -------------------------------------------//
//----------------------------------------------------------------------------//
// Client : Messaging Architechs
// Date   : 6 jan 2009
//----------------------------------------------------------------------------*/

/*----------------------------------------------------------------------------*/
// 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
/*----------------------------------------------------------------------------*/
importJavascript("/includes/js/plugins/jquery.dimensions.js");
importJavascript("/includes/js/plugins/jquery.thicker.js");

importJavascript("/includes/js/plugins/jquery.thickbox.js");
importCss("/includes/js/plugins/thickbox.css", "screen");

importJavascript("/includes/js/swfobject.js");

/*----------------------------------------------------------------------------*/
// Declarations jQuery
/*----------------------------------------------------------------------------*/

/* - Equalcols height function
   ------------------------------------*/
function equalHeight(group) {
    tallest = 0;
    group.each(function() {
        thisHeight = jQuery(this).height();
        if(thisHeight > tallest) {
            tallest = thisHeight;
        }
    });
    group.height(tallest);
}

/* - Parse Content Boxes (Columns)
   ------------------------------------*/
function parseContentBox() {
	jQuery("#Content .ContentBox").each(function(){
		if(!jQuery(this).hasClass("OneCol"))
			equalHeight(jQuery(".Col", this));
	});
}

jQuery(document).ready(function(){
						   
	/* -------------------------------------- Common ---- */
	// Language DropDown List
	jQuery("#Header .Language a:first").click(function(){
		var dropList = jQuery("#Header .Language ul");
		dropList.show();
		jQuery(document).bind("click", function(){
			dropList.hide();
			jQuery(this).unbind("click");
		});
		return false;
	});
	// Call To Action Banners
	jQuery("body .CallToAction-Black, body .CallToAction-Grey").each(function(){
		var blocLink = jQuery("a", this).attr("href");
		jQuery("*", this).css("cursor", "pointer").click(function(){
			window.location = blocLink;
			return false;
		});
	})

	/* ------------------------------------ Homepage ---- */
	// Pub Slideshow
	if(!jQuery("#AccSlideShow .GoTo").length == 0){
		jQuery("#AccSlideShow").thicker({
			childrens: "div",
			idThickerPrevious: "#AccSlideShow .Nav .Prev a",
			idThickerNext: "#AccSlideShow .Nav .Next a",
			idThickerGoTo: "#AccSlideShow .Nav .GoTo a",
			effect: "fade",
			delay: 6000,
			speed: 500
		});
  }
	// Homepage > Summary Tabs
	jQuery("#Summaries .Nav a").click(function() {
		jQuery("#Summaries .Nav a").removeClass("On");
		jQuery("#Summaries .Bloc").hide();
		jQuery(this).addClass("On");
		jQuery("#Summaries .Bloc." + jQuery(this).attr("rel")).show();
		return false;
	});
	// Customer Logo Carousel
	if(jQuery("#AccLogoCarousel li").length >0 )
		jQuery("#AccLogoCarousel").thicker({
			childrens: "li",
			effect: "fade",
			delay: 2500,
			speed: 500
		});
	// Call To Action Buttons Home
	jQuery("body.Home .CallToAction .Bloc").each(function(){
		var blocLink = jQuery("a", this).attr("href");
		jQuery("*", this).css("cursor", "pointer").click(function(){
			window.location = blocLink;
			return false;
		});
	}).hover(function(){
		jQuery(".Background", this).css("opacity", 0.4);
		if ( jQuery(".Over", this).html() != null ) {
			jQuery(".Out", this).hide();
			jQuery(".Over", this).show();
		}
	},function(){
		jQuery(".Background", this).css("opacity", 1.0);
		if ( jQuery(".Over", this).html() != null ) {
			jQuery(".Over", this).hide();
			jQuery(".Out", this).show();
		}
	});

	/* ------------------------------------ Products ---- */
	// Products Details > Tabs
	jQuery("body.Products-Details #Content .ContentBox .Tabs a").click(function(){
		var parentContentBox = jQuery(this).parents(".ContentBox");
		jQuery(" .Tabs a", parentContentBox).removeClass("On");
		jQuery(".Col", parentContentBox).hide();
		jQuery(this).addClass("On");
		jQuery("#" + jQuery(this).attr("rel"), parentContentBox).show();
		return false;
	});
	jQuery("body.Products-Details #Content .ContentBox a.NextTab").click(function(){
		var parentTabContent = jQuery(this).parents(".Col").attr("id");
		var currentTab = jQuery("body.Products-Details #Content .ContentBox .Tabs a[@rel=" + parentTabContent + "]");
		currentTab.parents("li").next().children("a").click();
		return false;
	});

	/* ------------------------------------ Resources ---- */
	// Resources > Webcasts (Accordion)
	var lastTrigger = null;
	jQuery("#Accordion div").hide();
	jQuery("#Accordion .LinkWithChevron").click(function(){
		if(!jQuery(this).parents("li").hasClass("On")){
			jQuery("#Accordion li.On").each(function(){
				jQuery(this).removeClass("On");
				jQuery(this).children("div").slideUp(400);
			});
		}
		jQuery(this).parents("li").toggleClass("On");
		jQuery(this).next("div").slideToggle(400);
		return false;
	});

	/* ------------------------- Identify your risks ---- */
	// ToolTips
	jQuery(".ToolTip").click(function(e){
		if(jQuery("#ToolTipContainer").length == 0){
			jQuery("body").append('<div id="ToolTipContainer"><img src="/images/Img_ToolTip_Close.gif" alt="" width="7" height="7" class="Close" /><div class="body"></div><img src="/images/Img_ToolTip_Arrow.gif" alt="" width="220" height="15" /></div>')
		}
		var toolTip = jQuery("#ToolTipContainer");
		jQuery(".body", toolTip).html(jQuery(this).next(".ToolTipContent").html());
		var mX = e.clientX + jQuery(document).scrollLeft();
		var mY = e.clientY + jQuery(document).scrollTop();
		toolTip.css({
			left: mX - 110,
			top: mY - (toolTip.height() + 10)
		});
		toolTip.show();
		jQuery(document).bind("click", function(){
			toolTip.hide();
			jQuery(this).unbind("click");
		});
		return false;
	});
	//Tabs
	jQuery("body.EmailRisk-Results #Content .ContentBox .Tabs a").click(function() {
		var parentContentBox = jQuery(this).parents(".ContentBox");
		jQuery(" .Tabs a", parentContentBox).removeClass("On");
		jQuery(".ContentTab").hide();
		jQuery(this).addClass("On");
		jQuery("#" + jQuery(this).attr("rel")).show();
		return false;
	});


});

function ValidateForm(groupName, OnlyOneErrorByField, classError) {
    var msg = new Array();
    var ctl = new Array();
    var showsummary = false;
    if (classError != '') jQuery('.' + classError).removeClass(classError);

    //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) {
            showsummary = true;
            if (OnlyOneErrorByField) {
                var exist = false;
                for (var y = 0; y < ctl.length; y++) {
                    if (ctl[y] == Page_Validators[i].controltovalidate)
                        exist = true;
                }
                if (!exist) {
                    msg[msg.length] = Page_Validators[i].errormessage;
                    if (classError != '') jQuery('#' + Page_Validators[i].controltovalidate).addClass(classError);
                }
            }
            else {
                msg[msg.length] = Page_Validators[i].errormessage;
                if (classError != '') jQuery('#' + Page_Validators[i].controltovalidate).addClass(classError);
            }
            ctl[i] = Page_Validators[i].controltovalidate;
        }
    }
    if (showsummary) {
        $(".ValidationSummary").css('display', 'inline');
    }
    else {
        $(".ValidationSummary").css('display', 'none');
    }

    return msg;
}

function CheckForm(validationGroup) {
    if (typeof (Page_ClientValidate) == 'function') {
        if (!Page_ClientValidate(validationGroup)) {
            var msg = ValidateForm(validationGroup, true, "Erreur");
            return false;
        }
    }
    return true;
}
