﻿// JScript File

function openLW(LWHref, LWWidth, LWHeight, Title) {

    LWWidth = parseInt(stripAlphaChars(LWWidth));
    LWHeight = parseInt(stripAlphaChars(LWHeight));

    $.fancybox({
        'padding': 0,
        'autoScale': false,
        'transitionIn': 'none',
        'transitionOut': 'none',
        'title': Title,
        'width': LWWidth,
        'height': LWHeight,
        'href': LWHref,
        'type': 'iframe'
    });
}


function stripAlphaChars(str) {
    str = str.toString().replace(/\D/g, '');
    return str;
}

function closeLW() {
    $.fancybox.close();
}

function UpdateSideCarts(){
        if(window.updateMiniCart){updateMiniCart('');}
        if(window.updateCart){updateCart();}
        //if(window.jAddSlideDown){jAddSlideDown();}
    }
    
function ToggleGiftWrapping(rbClickedButton, ContainingDiv){
        var myDiv = document.getElementById( ContainingDiv );
        var inputArr = myDiv.getElementsByTagName( "input" );
        for (var i = 0; i < inputArr.length; i++)
        {
            if(inputArr[i].value == 'rbGiftWrapping' && inputArr[i].id != rbClickedButton)
                document.getElementById(inputArr[i].id).checked = false;
        }
    }
    
function textboxMultilineMaxNumber(txt,maxLen){
        try{
            if(txt.value.length > (maxLen-1))return false;
           }catch(e){
        }
    }
    
function opacity(id, opacStart, opacEnd, millisec) { 
        var SideCartObject = document.getElementById(id);
        
        if(SideCartObject!=null)
        {
        
            var speed = Math.round(millisec / 100); 
            var timer = 0; 
            
            SideCartObject.style.display = "inline";

            if(opacStart > opacEnd) { 
                for(i = opacStart; i >= opacEnd; i--) { 
                    setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed)); 
                    timer++; 
                } 
            } else if(opacStart < opacEnd) { 
                for(i = opacStart; i <= opacEnd; i++) 
                    { 
                    setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed)); 
                    timer++; 
                } 
            } 
        }
    } 

function changeOpac(opacity, id) { 
        var object = document.getElementById(id).style; 
        object.opacity = (opacity / 100); 
        object.MozOpacity = (opacity / 100); 
        object.KhtmlOpacity = (opacity / 100); 
        object.filter = "alpha(opacity=" + opacity + ")"; 
    } 
    
 //Setup the client side tab menu control
if(window.jQuery)
{
    jQuery(function ($) {
        jQuery("#TabSystem #Menu a").click(function () { var index = jQuery("#TabSystem #Menu a").removeClass("active").index(jQuery(this).addClass('active')); jQuery("#TabSystem .tab").hide().eq(index).show(); return false; })
        //$('title').text(stripHTML($('title').text()));

        sidecart.init();
    });
}

function stripHTML(str) {
    var re = /(<([^>]+)>)/gi;
    str = str.replace(re, '');
    return str;
}

sidecart = {

    /**
    * @field
    * @description how far down the sidecart should slide
    */
    moveBy: '',
    /**
    * @field
    * @description whether the mouse is currently over the item summary of the sidecart
    */
    mouseInHeader: '',
    /**
    * @field
    * @description whether the mouse is currently over the sliding down sidecart
    */
    mouseInSlider: false,

    /**
    * @field
    * @description the current number of items in the users shopping cart
    */
    itemsInCart: 0,


    /**
    * @description causes the sidecart to be updated using radajax
    */
    cartUpdatedCallback: function () {
        updateCart();
    },

    /**
    * @description eventually called by the callback of the ajax request on updating the sidecart.
    * resets the position and displays an adding message if the item count has changed. 
    * there is no need to restart the animation of the cart as the events are already bound on page load 
    */
    sideCartCallBack: function () { // gets called by the ajax request on sidecart
        var self = this;

        $("#SlideCart").height(0);
        $('#SlideCartLiner').css('top', -1000);
        if (self.itemsInCart != $('#SlideCart div.item').size()) {
            self.setPositionOfSlider();
            jQuery("#SideCartItemAdded").fadeIn('slow',function(){ setTimeout(function(){ jQuery("#SideCartItemAdded").fadeOut('slow');}, 500)});
        }
    },

    /**
    * @private
    * @description sets the sidecart silder to be hidden off the page and sets the moveby field to the height of the slider
    */
    setPositionOfSlider: function () {
        var self = this;
        //move the sidecart drop down up the value of it's new height
        self.itemsInCart = $('#SlideCart div.item').size();
        
        
        $('#SlideCart').height(0).show();
        $('#SlideCartLiner').css('top', moveBy * -1).css('display', 'block');
        self.moveBy = $('#SlideCartLiner').height();
        
    },

    /**
    * @private
    * @description determines when to call sol.aws.cart.sidecart.slideup and sol.aws.cart.sidecart.slidedown functions 
    */
    cartAnimation: function () {
        var self = this;
        mouseInHeader = mouseInSlider = false;
        $('#SideCartHolder').live('mouseenter', function () { self.mouseInHeader = true; self.sliderDown(); }).live('mouseleave', function () { self.mouseInHeader = false; self.sliderUp(); });
        $('#SlideCartLiner').live('mouseenter', function () { self.mouseInSlider = true; self.sliderDown(); }).live('mouseleave', function () { self.mouseInSlider = false; setTimeout(function () { if (self.mouseInHeader == false) { self.sliderUp(); } }, 1); });
    },

    /**
    * @description slides the sidecart up
    */
    sliderUp: function () {
        var self = this;
        if (!self.mouseInSlider) {

            $("#SlideCart").animate({ 'height': '0px' }, { duration: 1000, queue: false, complete: function () { } });
            $('#SlideCartLiner').animate({ 'top': self.moveBy * -1 }, { duration: 1000, queue: false, complete: function () { } });
        }

    },

    /**
    * @description slides the sidecart down
    */
    sliderDown: function () {
        var self = this;
        if (!$("#SideCart > div").hasClass('emptyCart') && this.itemsInCart > 0) {
            $("#SlideCart").css('width', $('#SideCartHolder').width()).show().animate({ 'height': self.moveBy + 'px' }, { duration: 1000, queue: false }); $('#SlideCartLiner').animate({ 'top': 0 }, { duration: 1000, queue: false, complete: function () { } });
        }
    },


    /**
    * @description the starting point of the animations and functions on the sidecart
    */
    init: function () {
        //this.getSideCart();               
        if ($('#SideCart').hasClass('stayDown')) {
            $('#SideCart #SlideCartLiner').css('top', 0);
            $('#SlideCart').css('height', 'auto');
        } else {
            this.cartAnimation();
            this.setPositionOfSlider();
            this.sideCartCallBack();
        }

    }
}
