﻿$(document).ready(function ()
{
    LoadFirst();
    $('ul#controls a').click(function ()
    {
        if ($(this).attr('class') != 'current')
        {
            var itemID = $(this).attr('id');
            Load(itemID);
        }
    });
    $('ul#controls li a').click(function ()
    {
        $('ul#controls li a').removeClass('current');
        $(this).addClass('current');
    });
    setInterval(nextslide, 5000);
});

function nextslide()
{
    var $li = $('li a.current').parent('li');
    if ((typeof $li == 'undefined') || $li.length == 0)
    {
        LoadFirst();
        return;
    }
    var $nextli = $li.next('li');
    if ((typeof $nextli == 'undefined') || $nextli.length == 0)
    {
        LoadFirst();
        return;
    }
    var $item = $nextli.find('a');
    if ((typeof $item == 'undefined') || $item.length == 0)
    {
        LoadFirst();
        return;
    }
    var $itemID = $item.attr('id');
    if ((typeof $itemID == 'undefined') || $itemID.length == 0)
    {
        LoadFirst();
        return;
    }
    $('ul#controls li a').removeClass('current');
    $($nextli).children('a').addClass('current');
    Load($itemID);
}

function LoadFirst()
{
    Load($('ul#controls a').first().attr('id'));
    $('ul#controls li a').removeClass('current');
    $('ul#controls li a').first().addClass('current');
}

function Load(itemID)
{
    if (typeof $('.imagePlace').data(itemID) != 'undefined')
    {
        var item = $('.imagePlace').data(itemID);
        LoadContent(item);
        return;
    }
    $('.imagePlace').empty().html('<div id="loading"></div>');
    $.ajax({
        type: "POST",
        url: "Ajax/BetterSlider.aspx/GetSlideItem",
        data: "{'itemID' : '" + itemID + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        cache: true,
        success: function (msg)
        {
            if (msg.d != null)
            {
                $('.imagePlace').data(itemID, msg.d);
                LoadContent(msg.d);
            }
            else
            {
                $('.imagePlace').empty().html('<div id="error"></div>');
            }
        },
        error: function ()
        {
            $('.imagePlace').empty().html('<div id="error"></div>');
        }
    });
}

function LoadContent(content)
{
    if (content.Type == 'Image')
    {
        var img = new Image();
        $(img).load(function ()
        {
            $(this).hide();
            $('.imagePlace').empty().append(this);
            $(this).fadeIn();
        }).error(function ()
        {
            $('.imagePlace').empty().html('<div id="error"></div>');
        }).attr('src', content.Src);
        $('.imagePlace img').css('display', 'null');
    }
    else if (content.Type == 'Flash')
    {
        $('.imagePlace').empty().html('<span class="flash-object"><embed src="' + content.Src + '" width="' + content.Width + '" height="' + content.Height + '">');
    }
}
