/*----------------------------------------------------------------------------//
// Javascript pour le site client  -------------------------------------------//
//----------------------------------------------------------------------------//
// Client : -
// Note   : -
// Date   : -
//----------------------------------------------------------------------------*/

/*----------------------------------------------------------------------------*/
// 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/swfobject.js");
importJavascript("/includes/js/plugins/jquery.timer.js");
importJavascript("/includes/js/plugins/jquery.thicker.js");
importJavascript("/includes/js/plugins/jquery.colorbox.js");
importCss("/includes/colorbox.css", "screen");

/*----------------------------------------------------------------------------*/
// Declarations jQuery
/*----------------------------------------------------------------------------*/

$(document).ready(function() {

    $(".RealUploadField").css({ opacity: 0 });

    $(".RealUploadField").change(function() {
        $(this).parent().children(".TxtBox").val($(this).val());
    });

    $(".ResetButton").click(function() {
        $(this).parent().children(".RealUploadField").val("");
        $(this).parent().children(".TxtBox").val("");
        return false;
    });

    $(".RealUploadField").mouseover(function() {
        $(this).parent().children(".UploadButton").css("color", "#001B1F");
        $(this).parent().children(".UploadButton").css("background-position", "0 -26px");
        $(this).parent().children(".UploadButton").children("span").css("background-position", "right -26px");
    });
    $(".RealUploadField").mouseout(function() {
        $(this).parent().children(".UploadButton").css("color", "#FFF");
        $(this).parent().children(".UploadButton").css("background-position", "0 0");
        $(this).parent().children(".UploadButton").children("span").css("background-position", "right 0");
    });

    $("a.ContentLinkButton").each(function() {
        $(this).html("<span>" + $(this).text() + "</span>");
    });

    $("a.ContentVideoButton").each(function() {
        $(this).html("<img alt='' src='/images/Bt_VideoLauncher.png'/><span>" + $(this).text() + "</span>");
        $(this).parent("p").addClass("ContentVideoButton");
        $(this).removeClass("ContentVideoButton");
    });

    $("a[rel='PhotoGallery']").colorbox({ transition: "elastic" });
    $("#VideoGalleryLauncher,p.ContentVideoButton a").colorbox({ width: "682px", height: "570px", inline: true, href: "#VideoContent" });
    
    $("#PhotoGalleryLauncher").click(function() {
        $("#PhotoGalleryContainer a:first").click();
        return false;
    });

    // -----------------------------------------------------------------------------------------------------------------
    // Input label focus -----------------------------------------------------------------------------------------------
    // -----------------------------------------------------------------------------------------------------------------
    if (jQuery(".ContentForm .TxtBox,.ContentForm .TxtArea").length > 0) {
        jQuery(".ContentForm .TxtBox,.ContentForm .TxtArea").each(function() {
            jQuery(this).focus(function() {
                if (jQuery(this).val().length == 0 || jQuery(this).val() == jQuery(this).attr("title")) {
                    jQuery(this).val("");
                }
            });
            jQuery(this).blur(function() {
                if (jQuery(this).val().length == 0 || jQuery(this).val() == jQuery(this).attr("title")) {
                    jQuery(this).val(jQuery(this).attr("title"));
                }
            });
        });
    }

    // -----------------------------------------------------------------------------------------------------------------
    // Menus déroulants stylisés ---------------------------------------------------------------------------------------
    // -----------------------------------------------------------------------------------------------------------------
    jQuery(".DropDownMenu a.Trigger").click(function() {
        jQuery(".DropDownMenu ul").toggleClass("Open");
        jQuery(document).bind("click", function() {
            jQuery(".DropDownMenu ul").removeClass("Open");
            jQuery(this).unbind("click");
        });
        return false;
    });

    jQuery(".DropDownMenu ul li a").click(function() {
        jQuery(".SelectList").val(jQuery(this).text());
        jQuery(".DropDownMenu a.Trigger").text(jQuery(this).text());
        jQuery(this).parents("ul").removeClass("Open");
        return false;
    });

    // -----------------------------------------------------------------------------------------------------------------
    // Ticker des partenaires ------------------------------------------------------------------------------------------
    // -----------------------------------------------------------------------------------------------------------------
    if ($("#ThickerContainer .InnerContainer").children(".ThickerItem").length >= 2) {
        jQuery("#ThickerContainer .InnerContainer").thicker({
            idThickerPrevious: "#Previous",
            idThickerNext: "#Next",
            effect: "slide",
            delay: 6000,
            speed: 500
        });
    }
    else $(".ThickerItem").css("display", "block");

    // -----------------------------------------------------------------------------------------------------------------
    // Video player pour les projets -----------------------------------------------------------------------------------
    // -----------------------------------------------------------------------------------------------------------------
    $("#VideoGalleryLauncher").click(function() {
        var videoPath = $("#VideoGalleryContainer").children("a").attr("href");
        loadVideo(videoPath);
        return false;
    });

    // -----------------------------------------------------------------------------------------------------------------
    // Video player pour les pages de contenu --------------------------------------------------------------------------
    // -----------------------------------------------------------------------------------------------------------------
    $("p.ContentVideoButton a").click(function() {
        var videoPath = $(this).attr("href");
        loadVideo(videoPath);
        return false;
    });

    // -----------------------------------------------------------------------------------------------------------------
    // Rotator à l'accueil ---------------------------------------------------------------------------------------------
    // -----------------------------------------------------------------------------------------------------------------
    var wait = 0;

    if (jQuery("#Rotator").length == 1) {
        jQuery("#Rotator .RotatorItem:first").css("display", "block");
        jQuery("ul#RotatorPager li:first").addClass("On");
        var totalRotatorItems = 0;
        var cpt = 1;

        jQuery("#Rotator .RotatorItem").each(function() {
            totalRotatorItems++;
        });

        // Counter pour faire défiler les images
        jQuery.timer({ name: "Counter", interval: 5 }, function() {

            jQuery("#Rotator .RotatorItem[id=Item" + cpt + "]").stop().fadeOut(300);
            cpt++;
            resetPager();

            jQuery("#Rotator .RotatorItem[id=Item" + cpt + "]").stop().fadeIn(300);
            jQuery("ul#RotatorPager li[id=RPager" + cpt + "]").addClass("On");

            if (cpt > totalRotatorItems) {
                cpt = 1;
                jQuery("#Rotator .RotatorItem").stop().fadeOut(300);
                jQuery("#Rotator .RotatorItem:first").stop().fadeIn(300);
                jQuery("ul#RotatorPager li:first").addClass("On");
            }
        });
    }

    $("#RotatorPager li").click(function() {
        if (wait == 0) {
            wait = 1;
            jQuery(jQuery.stopTimer("Counter"));
            resetPager();
            resetRotatorItems();
            $(this).addClass("On");
            var selectedItem = $(this).attr("id").substr(6);
            jQuery("#Rotator .RotatorItem[id=Item" + selectedItem + "]").stop().fadeIn(300, function() {
                wait = 0;
            });
        }
    });
});

