/**
 * @author joel
 */

var bottonRotatorPosition = 0;
$(function(){
    $('ul li:last').css('margin-right', '-1px');
    $('#text_rotatorContainer li a:first').removeClass('unselected').addClass('selected');
    $('.main_images:first').fadeIn(1000);

    $('#drop-counter').css({'margin' : '0 0 -13px 0'});
    
    var fourDivWidth = $('#fourDiv').width();
    var text_rotator_reelWidth = $('.text_rotator_reel').width();
    var text_rotator_reelSize = $('#fourDiv .text_rotator_reel').size();
    (!text_rotator_reelSize) ? $('.footer-arow-first').css({'margin' : '0 0 0 280px'})  : $('.footer-arow-first').css({'margin' : '0 0 40px 0'});

    $('#text_rotatorContainer').width(text_rotator_reelWidth * text_rotator_reelSize);
    
    $('#text_rotatorContainer li a').click(function(){
        $('#text_rotatorContainer li a').removeClass('selected').addClass('unselected');
        
  
        $(this).addClass('selected');
        
        $('.textRotator .main_images').css({
            'display': 'none'
        });
        $('.textRotator .main_images:hidden').eq($('.text_rotator_reel li a').index(this)).fadeIn(1000);
     

         setImgCaption($('img', '.textRotator .main_images:visible').attr('alt'));
         
        $('.detail-image-nav div').removeClass('active');
    	
        $('.detail-image-nav div').eq($('.text_rotator_reel li a').index(this) + 1).addClass('active');
        pagingDetails();
        disavilite();
    });
    

    
    $('#NextArticle, #PreviousArticle').click(function(){
    
        if ($(this).hasClass('disable')) 
            exit;
        
        var rotateArticle = $('#text_rotatorContainer li a.selected').index('#text_rotatorContainer li a');
        var articleSize = $('#text_rotatorContainer li a').size();
        
        rotateArticle = ($(this).attr('id') == 'NextArticle') ? rotateArticle + 1 : rotateArticle - 1;
        if (rotateArticle == articleSize || rotateArticle < 0) {
            rotateArticle = 0;
        }
        
        $('.textRotator .main_images').css({
            'display': 'none'
        });

        setImgCaption($('img', $('.textRotator .main_images:hidden').eq(rotateArticle)).attr('alt'));
        $('#text_rotatorContainer li a').removeClass('selected').addClass('unselected');
        $('#text_rotatorContainer li a').eq(rotateArticle).addClass('selected');

        $('.detail-image-nav div').removeClass('active');
        $('.detail-image-nav div').eq(rotateArticle + 1).addClass('active');

        $('.textRotator .main_images:hidden').eq(rotateArticle).fadeIn(1000);
       
        
        var f = $('#text_rotatorContainer li').eq(rotateArticle).parent('.text_rotator_reel').index('#text_rotatorContainer .text_rotator_reel');
        var g = $('#text_rotatorContainer li a.selected').index('#text_rotatorContainer li a');
        var p = $('#text_rotatorContainer li').eq($('#text_rotatorContainer li a.selected').index('#text_rotatorContainer li a')).parent('.text_rotator_reel').index('#text_rotatorContainer .text_rotator_reel');
        
        f++;
        
        if ($('#text_rotatorContainer li a').eq(g - 1).hasClass('last') && $(this).attr('id') == 'NextArticle') {
            if (bottonRotatorPosition < p || p == 0) {
                bottonRotatorPosition++;
                articleRotatar();
            }
        }
        if ($('#text_rotatorContainer li a').eq(g).hasClass('last') && $(this).attr('id') == 'PreviousArticle') {
        
            if (bottonRotatorPosition > p) {
                bottonRotatorPosition--;
                articleRotatar();
            }
        }
        
        pagingDetails();
        disavilite();
    });
    
    $('#bottomPrevious, #bottomNext').click(function(){
        if ($(this).hasClass('disable')) 
            exit;
        bottonRotatorPosition = ($(this).attr('id') == 'bottomNext') ? bottonRotatorPosition + 1 : bottonRotatorPosition - 1;
        articleRotatar();
       // pagingDetails();
    });
    
    $('#LatestArticles').click(function(){
    
        $('#text_rotatorContainer li a').removeClass('selected').addClass('unselected');
        $('ul.text_rotator_reel:last').find('a:first').addClass('selected');
        $('.main_images:visible').css('display', 'none');
        $('.textRotator .main_images:hidden').eq($('ul.text_rotator_reel:last').find('a:first').index('ul.text_rotator_reel li a')).fadeIn(1000);
        
        bottonRotatorPosition = $('.text_rotator_reel').size();
        bottonRotatorPosition--;
        articleRotatar();
        pagingDetails();
        disavilite();
    });
    
    function pagingDetails(){
  
        $(function(){
            var itemPosition = $('#text_rotatorContainer li a.selected').index('#text_rotatorContainer li a');
            var itemTotal = $('#text_rotatorContainer li a').size();
            var seek = (itemPosition + 1) > ((bottonRotatorPosition + 1) * 4) ? 3 * 4 : ((bottonRotatorPosition + 1) * 4);
            
            $('#counter').text('Displaying ' + (itemPosition + 1) + '-' + seek + ' of ' + itemTotal);
        });
    }
    
    function articleRotatar(){
        $(function(){
            
            $('#text_rotatorContainer').animate({
                'left': -bottonRotatorPosition * 366
            }, 500);
            

            disavilite();
            mainArrow();
        });
    }
    function disavilite(){
        (text_rotator_reelSize == (bottonRotatorPosition + 1)) ? $('#bottomNext').removeClass('able').addClass('disable') : $('#bottomNext').removeClass('disable').addClass('able');
        (!bottonRotatorPosition) ? $('#bottomPrevious').removeClass('able').addClass('disable') : $('#bottomPrevious').removeClass('disable').addClass('able');
        
        ($('#text_rotatorContainer li a.selected').index('#text_rotatorContainer li a') == 0) ? $('#PreviousArticle').removeClass('able').addClass('disable') : $('#PreviousArticle').removeClass('disable').addClass('able');
        (($('#text_rotatorContainer li a.selected').index('#text_rotatorContainer li a') + 1) == $('#text_rotatorContainer .text_rotator_reel li').size()) ? $('#NextArticle').removeClass('able').addClass('disable') : $('#NextArticle').removeClass('disable').addClass('able');
    }
    function mainArrow(){
       
        if (bottonRotatorPosition==0){
            $('#bottomPrevious').hide();
        }else{
             $('#bottomPrevious').show();
        }
        
        var last_item = bottonRotatorPosition;
        last_item++;
      
         if (last_item==text_rotator_reelSize || text_rotator_reelSize <= 1){
            $('#bottomNext').hide();
        }else{
             $('#bottomNext').show();
        }
    }
    function setImgCaption(caption){

        if (!caption){
            caption = "&nbsp;";
            
        }
         $('#caption1').html(caption);
    }
    pagingDetails();
    disavilite();
    setImgCaption(false);
    mainArrow();
});


