﻿// JScript File
var tooltip = function() {
    var id = 'ToolTip';
    var top = 3;
    var left = 3;
    var maxw = 300;
    var speed = 10;
    var timer = 10;
    var endalpha = 85;
    var alpha = 0;
    var tt, t, c, b, h;
    var ie = document.all ? true : false;
    return {
        show: function(v, w) {
            if (tt == null) {
                tt = document.createElement('div');
                tt.setAttribute('id', id);
                t = document.createElement('div');
                t.setAttribute('id', id + 'Top');
                c = document.createElement('div');
                c.setAttribute('id', id + 'Cont');
                b = document.createElement('div');
                b.setAttribute('id', id + 'Bot');
                tt.appendChild(t);
                tt.appendChild(c);
                tt.appendChild(b);
                document.body.appendChild(tt);
                tt.style.opacity = 0;
                tt.style.filter = 'alpha(opacity=0)';
                document.onmousemove = this.pos;
            }
            tt.style.display = 'block';
            c.innerHTML = v;
            tt.style.width = w ? w + 'px' : 'auto';
            if (!w && ie) {
                t.style.display = 'none';
                b.style.display = 'none';
                tt.style.width = tt.offsetWidth;
                t.style.display = 'block';
                b.style.display = 'block';
            }
            if (tt.offsetWidth > maxw) { tt.style.width = maxw + 'px' }
            h = parseInt(tt.offsetHeight) + top;
            clearInterval(tt.timer);
            tt.timer = setInterval(function() { tooltip.fade(1) }, timer);
        },

        pos: function(e) {
            // For IE6 compatibity - nick 22/08/2008 11:32
            var u = 0, l = 0;
            if (typeof (window.pageYOffset) == 'number') {
                //Netscape compliant
                u = window.pageYOffset;
                l = window.pageXOffset;
            } else if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {
                //DOM compliant
                u = document.body.scrollTop;
                l = document.body.scrollLeft;
            } else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
                //IE6 standards compliant mode
                u = document.documentElement.scrollTop;
                l = document.documentElement.scrollLeft;
            }

            u = ie ? event.clientY + u : e.pageY;
            l = ie ? event.clientX + l : e.pageX;
            tt.style.top = (u - h) + 'px';
            tt.style.left = (l + left) + 'px';
            tt.style.position = 'absolute';
        },

        fade: function(d) {
            var a = alpha;
            if ((a != endalpha && d == 1) || (a != 0 && d == -1)) {
                var i = speed;
                if (endalpha - a < speed && d == 1) {
                    i = endalpha - a;
                } else if (alpha < speed && d == -1) {
                    i = a;
                }
                alpha = a + (i * d);
                tt.style.opacity = alpha * .01;
                tt.style.filter = 'alpha(opacity=' + alpha + ')';
            } else {
                clearInterval(tt.timer);
                if (d == -1) { tt.style.display = 'none' }
            }
        },
        hide: function() {
            clearInterval(tt.timer);
            tt.timer = setInterval(function() { tooltip.fade(-1) }, timer);
        }
    };
} ();


function openLW(LWHref, LWWidth, LWHeight, Title) {

    $('a#FancyBoxClick').remove();
    $('body').prepend("<a id='FancyBoxClick' class='iframe2' title='" + Title + "' href='" + LWHref + "'></a> ");
    //LWWidth, LWHeight can be used as passed if required
    LWWidth = parseInt(stripAlphaChars(LWWidth));
    LWHeight = parseInt(stripAlphaChars(LWHeight));
    $(".iframe2").fancybox({
        'hideOnContentClick': false,
        'overlayColor': '#000',
        'overlayOpacity': '.8',
        'frameHeight': LWHeight,
        'frameWidth': LWWidth
    });
    $('a#FancyBoxClick').click();
    $('#fancy_overlay').css('opacity', 0.5);
}

function stripAlphaChars(str) {
    str = str.replace(/\D/g, '');
    return str;
}

