﻿// JScript File

// 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': '.1',
        'frameHeight': LWHeight,
        'frameWidth': LWWidth
    });
    $('a#FancyBoxClick').click();
    $('#fancy_overlay').css('opacity', .5);
}

function fancyClosed(){
    alert('im closed');
}

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.jAddSlideDown){jAddSlideDown();}
}

/*side cart code*/
var moveBy, mouseInHeader, mouseInSlider, itemsInCart;
//on dom ready
$(function() {
    if (!$("#SideCart").hasClass("stayDown")) {
        setPositionOfSlider();
        cartAnimation();
    } else {
        $("#SlideCart").removeClass('hidden');
    }
    itemsInCart = $('#SlideCart div.item').size();
   
});

//called when the ajax request is complete on voucher update in payment step
function updateSummary() {
    if (window.updateSummary) {updateSummary(); }
}

//called when the ajax request is complete
function cartUpdatedCallback() {
    setPositionOfSlider(); //reposition the cart slider
    if (itemsInCart != $('#SlideCart div.item').size()) {
        $("#SideCartItemAdded").fadeIn('slow', function() {
            setTimeout(function() { $("#SideCartItemAdded").fadeOut('slow') }, 1500);
            cartAnimation(); //restart the animation of the cart slider
        });
    } else {
        cartAnimation(); //restart the animation of the cart slider
    }
}


//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);
}

function RestartZoom() {
    //$('.MagicZoom img').load(function() { MagicZoom.start(document.getElementById('ProdDisp_MainImageA')); })
    MagicZoom_findZooms();
}

//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);

    //Change Breadcrumb html
    var breadCrumbText = $('#BreadCrumb a.breadcrumb:last').text();
    $('#BreadCrumb a.breadcrumb:last').replaceWith('<span class="breadcrumb last">' + breadCrumbText + '</span>');

    //remove inline crap from cms built forms
    $('span.ValidateError img').remove();

    //remove the text from the login area
    $('#Panel1 input.text').val("");

    //add title attribute to match text to drop lists
    $('select option').each(function() {
        $(this).attr('title', $(this).text());
    });

    //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) {
        e.preventDefault();

    });

    //swap submit buttons
    $('input.replaced').each(function() {
        newlink = document.createElement('a');
        newlink.innerHTML = "<span>" + $(this).val() + "</span>"
        newlink.setAttribute('onclick', '$(this).siblings().click()')
        $(this).after(newlink);
        $(newlink).click(function(e) {
            e.preventDefault();
            $(this).siblings().click();
        }).addClass('btnAction replacer');
    }).hide();

    $('#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');
        }
    });
    setUpTabs();
    validationHelpers();
    siteCustom();

    $('body').removeClass($('body').attr('class'));
});

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(); return false; });
    $("#TabSystem a:first").click();
}


function selectTab() {
    var location = window.location.toString();
    setTimeout(function() {
    }, 1);

    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 siteCustom() {
    //$('select.caseNum').change()
}











function validationHelpers() {
    $('.ValidateError br,.ValidateError img').remove();
    $('#Content .form tr').each(function() {
        if ($(this).find('td').size() < 3) {
            $(this).append('<td class="validator"></td>');
        }
    });

    $('#Content .form .text,#Content .form textarea').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'));
            id.focusOnError = 'f';
            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, textarea').each(function() {
            $(this).blur();
        });
    });

}

function textboxMultilineMaxNumber(element, maxLength) {
    //alert($(element).val().length);
}



