﻿var currentPage = 0;
var currentMethod = 'GetAllTopFlop';
var currentVideo = '';
function Tracking(trace){var image=new Image();image.src=trace;}
function Redirect(url){window.open(url);}
function RedirectHome(){window.location.href = "Default.aspx";}
function HideVideos(){currentVideo = $get('playerVideo').innerHTML;$get('playerVideo').innerHTML='';}
function ShowVideos(){$get('playerVideo').innerHTML = currentVideo;}
window.onload = function()
{
    InitPage();    
};

InitPage = function()
{
    currentPage = 0;
    $get('aPrev').style.display = 'none';
};

AddTopFlop = function()
{
    var tbp = $get('tbPseudo').value;
    var tbm = $get('tbMessage').value;
    tbm = tbm.substring(0,68);
    if(tbm=="" || tbm=="Tape ton texte ici"){
        $get('status').innerHTML = '';
    } else {
        if(tbm.length < 10){
            $get('status').innerHTML = '';
        }else{
            tbp = tbp.replace(/(<)|(>)|(&)|(")|(\')|(\[)|(\])/gi, '');
            tbm = tbm.replace(/(<)|(>)|(&)|(")|(\')|(\[)|(\])/gi, '');
            if (tbp == "") { tbp = "Anonyme"; }
            if (tbp == "Ton Pseudo") { tbp = "Anonyme"; }
            InvokeAddService("InsertTopFlop", tbp, tbm);
            $get('status').innerHTML = '';
        }
    }
};

UpPage = function()
{
    currentPage++;
    RefreshAllFromService(currentMethod);
    if(currentPage > 0){ $get('aPrev').style.display = 'block'; }
};

DownPage = function()
{
    currentPage--;
    RefreshAllFromService(currentMethod);
    if(currentPage == 0){ $get('aPrev').style.display = 'none'; }
};

ServiceError = function(error, context)
{
    alert(error.get_message());
};

UpdateTopCount = function(id)
{
    Tracking('http://g.msn.fr/FR1003/823');
    $get('status').innerHTML = "Chargement...";
    Sys.Net.WebServiceProxy.invoke(
        "WebServices/TopFlopWebService.asmx", 
        "UpdateTopCount", 
        false, 
        { page: currentPage, method: currentMethod, id: id }, 
        ServiceComplete, 
        ServiceError, 
        ''
    );
    SetRateCookie(id);
}

UpdateFlopCount = function(id)
{
    Tracking('http://g.msn.fr/FR1003/824');
    $get('status').innerHTML = "Chargement...";
    Sys.Net.WebServiceProxy.invoke(
        "WebServices/TopFlopWebService.asmx", 
        "UpdateFlopCount", 
        false, 
        { page: currentPage, method: currentMethod, id: id }, 
        ServiceComplete, 
        ServiceError, 
        ''
    );
    SetRateCookie(id);
};

SetRateCookie = function(id)
{
    if($.cookie('DQJMSN_RATE') == null){
        $.cookie('DQJMSN_RATE', id);
    } else {
        $.cookie('DQJMSN_RATE', $.cookie('DQJMSN_RATE') + '-' + id);
    }
}

GetAllFromService = function(method)
{
    InitPage();
    currentMethod = method;
    $get('status').innerHTML = "Chargement...";
    InvokeService(method, currentPage);
};

RefreshAllFromService = function(method)
{
    currentMethod = method;
    InvokeService(method, currentPage);
};

InvokeAddService = function(method, pseudo, msg)
{
    Sys.Net.WebServiceProxy.invoke(
        "WebServices/TopFlopWebService.asmx", 
        method,
        false,
        { author: pseudo, message:msg }, 
        AddServiceComplete, 
        ServiceError,
        ''
    );
};

InvokeWinService = function(method)
{
    Sys.Net.WebServiceProxy.invoke(
        "WebServices/TopFlopWebService.asmx", 
        method,
        false,
        '',
        WinServiceComplete, 
        ServiceError,
        ''
    );
    
};

WinServiceComplete = function(result, context)
{
    if (result.length > 0)
    {
        window.location.href = result;
    }
}

AddServiceComplete = function(result, context)
{
    $get('divMsgEcho').innerHTML = ($get('tbMessage').value).substring(0,69);
    $get('divMsgEcho').style.display = 'block';
    setTimeout(function(){
        $get('divMsgEcho').innerHTML = '';
        $get('divMsgEcho').style.display = 'none';
    }, 5000);
    $get('tbPseudo').value = "";
    $get('tbMessage').value = "";
    InvokeWinService("IsTimeWinner");
};

InvokeService = function(method, page)
{
    $get('status').innerHTML = "Chargement...";
    Sys.Net.WebServiceProxy.invoke(
        "WebServices/TopFlopWebService.asmx", 
        method, 
        false, 
        { page: page }, 
        ServiceComplete, 
        ServiceError, 
        ''
    );
};

ServiceComplete = function(result, context)
{
    if (result.length > 0)
    {
        RenderFromServiceComplete(result);
    }
    $get('status').innerHTML = "";
};

RenderFromServiceComplete = function(result)
{
    $('.cmsg').empty();
    var i = 0, l = result.length, j = 0, k = 0;
    if($.cookie('DQJMSN_RATE')){
        k = $.cookie('DQJMSN_RATE').split('-');
    }
    for (i = 0; i < l; i++)
    {
        var mode = "enable";        
        for(j = 0; j < k.length; j++)
        {
            if(result[i].ID == k[j]) { mode = "disable"; }
        }
        $('.cmsg').append(RunTemplate(result[i].ID, result[i].Message, result[i].Author, mode));
        
    }
    if(result.length != 6) {
        $get('aNext').style.visibility = 'hidden';
    } else {
        $get('aNext').style.visibility = 'visible';
    }
};

var enableItemTemplate = '<div class="cmsgitem">';
enableItemTemplate += '    <input type="hidden" value="[ID]" />';
enableItemTemplate += '    <div class="cmsgc">';
enableItemTemplate += '        <span class="msgtxt">...[MSG]</span>';
enableItemTemplate += '        <span class="msgfrom">de [FROM]</span>';
enableItemTemplate += '    </div>';
enableItemTemplate += '    <div class="cmsgvote">';
enableItemTemplate += '        <img class="hand" src="App_Themes/City/Images/ib-flop.gif" alt="Flop !" style="border-width:0px;" onclick="UpdateFlopCount(\'[FLOP]\')" />';
enableItemTemplate += '        <img class="hand" src="App_Themes/City/Images/ib-top.gif" alt="Top !" style="border-width:0px;" onclick="UpdateTopCount(\'[TOP]\')" />';
enableItemTemplate += '    </div>';
enableItemTemplate += '</div>';

var disableItemTemplate = '<div class="cmsgitem">';
disableItemTemplate += '    <input type="hidden" value="[ID]" />';
disableItemTemplate += '    <div class="cmsgc">';
disableItemTemplate += '        <span class="msgtxt">...[MSG]</span>';
disableItemTemplate += '        <span class="msgfrom">de [FROM]</span>';
disableItemTemplate += '    </div>';
disableItemTemplate += '    <div class="cmsgvote">';
disableItemTemplate += '        <img src="App_Themes/City/Images/ib-flop-disabled.gif" alt="Flop !" style="border-width:0px;" />';
disableItemTemplate += '        <img src="App_Themes/City/Images/ib-top-disabled.gif" alt="Top !" style="border-width:0px;" />';
disableItemTemplate += '    </div>';
disableItemTemplate += '</div>';

RunTemplate = function(id, msg, from, mode)
{
    if(mode=="enable"){
        return enableItemTemplate.replace('[ID]', id).replace('[FLOP]', id).replace('[TOP]', id).replace('[MSG]', msg).replace('[FROM]', from);
    } else {
        return disableItemTemplate.replace('[ID]', id).replace('[MSG]', msg).replace('[FROM]', from);
    }
};

LightboxClick = function()
{
    window.open('LightBox.aspx', 'LightBox', config='height=490, width=510, toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, directories=no, status=no');
}

ToggleVideo = function(video)
{
    switch(video)
    {
        case '1':
            $get('playerVideo').innerHTML = '<object width="320" height="242"><param name="movie" value="http://www.dailymotion.com/swf/x7eswp&related=0"></param><param name="allowFullScreen" value="true"></param><param name="allowScriptAccess" value="always"></param><embed src="http://www.dailymotion.com/swf/x7eswp&related=0" type="application/x-shockwave-flash" width="320" height="242" allowfullscreen="true" allowscriptaccess="always"></embed></object>';
        break;
    case '2':
            $get('playerVideo').innerHTML = '<object width="320" height="242"><param name="movie" value="http://www.dailymotion.com/swf/x6o1ba&related=0"></param><param name="allowFullScreen" value="true"></param><param name="allowScriptAccess" value="always"></param><embed src="http://www.dailymotion.com/swf/x6o1ba&related=0" type="application/x-shockwave-flash" width="320" height="242" allowfullscreen="true" allowscriptaccess="always"></embed></object>';
        break;
    case '3':
            $get('playerVideo').innerHTML = '<object width="320" height="242"><param name="movie" value="http://www.youtube.com/v/XZgwiPZdy5Y&hl=en_US&fs=1&"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/XZgwiPZdy5Y&hl=en_US&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="320" height="242"></embed></object>';
        break;
    }
}