﻿

$(document).ready(function () {
    
    var $container = $('#hero').cycle({

        fx: 'fade',
        pause: 1,
        timeout: 5000,
        cleartypeNoBg: true
    });

    $('#hero .right .buttons .link1').click(function () {
        $container.cycle(0);
        return false;
    });
    $('#hero .right .buttons .link2').click(function () {
        $container.cycle(1);
        return false;
    });
    $('#hero .right .buttons .link3').click(function () {
        $container.cycle(2);
        return false;
    });
    $('#hero .right .buttons .link4').click(function () {
        $container.cycle(3);
        return false;
    });

    $('.nav > li').hover(function () {
        $(this).children('#container').stop(true, true).show(); //.css({visibility:'hidden'});

        var obj = $(this).children('#container');
        var parent = obj.parent().parent();
        var tparent = obj.parent();

        var obj_left = obj.offset().left + obj.outerWidth();
        var parent_left = parent.offset().left + parent.outerWidth();

        var container_right = (((obj.offset().left + obj.outerWidth()) - parent_left) + 10) * -1;
        if (obj_left >= parent_left) {
            obj.css({ left: container_right });
        }
        $(this).children('#container').css({ visibility: 'visible' });
    }).mouseleave(function () {
        $(this).children('#container').stop(true, true).hide();
    });
    /*
    $('.nav > li').children('#container').each(function () {
    var obj = $(this);
    var parent = obj.parent().parent();
    var tparent = obj.parent();

    var obj_left = obj.offset().left + obj.outerWidth();
    var parent_left = parent.offset().left + parent.outerWidth();
        
    var container_right = (((obj.offset().left + obj.outerWidth()) - parent_left) + 10) * -1;
    if (obj_left >= parent_left) {
    obj.css({ left: container_right });
    }
    }).hide();
    */
    $('.box select').dropdown();
    $('.promo select').dropdown();
    $('.promo').find('.question_mark').simpletip({ content: '<p>Narrow your search criteria with each of the drop down menus. Can’t find what you’re looking for? <a href="/products">View all products</a>.</p>' });
    $('.box').find('.question_mark').simpletip({ content: '<span>Narrow your search criteria with each of the drop down menus. Can’t find what you’re looking for? <a href="/products">View all products</a>.</span>' });

    //    $('#searchwrapper').hover(function () {
    //        $('#searchwrapper').removeClass('normal').addClass('focus');
    //    }, function () {
    //        if ($('#searchwrapper .searchbox').val() == '') {
    //            $('#searchwrapper').removeClass('focus').addClass('normal');
    //        }
    //    });

    //    $('#navigation .imgNavRollover').each(function () {
    //        var t = $(this);
    //        var src1 = t.attr('src');
    //        var newSrc = src1.substring(src1.lastIndexOf('/'), src1.lastIndexOf('.'));
    //        newSrc = 'images/navigation' + newSrc;
    //        t.hover(function () {
    //            $(this).attr('src', newSrc + '-on.' + /[^.]+$/.exec(src1));
    //        }, function () {
    //            $(this).attr('src', newSrc + '.' + /[^.]+$/.exec(src1));
    //        });

    //    });

    //    $.fn.cycle.updateActivePagerLink = function (pager, currSlideIndex) {

    //        $(pager).find('li').removeClass('activeLI')
    //        .filter('li:eq(' + currSlideIndex + ')').addClass('activeLI');
    //    };


    $('.carousel').cycle({
        fx: 'scrollHorz',
        timeout: 0,
        prev: '#back',
        next: '#next',
        nowrap: 1,
        after: onAfter
    });

    $('.widget-carousel').cycle({
        fx: 'fade',
        timeout: 0,
        prev: '#back',
        next: '#next',
        nowrap: 1,
        after: onAfter2
    });
});

function onAfter(curr, next, opts) {
    var index = opts.currSlide;
    if (index == 0) {
        $('#back').removeClass().addClass('disabeled');
        //$('#back > img').attr('src', '/images/carousel-products-back.jpg');
        $('#back').removeAttr('href');
    }
    else {
        $('#back').removeClass().addClass('active');
        //$('#back > img').attr('src', '/images/carousel-products-back-on.jpg');
        $('#back').attr('href', '#');
    }
    if (index == opts.slideCount - 1) {
        $('#next').removeClass().addClass('disabeled');
        //$('#next > img').attr('src', '/images/carousel-products-next.jpg');
        $('#next').removeAttr('href');
    }
    else {
        $('#next').removeClass().addClass('active');
        //$('#next > img').attr('src', '/images/carousel-products-next-on.jpg');
        $('#next').attr('href', '#');
    }
    $('.carousel-pager img').eq(index).attr('src', '/images/active-page.jpg');
    $('.carousel-pager img').not(':eq(' + index + ')').attr('src', '/images/inactive-page.jpg');
}

function onAfter2(curr, next, opts) {
    var index = opts.currSlide;
    if (index == 0) {
        $('#back').removeAttr('href');
    }
    else {
        $('#back').attr('href', '#');
    }
    if (index == opts.slideCount - 1) {
        $('#next').removeAttr('href');
    }
    else {
        $('#next').attr('href', '#');
    }
    $('#mediawidget .pager img').eq(index).attr('src', '/images/active-page.jpg');
    $('#mediawidget .pager img').not(':eq(' + index + ')').attr('src', '/images/inactive-page.jpg');
}

