
AS = {};

// run scripts on load
jQuery( function()
{
	// add .JS class
	jQuery('html').addClass('JS');
	
	// fixes IE6 cursor flicker
	try {
		document.execCommand("BackgroundImageCache", false, true);
	} catch(err) {};
	
	// initialise the homepage carousel
	var itemCount = jQuery('#carousel').find('li').length;
	var stopped = false;
	
    var carousel = jQuery('#carousel').jcarousel({
    	scroll: 2,
        animation: 1200,
        auto: 5,
        wrap: 'circular',
        initCallback: function(carousel, state) {
   		    // add pause button
   		    carousel.container
   		    	.append(
   		    		jQuery(document.createElement('div'))
						.addClass('jcarousel-stop')
						.click(function() {
							// add click functionality to pause button
							var e = jQuery(this);
							if(!stopped) {
								carousel.stopAuto();
								e.addClass('jcarousel-stopped');
							} else {
								carousel.startAuto();
								e.removeClass('jcarousel-stopped');
							}
							stopped = !stopped;
							if(e.css('filter') != 'none') {
								var filter = e.css('filter');
								e.css('filter', filter.replace((stopped) ? 'stop' : 'stopped', (stopped) ? 'stopped' : 'stop'));
							}
						})
				);
        },
        itemVisibleInCallback: {
        	onBeforeAnimation: function(carousel, item, i, state, evt) {
        		// add the items from the back to the front
        		if(state == 'prev')
					carousel.add(i, carousel.get(itemCount + i).html());
        	},
        	onAfterAnimation: function(carousel, item, i, state, evt) {
        		// remove the items from the back, now duplicated at the front
        		// stop animation from reactivating after animation
        		carousel.stopAuto();
        		if(state == 'prev')
					carousel.remove(itemCount + i);
				if(!stopped)
	        		carousel.startAuto();
        	}
        },
        itemVisibleOutCallback: {
        	onAfterAnimation: function(carousel, item, i, state, evt) {
        		if(state == 'next') {
        			// duplicate the items at the front and place it at the end
        			// then remove the items from the front
	        		carousel.add(itemCount + i, jQuery(item).html());
	        		carousel.remove(i);
	        	}
	        	if(!stopped)
	        		carousel.startAuto();
        	}
        }
    });
	
	// run supersleight
	jQuery('#content').supersleight();
	
	// set external links
	jQuery('a[rel=external]').attr('target', '_blank');
	
	// initialise the quick search box
	jQuery('#quick-search input.query').focus(function()
	{
		jQuery(this).attr('value', '');
	});
	
	// initialise the homepage style switcher if it exists
	if(jQuery('#mini-nav').length > 0 ){
		AS.switcher.init();
	}
	
	// initialise the navigation slider if it exists
	if(jQuery('#cat-nav').length > 0 ){
		AS.sliderNav.init();
	}
	
	// order table rollovers
	jQuery('table.order-details tbody tr').hover(function()
	{
		jQuery(this).addClass('highlighted');
	}, function()
	{
		jQuery(this).removeClass('highlighted');
	});
	
	// order history details table rollovers
	jQuery('table.order-history-details tbody tr').hover(function()
	{
		jQuery(this).addClass('highlighted');
		
	}, function()
	{
		jQuery(this).removeClass('highlighted');
	});
	
	// gift message counter
	if(jQuery('#gift-message').length > 0 )
	{
		jQuery('#gift-message').charCounter(250, {
			container: '#gift-message-counter',
			format: '%1 Characters left',
			pulse: false
		});
	}
	
	// show/hide delivery address
	jQuery('#delivery-address').hide();
	jQuery('#different-address').click(function(){
		if(jQuery(this).is(':checked'))
		{
			jQuery('#delivery-address').show();
		}
		else
		{
			jQuery('#delivery-address').hide();
		}
	});
});

