/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


$(document).ready(function(){

    var fadeTime = 200;
    var maxWidth = 840;
    var lastClicked;
    initRangeArrows();

    function initRangeArrows(){

        $('.w_range').each(function(){
            //finds total width of items
            var width = 0;
            $(this).find('.item').each(function(){
                width += $(this).width();
            });
            if(width > maxWidth){
               $(this).children(".scr_arrow_right").css('display', 'block');
            }
        });
    }


    $(".item-range").children(".expander").bind('click',function(){

        //finds the containers
        var range = $(this).parent().parent().parent().parent().parent();
        var detail = range.prev();

        //shows the detail pane
        range.css('display', 'none');
        detail.css('display', 'block');

        var rangeClass = $(this).attr('class');
        var detailNumber = rangeClass.replace('expander ', '');

        lastClicked = detailNumber;

        //hide all list items
        detail.find("ul").children("li").each(function(){
            $(this).css('display', 'none');
        });

        var detailLi = detail.find("ul").children("."+detailNumber);
        detailLi.fadeIn(fadeTime);
        
        //handles the left arrow
        var leftArrow = detail.find('.scr_arrow_left');
        var firstLi = detail.find("ul").children('li').first().attr('class');
        var firstID = firstLi.replace('item ', '');
        if(parseInt(detailNumber) != parseInt(firstID)){
            leftArrow.css('display', 'block');
        }

        //handles the right arrow
        var rightArrow = detail.find('.scr_arrow_right');
        var lastLi = detail.find("ul").children('li').last().attr('class');
        var lastID = lastLi.replace('item ', '');
        if(parseInt(detailNumber) != parseInt(lastID)){
            rightArrow.css('display', 'block');
        }

    });     

     $(".item-detail").find(".close").bind('click', function(){
         
        //finds the containers
        var detail = $(this).parent().parent().parent().parent().parent().parent();
        var range = detail.next();

        //shows the range pane
        detail.fadeOut(fadeTime, function(){
            range.css('display', 'block');
        });

        //hides all the li's
         detail.find("ul").children("li").each(function(){
            $(this).css('display', 'none');
        });

        //hides the arrows
        var leftArrow = detail.find('.scr_arrow_left');
        var rightArrow = detail.find('.scr_arrow_right');
        leftArrow.fadeOut(fadeTime);
        rightArrow.fadeOut(fadeTime);

     });

     //arrows on the detail
     $(".w_detail").find(".scr_arrow_right").bind('click',function(){
        $(this).prev().find('.'+lastClicked).fadeOut(fadeTime, function(){
            lastClicked = parseInt(lastClicked) + 1;
            $(this).parent().find('.'+lastClicked).fadeIn(fadeTime);
        });

        initDetailArrows($(this).parent(), (parseInt(lastClicked)+1));

     });

     $(".w_detail").find(".scr_arrow_left").bind('click',function(){
        $(this).parent().next().find('.'+lastClicked).fadeOut(fadeTime, function(){
            lastClicked = parseInt(lastClicked) - 1;
            $(this).parent().find('.'+lastClicked).fadeIn(fadeTime);
        });

        initDetailArrows($(this).parent().parent(), (parseInt(lastClicked)-1));

     });

     function initDetailArrows(detail, itemToShow){

        //handles the left arrow
        var leftArrow = detail.find('.scr_arrow_left');
        var firstLi = detail.find("ul").children('li').first().attr('class');
        var firstID = firstLi.replace('item ', '');
        if(itemToShow == parseInt(firstID)){
            leftArrow.fadeOut(fadeTime);
        }else{
            leftArrow.fadeIn(fadeTime);
        }

        //handles the right arrow
        var rightArrow = detail.find('.scr_arrow_right');
        var lastLi = detail.find("ul").children('li').last().attr('class');
        var lastID = lastLi.replace('item ', '');
        if(itemToShow == parseInt(lastID)){
            rightArrow.fadeOut(fadeTime);
        }else{
             rightArrow.fadeIn(fadeTime);
        }
     }

     var lastMoveRight = 0;

     var animating = false;

     $(".w_range").find(".scr_arrow_right").bind('click',function(){
         if(!animating){
             animating = true;
            var ul = $(this).parent().children().children('ul');
            var currentLeft = parseInt(ul.css('marginLeft'));
            var numShowing = 0;
            var totalWidth = 0;
            var nextWidth = 0;
            var shownWidth = 0;
            var nextSet = false;
            ul.children().each(function(){
                 totalWidth += $(this).width();
                 if(parseInt(totalWidth) > 840-currentLeft && !nextSet){
                     nextWidth = parseInt(totalWidth);
                     nextSet = true;
                 }else if(!nextSet){
                     shownWidth = parseInt(totalWidth);
                 }
            });


            var newLeft = nextWidth-shownWidth;

            lastMoveRight = newLeft;

            if(totalWidth == (shownWidth+newLeft)){
                $(this).fadeOut(fadeTime);
            }
            $(this).parent().find(".scr_arrow_left").fadeIn(fadeTime);
            ul.animate({marginLeft: "-="+newLeft},1000, function(){ animating = false});
         }//if !animating
     });

     $(".w_range").find(".scr_arrow_left").bind('click',function(){
         if(!animating){
            var ul = $(this).parent().parent().children().children('ul');
            var currentLeft = parseInt(ul.css('marginLeft'));
            var numShowing = 0;
            var totalWidth = 0;
            var prevWidth = 0;
            var prevSet = false;
            var totalPrevWidth = 0;
            ul.children().each(function(){
                 totalWidth += $(this).width();
                 if((totalWidth >= (currentLeft*-1)) && !prevSet){
                     prevWidth = parseInt($(this).width());
                     totalPrevWidth = totalWidth;
                     prevSet = true;
                 }
            });

            var newLeft = totalPrevWidth-prevWidth;

            if(newLeft == 0){
                $(this).fadeOut(fadeTime);
            }
            $(this).parent().parent().find(".scr_arrow_right").fadeIn(fadeTime);
            ul.animate({marginLeft: -newLeft},1000, function(){ animating = false});
         }
     });

     //community page --------------------------------------------------------->

     //if slideshow exists
     if($("#slideshow").length != 0){


        var animating = false;
        var movesRight = 0;

         var numImages = $("#slideshow").find(".w_image").length;
         //if the images go off the page
         if(numImages > 5 ){
             $("#w_slideshow").children(".scr_arrow_right").css('display', 'block');
         }

         $("#w_slideshow").children(".scr_arrow_right").click(function(){
             if(!animating){
                 animating = true;
                 movesRight++;
                 if(movesRight+5 == numImages){
                     $("#w_slideshow").children(".scr_arrow_right").css('display', 'none');
                 }

                 var marginLeft = $("#slideshow").children("ul").css('marginLeft');

                 if(marginLeft == "0px"){
                     $("#slideshow").children("ul").animate({marginLeft: "-="+113}, function(){
                        animating = false;
                     });
                     $("#w_slideshow").find(".scr_arrow_left").css('display', 'block');
                 }else{
                     $("#slideshow").children("ul").animate({marginLeft: "-="+159}, function(){
                        animating = false;
                     });
                 }
             }//if !animating
         });


        $("#w_slideshow").find(".scr_arrow_left").click(function(){
            if(!animating){
                animating = true;
                movesRight--;

                if(movesRight == 0){
                    $("#w_slideshow").find(".scr_arrow_left").css('display', 'none');
                }

                if(movesRight+5 < numImages){
                    $("#w_slideshow").children(".scr_arrow_right").css('display', 'block');
                }

                var marginLeft = $("#slideshow").children("ul").css('marginLeft');

                var numMargin = parseInt(marginLeft.replace("px", ""));


                if((numMargin % 159) != 0){
                    $("#slideshow").children("ul").animate({marginLeft: "+="+113}, function(){
                        animating = false;
                     });
                }else{
                     $("#slideshow").children("ul").animate({marginLeft: "+="+159}, function(){
                        animating = false;
                     });
                }
            }//if !animating
        });

     }

     

});
