function bookmark(url, title) {
    if ((navigator.appName == "Microsoft Internet Explorer") && (parseInt(navigator.appVersion) >= 4)) {
        window.external.AddFavorite(url, title);
    } else if (navigator.appName == "Netscape") {
        window.sidebar.addPanel(title, url, "");
    } else {
        alert("Press CTRL-D (Netscape) or CTRL-T (Opera) to bookmark");
    }
}

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 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;
        }
    }
}

function pageLoad() {
    $('.ddlCustom').sexyCombo({ skin: 'custom' });
    $('.ddlCustomQty').sexyCombo({ skin: 'custom customQty' });
}

$(document).ready(function() {

    $('div.lightbox a').lightBox({
        imageLoading: '/system/assets/images/lightbox/lightbox-ico-loading.gif',
        imageBtnClose: '/system/assets/images/lightbox/lightbox-btn-close.gif',
        imageBtnPrev: '/system/assets/images/lightbox/lightbox-btn-prev.gif',
        imageBtnNext: '/system/assets/images/lightbox/lightbox-btn-next.gif'
    });

    $('#newsBar ul.feedListing a').click(function(e) {
        e.preventDefault();
        // var $e = $(this), $a = $e.find('a'), h = $a.attr('href');
        var $e = $(this), h = $e.attr('href');
        console.log(h);
    });

    $('.searchBox input.textBox').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);
        }
    });

    $('input.textClear').focus(function() {
        var t = $(this).attr('title');
        var v = $(this).val();
        if (v == t) {
            $(this).val('');
        }
    }).blur(function() {
        var t = $(this).attr('title');
        var v = $(this).val();
        if (v == '') {
            $(this).val(t);
        }
    });

    $('img.shadow').each(function(i, e) {
        var $e = $(e),
        h = $e.height(),
        imgWrap = $('<div class="imgWrap"></div>');
        imgWrap.height(h);
        if ($e.css('float') == 'left') { imgWrap.addClass('imgWrapLeft'); }
        if ($e.css('float') == 'right') { imgWrap.addClass('imgWrapRight'); }
        $e.wrap(imgWrap);
    });

    var thumbClickHandler = function() {
        $('#productImage > a').click();
    };

    // Product Lightbox Hack
    $('.productThumbnails img').hover(function() {
        var $e = $(this);
        $e.click();
        $e.bind('click', thumbClickHandler);
    }, function() {
        var $e = $(this);
        $e.unbind('click', thumbClickHandler);
    }).each(function(i, e) {
        var oc, s, f, l, url;
        oc = $(e).attr('onclick');
        oc = oc.toString();
        s = oc.split(',');
        f = s[1].indexOf("'");

        if (f > -1) {
            l = s[1].lastIndexOf("'");
        } else {
            f = s[1].indexOf('"');
            l = s[1].lastIndexOf('"');
        }

        f = f + 1;
        url = s[1].slice(f, l);
        $(this).parent().attr('href', url);

        var $lightbox = $('<a class="lightboxLink" href="' + url + '" />');
        if (i == 0) { $lightbox.addClass('active'); }
        $('.productLightbox').append($lightbox);
    }).click(function(e) {
        var $e = $(this), i = $e.index();
        $('.productLightbox a').eq(i).addClass('current').siblings().removeClass('current');
    });

    $('.productLightbox a').lightBox({
        imageLoading: '/system/assets/images/lightbox/lightbox-ico-loading.gif',
        imageBtnClose: '/system/assets/images/lightbox/lightbox-btn-close.gif',
        imageBtnPrev: '/system/assets/images/lightbox/lightbox-btn-prev.gif',
        imageBtnNext: '/system/assets/images/lightbox/lightbox-btn-next.gif'
    }).first().addClass('current');

    $('#productImage a').click(function(e) {
        e.preventDefault();
        $('.productLightbox a.current').click();
    });

    $('a.lnkBookmark').click(function(e) {
        e.preventDefault();
        var url = window.location.href;
        var title = document.title;
        bookmark(url, title);
    });


    $(':first-child').not('.st_sharethis').addClass('first');
    $(':last-child').not('.st_sharethis').addClass('last');
    $('a[rel=newWindow]').attr('target', '_blank');


});
