var slideshow = new Array();
slideshow[0]  = new Array("1_1.png", "1_2.png", "1_3.png", "1_4.png", "1_5.png", "1_6.png");
slideshow[1]  = new Array("2_1.png", "2_2.png", "2_3.png", "2_4.png", "2_5.png", "2_6.png");
slideshow[2]  = new Array("3_1.png", "3_2.png", "3_3.png", "3_4.png", "3_5.png", "3_6.png");
slideshow[3]  = new Array("4_1.png", "4_2.png", "4_3.png", "4_4.png", "4_5.png", "4_6.png");
slideshow[4]  = new Array("5_1.png", "5_2.png", "5_3.png", "5_4.png", "5_5.png", "5_6.png");

var PATH_IMAGE = "media/images/home/";
var display    = "";
var html       = "";
var z_index    = 10;

function initialize_slideshow(APP_ROOT, random_slideshow)
{
    html = $("div#wrap div#main_content div#slideshow").html();
    html += "<div class=\"slideshow current_slideshow\" style=\"z-index : " + z_index + "\">";
    for (var x = 0; x < slideshow[random_slideshow].length; x++)
    {
        var div_class = slideshow[random_slideshow][x].substr(0, slideshow[random_slideshow][x].indexOf(".", 0));

        //html += "<div class=\"image image_" + div_class + "\"><img src=\"" + APP_ROOT + PATH_IMAGE + slideshow[random_slideshow][x] + "\" border=\"0\" alt=\"\" /></div>";
        html += "<div class=\"image_" + div_class + "\" style=\"float : left; filter : alpha(opacity=0); opacity : 0;\"><img src=\"" + APP_ROOT + PATH_IMAGE + slideshow[random_slideshow][x] + "\" border=\"0\" alt=\"\" /></div>";
    }
    html += "</div>";

    $("div#wrap div#main_content div#slideshow").html(html);

    z_index++;

    start_slideshow(slideshow[random_slideshow], APP_ROOT, random_slideshow);
}

function start_slideshow(slideshow, APP_ROOT, random_slideshow)
{
    var div_class         = new Array()
    var slideshow_shuffle = slideshow.sort(shuffle);
    var start_index       = 0;

    for(var x = 0; x < slideshow_shuffle.length; x++)
    {
        div_class[x] = slideshow[x].substr(0, slideshow[x].indexOf(".", 0));
    }

    fade_in_image(div_class, start_index, APP_ROOT, random_slideshow);
}

function fade_in_image(div_class, index, APP_ROOT, random_slideshow)
{
    $("div.current_slideshow div.image_" + div_class[index]).animate({opacity : 1}, 750, function ()
    {
        index++;

        if (index < div_class.length)
        {
            fade_in_image(div_class, index, APP_ROOT, random_slideshow);
        }
        else
        {
            $(this).oneTime(15000, function()
            {       
                reset_slideshow(APP_ROOT, random_slideshow);
            });
        }
    });
}

function shuffle()
{ 
    return (Math.round(Math.random()) - 0.5);
}

function reset_slideshow(APP_ROOT, random_slideshow)
{
    slideshow.splice(random_slideshow, 1);

    var new_random_slideshow = Math.floor(Math.random() * (slideshow.length - 1));

    if (slideshow.length > 0)
    {
        initialize_slideshow(APP_ROOT, new_random_slideshow);
    }
    else
    {
        //alert(slideshow.length);
    }
}
