/*
 * Javascript Functions
 *
 * Copyright (c) CLICKSPORTS
 * $Rev: 199 $
 * $Author: cs $
 * $Date: 2009-01-15 17:38:25 +0100 (Do, 15 Jan 2009) $
 *
 */

// Set some common attributes
document.observe('dom:loaded', function() {
	setScrollTopLinks();
	getFieldsAllFields();
	createExtLinks('external');
	createJumpMenu('jumpable');
});

function setScrollTopLinks() {
	
	$$('a.scrolltop').each(function(st_link) {

		st_link.observe('click', function(e) {
			elm = Event.element(e);
			Event.stop(e);
			new Effect.ScrollTo('site');
		});

	});
}

function getFieldsAllFields() {
	$$('input.autoclean').each(function(f) {
		new emptyFormField(f);
	});
}

/**
 * Function to show big images in a container.
 */
function setPreviewImages() {
	
	// Hide the big images initially
	$$('ul#image_preview li').invoke('hide');
	$$('ul#image_preview li:first-child').invoke('show');
	
	$$('div.container ul li img').each(function(img) {
		
		img.observe('mouseover', function(e) {
			elm = Event.element(e);
			elm_big = elm.id.replace('_small', '_big');
			$$('ul#image_preview li').invoke('hide');
			$(elm_big).show();
		}.bind(this));
		
	})
	
}

/**
 * Class to empty fields if wanted
 * @param {Object} elm
 */
var emptyFormField = Class.create({
	
	initialize: function(elm) {

		this.elm = $(elm);
		this.oldValue = this.elm.value;
		
		this.elm.observe('click', function(e) {
			val = Event.element(e);
			if(val.value == this.oldValue) val.value='';
		}.bindAsEventListener(this));

		elm.observe('blur', function(e) {
			val = Event.element(e);
			if(val.value.length == 0) val.value=this.oldValue;
		}.bindAsEventListener(this));
	}
});

/**
 * Create external links by 
 * @param {Object} classname
 */
function createExtLinks(classname) {
	
	$$('a.' + classname).each(function(l) {
		l.writeAttribute('target', '_blank');
	});
}

/**
 * Create special effects for special
 * navigation mouseovers.
 */
function createSpecialEffects(){

	// Cache needed elements
	elms = $$('div#products_new ul li a img');
	runSuccess = false;
	
	elms.each(function(slink){
	
		slink.observe('mouseover', function(e) {
			
			runSuccess = false;
			
			if(typeof(this.srcelm) == 'undefined') this.srcelm = Event.element(e)
			fadeArray = elms.without(this.srcelm);

			fadeArray.each(function(ef) {
				if(ef.getStyle('opacity') != 0.3)
				ef.fade({
					duration: 0.5,
					to: 0.3,
					scope: 'specials',
					limit: 1,
					afterFinish: function() { ef.addClassName('finished') }
				});
			});

			this.srcelm.appear({
				duration: 0.5,
				to: 1.0,
				scope: 'specials',
				limit: 1,
				beforeStart: function() { runSuccess = true; }
			});
			if(typeof(timer) != 'undefined') window.clearTimeout(timer)
		});

		slink.observe('mouseout', function(e) {

			if (runSuccess) {

				timer = setTimeout(function() {
					fadeArray.each(function(ef) {
						if(!ef.hasClassName('finished')) return;
						ef.appear({
							duration: 0.5,
							to: 1.0,
							scope: 'specials',
							limit: 1,
							afterFinish: function() { ef.removeClassName('finished') }
						});
					});
				}, 500)
			}			
		});
	});
}

/**
 * Creates a jumpmenu for all select elements with classname of className
 * @param {Object} className
 */
function createJumpMenu(className) {

	// Get all elements and assing an new eventlistener on them
	$$('select.' + className).each(function(jumpmenu) {
		Event.observe(jumpmenu, 'change', function(e) {
			jump_value = Event.element(e).value;
			if(jump_value != "") location = jump_value;
		});
	});
}