﻿// slider settings
var currentSlide = '';
var slideLoaded = false;
var currentSlideIndex = 0;
var newSlideIndex = 0;
var slideSpeed = 150;
var slideDelay = 25;
var slideDirection = 0;
var slideInProgress = false;
var slideNavigationElement = "#navigation";

$(function() {
    $(window).bind('hashchange', function() {
        hash = window.location.hash;
        if (hash.length > 0) {
            hash = "/" + hash;
            if (slideLoaded) {
                $(slideNavigationElement + ' li').each(function(i) {
                    if ($(this).find('a').first().attr('href') == hash) {
                        LoadSlide(this);
                        return true;
                    }
                });
            }
            else {
                currentSlide = hash;
                slideLoaded = true;
                LoadDefaultSlide();
            }
        }
        else {
            currentSlide = $(slideNavigationElement + ' a').first().attr('href');
            slideLoaded = true;
            LoadDefaultSlide();
        }
    });
    $(window).trigger('hashchange');
});

var cache = [];
function PreLoadImages() {
    var args_len = arguments.length;
    for (var i = args_len; i--; ) {
        var cacheImage = document.createElement('img');
        cacheImage.src = arguments[i];
        cache.push(cacheImage);
    }
}

$(document).ready(function() {
    PreLoadImages(
        "/images/homenav/home-on.png",
        "/images/homenav/products-on.png",
        "/images/homenav/buy-on.png",
        "/images/homenav/promotions-on.png",
        "/images/homenav/customersupport-on.png",
        "/images/homenav/magellan-on.png",
        "/images/homenav/home-off.png",
        "/images/homenav/products-off.png",
        "/images/homenav/buy-off.png",
        "/images/homenav/promotions-off.png",
        "/images/homenav/customersupport-off.png",
        "/images/homenav/magellan-off.png"
    );
    
    $(slideNavigationElement + ' li').each(function() {
        $(this).find('a').first().text('\xA0');
        $(this).click(function() {
            HandleNavigationClick(this);
        });
    });    
});

function HandleNavigationClick(slide) {
    if (!slideInProgress) {
        hash = $(slide).find('a').first().attr('href');
        $(slideNavigationElement + ' li').each(function(i) {
            if ($(this).find('a').first().attr('href') == hash) {
                LoadSlide(this);
                return true;
            }
        });
    }
    else {
        return false;
    }
}

function LoadDefaultSlide() {
    $(slideNavigationElement + ' a').each(function(index) {
        if ($(this).attr('href') == currentSlide) {
            currentSlideIndex = index;
            return true;
        };
    });

    SetActiveNavigation(currentSlideIndex);
    
    $('#slides .slide').each(function(index) {
        if (index == currentSlideIndex) {
            $(this).show();
        }
        else {
            $(this).hide();
        }
    });    
}

function LoadSlide(slide) {
    slide = $(slide).find('a').first();
    if ($(slide).attr('href') != currentSlide) {
        if (!slideInProgress) {
            slideInProgress = true;
            $(slideNavigationElement + ' a').each(function(index) {
                if ($(slide).attr('href') == $(slideNavigationElement + ' a').eq(index).attr('href')) {
                    newSlideIndex = index;
                    return true;
                }
            });
            currentSlide = $(slide).attr('href');
            slideDirection = newSlideIndex < currentSlideIndex ? -1 : 1;
            SetActiveNavigation(newSlideIndex);
            LoadNextSlide();
            return true;
        }
    }
    window.location.hash = currentSlide.substring(2, currentSlide.length);
    return false;
};

function LoadNextSlide() {
    if (currentSlideIndex != newSlideIndex) {
        $('#slides .slide').eq(currentSlideIndex).hide('slide', { direction: GetSlideDirection() }, slideSpeed);
        setTimeout('ShowNextSlide()', slideSpeed + slideDelay);
    }
    else {
        slideInProgress = false;
    }
}

function ShowNextSlide() {
    currentSlideIndex += slideDirection;
    $('#slides .slide').eq(currentSlideIndex).show('slide', { direction: GetSlideDirection() == 'left' ? 'right' : 'left' }, slideSpeed);
    setTimeout('LoadNextSlide()', slideSpeed);
}

function GetSlideDirection() {
    return slideDirection < 0 ? 'right' : 'left';
}

function SetActiveNavigation(slideIndex) {
    if (slideIndex == -1) {
        slideIndex = currentSlideIndex;
    }
    $(slideNavigationElement + ' li').each(function(index) {
        if (index == slideIndex) {
            replaceClass = '';
            $.each($(this).attr('class').split(' '), function(index, item) {
                if ((item != 'homenavactive') && (item.indexOf('nav') == 0)) {
                    replaceClass = item;
                    return true;
                }
            });
            $(this).removeClass('homenavactive homenav').addClass('homenavactive');
            if (replaceClass.indexOf('-active') < 0) {                
                $(this).removeClass(replaceClass).addClass(replaceClass + '-active');
            }
        }
        else {
            replaceClass = '';
            $.each($(this).attr('class').split(' '), function(index, item) {
                if ((item != 'homenavactive') && (item.indexOf('nav') == 0)) {
                    if (item.indexOf('-active') > 0) {
                        replaceClass = item;
                        return true;
                    }
                }
            });
            $(this).removeClass('homenavactive homenav').addClass('homenav');
            $(this).removeClass(replaceClass).addClass(replaceClass.slice(0, replaceClass.length - 7));
        }
    });
}




