jQuery(function($) {
    var browser = jQuery.browser;
    var version = jQuery.browser.version;

    if (browser = 'msie' && version == 6.0) {
        // Removes jQuery generated 'Rounded Corners' on IE6 due to issues
        $('#topnav li ul').each(function() {
            $(this).find('ul.roundedBtm').removeClass('roundedBtm');
            $('div#registerNow').removeClass('roundedBtm');
        });

        $('#dropTabs').each(function() {
            $('span.roundedBtm').removeClass('roundedBtm');
        });
    }

});




//SET FONT SIZE
function setFontSize(size)
{
    $('body').css('font-size', size);
    $.cookie('font-size', size, { expires: 365, path: '/'});
}

// Text Resize
jQuery(function($) {
    var originalFontSize = $('body').css('font-size');

    // Increase Font Size
    $(".increaseFont").click(function() {
        var currentFontSize = $('body').css('font-size');
        var currentFontSizeNum = parseFloat(currentFontSize, 10);
        var largestFontSize = 16;

        var newFontSize = currentFontSizeNum;
        if (currentFontSizeNum < largestFontSize) {
            newFontSize = Math.floor(currentFontSizeNum + 2);
        }
        setFontSize(newFontSize);

/*
        if (currentFontSizeNum < largestFontSize) {
            var newFontSize = Math.floor(currentFontSizeNum + 2);
            $('body').css('font-size', newFontSize);
            $.cookie('font-size', newFontSize, { expires: 365});
        }

        else {
            $('body').css('font-size', largestFontSize);
            $.cookie('font-size', largestFontSize, { expires: 365});
        }
*/
        return false;
    });
    // Decrease Font Size
    $(".decreaseFont").click(function() {
        var currentFontSize = $('body').css('font-size');
        var currentFontSizeNum = parseFloat(currentFontSize, 10);
        var smallFontSize = 12;

        var newFontSize = currentFontSizeNum;
        if (currentFontSizeNum > smallFontSize) {
            newFontSize = Math.floor(currentFontSizeNum - 2);
        }
        setFontSize(newFontSize);
/*
        if (currentFontSizeNum > smallFontSize) {
            var newFontSize = Math.floor(currentFontSizeNum - 2);
            $('body').css('font-size', newFontSize);
            $.cookie('font-size', newFontSize, { expires: 365});
        }

        else {
            $('body').css('font-size', smallFontSize);
            $.cookie('font-size', smallFontSize, { expires: 365});
        }
*/        
        return false;
    });
});


// Main Navigation
jQuery(function($) {
    //$('#topnav li ul').hide();

    $('#topnav li').hover(function() {
        $('ul', this).fadeIn('fast');
        $(this).addClass('navHover');
    }, function() {
        $('ul', this).fadeOut('fast');
        $(this).removeClass('navHover');
    });
});

// Alternate Site Dropdowns
jQuery(function($) {
    $('#drop1, #drop2').hide();

    $('#dropTabs p').toggle(function() {
        var content = $(this).find('a').attr('href');
        $('#dropTabs p').find('a').addClass('selected');
        $(this).siblings().css({'visibility':'hidden'});
        $(this).parent('#dropTabs').addClass('dropOpen');
        $(content).slideDown();
    }, function() {
        var content = $(this).find('a').attr('href');

        $('#dropTabs p').find('a').removeClass('selected');
        $(content).slideUp('fast');
        $('#dropTabs p').css({'visibility':'visible'});
        $(this).parent('#dropTabs').removeClass('dropOpen');
    });
    return false;
});