/* navigation slider
-------------------------------------------------------------------------*/
AS.sliderNav = {
	
	// animation speeds
	headerSpeed: 500,
	toggleNavSpeed: 750,
	slideSpeed: 500,
	
	// animation locks
	animCounter: 0,
	
	// initialise menu
	init: function(){
		
		// close the tabs that aren't open and set the styles
		if ( jQuery('#cat-nav li.selected, #cat-nav li:has(li.selected)').is('li') == 0 ) {
			jQuery('#cat-nav li.selected, #cat-nav li:first').addClass('open').siblings().find('ul').hide();
			jQuery('#cat-nav li.selected a, #cat-nav li:first > a').css({ backgroundPosition: '0 0' });
		} else {
			jQuery('#cat-nav li.selected, #cat-nav li:has(li.selected)').addClass('open').siblings().find('ul').hide();
			jQuery('#cat-nav li.selected a, #cat-nav li:has(li.selected) > a').css({ backgroundPosition: '0 0' });
		}
		
		jQuery('#cat-nav > ul > li:not(.open) a').css({ backgroundPosition: '0 -60px', color: '#ff0066' });

		jQuery('#cat-nav ul.nav-slider li:not(.selected) a').each(function()
		{
			indentPos = jQuery(this).css('textIndent').replace('px', '');
			jQuery(this).attr('startPos', indentPos).css({ backgroundPosition: '-240px 0' });
		});
	
		// bind event clicks for slider and navigation
		this.bindEventHandlers();
		
	},
	
	bindEventHandlers: function(){
		
		// bind events
		jQuery('#cat-nav li.nav-head > a').bind('click', function(e){ AS.sliderNav.toggleNav(e.target); return false; });
		jQuery('#cat-nav ul.nav-slider li:not(.selected) a').bind('mouseover', function(e){ AS.sliderNav.slideRight(e.target); });
		jQuery('#cat-nav ul.nav-slider li:not(.selected) a').bind('mouseout', function(e){ AS.sliderNav.slideLeft(e.target); });
		
	},
	
	toggleNav: function(elem){
		
		if(!this.animCounter > 0)
		{
			this.openNav(jQuery(elem).parent());
			jQuery(elem).parent().siblings().each(function()
			{
				AS.sliderNav.closeNav(this);  
			});
		}
	},
	
	openNav: function(elem){
		
		this.animCounter += 1;
		jQuery(elem).children('a').stop().animate({ backgroundPosition: '(0 0)', color: '#ffffff' }, { duration: this.headerSpeed });
		jQuery(elem).children('ul').css({position: 'relative'}).slideDown(this.toggleNavSpeed, function(){ AS.sliderNav.animCounter -= 1; jQuery(this).css({position:'static'}); });
												
	},
	
	closeNav: function(elem){
		
		this.animCounter += 1;
		jQuery(elem).children('a').stop().animate({ backgroundPosition: '(0 -60px)', color: '#ff0066' }, { duration: this.headerSpeed });
		jQuery(elem).children('ul').css({position: 'relative'}).slideUp(this.toggleNavSpeed, function(){ AS.sliderNav.animCounter -= 1; jQuery(this).css({position:'static'}); });
		
	},
	
	slideRight: function(elem){
		
		if(!this.animCounter > 0)
		{
			indentPos = parseInt(jQuery(elem).attr('startPos'));
			newPos = indentPos + 8;
			jQuery(elem).stop().animate({
				backgroundPosition: '(0 0)',
				textIndent: newPos + 'px',
				color: '#333333'
			}, { duration: this.slideSpeed });
		}
	
	},
	
	slideLeft: function(elem){
		
		if(!this.animCounter > 0)
		{
			resetPos = jQuery(elem).attr('startPos');
			jQuery(elem).stop().animate({
				backgroundPosition: '(-240px 0)',
				textIndent: resetPos + 'px',
				color: '#ff0066'
			}, { duration: this.slideSpeed });
		}
		
	}
};

/* homepage switcher
-------------------------------------------------------------------------*/
AS.switcher = {
	
	selectedDisplay: '',
	
	init: function(){

		// Cache Images
		// jQuery('<img src="assets/css/annsummers/img/bg-lingerie.jpg" width="1" height="1px" style="visibility: hidden;" />').appendTo('body');
		// jQuery('<img src="assets/css/annsummers/img/bg-sextoys.jpg" width="1" height="1px" style="visibility: hidden;" />').appendTo('body');
		// jQuery('<img src="assets/css/annsummers/img/bg-knickerbox.jpg" width="1" height="1px" style="visibility: hidden;" />').appendTo('body');
		
		// enable rollover buttons
		jQuery('#mini-nav li:not(.selected) a').addClass('enabled');
		jQuery('#mini-nav li.selected a').css('left', '10px');
		
		this.selectedDisplay = jQuery('#mini-nav li.selected a').attr('href').replace('#', '');
		
		// bind event clicks for slider and navigation
		this.bindEventHandlers();
		
	},
	
	bindEventHandlers: function(){
		
		// bind events
		jQuery('#mini-nav li a').bind('mouseover', function(e){ AS.switcher.slideRight(e.target); });
		jQuery('#mini-nav li a').bind('mouseout', function(e){ AS.switcher.slideLeft(e.target); });
		jQuery('#mini-nav li a').bind('click', function(e){ AS.switcher.switchDisplay(e); return false; });
		
	},
	
	slideRight: function(elem){
		
		// slide right if enabled
		if( jQuery(elem).hasClass('enabled') )
			jQuery(elem).stop().animate({'left': '10px'}, {duration: 500});
	
	},
	
	slideLeft: function(elem){
		
		// slide left if enabled
		if( jQuery(elem).hasClass('enabled') )
			jQuery(elem).stop().animate({'left': '0'}, {duration: 500})
	
	},
	
	switchDisplay : function(e){
		
		// get the display target
		display = jQuery(e.target).attr('href').replace('#', '');
		
		// switch display
		jQuery('body').removeClass('pg-' + this.selectedDisplay).addClass('pg-' + display);
		jQuery('#intro-' + this.selectedDisplay).css('visibility', 'hidden');
		jQuery('#intro-' + display).css('visibility', 'visible');
		jQuery('#mini-' + display).addClass('selected');
		jQuery('#mini-' + display + ' a').removeClass('enabled');
		
		// set nav
		this.switchNav(display, this.selectedDisplay);
		
		// store current display
		this.selectedDisplay = display;
		
	},
	
	switchNav: function(switchOn, switchOff) {
		
		jQuery('#mini-' + switchOff).removeClass('selected');
		jQuery('#mini-' + switchOff + ' a').addClass('enabled');
		
		this.slideLeft('#mini-' + switchOff + ' a');
	}
	
};

