sfHover = function() {
	var sfEls = document.getElementById("mainNavigation").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}


var creativeWork = new Object();
creativeWork.cache 		= new Array();
creativeWork.isRequest	= new Array();

creativeWork.replaceContent = function(uid, elementName, language) {
	if ( elementName === undefined ) elementName = 'menuitem';
	if ( language === undefined) language = 0;
	
	var menuElement			= elementName + uid;
	// var menuElement			= 'menuitem' + uid;
	var widthOfElement		= $(menuElement).getStyle('width').toInt();
	var heightOfElement		= $(menuElement).getStyle('height').toInt();
	var positionOfElement	= $(menuElement).getPosition();
	var xOffset				= 20;
	var yOffset				= 45;
	
	if(Browser.Engine.name == "trident") {
		widthOfElement = $(menuElement).offsetWidth;
		heightOfElement	= $(menuElement).offsetHeight;
		xOffset				= 35;
	}
	
	var newLeftOfElement	= positionOfElement.x + widthOfElement - xOffset;
	var newTopOfElement		= positionOfElement.y - yOffset + (heightOfElement / 2);

	if(elementName == 'menuitem') {

		$('showRoomBox').removeClass('bottomArrow');
		$('showRoomBox').set('styles', {
		    'left': newLeftOfElement + 'px',
		    'top' : newTopOfElement + 'px'
		});
	} else {
		
		var windowHeight	= window.getSize().y;
		
		newTopOfElement		= positionOfElement.y + yOffset + (heightOfElement / 2);
		var bottomOfElemen	= windowHeight - newTopOfElement;
		
		$('showRoomBox').addClass('bottomArrow');

		$('showRoomBox').set('styles', {
		    'left'		: newLeftOfElement + 'px',
		    'top'		:'auto',
		    'bottom'	: bottomOfElemen + 'px'
		});
	}
		
		
	if(creativeWork.cache[uid]) {
		$('showRoomBox').set('html', creativeWork.cache[uid]);
		return true;		
	}

	if(creativeWork.isRequest[uid] == 1) return false;
	
	var req = new Request({
			
		url: '/index.php?id=' + uid + '&border=1&L=' + language,

		onSuccess: function(txt){
			$('showRoomBox').set('html', txt);
			creativeWork.cache[uid]		= txt;
			creativeWork.isRequest[uid]	= 0;
		},

		// Our request will most likely succeed, but just in case, we'll add an
		// onFailure method which will let the user know what happened.
		onFailure: function(){
			$('showRoomBox').set('text', '');
			creativeWork.isRequest[uid]	= 0;
		}

	});

	creativeWork.isRequest[uid]	= 1;
	req.send();
	return true;
}

creativeWork.clearShowRoomContent = function () {

	$('showRoomBox').empty();
	$('showRoomBox').set('styles', {
	    'left': '-999px'
	});
	
	return true;
}


function initNoobSlider(width) {
	
	if(mouseAnimation.isSliderReady() != true) {
		window.setTimeout('initNoobSlider('+width+')', 100);
		return false;
	}

	var nS4 = new noobSlide({
		box: $('slider'),
		items: $$('#slider div'),
		size: width,
		autoPlay: true,
		interval: 10000,
		handles: $$('#controls span'),
		addButtons: {
			previous: $('sliderPrevButton'),
			next: $('sliderNextButton')
		},
		onWalk: function(currentItem,currentHandle){
			this.handles.removeClass('active');
			currentHandle.addClass('active');
		}
	});
	$$('#slider div a').slimbox();
}