// Image Gallery by Galleria
var overlayImg = new Array(4);
jQuery(function($) {

    $('.gallery_nostyle').addClass('imageGallery'); // adds new class name to maintain degradability

    $('ul.imageGallery').galleria({
        history   : false, // activates the history object for bookmarking, back-button etc.
        clickNext : false, // helper for making the image clickable
        insert    : '#largeImage', // the containing selector for our main image
        onImage   : function(image, caption) { // let's add some image effects for demonstration purposes

            // fade in the image & caption
            if (! ($.browser.mozilla && navigator.appVersion.indexOf("Win") != -1)) { // FF/Win fades large images terribly slow
                image.css('display', 'none').fadeIn(1000);
            }
            caption.css('display', 'none');

            // adding Lightbox 'view larger' link to image container
            var imgSrc = $('li.active').children('img').attr('id');
            var imgLength = imgSrc.length;
            var imgID = imgSrc.substring(imgLength - 1);
            var imageTitle = $('li.active').children('img').attr('title');
            var viewLargerLabel = document.getElementById('localizedViewLargerLabel').innerHTML;

            $('.galleria_wrapper').append('<a href="' + overlayImg[imgID - 1] + '" title="' + imageTitle + '">' + viewLargerLabel + '</a>');

            // Lightbox Activation for Gallery
            $('.galleria_wrapper a').lightBox();
        }

    });
});

// clear form
function clearPopupForm(formContainer) {
    $(formContainer + ' :input').each(function(idx, inputEl) {
        if ((inputEl.type != 'hidden') && (inputEl.type != 'submit')) {
            inputEl.value = '';
        }
    });
}

// Show/Hide
jQuery(function($) {
    $('#signInBox, #languageBox, #shareBox, #emailConfirm, #emailBox, #addToCart, #addProduct, #forgetPassBox, #faqsAnswers p, #faqsAnswers div, .categoryGroup').hide();

    $('.showHide').click(function() {

        var toggleBox = $(this).attr('href');
        $(toggleBox).fadeIn();
        $(toggleBox).hover(function() {
            $(this).show();
        }, function() {
            //            $(this).fadeOut();
        });
        return false;
    });

    $('.showClose').click(function() {
        var toggleBox = $(this).attr('href');
        $(toggleBox).fadeIn();
        return false;
    });

    $('.showCloseRelativePosition').click(function() {
        var toggleBox = $(this).attr('href');
        var tipPos = $(this).position();
        var popy = tipPos.top - 100;

        $(toggleBox).css({top:popy}).fadeIn().end();

        //        $(toggleBox).fadeIn();
        return false;
    });

    $('.showCloseRelativePositionProductDetail').click(function() {
        var toggleBox = $(this).attr('href');

        var tipPosParent = $(this).parent().parent().parent().parent().parent().parent().parent().position();
        var tipPos = $(this).position();
        var item = $(this);


        var offset = item.position().top;
        var offsets = "Offsets:";
        for(var i = 1; i < 12; i++)
        {
            offsets = offsets + "\n" + i + ": " + item.attr('class') + " - " + item.position().top;

            if( item.attr('class') == "tabModule")
            {
                offset = offset + item.position().top + 10 ;
            }

            if( item.attr('class') == "tabContent")
            {
                offsets = offsets + "\n" + i + ": scrolltop - " + item.scrollTop();

                offset = offset + item.position().top;
                offset = offset + item.scrollTop();
            }

            item = item.parent();
        }
        //alert(offsets);


        //var popy = tipPos.top + tipPosParent.top + 10;
        
        //alert(popy);
    //    alert("t:" + tipPos2.top);

//        $(toggleBox).css({top:popy}).fadeIn().end();
        $(toggleBox).css({top:offset}).fadeIn().end();
        return false;
    });

    $('.closeThis').click(function() {
        var toggleBox = $(this).attr('href');

        if ("#emailBox" == toggleBox) {
            clearPopupForm(toggleBox);
            var validator = $("#emailAFriendForm").validate();
            validator.resetForm();
            //            console.log('validatorrrrrr');
            //            console.warn(validator);
        }

        $(toggleBox).fadeOut();
        return false;
    });

    $('.faqsShowHide').click(function() {
        var toggleBox = $(this).attr('href');
        $(toggleBox).slideToggle();
        return false;
    });

    $('.closeAnswer').click(function() {
        $(this).parent().parent().slideToggle();
        return false;
    });

    /*
     $('.forgetPass').click(function() {
     var toggleBox = $(this).attr('href');
     $('#signInBox').hide();
     $(toggleBox).fadeIn();
     return false;
     });
     */

    $('.closePassFind').click(function() {
        var toggleBox = $(this).attr('href');
        $('#forgetPassBox').fadeOut();
        return false;
    });

});

