﻿window.onload = Initialise;
window.onunload = Cleanup;

var images = new Array();
images[0]="/Images/logo_w3c_xhtml.gif";
images[1]="/Images/logo_w3c_css.gif";
images[2]="/Images/logo_w3c_wcag1AA.gif";
images[3]="/Images/logo_paypal.gif";

var currentImage = 0;

var refreshRate = 2500.0;
var timeToFade = 1000.0;

var irTimer = null;

function Initialise()
{
    var imageRotatorDIV = document.getElementById("imageRotator");
    imageRotatorDIV.style.background = "white url(" + images[currentImage] + ")  no-repeat center";
    
    StartTimer();
}

function Cleanup()
{
    StopTimer();
}

function StartTimer()
{
    StopTimer();
    
    irTimer = setInterval("OnTick()", refreshRate);
}

function StopTimer()
{
    if (irTimer != null)
    {
        clearInterval(irTimer);
    }
}

function OnTick()
{
    var fadeImageRotatorDIV = document.getElementById("fadeImageRotator");
    var imageRotatorDIV = document.getElementById("imageRotator");

    var nextImage = currentImage + 1;    
    if (nextImage >= images.length)
    {
        nextImage = 0;
    }
    
    // Set the img src to the same as the current image which we want to fade out
    fadeImageRotatorDIV.style.background = "white url(" + images[currentImage] + ") no-repeat center";
    
    // Set the current image div to the next image which we want to fade to
    imageRotatorDIV.style.background = "white url(" + images[nextImage] + ") no-repeat center";
    
    // Next image
    currentImage = nextImage;
    
    // Fade out the old image   
//    var obj=new AjaxControlToolkit.Animation.FadeOutAnimation(fadeImageRotatorDIV, 1, 30, 0, 1, true);
//    obj.play();    
}

function CreateAttribute(element, attributeName, attributeValue)
{
    var attribute = document.createAttribute(attributeName);
    attribute.value = attributeValue;      
    element.attributes.setNamedItem(attribute);   
}

function showModalPopupViaClient(alt, src, ev)
{
    var fullImageIMG = document.getElementById("fullImage");
    CreateAttribute(fullImageIMG, "alt", alt);
    CreateAttribute(fullImageIMG, "src", src);

//    ev.preventDefault();
    var modalPopupBehavior = $find('programmaticModalPopupBehavior');
    modalPopupBehavior.show();
}

function hideModalPopupViaClient(ev)
{
    ev.preventDefault();        
    var modalPopupBehavior = $find('programmaticModalPopupBehavior');
    modalPopupBehavior.hide();
    
    var fullImageIMG = document.getElementById("fullImage");
    CreateAttribute(fullImageIMG, "alt", "");
    CreateAttribute(fullImageIMG, "src", "");
}

function initShadowBox()
{
    var options = 
        {
            overlayColor: '#4b4b4b',
            overlayOpacity: '0.9',
            onClose:shadowBoxClosed
        }; 
    Shadowbox.init(options); 
}

function showShadowBox(elementID, title)
{
    StopTimer();

    var element = document.getElementById(elementID);
    if (element != null)
    {
        Shadowbox.open
        (
            {
                player: 'html', 
                title:title, 
                content:element.innerHTML, 
                width:800, 
                height:120, 
                overlayOpacity:0, 
                resizeDuration:0.2,
                onClose:shadowBoxClosed
            }
        );
    }
}

function shadowBoxClosed(gallery)
{
    StartTimer();
}
function refreshProducts()
{
    $('#productsDiv').html('loading...');
 
    try
    {
        var locale = $('input[name=currencyGroup]:checked').val() 
               
        $.ajax(
            {
                type: "POST",  
                url: "ODSWebServices.asmx/GetProducts",  
                contentType: "application/json; charset=utf-8",  
                dataType: "json",  
                data: '{"locale":"' + locale + '"}',  
                success: function(msg)
                         {
                            $('#productsDiv').removeClass('loading');
                            $('#productsDiv').html(msg.d);
                         }
                         ,  
                error: function(xhr, msg)
                       {  
                            try
                            {
                                $('#productsDiv').html(xhr.responseText);
                            }
                            catch (exception)
                            {
                                $('#productsDiv').html(exception);
                            }    
                        }  
            });
    }
    catch (e)
    {
        $('#productsDiv').html(e);
    }
}