function resetRotatorItems(){
	jQuery("#Rotator .RotatorItem").each(function(){
		jQuery(this).stop().fadeOut(300);
	});
}

function resetPager(){
	jQuery("ul#RotatorPager li").each(function(){
		jQuery(this).removeClass("On")
	});
}

function addFlashContent(id,path){
	//<![CDATA[
		var params = {
			wmode: "transparent",
			scale: "noscale"
		};
		swfobject.embedSWF(path, id, "241", swfHeight, "10.0.0", "", {} , params);
		swfobject.createCSS("#"+id , "outline:none");
	//]]>
}
function loadVideo(path) {
	//<![CDATA[
		var skin = "/includes/swf/SkinOverPlaySeekMute.swf";
		var flashvars = { videoPath: path, skinPath: skin };
		var params = { menu: "false", wmode: "opaque" };
		var attributes = { id: "VideoContent", name: "VideoContent" };
		swfobject.embedSWF("/includes/swf/player.swf", "VideoContent", "640", "480", "10.0.0", "", flashvars, params, attributes);
		swfobject.createCSS("#VideoContent", "outline:none");
	//]]>
}

function ValidateForm(groupName, OnlyOneErrorByField, classError) {
    var msg = new Array();
    var ctl = new Array();
    var classValidated = "Validated";

    if (classError != '') jQuery('.' + classError).removeClass(classError);
    if (classValidated != '') jQuery('.' + classValidated).removeClass(classValidated);

    for (var i = 0; i < Page_Validators.length; i++) {
        if (Page_Validators[i].validationGroup == groupName && !Page_Validators[i].isvalid) {
            if (OnlyOneErrorByField) {
                var exist = false;
                for (var y = 0; y < ctl.length; y++) {
                    if (ctl[y] == Page_Validators[i].controltovalidate)
                        exist = true;
                }

                //if (!exist) {
                jQuery('#' + Page_Validators[i].controltovalidate + ' + span').removeClass(classValidated);
                    msg[msg.length] = Page_Validators[i].errormessage;
                    if (classError != '') jQuery('#' + Page_Validators[i].controltovalidate + ' + span').addClass(classError);
                    jQuery('#' + Page_Validators[i].controltovalidate + ' + span').html(msg[msg.length - 1]);
                //}
            }
            else {
                msg[msg.length] = Page_Validators[i].errormessage;
                if (classError != '') jQuery('#' + Page_Validators[i].controltovalidate + ' + span').addClass(classError);
                jQuery('#' + Page_Validators[i].controltovalidate + ' + span').html(msg[msg.length - 1]);
            }

            ctl[i] = Page_Validators[i].controltovalidate;
        }
        else if (Page_Validators[i].validationGroup == groupName && Page_Validators[i].isvalid) {
            if (OnlyOneErrorByField) {
                var exist = false;
                for (var y = 0; y < ctl.length; y++) {
                    if (ctl[y] == Page_Validators[i].controltovalidate)
                        exist = true;
                }

                if (!exist) {
                    jQuery('#' + Page_Validators[i].controltovalidate + ' + span').html(jQuery('#InputOk').val());
                    jQuery('#' + Page_Validators[i].controltovalidate + ' + span').addClass(classValidated);
                }
            }
            else {
                jQuery('#' + Page_Validators[i].controltovalidate + ' + span').html(jQuery('#InputOk').val());
                jQuery('#' + Page_Validators[i].controltovalidate + ' + span').addClass(classValidated);
            }

            ctl[i] = Page_Validators[i].controltovalidate;
        }
    }

    return msg;
}

function CheckForm(pId) {
    if (typeof (Page_ClientValidate) == 'function') {
        if (!Page_ClientValidate(pId)) {
            var msg = ValidateForm(pId, true, "Error");
            return false;
        }
    }
    return true;
}
