/*
For preloading background image hover states to avoid flickers in IE.
Use if sprites are not an option (i.e. when hit states must be larger thus exposing the other states in the viewable area).
*/
var PreLoadBG = new Class({
	Implements: [Options],
	options:{
		imgArray: [],
		injectEl: null
	},
	initialize: function(options){
		this.setOptions(options);
		this.imgs = this.options.imgArray;
		this.injectEl = this.options.injectEl;
		if(this.imgs != null || this.injectEl != null){this.loadImgs();};
	},
	loadImgs: function(){
		new Asset.images(this.imgs,{
			onComplete: function(){
				this.writeImgs();
			}.bind(this)
		});
	},
	writeImgs: function(){
		new Element('div',{'id':'imgCache','styles':{'display':'none'}}).injectAfter(this.injectEl);
		this.imgs.each(function(item){
			new Element('img',{src:item}).inject('imgCache');
		});
	}
});


/*
	SpriteTabs creates a tabbing mechanism for tabs created with a sprite navigation.
*/
var SpriteTabs = new Class({
	Implements: [Options],
	options:{
		tabEl: null,
		tabHrefs: null,
		contentEls: null,
		bgPositions: null,
		initPosition: '0 0'
	},
	initialize: function(options){
		this.setOptions(options);
		this.tabEl = this.options.tabEl;
		this.tabHrefs = this.options.tabHrefs;
		this.contentEls = this.options.contentEls;
		this.bgPositions = this.options.bgPositions;
		this.initPosition = this.options.initPosition;
		this.setEvents();
	},
	setEvents: function(){
		this.tabHrefs.each(function(item,i){
			item.addEvent('click',function(e){
				e.preventDefault();
				this.hideAll();
				this.tabEl.setStyle('background-position',this.bgPositions[i]);
				this.contentEls[i].setStyle('display','block');
			}.bind(this));
		}.bind(this));
	},
	hideAll: function(){
		this.tabEl.setStyle('background-position',this.initPosition);
		this.contentEls.each(function(el){
			el.setStyle('display','none');
		});
	}
});

/*
	Creates an interface that slides through any number of divs
	Slide can start automatically or by user interaction with previous/next buttons
	Could be used for front-page flash-like sliding articles or for photo slideshows
*/
var slider = new Class({
	Implements: [Events, Options],
	
	options: {
		interval: 9000,
		slideTag : 'div',
		slideWidth : 100,
		slideDuration : 800,
		slideWrapper: null,
		slideContainer : null,
		prevClass : '',
		nextClass : '',
		autoRotate : true
	},
	
	initialize: function(options) {
		this.timer; //timer used for animations
		this.scene = 0;
	
		this.setOptions(options);
		this.interval = this.options.interval;
		this.slideTag = this.options.slideTag;
		this.slideWidth = this.options.slideWidth;
		this.slideDuration = this.options.slideDuration;
		this.slideWrapper = this.options.slideWrapper;
		this.slideContainer = this.options.slideContainer;
		this.prevClass = this.options.prevClass;
		this.nextClass = this.options.nextClass;
		this.autoRotate = this.options.autoRotate;
		
		if ($defined(this.slideWrapper) && $defined(this.slideContainer)) {
			this.numberOfScenes = this.slideContainer.getChildren('div').length;
			
			//sets width of slideContainer div depending on div width and number of scenes
			this.slideContainer.setStyle('width', this.slideWidth*(this.numberOfScenes+1));
			
			//duplicate first div and add it to the end (this will allow div to look like it is sliding left from the last div)
			var firstDiv = this.slideContainer.getElement('div');
			firstDiv.clone().inject(this.slideContainer, 'bottom');
			
			//initialize tween
		    this.slideFx = new Fx.Tween(this.slideContainer, {
				duration: this.slideDuration,
				transition: 'quad:out',
				link: 'chain',
				onComplete: function() {
			        if (this.scene == 0) { 
			            this.slideContainer.setStyle('left', '0');
			        };
			    }.bind(this)
			});
			
			if (this.autoRotate) this.startAnimation();
			
			//create prev/next buttons
			this.prevButton = new Element('a', {
				'href': '#',
				'id': 'previous',
				'class': this.prevClass,
				'html': 'Previous',
				'events': {
					'click': function(e) {
						e.preventDefault();
						this.stopAnimation();
						this.slideToPrev();
					}.bind(this)
				}
			});
			
			this.nextButton = new Element('a', {
				'href': '#',
				'id': 'next',
				'class': this.nextClass,
				'html': 'Next',
				'events': {
					'click': function(e) {
						e.preventDefault();
						this.stopAnimation();
						this.slideToNext();
					}.bind(this)
				}
			});
	
			//inject prev/next buttons
			this.prevButton.inject(this.slideWrapper);
			this.nextButton.inject(this.slideWrapper);
		};

	},
	
	startAnimation: function() {
		this.timer = this.slideToNext.periodical(this.interval, this);
	},
	
	stopAnimation: function() {
		$clear(this.timer);
	},
	
	slideToNext: function() {
		//set nextScene var
	    //if on last scene, loop back to beginning
	    var nextScene;
	    if (this.scene == this.numberOfScenes - 1) { nextScene = 0 }
	    else { nextScene = this.scene + 1 };

		var newPosition = -(this.slideWidth*(this.scene+1));
		this.slideFx.start('left', newPosition);
		
		this.scene = nextScene;
	},
	
	slideToPrev: function() {
		//set nextScene var
	    //if on first scene, loop to end
	    var nextScene = this.scene - 1;
	    
	    if (nextScene < 0) {
	    	nextScene = this.numberOfScenes - 1;
	    	
	    	var endDivPosition = -(this.slideWidth*this.numberOfScenes);
	    	this.slideContainer.setStyle('left', endDivPosition);
	    	
	    	var newPosition = -(this.slideWidth*nextScene);
			this.slideFx.start('left', newPosition);
	    }
	    else {
	    	var newPosition = -(this.slideWidth*(this.scene-1));
			this.slideFx.start('left', newPosition);
	    }

		this.scene = nextScene;
	}

});

function popTaf(url){
	window.open(url,"TAF","width=550,height=450,titlebar=no,toolbar=no,scrollbars=nomenubar=no,resizeable=no");
}

window.addEvent('load',function(){
	if($('ctl00_SPWebPartManager1_g_358a038b_9704_46ce_8b69_4ecf93c611cd_btnSubmit')){
		$('ctl00_SPWebPartManager1_g_358a038b_9704_46ce_8b69_4ecf93c611cd_btnSubmit').addEvent('mouseover',function(){
			$('ctl00_SPWebPartManager1_g_358a038b_9704_46ce_8b69_4ecf93c611cd_btnSubmit').set('src','/honeymaid/assets/images/taf/send-email-over.gif')
		}.bind(this));

		$('ctl00_SPWebPartManager1_g_358a038b_9704_46ce_8b69_4ecf93c611cd_btnSubmit').addEvent('mouseout',function(){
			$('ctl00_SPWebPartManager1_g_358a038b_9704_46ce_8b69_4ecf93c611cd_btnSubmit').set('src','/honeymaid/assets/images/taf/send-email.gif')
		}.bind(this));
	}
});

function sortBy (category) {
	var defaultValue = "R-AVGRATING*1"; //set default
	var urlObject = new URI(window.location.href);
	
	var rsort = urlObject.getData("rsort");
	if (!rsort) rsort = defaultValue;
	if (rsort == category) rsort = rsort.replace("0", "1")
	else rsort = category;
	
	urlObject = urlObject.setData("rsort", rsort);
	location.href = urlObject.toString();
}
