function asFormatDate(date) {
    if (date != '') {
        var d = new Date(date);
        document.write(d.getDate() + '.' + (parseInt(d.getMonth()) + 1) + '.' + d.getFullYear().toString().substring(2) + ' - ');
    }
}

function flashWrite(flashfile,x,y,flashvar) {
	document.write("<object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0' width='"+ x +"' height='"+ y +"'>");
	document.write("<param name='movie' value='"+ flashfile +"' />");
	document.write("<param name='quality' value='high' />");
	document.write("<param name='flashvars' value='"+ flashvar +"' />");
	document.write("<param name='wmode' value='transparent' />");
	document.write("<embed src='"+ flashfile +"' wmode='transparent' ");
	document.write("flashvars='"+ flashvar +"'  ");
	document.write("quality='high' pluginspage='http://www.macromedia.com/go/getflashplayer' type='application/x-shockwave-flash' width='"+ x +"' height='"+ y +"'></embed>");
	document.write("</object>");
}

function printYear() {
	var today = new Date();
	document.write(today.getFullYear());
}

function writeEmail(email) {
	document.write('<a href="mailto:'+email+'">'+email+'</a>');
}

function changeTextSize(textSize) {
    $('body').removeClass('smallText mediumText largeText').addClass(textSize);
    createCookie('style', textSize, 7);
}

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;
}

$(window).load(function() {
    var h = 0;
    $('div.manufacturers div.box').each(function() {
        var th = $(this).height();
        if (th > h) {
            h = th;
        }
    }).height(h);
    $('div.manufacturers div.box img').each(function() {
        var th = $(this).height();
        var mTop = ((h - th) / 2);
        $(this).css({ marginTop: mTop });
    })
});

$(document).ready(function() {
    $('div.colRetail a, div.colTrade a').bigTarget({ clickZone: 'div:eq(0)' });
    $('div.categoryItem a').bigTarget({ clickZone: 'div:eq(0)' });
    $('div#leftCol div.leftBoxLink a').bigTarget({ clickZone: 'div:eq(0)' });

    $('div#introBox .inner').css('height', 'auto');
    var boxHeight = $('div#introBox .inner').height();
    $('div#introBox .inner').css('height', '101px');

    $('div#introBox a.lnkDropDown').click(function(e) {
        e.preventDefault();
        if ($(this).hasClass('open')) {
            $('div#introBox .inner').animate({ 'height': 101 }, 600);
            $(this).text('Read More').removeClass('open');
        } else {
            $('div#introBox .inner').animate({ 'height': boxHeight }, 600);
            $(this).text('Read Less').addClass('open');
        }

    });

    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'
        });
    }


    $('.searchBox input.textBox').attr('rel', $('.searchBox input.textBox').val()).focus(function() {
        var t = $(this).attr('rel');
        var v = $(this).val();
        if (v == t) {
            $(this).val('');
        }
    }).blur(function() {
        var t = $(this).attr('rel');
        var v = $(this).val();
        if (v == '') {
            $(this).val(t);
        }
    });

    var h = 0;
    $('table.tblTradeFeeds div.tradeFeed ul.feedListing li').each(function() {
        var th = $(this).height();
        if (th > h) {
            h = th;
        }
    }).height(h);

    $('div.productListing div.product').hover(function() {
        $(this).children('div.productDetails').show();
    }, function() {
        $(this).children('div.productDetails').hide();
    });

    $('#banner div.carouselMask ul').adidoCarousel({ animDelay: 200, animSpeed: 1000, mode: 'fade' });
    $('div.contentBar div.contentBarBackground div.carouselMask > ul').adidoCarousel({ animDelay: 200, animSpeed: 1000, mode: 'fade' });

    $('ul.faq li h2').click(function() {
        $(this).siblings().slideToggle().end().parent().toggleClass('open');
    }).siblings().hide();

    $('select.ddlCustom').each(function() {
        var $this = $(this);
        var ddlAlt = false;
        var ddlWrap = $('<span class="ddlWrap"></span>');
        var ddlBtn = $('<span class="ddlBtn"></span>');
        var ddlVal = $('<span class="ddlVal"></span>');
        var ddlClear = $('<div class="clear"></div>');
        var ddlDrop = $('<span class="ddlDrop"></span>');
        var ddlDropList = $('<ul></ul>');
        var ddlOpen = false;
        // Generate a new list for the dropdown
        $(this).children('option').each(function() {
            var v = $(this).val();
            var t = $(this).text();
            var ddlOption = $('<li></li>');
            ddlOption.text(t).attr('title', v);
            if (ddlAlt) {
                ddlOption.addClass('alt');
                ddlAlt = false;
            } else {
                ddlAlt = true;
            }
            ddlDropList.append(ddlOption);
        });

        // Clicking on an item in the drop down list
        ddlDropList.children('li').click(function(e) {
            var tv = $(this).attr('title');
            $this.val(tv).change();
        });

        $this.change(function() {
            var updV = $(this).children(':selected').val();
            var updT = $(this).children(':selected').text();
            ddlVal.text(updT).attr('value', updV);
        });

        ddlDrop.hide();

        //
        ddlWrap.click(function() {
            if (!ddlOpen) {
                ddlDrop.css({ 'height': 'auto' });
            }

            ddlDrop.stop().slideToggle(function() {
                if (ddlOpen) {
                    ddlOpen = false;
                } else {
                    ddlOpen = true;
                }
            });
        });
        ddlWrap.bind("clickoutside", function() {
            if (ddlOpen) { ddlDrop.stop().slideUp(function() { ddlOpen = false }) };
        });

        // Default item
        var defV = $(this).children(':selected').val();
        var defT = $(this).children(':selected').text();

        ddlVal.text(defT).attr('value', defV);

        ddlDrop.append(ddlDropList);

        $(this).wrap(ddlWrap).parent().append(ddlVal).append(ddlBtn).append(ddlDrop).append(ddlClear);
        $(this).hide();
    });

    // Remove menu is there's no items
    if ($('div#leftCol div#vNav').size() < 1) {
        $('div#leftCol .sideMenu').css('display', 'none');
    }


    $(':first-child').addClass('first');
    $(':last-child').addClass('last');
    $('a[rel=newWindow]').attr('target', '_blank');

});
