﻿// JScript File
var WidgetBackgroundColor = '_blue';

function GetWidgetScrollerValue(WidgetControlID, what)
{
    if (what.substr(0, 5) == 'Poll:')
    {
        var PollID = what.substr(5);
        var AnswerID = -1;
        var cnt = 1;
        while  (document.getElementById(WidgetControlID + 'chkPollAnswer' + cnt))
        {
            if (document.getElementById(WidgetControlID + 'chkPollAnswer' + cnt).checked)
            {
                AnswerID = document.getElementById(WidgetControlID + 'chkPollAnswer' + cnt).value;
                break;
            }
            cnt++;
        }
        sndReq('/XML/WidgetPollVote.aspx?WidgetControlID=' + WidgetControlID + '&PollID=' + PollID + '&AnswerID=' + AnswerID);
    }
    else
    {
        var PageNo = eval('WidgetPageNumber' + WidgetControlID);
        var WidgetID = eval('WidgetID' + WidgetControlID);
        sndReq('/XML/getWidgetContent.aspx?What=' + what + '&WidgetControlID=' + WidgetControlID + '&WidgetID=' + WidgetID + '&PageNo=' + PageNo, 'LoadWidget' + WidgetControlID);
    }
}

function LoadWidgetData(doc)
{
    var x = GetXMLObject(doc).childNodes[0];
    var WidgetID = x.getAttribute('WidgetID');
    var WidgetControlID = x.getAttribute('WidgetControlID');
    var PageNumber = x.getAttribute('PageNumber');
    var TotalPages = x.getAttribute('TotalPages');
    var What = x.getAttribute('What');
    var Content = getInnerText(x.childNodes[0].childNodes[0]);
    var Script = '';
    if ((x.childNodes[1]) && (x.childNodes[1].childNodes[0])) Script = getInnerText(x.childNodes[1].childNodes[0]);
    
    if (Content == 'EMPTY')
    {
        var ttl = document.getElementById('tblFullWidget' + WidgetControlID);
        if (ttl) 
            ttl.style.display = 'none';
        else
        {
            //try and get parent holder
            var sp = WidgetControlID.split('_');
            var parWCID = '';
            for (var i=0; i<sp.length-1; i++)
                parWCID += sp[i] + '_';
            parWCID = parWCID.substr(0, parWCID.length-1);
            ttl = document.getElementById('tblFullWidget' + parWCID);
            if (ttl)
                ttl.style.display = 'none';
        }
    }
    
    var clContent = document.getElementById('clWidgetContent' + WidgetControlID);
    var divContent = document.getElementById('divWidgetContent' + WidgetControlID);
    var divContentSlider = document.getElementById('divWidgetContentSlider' + WidgetControlID);
    var clPrevious = document.getElementById('clWidgetPrevious' + WidgetControlID);
    var clNext = document.getElementById('clWidgetNext' + WidgetControlID);
    var clText = document.getElementById('clWidgetText' + WidgetControlID);
    
    if (clContent)
        clContent.innerHTML = Content;
    else
    {
        divContent.style.height = '';
        divContent.innerHTML = Content;
    }
    
    if (clNext)
    {
        var prev = '<img src="' + jsImagePath + 'Widgets/leftArrow_dis.gif" />';
        var next = '<img src="' + jsImagePath + 'Widgets/rightArrow_dis.gif" />';
    
        if (PageNumber != 0)
            prev = '<a href="javascript:ChangeWidgetPaging(\'' + WidgetControlID + '\', ' + ((1*PageNumber) - 1) + ', \'' + What + '\')"><img border="0" src="' + jsImagePath + 'Widgets/leftArrow.gif" /></a>';
       
        if (PageNumber != TotalPages)
            next = '<a href="javascript:ChangeWidgetPaging(\'' + WidgetControlID + '\', ' + ((1*PageNumber) + 1) + ', \'' + What + '\')"><img border="0" src="' + jsImagePath + 'Widgets/rightArrow.gif" /></a>';
      
        clNext.innerHTML = next;
        clPrevious.innerHTML = prev;
        
        if (clText)
            clText.innerHTML = '<b>' + (1*PageNumber+1) + '</b> of ' + (1*TotalPages+1);
    }
    if (Script != '') eval(Script);
    StartWidgetSlide(WidgetControlID);
}

