/// <returns type="jQuery" />
/// <reference path="lib/jquery-1.2.6.min.js" />
function entsub(e) {
if (!e) e=window.event;
if (e.keyCode == 13)
	{OpenSearchBox();return false;}
}

$(document).ready(function() {

    /* retirer le border des derniers items*/
    $("ul#footer_list li:last-child, ul.link_list li:last-child, ul.title_list li:last-child, ul.sub_menu li:last-child, ul.link_list3 li:last-child, ul.mag_videos li:last-child, ul.link_list2.bis li:last-child").css({ border: "0" });
    $("ul#nav li:last-child, ul#sub_nav li:last-child").css({ background: "none" });

    /* faire apparaitre les sous menus */
    //		$("ul#nav > li > a").mouseover(function () {
    //		$(this).siblings("ul.sub_menu:not(:animated)").slideDown("fast");
    //		$(this).parent().siblings().children("ul.sub_menu:not(:animated)").slideUp("fast");
    //                    $(this).parent().addClass("on2");
    //                    }) ;
    //        $("ul#nav > li").bind("mouseleave",function() {
    //                    $(this).children("ul.sub_menu:not(:animated)").slideUp("fast");
    //                    $(this).removeClass("on2");
    //         }) ;
    $("ul#nav > li").hover(function() {
        $(this).addClass("on2").children("ul.sub_menu:has(li)").slideDown("fast");
        $(this).siblings().children("ul.sub_menu:not(:animated)").slideUp("fast");
    },
	            function() {
	                $("ul#nav > li").removeClass("on2");
	                $(this).children("ul.sub_menu:not(:animated)").slideUp("fast");
	            });
    $("ul#nav > li").mousemove(function() {
        $(this).siblings().children("ul.sub_menu").slideUp("fast");
    });


    /* opacité sur les logos*/
    $("ul.logos li").animate({ opacity: 0.3 }, 0);
    $("ul.logos li").hover(function() {
        $(this).fadeTo("fast", 1);
    },
			function() {
			    $(this).fadeTo("fast", 0.3);
			});

    /* masques */
    $(".thumb0")
			.wrap("<div class='thumb mask0_container'></div>")
			.after("<div class='mask0'></div>");

    $(".thumb1")
			.wrap("<div class='thumb mask1_container'></div>")
			.after("<div class='mask1'></div>");
    $(".thumb2")
			.wrap("<div class='thumb mask2_container'></div>")
			.after("<div class='mask2'></div>");

});

function OpenSearchBox() {
    if ($("#fSearch").val()!="")
	{Shadowbox.open({ type: 'iframe', content: 'Search.ASPX?q=' + $("#fSearch").val(), width: 750, height: 600, title: 'Rechercher' }); return false;}
	else
	{return false;}
};

function NewsLetterOptOut(WebServiceUrl) {
    var url = location.search;
    var ThisUserId = $.query.get('UserId');
    
    $.get(WebServiceUrl + "WebServiceOptOut.ashx", {UserId:ThisUserId }, function(data) {
        $('#WebServiceMessage').text('Vous ne recevrez plus la newsletter...');
        $('#WebServiceMessage').fadeIn("fast");
    });
};

function CreateFlvPlayer(VideoUrl, PlayerWidth, PlayerHeight, CoverUrl, ContainerId, ObjectId, PlayerAutoStart) {
    var flashvars = {
        file: VideoUrl,
        autostart: PlayerAutoStart,
        image: CoverUrl
    }
    var params = {
        allowfullscreen: "true",
        allowscriptaccess: "always",
        wmode: "transparent"
    }
    var attributes = {
        id: ObjectId,
        name: ObjectId
    }
    swfobject.embedSWF("flash/SimplePlayer.swf", ContainerId, PlayerWidth, PlayerHeight, "9.0.115", "false", flashvars, params, attributes);
};

function pub13rand(zonen, rootUrl) {
    if (!document.phpAds_used) document.phpAds_used = ',';
    phpAds_random = new String(Math.random()); phpAds_random = phpAds_random.substring(2, 11);
    document.write("<" + "script language='JavaScript' type='text/javascript' src='");
    document.write(rootUrl + "/ads/adjs.php?n=" + phpAds_random);
    document.write("&amp;what=zone:" + zonen + "");
    document.write("&exclude=" + document.phpAds_used);
    if (document.referrer)
        document.write("&referer=" + escape(document.referrer));
    document.write("'><" + "/script>");
}

function GetOnAirProductId() {
    $.get("GetOnAirProductId.ashx", function(data) {
        $('#' + data).addClass("on");
    });

};

function IsMember() {
    var MemberId = $.cookie('UserId');

    if (MemberId == null || MemberId == 'null' || MemberId == "null")
    { return false; }
    else
    { return true; }
};

function HasPlayed(GameId) {
    var HasPlayedThisGame = $.cookie(GameId)

    if (HasPlayedThisGame == null || HasPlayedThisGame == 'null' || HasPlayedThisGame == "null")
    { return false; }
    else
    { return true; }
};

