
currentSlide = 0;
slides = new Array();
timeoutInterval = 10000;
changeSpeed = 1000;
intervalID = -1;
done = 0;
 
$(function() {
       
    $.ajax({url: "slideshows/", success: function(response) {
        var i;
        var content = "";        
        var thumbs = "";   
        $("#slidesContainer").css("width", (response.length * 958 + 17) + "px");
        
        for(i = 0; i < response.length; i++)
        {
            if(i==0) { 
                addClass = "class='current second-child'"; 
            } else if(i==response.length-1) {
                addClass = 'class="last-child"';
            } else {
                addClass = '';
            }
            slides[i] = response[i].Slideshow;           
            content += "<div class=\"slide\"><img src=\""+grWP+"/uploads/crop_" + slides[i].filename + "\" alt=\"Grunnarbeid\" /></div>";
            thumbs += "<li "+addClass+"><a href='#'><img src='"+grWP+"/uploads/thumbnail_" + slides[i].filename + "' alt='" + slides[i].title + "' id='"+(i+1)+"' /></a></li>";
        }
        $('ul.small_thumbs').append(thumbs);            
        $(".mp_title h1").html("" + slides[0].title + "");
        $(".mp_desc h3").html("" + slides[0].description + "");
        Cufon.refresh();
        
        //get the width of the content and resize
        dynamicWidthTD();
        
        $("#slideInner").html(content);
        done = 1;
    }, async: false, dataType:'json' });
    
    if(done==1) {
        slideshowFunctionality(); 
    }                                                                            
                                                                                
});

function slideshowFunctionality() {

    if(done == 1) {
        $("ul.small_thumbs li a").click(function(ev) {
            
            ev.preventDefault(); 
            //alert($(ev.target).attr("id"));
            //alert($("ul > li").index($(".second-child")));
            
            if(intervalID != -1)
            {
                clearInterval(intervalID);
                intervalID = setInterval(slideImage, timeoutInterval);
            }
            
            currentSlide = $(ev.target).attr("id") - 1;
            
            $(".mp_title").animate({marginLeft: "-"+wott+"px"}, 500);
            $(".mp_desc").animate({marginLeft: "-"+wotd+"px"}, 500, function()    {
                
                $("#slideInner").animate({marginLeft: (-958 * currentSlide) + "px"}, 1000, function() { 
                    $(".mp_title h1").html("" + slides[currentSlide].title + "");
                    $(".mp_desc h3").html("" + slides[currentSlide].description + ""); 
                    Cufon.refresh();
                    //get the width of the content and resize
                    dynamicWidthTD();
                    
                    $(".mp_title").animate({marginLeft: "0"}, 500);
                    $(".mp_desc").animate({marginLeft: "0"}, 500);
                });
                
            });
            
            $("ul.small_thumbs li").removeClass("current");
            $("ul.small_thumbs li:nth-child(" + (currentSlide + 2) + ")").addClass("current");   

        });
                                                                                
        intervalID = setInterval(slideImage, timeoutInterval);
    }
}
                                
function slideImage()
{
    $("ul.small_thumbs li:nth-child(" + (currentSlide + 2) + ")").removeClass("current");
    
    if(currentSlide == (slides.length - 1))
    {
        currentSlide = 0;
    }
    else
    {
        currentSlide++;
    }

    
    $(".mp_title").animate({marginLeft: "-"+wott+"px"}, 1000);
    $(".mp_desc").animate({marginLeft: "-"+wotd+"px"}, 1000, function()    {
        $("#slideInner").animate({ "marginLeft": (-958 * currentSlide) }, changeSpeed, function()    {

            $(".mp_title h1").html("" + slides[currentSlide].title + "");
            $(".mp_desc h3").html("" + slides[currentSlide].description + "");
            Cufon.refresh(); 
              
            //get the width of the content and resize
            dynamicWidthTD();
        
            $(".mp_title").animate({marginLeft: "0"}, 1000);
            $(".mp_desc").animate({marginLeft: "0"}, 1000);
            
            $("ul.small_thumbs li:nth-child(" + (currentSlide + 2) + ")").addClass("current");
        });
    });
}

function dynamicWidthTD() {

   newArray = slides[currentSlide].options.split(', \"');

    wott = stripslashes(newArray[4]);
    //wotd = $('.mp_desc h3').width() + 150;
    wotd = stripslashes(newArray[5]);

    $('.mp_title').css({width:wott});
    $('.mp_desc').css({width:wotd});
}
function stripslashes(str) {
    str=ignoreSpaces(str);
    str=str.replace('"','');
    str=str.replace('}','');
    return str;
}
function ignoreSpaces(string) {
    var temp = "";
    string = '' + string;
    splitstring = string.split(" ");
    for(i = 0; i < splitstring.length; i++)
    temp += splitstring[i];
    return temp;
}