function stripHTML(str) {
    var re = /(<([^>]+)>)/gi;
    str = str.replace(re, '');
    return str;
}
   
function closeLW() {
   $.fn.fancybox.close()
}

function UpdateSideCarts() {
    if (window.updateMiniCart) { updateMiniCart(''); }
    if (window.updateCart) { updateCart(); }
    if (window.updateRollDownCart) { updateRollDownCart(); }

  
}

/*side cart code*/
var moveBy, mouseInHeader, mouseInSlider, itemsInCart;
//on dom ready
$(function() {
    sliderUp();
    if (!$("#SideCart").hasClass("stayDown")) {
        setPositionOfSlider();
        cartAnimation();
    } else {
        $("#SlideCart").removeClass('hidden');
    }
    itemsInCart = $('#SlideCart div.item').size();

});

//called when the ajax request is complete
function cartUpdatedCallback() {
    
    $('#ItemAdded').slideDown(function() {
        setTimeout(function() {
        $('#ItemAdded').slideUp(function() {
        sliderUp();
                setPositionOfSlider();
                cartAnimation();
            });

        }, 1000);
    });
    
    
   
}


//move the sidecart drop down up the value of it's new height
function setPositionOfSlider() {
    moveBy = $('#SlideCart').height();
    $('#SlideCartLiner').css('top', moveBy * -1).css('display', 'block');
}

//determines when to call slideup and slidedown  
function cartAnimation() {
    mouseInHeader = mouseInSlider = false;
    $('#SideCart').mouseenter(function() { mouseInHeader = true; sliderDown(); }).mouseleave(function() { mouseInHeader = false; sliderUp(); });
    $('#SlideCartLiner').mouseenter(function() { mouseInSlider = true; sliderDown(); }).mouseleave(function() { mouseInSlider = false; setTimeout(function() { if (mouseInHeader == false) { sliderUp(); } }, 1); });
}

//slides the cart up
function sliderUp() {
    setTimeout(function() {
        if (mouseInSlider == false) {
            $("#SlideCart").animate({ 'height': '0px' }, { duration: 1000, queue: false, complete: function() { } });
            $('#SlideCartLiner').animate({ 'top': moveBy * -1 }, { duration: 1000, queue: false, complete: function() { } });
        }
    }, 1);
}

//slides the cart down
function sliderDown() {
    if (!$("#SideCart > div").hasClass('emptyCart')) {
        $("#SlideCart").css('width', $('#SideCart').width()).animate({ 'height': moveBy + 'px' }, { duration: 1000, queue: false }); $('#SlideCartLiner').animate({ 'top': 0 }, { duration: 1000, queue: false, complete: function() { } });
    }
}
/* end side cart code*/

//will clear text of textbox on focus and reset value to original if left empty
function resetTextValue(selector) {
    var txtVal = $(selector).val();
    $(selector).focus(function() {
        if ($(this).val() == txtVal) { $(this).val(''); }
    }).blur(function() {
        if ($(this).val() == '') { $(this).val(txtVal); }
    });
}

