var lastImage;
var timeout;
var selector = '#image_menu';
var nextSelector = '#image_menu2';
var imageRotator = '#image_container';

$(document).ready(function() 
{
    timeout = 5000;
    lastImage = 0;
    setTimeout("UpdateImage()", timeout);

    // setup mouse-over for image links
    $("#image_links div").hover(function() {
        $(".left_side", this).attr('style', 'background-image: url(/images/button/buttonleft.png)');
        $(".middle", this).attr('style', 'background-image: url(/images/button/buttonmiddle.gif)');
        $(".right_side", this).attr('style', 'background-image: url(/images/button/buttonright.png)');
        $(".middle a", this).attr('style', 'color: #FFFFFF');
    }, function() {
        $(".left_side", this).attr('style', "background-image: url('')");
        $(".middle", this).attr('style', "background-image: url('')");
        $(".right_side", this).attr('style', "background-image: url('')");
        $(".middle a", this).attr('style', 'color: #422121');
    });
    
    // show more company description text
    $('#more_comp_desc').livequery('click',function(event){
        $(".more_static .more_compdesc").css('display', 'inline');
        $("#less_comp_desc").css('display', 'inline');
        $(this).css('display', 'none');
        event.preventDefault();
    });
    
    // show less company description text
    $('#less_comp_desc').livequery('click',function(event){
        $(".more_static .more_compdesc").css('display', 'none');
        $("#more_comp_desc").css('display', 'inline');
        $(this).css('display', 'none');
        event.preventDefault();
    });
    
    /** Handling for the image slider on the home page **/
    //Starts the image rotation on home page load
    $('#image_container').livequery(function(){
        timedImageRotation();
    });
    
    //Handle the nav slider pause click
    $('#slider_nav img[pause]').click(function(){
       var active = $(this);
       var inActive = $('#slider_nav img.nav_hidden[pause]')
       
       if($(inActive).attr('pause') == '1'){
           $(imageRotator).stopTime();
       }else{
           timedImageRotation();
       }
       
       $(active).toggleClass('nav_hidden');
       $(inActive).toggleClass('nav_hidden');
    });
    
    //Handle the slider left click
    $('#image_container #slider_left').click(function(){
        if($('#slider_nav img[pause="0"][class!="nav_hidden"]').length > 0){
            $('#slider_nav img[pause][class!="nav_hidden"]').click();
        }
        imageRotation('left', 0);
    });
    
    //Handle the slider right click
    $('#image_container #slider_right').click(function(){
        if($('#slider_nav img[pause="0"][class!="nav_hidden"]').length > 0){
            $('#slider_nav img[pause][class!="nav_hidden"]').click();
        }
        imageRotation('right', 0);
    });
    
    //Handle the slider nav round button click
    $('img[slidenum]').click(function(){
        if($('#slider_nav img[pause="0"][class!="nav_hidden"]').length > 0){
            $('#slider_nav img[pause][class!="nav_hidden"]').click();
        }
        
        imageRotation('', $(this).attr('slidenum'));
    });
    
    //Handle the magazine feature popup
    $('a[href="wheretofindthemagazine"]').click(function(e){
        e.preventDefault(); 
        if($('#slider_nav img[pause="0"][class!="nav_hidden"]').length > 0){
            $('#slider_nav img[pause][class!="nav_hidden"]').click();
        }
        $(this).parent().children('.feature_magazine_popup').toggle();
    });
});

/**
 * Set the time for the slider rotation
 */
function timedImageRotation()
{
    $(imageRotator).everyTime(5000, function(){
        imageRotation('right', 0);
    });
}

/**
 * Rotate the image in the slider
 * 
 * @param direction The direction the slider should rotate
 * @param manual The manually select image number (currently done by pressing the nav circle buttons)
 */
function imageRotation(direction, manual)
{
    var nextImage;
    var imageCount = $(imageRotator).attr('imagecount');
    //var activeImage = $(imageRotator).children('[imagestate="active"]').attr('imgnum');
    
    if(manual != 0){
        if($('#image_container').attr('activeimage') != manual){
            nextImage = manual;
        }
    }else if(direction == 'right'){
        if($('#image_container').attr('activeimage') != imageCount){
            nextImage = Number($('#image_container').attr('activeimage')) + 1;
        }else{
            nextImage = 1;
        }
    }else if(direction == 'left'){
        if($('#image_container').attr('activeimage') != 1){
            nextImage = Number($('#image_container').attr('activeimage')) - 1;
        }else{
            nextImage = imageCount;
        }
    }
    if(nextImage != undefined){
        updateImages($('#image_container').attr('activeimage'), nextImage, direction);
        $('#image_container').attr('activeimage', nextImage);
    }
}

/**
 * Executes the animation for the image slider
 * 
 * @param activeImage The current image that is being displayed
 * @param nextImage The next image to be displayed
 * @param direction The direction of animation (default is to the right)
 */
function updateImages(activeImage, nextImage, direction)
{
    $('[imgnum="' + activeImage + '"]').attr('imagestate', 'inactive');
    $('[imgnum="' + nextImage + '"]').attr('imagestate', 'active');
    
    //Stop the queue
    $('[imgnum]').stop(false,true);
    
    if(direction == 'left'){
        $('[imgnum="' + activeImage + '"]').hide('slide', {direction: 'right', easing: "easeOutCubic"}, 1000, function(){
           $(this).attr('class', 'img_hidden');
        });
        $('[imgnum="' + nextImage + '"]').show('slide', {direction: 'left', easing: "easeOutCubic"}, 1000).attr('class', 'img_normal');
    }else{
        $('[imgnum="' + activeImage + '"]').hide('slide', {direction: 'left', easing: "easeOutCubic"}, 1000, function(){
           $(this).attr('class', 'img_hidden');
        });
        $('[imgnum="' + nextImage + '"]').show('slide', {direction: 'right', easing: "easeOutCubic"}, 1000).attr('class', 'img_normal');
    }

    //Change the nav
    $('[slidenum="' + activeImage + '"][slideactive]').addClass('nav_hidden');
    $('[slidenum="' + activeImage + '"][slideinactive]').removeClass('nav_hidden');

    $('[slidenum="' + nextImage + '"][slideactive]').removeClass('nav_hidden');
    $('[slidenum="' + nextImage + '"][slideinactive]').addClass('nav_hidden');
}

/** Legacy funtionality for the home page image updates **/
function UpdateImage()
{
    var images = new Array(
        'birdcage_veil.jpg', 
        'eco_tote.jpg',
        'flower_girl_baskets.jpg',
        'flutes.jpg',
        'heart_sand_ceremony.jpg',
        'jewelry_barrette.jpg',
        'love_bird_pillow.jpg',
        'love_letter_ceremony.jpg',
        'romantic_cake_topper.jpg',
        'sweet_silohuettes.jpg',
        'tablesetting_beach.jpg'
    );

    var file = images[lastImage];
    $(selector).fadeOut('slow');
    $(nextSelector).attr("style", "background-image: url('/images/features/" + file + "');").fadeIn('slow');

    var temp = nextSelector;
    nextSelector = selector;
    selector = temp;
    lastImage++;

    if (lastImage >= images.length) {
        lastImage = 0;
    }

    setTimeout("UpdateImage()", timeout);
}

