function printYear() {
	var today = new Date();
	document.write(today.getFullYear());
}

function writeEmail(email) {
	document.write('<a href="mailto:'+email+'">'+email+'</a>');
}

function changeTextSize(textSize) {
	document.body.className = 'site ' + textSize;
	return false;
}

function printPage() {
	window.print();
	return false;
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function minHeight(element,minHeight){
    var eH = $(element).innerHeight();
    if (eH < minHeight){
        $(element).height(minHeight+"px");
    }
}

function maxHeight (element, maxHeight){
    var eH = $(element).innerHeight();
    if(eH > maxHeight){
        $(element).height(maxHeight+"px");
    }
};

function maxWidth (element, maxWidth){
    var eW = $(element).innerWidth();
    if(eW > maxWidth){
        $(element).width(maxWidth+"px");
    }
};

function resizeImg (element, height, width){
    var eW = $(element).innerWidth();
    var eH = $(element).innerHeight();
    if(height=="auto"){
        ratio = eH/eW;
        height=width*ratio;
    } else if(width=="auto") {
        ratio = eW/eH;
        width=height*ratio;
    }
    $(element).width(width+"px").height(height+"px");
}

$(document).ready(function() {
    if ($('div.lightbox').length > 0) {
        $('div.lightbox a').lightBox({
            imageLoading: '/cms/outertemplates/images/lightbox/lightbox-ico-loading.gif',
            imageBtnClose: '/cms/outertemplates/images/lightbox/lightbox-btn-close.gif',
            imageBtnPrev: '/cms/outertemplates/images/lightbox/lightbox-btn-prev.gif',
            imageBtnNext: '/cms/outertemplates/images/lightbox/lightbox-btn-next.gif'
        });
    }

    /* Search Input Box Padding */
    /*var srcHeight = $("div.searchBox input.txtSearch").innerHeight();
    var srcBox = $("div.searchBox div").height();
    var srcDiff = srcBox - srcHeight;
    var srcPadd = srcDiff / 2;
    srcPadd = Math.round(srcPadd);
    var searchCSS = {
    "padding-top" : srcPadd+"px",
    "padding-bottom" : srcPadd+"px"
    }
    $("div.searchBox input.txtSearch").css(searchCSS);*/
    /* END :: Search Input Box Padding */

    /*$("#jsTEST").html();*/

    if ($.browser.msie && $.browser.version < "8") {

        $("div.infoColHeader").each(function() {
            var heightText = $(this).children().innerHeight();
            var heightContainer = $(this).innerHeight();
            var heightDiff = heightContainer - heightText;
            var padding = heightDiff / 2;
            padding = Math.round(padding);
            var heightReCalc = heightContainer - (padding * 2);
            var cssChange = {
                "padding-top": padding + "px",
                "padding-bottom": padding + "px",
                "height": heightReCalc + "px"
            }
            $(this).css(cssChange);
        });

        $("div.infoColHeader").css("display", "block");

        $("div#topBarHome div.cell").each(function() {
            var heightCont = $(this).children().innerHeight();
            var heightContainer = $(this).innerHeight();
            var heightDiff = heightContainer - heightCont;
            var padding = heightDiff / 2;
            padding = Math.round(padding);
            var heightReCalc = heightContainer - (padding * 2);
            var cssChange = {
                "padding-top": padding + "px",
                "padding-bottom": padding + "px",
                "height": heightReCalc + "px",
                "display": "block",
                "float": "left"
            }
            $(this).css(cssChange);
        });
    }


    if ($.browser.msie && $.browser.version < "7") {
        minHeight("div.infoColBtm", 293);
        minHeight("div#topBarBtm", 81);
        minHeight("div#miniBasketBtm", 81);
        resizeImg("div.productDetails div.productImage img", "auto", 203);
    }

    $(':first-child').addClass('first');
    $(':last-child').addClass('last');
    $('a[rel=newWindow]').attr('target', '_blank');

    $('div.catListing div.productBox:nth-child(2n+1)').css({
        clear: 'left'
    });

    shortenBreadcrumb({
        maxWidth: 570
    });

    $("div#productDropDownBtn a").click(function(e) {
        e.preventDefault();
        if ($(this).hasClass("open")) {
            $(this).removeClass("open").html("Report Overview");
            $("div#productDropDownCont").animate({ height: 0, paddingTop: 0 }, 600, function() {
                $(this).children().hide();
            });
        } else {
            $(this).addClass("open").html("Close");
            $("div#productDropDownCont").animate({ height: 270, paddingTop: 30 }).children().show();
        }
    });

    $("div#productTopDropDownBtn a").click(function(e) {
        e.preventDefault();
        if ($(this).hasClass("open")) {
            $(this).removeClass("open").html("Report Overview");
            $('div#productTopDropDownCont').animate({ height: 0, paddingBottom: 0 }, 600, function() {
                $(this).children().hide();
            });
        } else {
            $(this).addClass("open").html("Close");
            $('div#productTopDropDownCont').animate({ height: 270, paddingBottom: 30 }).children().show();
        }
    });

    $(".productTabs").each(function() {
        $(this).children('div').hide();
        $(this).children('div:first').show();
        $(this).find('ul li:first-child a').addClass('active');
    });

    /*
    $(".productTabs").children("div").hide();
    $(".productTabs div:first-child").show();
    $(".productTabs ul li:first a").addClass("active");
    */

    $(".productTabs ul li a").click(function(e) {
        e.preventDefault();
        if ($(this).hasClass("active")) {
            // DO NOTHING
        } else {
            var tabTarget = $(this).attr("href");
            $(this).parents('.productTabs').children('div').hide();
            $(tabTarget).show();
            $(this).parent('li').siblings().children('a').removeClass("active");
            $(this).addClass("active");
        }
    });

    $('.dub3Content img').not('.noFrame').each(function(i, e) {
        var $e = $(e), $wrap = $('<div class="imgWrap"></div>'), $wrapInner = ('<div class="imgWrapInner"></div>');
        if ($e.hasClass('left')) {
            $wrap.addClass('imgLeft');
        } else if ($e.hasClass('right')) {
            $wrap.addClass('imgRight');
        }
        $e.wrap($wrap).wrap($wrapInner);

    });
    /*$('.dub3Content img.imgFrameLarge').each(function(i, e) {
        var $e = $(e), $wrap = $('<div class="imgWrap imgWrapLarge"></div>'), $wrapInner = ('<div class="imgWrapInner"></div>');
        if ($e.hasClass('left')) {
            $wrap.addClass('imgLeft');
        } else if ($e.hasClass('right')) {
            $wrap.addClass('imgRight');
        }
        $e.wrap($wrap).wrap($wrapInner);

    });*/

});

function shortenBreadcrumb(options){
    /*  
        Shortens the breadcrumb to a specified width by removing the text from one list item
        at a time and replacing it with "..." - accepts an options object as an optional
        parameter with two options:
            shortenBreadcrumb({
                breadcrumb : $('div#breadcrumb > ul'),
                maxWidth : 725
            });
            
        The first is the selector for the breadcrumb ul, the second is the maximum width you
        want it to be.  If not set, the max width is the width of the container minus the width
        of the paragraph next to it ("You are here:").
            -JM
    */

    var breadcrumb = $('div#breadcrumb > ul');
    var maxWidth = parseInt($('div#breadcrumb').innerWidth()) - parseInt($('div#breadcrumb p').outerWidth(true)) + "px";
    
    if (options != undefined){
        if (options.breadcrumb != null) { breadcrumb = options.breadcrumb; }
        if (options.maxWidth != null) { maxWidth = options.maxWidth; }
    }
    
    var levelCount = breadcrumb.find('li').size();
    var shortEnough = false;
    var totalWidth;
    while (shortEnough == false) {
        totalWidth = 0;
        breadcrumb.children('li').each (function(){
            totalWidth += $(this).outerWidth(true);
        });
        if (totalWidth > maxWidth){
            var li = breadcrumb.children('li').not('.short').eq(1);
            li.addClass('short');
            li.children('a').attr('title', li.children('a').html());            
            li.children('a').html('...');
        }
        else {
            shortEnough = true;
        }
    }
}