function StartWidgetSlide(WidgetControlID)
{
    var FirstLoad = eval('WidgetFirstLoad' + WidgetControlID)
    eval('FadingDirection' + WidgetControlID + ' = 1');
    FadeInOutWidget(WidgetControlID, 10, 100, '');
    var Target = document.getElementById('divWidgetContent' + WidgetControlID).offsetHeight;
    var slider = document.getElementById('divWidgetContentSlider' + WidgetControlID);
    var current = slider.style.height.substr(0, slider.style.height.length-2)*1;
    
    if (FirstLoad) 
    {
        slider.style.height = Target + 'px';
        eval('WidgetFirstLoad' + WidgetControlID + '=false;')
        return;
    }
    var direction = 0;
    if (current > Target) direction = -1;
    if (current < Target) direction = 1;
    var difference = (current - Target) * direction
    //Check we are starting from a position divisble by slider step
    var steps = (((difference/WidgetSliderStep)^2)^0.5) + '';
    var bits = steps.split('.');
    
    if (bits.length > 1)
    {
        var intbit = bits[0];
        if (direction == -1)
            slider.style.height = ((intbit*1) * WidgetSliderStep) + 'px';
        else
            slider.style.height = ((intbit+1) * WidgetSliderStep) + 'px';
    }
    
    setTimeout('SlideWidget("' + WidgetControlID + '", ' + Target + ', ' + direction + ');', WidgetSliderSpeed);
}

function SlideWidget(WidgetControlID, Target, direction)
{
    var doMore = true;
    var slider = document.getElementById('divWidgetContentSlider' + WidgetControlID);
    var current = slider.style.height.substr(0, slider.style.height.length-2);
    
    var SetTo = ((1*current)+(direction*WidgetSliderStep))
    if (((SetTo > Target) && (direction == 1)) || ((SetTo < Target) && (direction == -1)))
    {
        SetTo = Target;
        doMore = false
    }
    
    slider.style.height = SetTo + 'px';
    
    if (doMore)
        setTimeout('SlideWidget("' + WidgetControlID + '", ' + Target + ', ' + direction + ');', WidgetSliderSpeed);

}

function ChangeWidgetPaging(WidgetControlID, PageNumber, what)
{
    eval('WidgetPageNumber' + WidgetControlID + '=' + PageNumber);
    var wbg = WidgetBackgroundColor;
    if (what.toLowerCase()=='images') wbg = '_white';
    document.getElementById('divWidgetContentLoading' + WidgetControlID).style.backgroundImage = 'url(' + jsImagePath + 'loading_bg' + wbg + '.gif)';
    eval('FadingDirection' + WidgetControlID + ' = -1');
    FadeInOutWidget(WidgetControlID, 90, 0, what);
}

function GetRSSContent(FeedID, WidgetControlID, NoItems, ShowSummary, ShowDate, RemoveImages)
{
    sndReq('/XML/getRSS.aspx?FeedID=' + FeedID + '&WidgetControlID=' + WidgetControlID + '&NoItems=' + NoItems + '&ShowSummary=' + ShowSummary + '&ShowDate=' + ShowDate + '&RemoveImages=' + RemoveImages);
}

var FadeInOutTimeOut;

function FadeInOutWidget(WidgetControlID, val, target, what)
{
    var dir = eval('FadingDirection' + WidgetControlID);
    if (val< 0) val = 0;
    SetOpacity('divWidgetContent' + WidgetControlID, val)
    if ((val < target) && (dir == 1))
        FadeInOutTimeOut = setTimeout('FadeInOutWidget("' + WidgetControlID + '", ' + (1*(val + 20)) + ', ' + target + ', "' + what + '")', FadeSpeed);
    else if ((val > 0) && (dir == -1))
        FadeInOutTimeOut = setTimeout('FadeInOutWidget("' + WidgetControlID + '", ' + (1*(val - 20)) + ', ' + target + ', "' + what + '")', FadeSpeed);
    else if ((val >= target) && (dir == 1))
    {
        eval('FadingDirection' + WidgetControlID + ' = 0');
        document.getElementById('divWidgetContentLoading' + WidgetControlID).style.backgroundImage = '';
    }
    else if ((val == 0) && (dir == -1))
    {
        //Call next widget content
        eval('FadingDirection' + WidgetControlID + ' = 0');
        GetWidgetScrollerValue(WidgetControlID, what);
    }
}