var slider_api = new Class({

    initialize: function(){
		this.menu				= $('controls');
		this.prev				= $('sliderPrevButton');
		this.next				= $('sliderNextButton');
		this.mainmenu			= $$('#mainNavigation ul');
		this.isInMainMenu		= false;
		this.currentImageLoad	= 1;
		this.jsImageList		= [];
		this.isReturnTrueSend	= false;
		
		this.mainmenu.each(function(item, index){
		   item.addEvent('mouseenter', function(event){
			   mouseAnimation.setMouseOutStatus();
			   mouseAnimation.isInMainMenu = true;
			});
		   
		   item.addEvent('mouseleave', function(event){
			   mouseAnimation.isInMainMenu = false;
			});
		});
		
		var animSpeed	= 600;
		this.myMenuFx	= new Fx.Tween(this.menu, {duration: animSpeed});
		this.myPrevFx	= new Fx.Tween(this.prev, {duration: animSpeed});
		this.myNextFx	= new Fx.Tween(this.next, {duration: animSpeed});

		this.horizontMin	= 0;
		this.horizontMax	= -30;
		this.verticalMin	= 0;
		this.verticalMax	= -24;
		
	},
    
    mouseInAnimation:function() {
    	if(this.isInMainMenu == true) return;
		if(this.menu.getStyle('top') == (this.verticalMin+'px')) return;
		
		this.myMenuFx.start('top', this.verticalMax, this.verticalMin);
		this.myPrevFx.start('left', this.horizontMax, this.horizontMin);
		this.myNextFx.start('right', this.horizontMax, this.horizontMin);
    },
    
    setMouseInStatus:function() {
    	if(this.isInMainMenu == true) return;

    	this.myMenuFx.cancel();
		this.myPrevFx.cancel();
		this.myNextFx.cancel();

		this.menu.setStyle('top', this.verticalMin);
		this.prev.setStyle('left', this.horizontMin);
		this.next.setStyle('right', this.horizontMin);
    },
    
    mouseOutAnimation:function() {
		if(this.menu.getStyle('top') == (this.verticalMax+'px')) return;

		this.myMenuFx.start('top', this.verticalMin, this.verticalMax);
		this.myPrevFx.start('left', this.horizontMin, this.horizontMax);
		this.myNextFx.start('right', this.horizontMin, this.horizontMax);
    },
    
    setMouseOutStatus:function() {
    	this.myMenuFx.cancel();
		this.myPrevFx.cancel();
		this.myNextFx.cancel();

		this.menu.setStyle('top', this.verticalMax);
		this.prev.setStyle('left', this.horizontMax);
		this.next.setStyle('right', this.horizontMax);
    },
    
    loadNextImage:function() {
    	if(this.currentImageLoad == 1) {
        	$('slider').setStyles({'opacity': 0, 'top': '23px'});
        	$('slider').fade('in');
    	}
    	
    	if(this.jsImageList.length < this.currentImageLoad) return false;
    	$('slider').set('html', $('slider').get('html') + this.jsImageList[this.currentImageLoad]);
    	
    	var sliderWidth	= 0;
    	$$('#slider img').each(function(el){
    		sliderWidth += el.getSize().x;
    	});
    	
    	$('slider').setStyle('width', sliderWidth);
    	this.currentImageLoad += 1;
    },
    
    isSliderReady:function() {
    	if(this.currentImageLoad >= this.jsImageList.length && this.isReturnTrueSend == false) {
    		this.isReturnTrueSend = true;
    		return true;
    	}
    	return false;
    }
 
 });



var crw_accordion = new Class ({
	
    initialize: function(){
		this.status = {
			'true': 'open',
			'false': 'close'
		};
		
		this.slider 		= [];
		this.sliderCookies	= [];
	},
	
	handleCookie : function(sliderElement, clickElement) {
		this.sliderCookies[sliderElement] = Cookie.read(sliderElement);
		if(this.sliderCookies[sliderElement] == 'close') {  
			this.slider[sliderElement].hide();
		} else {
			$(clickElement).addClass('active');
		}
	},
	
	addSlider : function(sliderElement, clickElement) {
		this.slider[sliderElement] = new Fx.Slide(sliderElement, {duration: 1000, transition: Fx.Transitions.Sine.easeOut});

		$(clickElement).addEvent('click', function(e){
			e.stop();
			accordion.slider[sliderElement].toggle();
		});
		
		this.slider[sliderElement].addEvent('complete', function() {
			var myCookie = Cookie.write(sliderElement, status[accordion.slider[sliderElement].open]);
			if(accordion.status[accordion.slider[sliderElement].open] == 'open') {
				$(clickElement).addClass('active');
			} else {
				$(clickElement).removeClass('active');
			}
		});
		
		this.handleCookie(sliderElement, clickElement);
	},
	
	addToBasket : function(prodid, crwid, count) {
		
		var req = new Request({
			url: '/index.php?id=350',
			onSuccess: function(txt){
			},
	
			// Our request will most likely succeed, but just in case, we'll add an
			// onFailure method which will let the user know what happened.
			onFailure: function(){
			}
	
		});
	
		req.send({
		    method: 'post',
		    data: 'cwBasket[prodid]='+prodid+'&cwBasket[crwid]='+crwid+'&cwBasket[action]=add&cwBasket[count]='+count
		});
	}
	
});

var accordion	= new crw_accordion();

function startAccordion() {
	
	var myElements 	= $$('.accordionContainer');
	var itemCount	= 0;
	myElements.each(function(item) {
		var header 	= item.getElement('h2.accordionHeader').get('id');
		var content	= item.getElement('div.accordionContent').get('id');
		accordion.addSlider(content, header);
		if(itemCount > 1) {
			accordion.slider[content].hide();
			$(header).removeClass('active');
		}
		itemCount	+= 1;
	});
	
}


