var flashvars = {};
var params = {};
params.wmode = "transparent";
var attributes = {};
swfobject.embedSWF("/cms/outertemplates/includes/flash/SP_logo_v2.swf", "logo", "110", "160", "9.0.0", "/cms/outertemplates/includes/flash/expressInstall.swf", flashvars, params, attributes);

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 pageLoad() {
    $('div.productGrid div.productBox:nth-child(4n+1)').css('border-left', '1px solid #C8C8C8');
    $('div.productGrid div.productBox').hover(function() {
        $(this).addClass('hover').css('z-index', '4').children('div.productHover').show();
    }, function() {
        $(this).removeClass('hover').css('z-index', '2').children('div.productHover').hide();
    });

//    $('div.advancedSearchTab a').click(function(e) {
//        e.preventDefault();
//        $('div#advancedSearch').slideToggle();
//        $(this).toggleClass('hidden');
//        if ($(this).hasClass('hidden')) {
//            $(this).text('Filter');
//        } else {
//            $(this).text('Hide');
//        }
//    });
}

$(document).ready(function() {
    $("img").lazyload();

    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'
        });
    }
    $('a.lightboxLink').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'
    });

    $('.jqzoom').each(function() {
        var h = $(this).attr('href');
        var noImg = '/system/cache/cms_site_products_images_NoImage_800_800_False.png';
        if (h != noImg) {
            $(this).jqzoom({ zoomWidth: 400, zoomHeight: 400, xOffset: 35, yOffset: -1 });
        }
    });


    $('div.feedSexBuzz div.carouselMask > ul').adidoCarousel({ autoStart: false, buttonNext: 'div.feedSexBuzz a.btnCarouselNext', buttonPrev: 'div.feedSexBuzz a.btnCarouselPrev', resizeMask: true });
    $('div.feedPleasureNews div.carouselMask > ul').adidoCarousel({ autoStart: false, buttonNext: 'div.feedPleasureNews a.btnCarouselNext', buttonPrev: 'div.feedPleasureNews a.btnCarouselPrev', resizeMask: true });

    // Home carousel functions

    $('div#homeCarousel div.right > div.carouselMask > ul').adidoCarousel({ autoStart: true, buttonNext: '.btnRootNext', mode: 'instant', pager: true, pagerAuto: false, pagerElement: 'div#homeCarousel ul.parentPager', speed: 25000 });

    $('div#homeCarousel div.carouselMask div.carouselMask ul').adidoCarousel({ anim: 'easeInOutQuad', animSpeed: 500, animDelay: 250, autoStart: true, lastItemTrigger: function(e, c, s, o) {
        if (e.parents('.acActive').size() > 0) {
            // alert("!");
            // $('div#homeCarousel div.right > div.carouselMask > ul').adidoCarousel.nextItem(e, c, _a, o);
            // $('a.btnRootNext').click();
        }
    }, mode: 'slideFade', pager: true, pagerElement: function(e) {
        var pager = e.parent().siblings('div.carouselPager');
        return pager;
    }, pagerStyle: 'numeric', speed: 5000
    });

    // END :: Home carousel functions

    $('div.productCarousel div.carouselMask ul').adidoCarousel({ anim: 'easeInOutQuad', animSpeed: 1000, animDelay: 0, buttonNext: 'div.productCarousel a.btnCarouselNext', buttonPrev: 'div.productCarousel a.btnCarouselPrev', startPosition: 4 })

    $('div.feedSexBuzz div.carouselMask a').bigTarget();
    $('div.feedPleasureNews div.carouselMask a').bigTarget();
    $('div.featuredProductsGrid div.cell a').bigTarget({ clickZone: 'div:eq(0)' });
    $('div.catListing div.productBox a').bigTarget({ clickZone: 'div:eq(0)' });
    $('div.productGrid div.productBox h3 a').bigTarget({ clickZone: 'div:eq(1)' });

    $('#hNav, div.boxGuarantee, div.boxGuaranteeInner, div.formAdvancedSearch, div.userSectionFrame').corners({ detectCSS: false });
    $('.miniBasketDropdown, div#linkBar, div.featuredProducts, #content').corners({ detectCSS: false });
    $('div.leftBox').corners({ detectCSS: false });
    $('div.storeLocator, div.storeLocator div.textBox').corners({ detectCSS: false });

    $('div.productIcons div').tipsy({ fade: true, gravity: $.fn.tipsy.autoNS });


    $('div.catListing div.productBox:nth-child(4n+1)').css('border-left', '1px solid #C8C8C8');

    /*$('div.productGrid div.productBox').hover(function() {
    $(this).addClass('hover').css('z-index', '4').children('div.productHover').show();
    }, function() {
    $(this).removeClass('hover').css('z-index', '2').children('div.productHover').hide();
    });*/


    $('a#lnkMiniBasket').toggle(function(e) {
        e.preventDefault();
        $('div#miniBasketDropdown').slideDown();
    }, function(e) {
        e.preventDefault();
        $('div#miniBasketDropdown').slideUp();
    });

    var total = $('table.miniBasket tr.total td.value').text();
    $('#miniBasketTop span.basketTotal').text(total);


    $('p.imgZoom').click(function() {
        $('#productImage a.lightboxLink').click();
    });

    $('div.advancedSearchTab a').click(function(e) {
        e.preventDefault();
        $('div#advancedSearch').slideToggle();
        $(this).toggleClass('hidden');
        if ($(this).hasClass('hidden')) {
            $(this).text('Filter');
        } else {
            $(this).text('Hide');
        }
    });

    var advSearch = $.query.get('adv');

    if (advSearch) {
        $('div.advancedSearchTab a').click();
    }

    $('div.ddContentLnk a').click(function(e) {
        e.preventDefault();
        if ($('#ddContent_hide').hasClass('open')) {
            $('#ddContent_hide').removeClass('open').slideUp(800);
            $(this).children('span').text('view more');
        } else {
            $('#ddContent_hide').addClass('open').slideDown(800);
            $(this).children('span').text('view less');
        }
    });

    $('.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);
        }

    });

    $(':first-child').addClass('first');
    $(':last-child').addClass('last');
    $('a[rel=newWindow]').attr('target', '_blank');

    $('.feedPleasureNews .newsBrief').each(function() {
        /*
        If the news brief is more than 100 characters, truncate to 100
        characters (to the nearest space) and set the full brief as the
        title attribute.
        */
        var fullString = $(this).text();
        
        if (fullString.length >= 100) {
            var shortString = fullString.substr(0, 100);
        
            shortString = shortString.substr(0, shortString.lastIndexOf(' '));
            $(this).text(shortString + '...').attr('title', fullString);
        }
    });

});