$(function()
{ 

    var sliderPosition = 0;
    var imageWidth = $('.window').width();
    var imageSum = $('.image_reel div').size();
    var imageReelWidth = imageWidth * imageSum;

    $('.image_reel').css({
        'width' :  imageReelWidth
    });
arow();
    function footerRotator(){


        $(".image_reel").animate({
            left: -imageWidth * sliderPosition
        }, 500 );
    }

    $('#footer-next, #footer-prev').click(function(){

        var rotator = 0;
        rotator = sliderPosition;
        if($(this).attr('id') == 'footer-next'){
            rotator++;
            if (rotator < imageSum){
                sliderPosition++;
            }
        }else if (rotator != 0){
            sliderPosition--;
        }

        footerRotator();
        footerArowDesibility();
    });

    function footerArowDesibility(){
        if (sliderPosition == 0){
            $('#footer-prev').hide();
        }else{
            $('#footer-prev').show();
        }

        if ((sliderPosition + 1) == imageSum){
            $('#footer-next').hide();
        }else{
            $('#footer-next').show();
        }

    }
    function arow()

    {
       if (imageSum > 1 ){
           $('.footer-arow-first').html('<div id="footer-prev" class="prev-footer">&lt;</div>');
           $('.footer-arow-second').html('<div id="footer-next" class="next-footer">&gt;</div>');
       }else{
           $('.footer-arow-first').html('<div>&nbsp;</div>');
           $('.footer-arow-second').html('<div>&nbsp;</div>');
       }
    }
 
    // Begin Added 8/17
    var selectedIndex = $('#selectedIndex').val();
    sliderPosition = Math.floor(selectedIndex / 7);
    footerRotator();
    // End Added 8/17

    footerArowDesibility();

    if (imageSum == 1){
     var link_footer =  $('.footer-center a').size();
     var parent_link =  $('.image_reel').width();
     
     if (link_footer < 7){
        $('.image_reel').css('padding-left', (parent_link - (link_footer * 65)) / 2);
     }
    }
    
});