var LazyLoad = new Class({

	Implements: [Options,Events],

	/* additional options */
	options: {
		range: 200,
		image: 'blank.gif',
		resetDimensions: true,
		elements: 'img',
		container: window,
		fireScroll: true, /* keeping for legacy */
		mode: 'vertical',
		startPosition: 0
	},

	/* initialize */
	initialize: function(options) {

		/* vars */
		this.setOptions(options);
		this.container = document.id(this.options.container);
		this.elements = $$(this.options.elements);
		var axis = (this.options.mode == 'vertical' ? 'y': 'x');
		this.containerDimension = this.container.getSize()[axis];
		this.startPosition = 0;

		var offset = (this.container != window && this.container != document.body ? this.container : "");

		/* find elements remember and hold on to */
		this.elements = this.elements.filter(function(el) {
			var elPos = el.getPosition(offset)[axis];
			/* reset image src IF the image is below the fold and range */
			if(elPos > this.containerDimension + this.options.range) {
				el.store('oSRC',el.get('src')).set('src',this.options.image);
				if(this.options.resetDimensions) {
					el.store('oWidth',el.get('width')).store('oHeight',el.get('height')).set({'width':'','height':''});
				}
				return true;
			}
		},this);

		/* create the action function */
		var action = function() {
			var cpos = this.container.getScroll()[axis];
			if(cpos > this.startPosition) {
				this.elements = this.elements.filter(function(el) {
					if((cpos + this.options.range + this.containerDimension) >= el.getPosition(offset)[axis]) {
						if(el.retrieve('oSRC')) { el.set('src',el.retrieve('oSRC')); }
						if(this.options.resetDimensions) {
							el.set({ width: el.retrieve('oWidth'), height: el.retrieve('oHeight') });
						}
						this.fireEvent('load',[el]);
						return false;
					}
					return true;
				},this);
				this.startPosition = cpos;
			}
			this.fireEvent('scroll');
			/* remove this event IF no elements */
			if(!this.elements.length) {
				this.container.removeEvent('scroll',action);
				this.fireEvent('complete');
			}
		}.bind(this);

		/* listen for scroll */
		window.addEvent('scroll',action);
		if(this.options.fireScroll) { action(); }
	}
});


/*-- QuickFinder --*/

function setProductLeftByPercent(percent) {
	
	var wInner	= $('quickfinderProductWrapper').getSize().x;
	var wOuter	= $('wrapWrapper').getSize().x;
	var wDiff	= wInner - wOuter;
	
	if(wDiff < 0) {
		
		var left = -(wDiff / 2);
		$('quickfinderProductWrapper').setStyle('left', left+'px');
		return true;
		
	}

	percent		= (50 - percent) * 2;
	var left 	= -(wDiff / 2);
	left		= left - (left * percent / 100); 
	
	$('quickfinderProductWrapper').setStyle('left', left+'px');
	
	$('quickfinderLeft').setStyle('display', 'block');
	$('quickfinderRight').setStyle('display', 'block');
	$('qfScrollWrapWrapper').setStyle('display', 'block');
}



var quickFinderSlider = new Class ({
	
    initialize: function(){
		this.isMouseInLeftControl 	= false;
		this.isMouseInRightControl 	= false;
		this.position				= 50;
		this.spinSlider				= false;
		
	},

	setMouseInLeft: function (){ 
		this.isMouseInLeftControl	= true;
	},
	
	setMouseOutLeft: function() {
		this.isMouseInLeftControl	= false;
	},
	
	setMouseInRight: function (){ 
		this.isMouseInRightControl	= true;
	},
	
	setMouseOutRight: function() {
		this.isMouseInRightControl	= false;
	},
	
	moveElement: function() {
		if(this.isMouseInLeftControl == true) {
			this.position	-= 0.1;
			if(this.position <= 0) this.position = 0;
		} else if(this.isMouseInRightControl == true) {
			this.position	+= 0.1;
			if(this.position >= 100) this.position = 100;
		}
		
		setProductLeftByPercent(this.position);
		this.spinSlider.set(this.position);
		
	
		window.setTimeout('QFSlider.moveElement()', 10);
	},
	
	start: function() {
		var sliderLeft = $('quickfinderLeft');
		var sliderRight = $('quickfinderRight');
		
		sliderLeft.addEvent('mouseenter', function() { QFSlider.setMouseInLeft(); });
		sliderLeft.addEvent('mouseleave', function() { QFSlider.setMouseOutLeft(); });
		
		sliderRight.addEvent('mouseenter', function() { QFSlider.setMouseInRight(); });
		sliderRight.addEvent('mouseleave', function() { QFSlider.setMouseOutRight(); });
		
		
		var vel = $('quickfinderScrollWrapper');
		this.spinSlider = new Slider(vel, vel.getElement('#quickfinderScroller'), {
			steps: 100,	// There are 35 steps
			range: [0],	// Minimum value is 8
			
			onChange: function(value) {
				setProductLeftByPercent(value);
				QFSlider.position = value;
			}
		});
		
		QFSlider.moveElement();
		
	}
	
});
	
var QFSlider	= new quickFinderSlider();

window.addEvent('domready', function(){

	var iEl 	= $$('#quickfinderProductWrapper .quickfinderProduct');
	if(iEl.length > 0) {
		
		var elcW	= 0;
		iEl.each(function(el) {
			elcW	+= el.getSize().x;
		});
		
		var elW		= elcW + (iEl.length * 22);
		
		$('quickfinderProductWrapper').setStyle('width', elW+'px');
		//$('quickfinderProductWrapper').setStyle('top', '10px');
		
		setProductLeftByPercent(50);
		

		var vel = $('quickfinderScrollWrapper');
		var spinSlider = new Slider(vel, vel.getElement('#quickfinderScroller'), {
			steps: 100,	// There are 35 steps
			range: [0],	// Minimum value is 8
			
			onChange: function(value) {
				setProductLeftByPercent(value);
				QFSlider.position = value;
			}
		});
		
		spinSlider.set(50);

		QFSlider.start();
		
	}
	
});