function RestricPageAreas() {
    //alert('ThisPage is restricted, IsMember() : ' + IsMember());
    //#shadowbox_body {background:none;border:none;
    if (IsMember() == false) { $('#container').hide(); $('#RestrictedMessageArea ').show(); $('#shadowbox_body ').css("border", "none"); $('#shadowbox_body ').css("background", "none"); }
    else { $('#container').show(); $('#RestrictedMessageArea ').hide(); };

}

function IsReallyNull(ThisValue) {

    if (ThisValue == null || ThisValue == 'null' || ThisValue == "null")
    { return true; }
    else
    { return false; }
};

function FilterGame(GameId) {

    if (IsMember() == false) { $('#IsNotMember').fadeIn("slow"); };
    if (HasPlayed(GameId) == true) { $('#HasPlayed').fadeIn("slow"); };
    if (IsMember() == true && HasPlayed(GameId) == false) { $('#SubmitGame').fadeIn("fast"); $('#UserId').val($.cookie('UserId')); };

};

function VoteForBattle(ThisBattleId, ThisFilmId, WebServiceUrl, PageUrl) {
    var AsVoted = $.cookie(ThisBattleId + 'Vote');
    pageTracker._trackPageview("/" + PageUrl);
    if (AsVoted == null || AsVoted == 'null' || AsVoted == "null" || AsVoted == "")
    {
        $.get(WebServiceUrl + "VoteForBattle.ashx", { BattleId: ThisBattleId, FilmId: ThisFilmId, Action: 'SetVote' }, function(data) {
        $.cookie(ThisBattleId + 'Vote', '1', { expires: 365 });
            var F1Vote = jQuery('F1Display', data).text();
            var F2Vote = jQuery('F2Display', data).text();
            $(".VoteButton").attr({ 'title': 'Votre unique !' });
            $(".VoteButton").css('cursor', 'not-allowed');$(".VoteButton").css('filter', 'alpha(opacity=10)');$(".VoteButton").css('-moz-opacity', '0.1');$(".VoteButton").css('opacity', '0.1');
            DisplayBattleResults(ThisBattleId, F1Vote, F2Vote);
        });
    }
    else {
        $(".VoteButton").attr({ 'title': 'Votre unique !' });
        $(".VoteButton").css('cursor', 'not-allowed'); $(".VoteButton").css('filter', 'alpha(opacity=10)'); $(".VoteButton").css('-moz-opacity', '0.1'); $(".VoteButton").css('opacity', '0.1');
        
    }
};

function CheckSurveyVotes(SurveyId, WebServiceUrl, Answers) {

    var AsVoted = $.cookie(SurveyId);
    if (AsVoted == null || AsVoted == 'null' || AsVoted == "null" || AsVoted == "") {
        $('#Survey_' + SurveyId).show();  
    }
    else { GetSurveyVotes(SurveyId, WebServiceUrl, Answers); }

};

function DisplaySurveyVotes(data) {
    jQuery('body').append(data);
};

function GetSurveyVotes(SurveyId, WebServiceUrl, Answers) {
    $.get(WebServiceUrl + "WebserviceSurveyVote.ashx", { SurveyId: SurveyId, Action: 'GetSurveyVote', Answers: Answers }, function(data) {
        DisplaySurveyVotes(data);
    });
};

function SetSurveyVote(SurveyId, AnswerId, WebServiceUrl, Answers) {
    $.get(WebServiceUrl + "WebserviceSurveyVote.ashx", { SurveyId: SurveyId, Action: 'SetSurveyVote', AnswerId: AnswerId, Answers: Answers }, function(data) {
    $.cookie(SurveyId, '1', { expires: 365 });
        pageTracker._trackPageview("/Sondages.Votes");
        DisplaySurveyVotes(data);
   });     
};

function SetVote(Elements, WebServiceUrl, ThisVoteValue, DataSet) {
    var AsVoted = $.cookie(Elements + 'Vote');
    if (AsVoted == null || AsVoted == 'null' || AsVoted == "null" || AsVoted == "")
    { DoVote(Elements, WebServiceUrl, ThisVoteValue, DataSet); }
    else {
        $("#VoteMessage_" + Elements).css("display:none");
        $("#VoteMessage_" + Elements).html("Vous avez d&eacute;j&agrave; vot&eacute; !");
        $("#VoteMessage_" + Elements).fadeIn("slow");
    }
}

function DoVote(Elements, WebServiceUrl, ThisVoteValue, DataSetId) {
    $.get(WebServiceUrl + "WebserviceVote.ashx", { ItemIds: Elements, Action: 'SetVote', VoteValue: ThisVoteValue, DataSet: DataSetId }, function(data) {
    $.cookie(Elements + 'Vote', '1', { expires: 365 });
        jQuery('body').append(data);
        $("#VoteMessage_" + Elements).css("display:none");
        $("#VoteMessage_" + Elements).html("Merci !");
        $("#VoteMessage_" + Elements).fadeIn("slow");
    });
}

