/* 
 * Functionality for Widget - PhotoStoryContent
 * 
 */

function initializePhotostorySlider(){
    /*Assign global variables needed for slide function*/
    itemWidth = $('#photoStorySlider ul li:last').outerWidth();             //alert(itemWidth);
    noOfItems = $('#photoStorySlider ul > *').size();                       //alert(noOfItems);
    noOfItemsPerScreen = $('#photoStorySlider').outerWidth() / itemWidth;   //alert(noOfItemsPerScreen);
    
    ul = $('#photoStorySlider ul');
    
    if (noOfItems > noOfItemsPerScreen) { /*Enough items to slide*/

        var selectedItem = $("#selectedPhoto").text(); /*Selected Main Photo must be displayed first in slider*/
        var noOfPreviousItems = selectedItem-1 ;
        var goLeft = noOfPreviousItems * itemWidth;

        //alert(selectedPhoto);
        ul.css({'left' : '-'+goLeft+'px'});

        if( selectedItem > 1 ){
            enableButton("prev");
        }
        if( (noOfItems - selectedItem) >= noOfItemsPerScreen){
            enableButton("next");
        }
    }/*else{ /*Condition where total items are less than the minimum amount to slide*/
       /* disableButton("next");
        disableButton("prev");
    }*/

}

window.onload = initializePhotostorySlider;

$(document).ready(function() {
    //enableButton("next");
    //enableButton("prev");
});

function slide(where){
    
    var firstItemInList = returnFirstItemInLIst();
   // alert(firstItemInList);
    switch (where)
        {
        case 'prev':
            var noOfPreviousItems = firstItemInList-1 ;
            var goLeft = 0;

            if(noOfPreviousItems >= noOfItemsPerScreen){
                goLeft = noOfItemsPerScreen * itemWidth; 
            }
            else{
                goLeft = noOfPreviousItems * itemWidth;                
            }
           // alert(noOfPreviousItems);
            //alert(ul.position().left);
            //alert(goLeft);
            goLeft = (ul.position().left) + goLeft ;
            break;
        case 'next':
            var noOfNextItems = noOfItems - firstItemInList ;
            var goRight = 0;

            if(noOfNextItems >= noOfItemsPerScreen){
                goRight = noOfItemsPerScreen * itemWidth; 
            }
            else{
                goRight = noOfNextItems * itemWidth;                
            }
            goRight = (ul.position().left) - goRight ;
            goLeft = goRight;
            break;
    }
   // alert(goLeft);

    if(goLeft == 0){
        disableButton("prev");
    }else{
        enableButton("prev")
    }

    var afterSlideFirstItemPosition = returnItemPositionInList(goLeft);
    if ( (noOfItems - afterSlideFirstItemPosition) >=  noOfItemsPerScreen ){
        enableButton("next");
        //alert('enable next');
    }else{
        disableButton("next");
        //alert('disable next');
    }

    //make the sliding effect using jQuery's animate function... '
    $('#photoStorySlider ul:not(:animated)').animate({'left' : goLeft},800,function(){
        ul.css({'left' : goLeft+'px'});
    });
}

function enableButton(which){   /*should keep prev next names as a standar naming*/
    $('div.'+which+' a').unbind('click');
    $('div.'+which+' a').click(function() {slide(''+which+'');});
    if($('div.'+which+' a img').attr("src").indexOf(""+which+"_btn_photostories_image_selectionoff") !=-1 ){
        $('div.'+which+' a img').attr("src",$('div.'+which+' a img').attr("src").replace(""+which+"_btn_photostories_image_selectionoff",""+which+"_btn_photostories_image_selection"));
    }
}

function disableButton(which){
    $('div.'+which+' a').unbind('click');
    $('div.'+which+' a img').attr("src",$('div.'+which+' a img').attr("src").replace(""+which+"_btn_photostories_image_selection",""+which+"_btn_photostories_image_selectionoff"));
}

function returnFirstItemInLIst(){
    return (Math.abs(ul.position().left)+itemWidth) / itemWidth;
}

function returnItemPositionInList(pos){
    return (Math.abs(pos)+itemWidth) / itemWidth;
}