// Rounded Corners
jQuery(function roundedCorners($) {
    $('.rounded').corner({
        tl: { radius: 5 },
        tr: { radius: 5 },
        bl: { radius: 5 },
        br: { radius: 5 },
        antiAlias: true
    });
    $('.roundedTop').corner({
        tl: { radius: 5 },
        tr: { radius: 5 },
        bl: { radius: 1 },
        br: { radius: 1 },
        antiAlias: true
    });
    $('.roundedBtm').corner({
        tl: { radius: 1 },
        tr: { radius: 1 },
        bl: { radius: 5 },
        br: { radius: 5 },
        antiAlias: true
    });

    // Alternate Rounded Corners
    $('.tabModuleCopy').wrap('<div class="tl"><div class="tr"><div class="bl"><div class="br">' + '</div></div></div></div>');
    $('.widgetModuleCopy').wrap('<div class="tl"><div class="tr"><div class="bl"><div class="br">' + '</div></div></div></div>');
});

// Tabs
jQuery(function($) {
    $('#tabs a').click(function() {
        $('div.tabContent').hide().filter(this.hash).fadeIn('normal');

        $('#tabs a').removeClass('selected');
        $(this).addClass('selected');

        return false;
    });

    if ($('#tabs a[class=preselected]').val() != undefined) {
        $('#tabs a[class=preselected]').click();
    }
    else {
        $('#tabs a').filter(':first').click();
    }

});


// Page Print
jQuery(function($) {
    $('.printBtn').click(function() {
        window.print();
        return false;
    });
});



// Input Button Hover
jQuery(function($) {
    $('span.blueBdr').hover(function () {
        $(this).addClass('btnHover');
    }, function() {
        $(this).removeClass('btnHover');
    });
});

// CSS Manipulations
jQuery(function($) {
    $('#shopRetailers').each(function() {
        $('.retailerLogo:last').css({'border-bottom':'none'});
    });
    $('#productGallery li').eq(3).addClass('last');
});

function roundedCorners() {
    $('.rounded').corner({
        tl: { radius: 5 },
        tr: { radius: 5 },
        bl: { radius: 5 },
        br: { radius: 5 },
        antiAlias: true
    });
    $('.roundedTop').corner({
        tl: { radius: 5 },
        tr: { radius: 5 },
        bl: { radius: 1 },
        br: { radius: 1 },
        antiAlias: true
    });
    $('.roundedBtm').corner({
        tl: { radius: 1 },
        tr: { radius: 1 },
        bl: { radius: 5 },
        br: { radius: 5 },
        antiAlias: true
    });

    // Alternate Rounded Corners
    $('.tabModuleCopy').wrap('<div class="tl"><div class="tr"><div class="bl"><div class="br">' + '</div></div></div></div>');
    $('.widgetModuleCopy').wrap('<div class="tl"><div class="tr"><div class="bl"><div class="br">' + '</div></div></div></div>');
}

function getWindowWidth()
{
    if( typeof( window.innerWidth ) == 'number' )
    {
        myWidth = window.innerWidth;
    }
    else if( document.documentElement && document.documentElement.clientWidth )
    {
        myWidth = document.documentElement.clientWidth;
    }
    else if( document.body && document.body.clientWidth )
    {
        myWidth = document.body.clientWidth;
    }

    return myWidth;
}

function toolTip() {
    $('a.Hover').mouseover(function(e) {
        var popup_div = $(this).attr('rel');

        var tipPos = $(this).position();
        var popx = tipPos.left;
        var popy = tipPos.top;

        var wordWidth = $(this).width();
        var wordHeight = $(this).height();

        var leftVal = (popx + wordWidth) - 10;

        if((leftVal + 550) > getWindowWidth()){
            leftVal = (popx + wordWidth) - 370;

            var el = $(popup_div);
            el.addClass('toolTipLeft');
            el.removeClass('tooltip');

            el = el.find('.tooltipContent').parent().parent().css({'padding-right':'0px'});
        }
        else {
            var el = $(popup_div);
            el.addClass('tooltip');
            el.removeClass('toolTipLeft');
        }

        leftVal = leftVal + "px";
        topVal = popy - (wordHeight * 2) + "px";

        $(popup_div).css({left:leftVal,top:topVal}).show();
        return false;
    });

    $('a.Hover').mouseout(function() {
        var popup_div = $(this).attr('rel');
        $(popup_div).hover(function() {
            $(this).show();
        }, function() {
            $(this).hide();
        });
        $(popup_div).hide();
    });
}