/* navigation slider
-------------------------------------------------------------------------
AS.sliderNav = {
	
	// animation speeds
	headerSpeed: 500,
	toggleNavSpeed: 750,
	slideSpeed: 500,
	
	// animation locks
	animCounter: 0,
	
	// initialise menu
	init: function(){
		
		// close the tabs that aren't open and set the styles
		jQuery('#cat-nav li.selected, #cat-nav li:has(li.selected)').addClass('open').siblings().find('ul').hide();
		jQuery('#cat-nav li.selected a, #cat-nav li:has(li.selected) > a').css({ backgroundPosition: '0 0' });
		jQuery('#cat-nav > ul > li:not(.open) a').css({ backgroundPosition: '0 -60px', color: '#ff0066' });
		jQuery('#cat-nav ul ul li:not(.selected) a').each(function()
		{
			indentPos = jQuery(this).css('textIndent').replace('px', '');
			jQuery(this).attr('startPos', indentPos).css({ backgroundPosition: '-240px 0' });
		});
	
		// bind event clicks for slider and navigation
		this.bindEventHandlers();
		
	},
	
	bindEventHandlers: function(){
		
		// bind events
		jQuery('#cat-nav > ul > li a').bind('click', function(e){ AS.sliderNav.toggleNav(e.target); return false; });
		jQuery('#cat-nav ul ul li:not(.selected) a').bind('mouseover', function(e){ AS.sliderNav.slideRight(e.target); });
		jQuery('#cat-nav ul ul li:not(.selected) a').bind('mouseout', function(e){ AS.sliderNav.slideLeft(e.target); });
		
	},
	
	toggleNav: function(elem){
		
		if(!this.animCounter > 0)
		{
			this.openNav(jQuery(elem).parent());
			jQuery(elem).parent().siblings().each(function()
			{
				AS.sliderNav.closeNav(this);  
			});
		}
	},
	
	openNav: function(elem){
		
		this.animCounter += 1;
		jQuery(elem).children('a').stop().animate({ backgroundPosition: '(0 0)', color: '#ffffff' }, { duration: this.headerSpeed });
		jQuery(elem).children('ul').slideDown(this.toggleNavSpeed, function(){ AS.sliderNav.animCounter -= 1; });
												
	},
	
	closeNav: function(elem){
		
		this.animCounter += 1;
		jQuery(elem).children('a').stop().animate({ backgroundPosition: '(0 -60px)', color: '#ff0066' }, { duration: this.headerSpeed });
		jQuery(elem).children('ul').slideUp(this.toggleNavSpeed, function(){ AS.sliderNav.animCounter -= 1; });
		
	},
	
	slideRight: function(elem){
		
		if(!this.animCounter > 0)
		{
			indentPos = parseInt(jQuery(elem).attr('startPos'));
			newPos = indentPos + 8;
			jQuery(elem).stop().animate({ backgroundPosition: '(0 0)', textIndent: newPos + 'px' }, { duration: this.slideSpeed });
		}
	
	},
	
	slideLeft: function(elem){
		
		if(!this.animCounter > 0)
		{
			resetPos = jQuery(elem).attr('startPos');
			jQuery(elem).stop().animate({ backgroundPosition: '(-240px 0)', textIndent: resetPos + 'px' }, { duration: this.slideSpeed });
		}
		
	}
};
*/