function SwitchOn(StarId, EId) {
    var thisElementId = StarId.replace('Star_', 'Link_');
    var ElementId = EId.replace('T_', '');
    var AsVoted = $.cookie(ElementId + 'Vote');
    
    if (AsVoted == null || AsVoted == 'null' || AsVoted == "null" || AsVoted == "") {
        var MyStar = document.getElementById(StarId);
        MyStar.src = "images/etoile_over_full.png";
    }
    else {
        $("#" + thisElementId).attr({ 'title': 'Votre unique !' })
        $("#" + thisElementId).css('cursor', 'not-allowed'); 
    }
}

function SwitchOff(StarId, ActivatedStar) {
    //alert(StarId + ', ' + ActivatedStar)
    var MyStar = document.getElementById(StarId);
    MyStar.src = ActivatedStar;
}


function GetVotes(Elements, WebServiceUrl, DataSet) {
    $.get(WebServiceUrl + "WebserviceVote.ashx", { ItemIds: Elements, Action: 'GetVote', DataSet: DataSet }, function(data) {
        jQuery('body').append(data);
    });
}

function CountComments(Elements, WebServiceUrl) {
    $.get(WebServiceUrl + "WebserviceVote.ashx", { ItemIds: Elements, Action: 'CountComments' }, function(data) {
        jQuery('body').append(data);
    });
}

function GetBattleResult(ThisBattleId, WebServiceUrl) {
    $.get(WebServiceUrl + "VoteForBattle.ashx", { BattleId: ThisBattleId, Action: 'GetVote' }, function(data) {
        var F1Vote = jQuery('F1Display', data).text();
        var F2Vote = jQuery('F2Display', data).text();
        DisplayBattleResults(ThisBattleId, F1Vote, F2Vote);
    });
};

function DisplayBattleResults(ThisBattleId, F1Vote, F2Vote) {
    $('#FilmVote_1').text(F1Vote);
    $('#FilmVote_2').text(F2Vote);
    $('#FilmVote_1').fadeIn("slow");
    $('#FilmVote_2').fadeIn("slow");
    $('#FilmHeader_1').fadeOut("slow");
    $('#FilmHeader_2').fadeOut("slow");

};

function PostPsyTestForm(FormId, GameId, WebServiceUrl) {
    
        var f = $('#f_jeu').serialize();
        $.post(WebServiceUrl + "SetPsyTestResults.ashx", f, function(data) {
            var ResponseId = jQuery('ResponseId', data).text();
            $('#f_jeu').fadeOut("slow");
            $('#Profile_' + ResponseId).fadeIn("slow");
            $('#PlayAgain').show();
        });
        return false; 
};

function PostGameForm(FormId, GameId, WebServiceUrl) {

    //alert(FormId + ", " + WebServiceUrl + ", " + GameId);

    var AsPlayed = $.cookie(GameId)
    if (AsPlayed == null || AsPlayed == 'null' || AsPlayed == "null") {
        var f = $('#f_jeu').serialize();
        $.post(WebServiceUrl + "SetGameResults.ashx", f, function(data) {
            var ResponsStatus = jQuery('Status', data).text();
            var ResponsMessage = jQuery('Message', data).text();
            $.cookie(GameId, '1', { expires: 365 });
            //alert(ResponsMessage);
            $('#f_jeu').fadeOut("fast");
            $('#ThankYou').fadeIn("fast");

        });
        return false;
    }

    else {
        $('#HasPlayed').fadeIn("fast");
    }

};

function RGBToHex(ColorString) {
    if (ColorString != null) {
        HexDigits = "0123456789abcdef";
        if (ColorString.charAt(0) == '#') {
            return ColorString;
        } else {
            ColorString = ColorString.substr(4);
            pos = ColorString.indexOf(',');
            Result = '#' + HexDigits.substr(Math.floor(ColorString.substr(0, pos) / 16), 1) + HexDigits.substr(ColorString.substr(0, pos) % 16, 1);
            ColorString = ColorString.substr(pos + 1);
            pos = ColorString.indexOf(',');
            Result = Result + HexDigits.substr(Math.floor(ColorString.substr(0, pos) / 16), 1) + HexDigits.substr(ColorString.substr(0, pos) % 16, 1);
            ColorString = ColorString.substr(pos + 1);
            pos = ColorString.indexOf(')');
            Result = Result + HexDigits.substr(Math.floor(ColorString.substr(0, pos) / 16), 1) + HexDigits.substr(ColorString.substr(0, pos) % 16, 1);
            return Result;
        }
    }
};

function doIframe() {
    o = document.getElementsByTagName('iframe');
    for (i = 0; i < o.length; i++) {
        if (/\bautoHeight\b/.test(o[i].className)) {
            setHeight(o[i]);
            addEvent(o[i], 'load', doIframe);
        }
    }
};

function setHeight(e) {
    if (e.contentDocument) {
        e.height = e.contentDocument.body.offsetHeight + 35;
    } else {
        e.height = e.contentWindow.document.body.scrollHeight;
    }
};

function addEvent(obj, evType, fn) {
    if (obj.addEventListener) {
        obj.addEventListener(evType, fn, false);
        return true;
    } else if (obj.attachEvent) {
        var r = obj.attachEvent("on" + evType, fn);
        return r;
    } else {
        return false;
    }
};

if (document.getElementById && document.createTextNode) {
    addEvent(window, 'load', doIframe);
};