//DOM ready
$(function() {
    resetTextValue('#SearchBox input[type=text]');
    //DD_roundies.addRule('.validationSummary, #AddToCart, *', '5px', true);

    $('body').removeClass($('body').attr('class'));

    //add title attribute to match text to drop lists
    $('select option').each(function() {
        $(this).attr('title', $(this).text());
    });

    //Change Breadcrumb html
    var breadCrumbText = $('#BreadCrumb a.breadcrumb:last').text();
    $('#BreadCrumb a.breadcrumb:last').replaceWith('<span class="breadcrumb last">' + breadCrumbText + '</span>');


    //make the enter key hit the submit button in the right place
    $('.form input').live('keypress', function(e) {
        var code = (e.keyCode ? e.keyCode : e.which);
        if (code == 13) { //Enter keycode
            $(this).closest('.form').find('input.submit').click();
            e.preventDefault();
        }
    });


    //hide existing validation errors on submit clicks
    $('a.btnAction').live('click', function() { $(this).closest('.form').find('.validationSummary > ul').hide(); });

    $('input[type=text]').addClass('text');
    $('input[type=password]').addClass('password text');
    //    $('tr').filter(':odd').addClass('odd');
    //    $('tr').filter(':even').addClass('even');
    $('.items .item').filter(':even').addClass('even');
    $('.items .item:first').addClass('first');
    helpFAQ();

    $('.skuSelection a.addToCart').click(function(e) {
        alert('');
        e.preventDefault();

    });

    //replace cms created form buttons with stylable trixy 
    //$('input.replace').each(function() {
    //    var temp = $(this);
    //    temp.hide();
    //    $(this).after("<a href='/' class='btnAction dynamic' title=" + temp.attr('title') + " ><span>" + temp.val() + "</span></a>");
    //    $(this).siblings().click(function(e) {
    //        e.preventDefault();
    //        temp.click();
    //    });
    //});



    $('#Navigation ul.rootGroup > li a').each(function() {
        if (document.location.href.indexOf($(this).attr('href')) != -1) {
            $(this).closest('li.item').addClass('selected')
            .parent().closest('li.item').addClass('selected');
        }
    });

    siteCustom();
    setUpTabs();
    validationHelpers();

});



function addRowClasses() {
    $('tr').filter(':odd').addClass('odd');
    $('tr').filter(':even').addClass('even');
}

//called by on product display page on update
function swatchHelper() {
    $('.swatchItem img').siblings().addClass('sibling');
    $('.swatch img').siblings('span').addClass('sibling');
}

/* faq type expanding list*/
function helpFAQ() {
    $('div.faq div.wrap').hide();
    $('div.faq a').click(function(e) {
        e.preventDefault();
        $(this).parents('div.faq').find('div.wrap').slideUp();
        if ($(this).siblings().hasClass('down')) {
            $(this).css('background-position', '-279px 3px').siblings().slideUp().removeClass('down');
        } else {
            $(this).parents('div.faq').find('div.wrap').removeClass('down').siblings().css('background-position', '-279px 3px');
            $(this).css('background-position', '0px 3px').siblings().slideDown().addClass('down');
        }
    });
}



function setUpTabs() {
    //Setup the client side tab menu control
    $("#TabSystem #Menu a").live('click',  function() { var index = $("#TabSystem #Menu a").removeClass("active").index($(this).addClass('active'));  $("#TabSystem .tab").hide().eq(index).show();
        if ($(this).attr('href') == "") { return false; }
    });    
     $("#TabSystem a:first").click();
     //$("#TabSystem a.active").click();
}

//search pages calls then when after ajax call from pager index change
function selectTab() {
    var location = window.location.toString();
    if (location.indexOf("#Info") > 0) {
        setTimeout(function() { $("#TabSystem a.information").click(); }, 1);//seems to need a timeout or it won't select the tab
    }
    else
    {
        setTimeout(function() { $("#TabSystem a.products").click(); }, 1);
    }
}

function validationHelpers() {
    //$('.ValidateError br,.ValidateError img').remove();

//    $('#Content .form table tr:not(#CheckoutAddressDetails tr)').each(function() {
//        if ($(this).find('td').size() < 3) {
//            $(this).append('<td class="validator"></td>');
//        }
//    });

    $('#Content .form .text').blur(function() {
        var theTR = $(this).closest('tr');
        theTR.find('.valid, .invalid').remove();
        theTR.find('span.error,span.ValidateError').each(function() {
            var id = document.getElementById($(this).attr('id'));
            ValidatorValidate(id);
            if (id.isvalid) {
                theTR.find('.valid').remove();
                theTR.find('td.validator').append('<span class="valid validator"></span>');
            } else {
                theTR.find('span.error,span.ValidateError').hide();
                theTR.find('td.validator').append('<span class="invalid validator" onclick="$(this).closest(\'tr\').find(\'.text\').focus()" onmouseover="tooltip.show(\'' + stripHTML(id.errormessage) + '\');" onmouseout="tooltip.hide();"></span>');

            }
        });

        if (theTR.find('span.validator').size() > 1) {
            theTR.find('span.valid').remove();
            if (theTR.find('span.validator').size() > 1) {
                theTR.find('span.validator').not('span.validator:first').remove();
            }
        }
    });

    $('#Content .form a.btnAction').click(function(e) {
        $(this).closest('.form').find('input.text').each(function() {
            $(this).blur();
        });
    });

}

