
var HomeRotator = new Class({
	totalimages: null,
	currentimage: null,
	images: null,
	
	initialize: function(path) {
		this.images = $$(path);
		
		this.totalimages = this.images.length;
		this.currentimage = 0;

		//	Set initial state
		if ( this.totalimages >= 1) {
			this.images.each(function(image, index) {
				if (index == this.currentimage) {
					image.setStyle('opacity', '1');
				} else {
					image.setStyle('opacity', '0');
				}
			}.bind(this));
	
			setInterval(this.rotateImage.bind(this), 5000);
		}
	},
	
	rotateImage: function () {
		nextimage = this.currentimage + 1;

		if (nextimage == this.totalimages) {
			nextimage = 0;
		}

		var myElementsEffects = new Fx.Elements([this.images[this.currentimage],this.images[nextimage]], {duration:1500});
		myElementsEffects.start({
		    '0': {
		    	'opacity': [1,0]
		    },
		    '1': {
		        'opacity': [0, 1]
		    }
		});

		this.currentimage = this.currentimage + 1;
		
		if (this.currentimage == this.totalimages) {
			this.currentimage = 0;
		}
	}
});


toggleMenu = function(element, show){
	if (show) {
		$(element).addClass('over');
	}
	else {
		$(element).removeClass('over');
	 }
}

window.addEvent('load', function() {
	if ($('customer_data_placeholder')) {
		var postObject = JSON.encode({
			action:	'EosSimpleParserWrapper',
			templatePath:	'includes/shop/customer_data.tpl'});
	
		var request = new Request.JSON({
			url:	'/sbeos/ajax/JsonCall.php',
			data:	{json: postObject},
			onComplete:	updateCustomerData
		}).send();
	}
});

updateCustomerData = function(response) {
	if (response.success) {
		var dummy = new Element('div');
		dummy.set('html', response.template);
		
		var divs = dummy.getChildren();
		if (divs.length == 2) {
			if($('customer_data_placeholder')){
				divs[0].replaces($('customer_data_placeholder'));
				divs[1].inject($('wishlist'), 'after');
			}
		} else if (divs.length == 3) {
			if($('customer_data_placeholder')){
				divs[0].replaces($('customer_data_placeholder'));
				divs[1].inject($('wishlist'), 'after');
				divs[2].inject($('pocket'), 'after');
			}
		}
	}	
};

window.addEvent('load', function() {
	$$('#search_mini_form .form-button').each(function(item) {
		item.addEvent('click', function() {
			$('search_mini_form').submit();
			return false;
		});
	});
});



makeScrollbar = function(content,scrollbar,handle,horizontal,ignoreMouse){
	var steps = (horizontal?(content.getScrollSize().x - content.getSize().x):(content.getScrollSize().y - content.getSize().y))
	var slider = new Slider(scrollbar, handle, {	
		steps: steps,
		mode: (horizontal?'horizontal':'vertical'),
		onChange: function(step){
			// Scrolls the content element in x or y direction.
			var x = (horizontal?step:0);
			var y = (horizontal?0:step);
			content.scrollTo(x,y);
		}
	}).set(0);
	if( !(ignoreMouse) ){
		// Scroll the content element when the mousewheel is used within the 
		// content or the scrollbar element.
		$$(content, scrollbar).addEvent('mousewheel', function(e){	
			e = new Event(e).stop();
			var step = slider.step - e.wheel * 30;	
			slider.set(step);					
		});
	}
	// Stops the handle dragging process when the mouse leaves the document body.
	$(document.body).addEvent('mouseleave',function(){slider.drag.stop()});
}

var textCharCounter = new Class({
	counterObject: null,
	areaObject: null,
	areaLimit: 350,
	areaCounter: 0,

	initialize: function(areaId, targetId, options) {
		this.areaLimit = options.limit;
		this.areaObject = $(areaId);
		this.counterObject = $(targetId);
		this.counterObject.innerHTML = this.areaLimit;
		this.setCountEvents();
	},

	setCountEvents: function(){
		this.areaObject.addEvent('keyup',this.checkAreaSize.bind(this));
	},

	checkAreaSize: function(){
		this.areaCounter = this.areaObject.get('value').length;
		if(this.areaCounter > this.areaLimit){
			this.counterObject.innerHTML = 0;
			this.areaObject.value = this.areaObject.getValue().substring(0,this.areaLimit);
		}else{
			this.counterObject.innerHTML = this.areaLimit - this.areaCounter;
		}
	}
});
textCharCounter.implement(new Events);
textCharCounter.implement(new Options);



window.addEvent('load', function(){
	var searchInput = $('search');
	
	if ($chk(searchInput)) {
		searchInput.addEvent('focus', function() {
			if(searchInput.get('value') == searchInput.defaultValue) {
				searchInput.set('value', '');
			}
		});
		searchInput.addEvent('blur', function() {
			if(searchInput.get('value') == '') {
				searchInput.set('value', searchInput.defaultValue);
			}
		});
	}
	
	var formMessage = $('confirmation_text');
	if ($chk(formMessage)) {
		$('block_content').setStyle('display', 'none');
	
		var messageText = formMessage.innerHTML;
		
		var newMessageUL = new Element('ul', {'class': 'messages'});
		newMessageUL.set('html', '<li class="success-msg"><ul><li>' + messageText + '</li></ul></li>');
		newMessageUL.inject($('block_title'), 'before');
	};
});