// News Banner Functions
function bannerFadeIn() {
    $('#newsBanner p').fadeIn();
    setTimeout(bannerFadeOut(), 3000);
}

function bannerFadeOut() {
    $('#newsBanner p').fadeOut();
    clearTimeout();
}

function onBodyLoadScripts() {

    //We need to run this tooltip js script again after on Body load because there are situations were the position isn't calculated properly until the body loads
    toolTip();

    setFontSizeFromCookie();
}

// VIDEO PLAYER
// execute your scripts when DOM is ready. this is a good habit
    jQuery(function($){
        // initialize scrollable
        $("div.scrollable").scrollable({
            size: 3,
            items: '#thumbs',
            hoverClass: 'hover'
        });

        // install flowplayer into flowplayer container
         //var player = $f("player", "flowplayer/flowplayer-3.0.7.swf");

        // install flowplayers
        // flowplayerSwf is defined in js.include.jsp, since we need to get our the SAL url using JSTL
        $("a.player").flowplayer(flowplayerSwf);







        // setup button action. it will fire our overlay
        $("#thumbs a[rel]").overlay({
            onLoad: function(content) {

                //this.getContent().find("div.close").hide();

                var videoDiv = this.getContent();

                var closeOffsetTop = videoDiv.position().top + 7.5;
                var closeOffsetLeft = videoDiv.position().left + videoDiv.width() + 35;




                //var item = $(this);
                var offsetTop = videoDiv.position().top - $('#bd').position().top;
                var offsetLeft = videoDiv.position().left - $('#bd').position().left;

                $(videoDiv).css({left:offsetLeft,top:offsetTop});
                $('#flowPlayerClose').css({left:closeOffsetLeft,top:closeOffsetTop});

                //this.getContent().find("div.close").show();
                $('#flowPlayerClose').show();
                // find the player contained inside this overlay and load it
                this.getContent().find("a.player").flowplayer(0).load();
            },
            onClose: function(content) {
                //this.getContent().find("div.close").hide();
                $('#flowPlayerClose').hide();
                $f().unload();

                // close exposing
                //$.expose.close();
            }
        });
});




// ##################################### RETRIEVER ###############################

//alert('links: ' + $('.retrieverPopupLink'));
jQuery(function($) {



    // iframe close
    $('.retrieverPopup .close').click(function() {
        //$(this).parent().parent().parent().hide();
        var item = $(this);
        var foundDiv = false;

        while(!foundDiv)
        {
            if(item.attr('class') == "retrieverPopup")
            {
                foundDiv = true;
                item.hide();
            }
            else
            {
                item = item.parent();
                if(item == null)
                {
                    foundDiv = true;
                }
            }
        }
        return false;
    });

    $('.retrieverPopupLink').click(function() {

        var popup_div = null;

        //alert('test: ' + $('#retrieverSigninBox'));

        if ($('#retrieverSigninBox').length > 0) {
            popup_div = '#retrieverSigninBox';

            var url = window.location.href;
            if (url.indexOf('#') > 0) {
                url = url.substring(0, url.indexOf('#'));
            }

            $('#retrieverSigninURL').attr('value', url + $(this).attr('rel'));
        }
        else {
            popup_div = $(this).attr('rel');
        }

        //alert('popup_div: ' + popup_div);
        //alert('div: ' + $(popup_div));

        var tipPos = $(this).position();
        var popx = tipPos.left;
        var popy = tipPos.top;
        var popupWidth = 300;

        leftVal = popx + 45 + "px";
        topVal = (popy - 20) + "px";

        //alert('popy: ' + popy + '\ntop: ' + topVal);

        //alert('$(popup_div): ' + $(popup_div));

        $(popup_div).css({left:leftVal,top:topVal}).show();


        return false;
    });
});