function LoginError() {

    $('#Login .loggedOut').show();
    $('#LoginIncorrect').fadeIn();
    $('body, #LoginIncorrect .close').one('click',function(e) {
        e.preventDefault();
        $('#LoginIncorrect').fadeOut();
    });
    
    $('#LoginIncorrect').click(function(e) {
        e.stopPropagation();
    });
    
}


function positionFooter() {
    var docHeight = $('#Header').outerHeight() - $("#Spacer").height();
    var docHeight2 = $('#Content').outerHeight() - $("#Spacer2").height();
    //      $('#DocH').text('d h:' +docHeight);
    //      $('#WinH').text('w h:' + $(window).height());


    if ($(window).height() > docHeight) {
        var diff = (docHeight - $(window).height()) * -1;
        var diff2 = (docHeight2 - $(window).height()) * -1;
        $("#Spacer").height(diff);
        $("#Spacer2").height(diff2);
    }
}

//Custom Stuff
function siteCustom() {

    ProductListAnimation();

    $('ul.horizontal > li span').each(function(e) {
        if ($(this).siblings().size() < 1) {
            $(this).css('display','block');
        }
    });

    $('a.noClick').live('click', function(e) {
        e.preventDefault();
    });

    $('.productDisp .thumbs a').live('click', function(e) {
        e.preventDefault();
        if (!$(this).hasClass('selected')) {
            $('.productDisp .thumbs a').removeClass('selected');
            $(this).addClass('selected');
            var selector = ".m" + $(this).parent().attr('class').split(' ')[1].substr(1);
            $('.productDisp .main .item').removeClass('selected').animate({
                opacity: 0
            }, {
                queue: false
            });
            $(selector).addClass('selected').css({ 'visibility': 'visible', 'opacity': 0 }).animate({
                opacity: 1
            }, {
                queue: false
            })
        }
    });
}

function ProductListAnimation() {

} 


function ProductDisplayCallback() {
    UpdateSideCarts();
}

function startCycler() {

    //MagicMagnify_findMagnifiers();
    //CycleMe();
    $('.MagicMagnifier').attr('class', 'MagicMagnify');
    setTimeout(function() {
        MagicMagnify_findMagnifiers();
    }, 250);

    //$('div.item object').load(function() { alert('loaded'); });

    setTimeout(function() {
        $('#Cycler').cycle('stop');
        $('#Cycler').cycle({
            prev: '#Next',
            next: '#Prev',
            pause: true, //will pause on hover if set
            fx: 'scrollHorz',
            timeout: 0,
            speed: 500,
            easing: 'linear',
            before: function() { },
            after: function() { }
        });
        $('#Cycler .item').css('visibility', 'visible');
    }, 50);

}

function CycleMe() {
    window.count = $('#Cycler .item').size() - 1;
    $('#Cycler .firstItem').addClass('current');

    $('#Next').click(function() {
        showNext();
    });

    $('#Prev').click(function() {
        showPrev();
    });

}

function showNext() {

    if ($('.current').index() < window.count) {
        $('.current').removeClass('current').next().addClass('current');
    } else {
        $('.current').removeClass('current');
        $('#Cycler .item:eq(0)').addClass('current');
    }
}
function showPrev() {
    if ($('.current').index() > 0) {
        $('.current').removeClass('current').prev().addClass('current');
    } else {
        $('.current').removeClass('current');
        $('#Cycler .item:eq(' + window.count + ')').addClass('current');
    }
}