function LoadPollWidgetContent(doc)
{
    var x = GetXMLObject(doc).childNodes[0];
    var WidgetControlID = x.getAttribute('WidgetControlID');
    var Content = getInnerText(x.childNodes[0]);
    var clContent = document.getElementById('divWidgetContent' + WidgetControlID);
    clContent.innerHTML = Content; 
    StartWidgetSlide(WidgetControlID);
}

function LoadRSSWidget(doc)
{
    var x = GetXMLObject(doc).childNodes[0];
    var WidgetControlID = x.getAttribute('WidgetControlID');
    var Content = getInnerText(x.childNodes[0]);
    var divContent = document.getElementById('divWidgetContent' + WidgetControlID);
    divContent.innerHTML = Content; 
    StartWidgetSlide(WidgetControlID);
}

function SubmitPollWidget(WidgetControlID, PollID)
{        
    var AnswerID = -1;
    var cnt = 1;
    while  (document.getElementById(WidgetControlID + 'chkPollAnswer' + cnt))
    {
        if (document.getElementById(WidgetControlID + 'chkPollAnswer' + cnt).checked)
        {
            AnswerID = document.getElementById(WidgetControlID + 'chkPollAnswer' + cnt).value;
            break;
        }
        cnt++;
    }
    if(AnswerID!=-1)
    {
        eval('FadingDirection' + WidgetControlID + ' = -1');
        document.getElementById('divWidgetContentLoading' + WidgetControlID).style.backgroundImage = 'url(' + jsImagePath + 'loading_bg' + WidgetBackgroundColor + '.gif)';
        FadeInOutWidget(WidgetControlID, 90, 0, 'Poll:' + PollID);
    }
}

function CountDownTo(WidgetControlID, container, datestring)
{
    var target = new Date(datestring);
    var now = new Date();
    var secs = (target-now)/1000;
    secs = secs + '';
    secs = secs.split('.')[0];
    var mins = secs/60; 
    mins = mins + '';
    mins = mins.split('.')[0];
    secs = secs - (mins*60)
    var hours = mins/60; 
    hours = hours + '';
    hours = hours.split('.')[0];
    mins = mins - (hours*60);
    var days = hours/24;
    days = days + '';
    days = days.split('.')[0];
    hours = hours - (days*24);
    var wd = ' seconds'
    if (secs == 1) wd = ' second'
    var h = secs + wd;
    if ((mins !=0) || (hours != 0) || (days != 0)) 
    {
        h = ' and ' + h
    }
    if (mins != 0)
    { 
        var com = ', ';
        if (h.substr(0, 5) == ' and ') com = ' ';
        wd = 'mins'
        if (mins == 1) wd = 'min'
        h = mins + ' ' + wd + com + h;
    }
    if (hours != 0)
    {
        var com = ', ';
        if (h.substr(0, 5) == ' and ') com = ' ';
        wd = 'hours'
        if (hours == 1) wd = 'hour'
        h = hours + ' ' + wd + com + h;
    }
    if (days != 0)
    {
        var com = ', ';
        if (h.substr(0, 5) == ' and ') com = ' ';
        wd = 'days'
        if (days == 1) wd = 'day'
        h = days + ' ' + wd + com + h;
    }
    document.getElementById(container).innerHTML =  h;
    
    if ((secs <= 0) && (mins <= 0) && (hours <= 0) && (days <=0))
        sndReq('/XML/Cricket/getTransferWindowWidget.aspx?WidgetControlID=' + WidgetControlID + '&WidgetID=' + eval('WidgetID' + WidgetControlID));
    else
        setTimeout('CountDownTo("' + WidgetControlID + '", "' + container + '", "' + datestring + '");', 100);
    
}