/*
 * Javascript Functions
 *
 * Copyright (c) CLICKSPORTS
 * $Rev: 57 $
 * $Author: svn-cs $
 * $Date: 2008-08-07 15:27:03 +0200 (Do, 07 Aug 2008) $
 *
 */

function yesorno (question, url) {
    ack = confirm(question);    
    if (ack) document.location.href = url;
}

var ScrollerController = Class.create();

ScrollerController.prototype = {
	
	initialize: function(element) {

		this.hide_scroller = true;
		this.element = element;
		this.controller_div = this.element.up().next();

		Event.observe(this.element, 'mouseover', this.show.bind(this));
		Event.observe(this.element, 'mouseout', this.hide.bind(this));
		Event.observe(this.controller_div, 'mouseover', this.regenerate.bind(this));
		
	},
	
	show: function() {
		if (this.hide_scroller) {

			this.element.setStyle({ opacity: '1.0'});
			this.controller_div.setStyle({ opacity: '1.0'});
			this.controller_div.setStyle({ display: 'block'});
		}
		this.hide_scroller = false;
		if(this.showfunc) this.showfunc.stop();
	},
	
	hide: function() {
		this.showfunc = new PeriodicalExecuter(function(pe) {
			
			if (!this.hide_scroller) {

				if(!this.element.hasClassName('active')) this.element.setStyle({ opacity: '0.3'});
				this.controller_div.setStyle({ opacity: '0.3'});
				this.controller_div.setStyle({ display: 'none'});

				this.hide_scroller = true;
				pe.stop();
			}
			
		}.bind(this), 0.3);
		
	},
	
	regenerate: function() {
		if(this.regenerate_pe) this.regenerate_pe.stop();
		this.regenerate_pe = new PeriodicalExecuter(function(reg_pe) {
			Event.observe(this.controller_div, 'mouseout', this.setFalse.bind(this));
			this.regenerate_pe.stop();
		}.bind(this), 0.2);
		this.hide_scroller = true;
	},
	
	setFalse: function() {
		this.hide_scroller = false;
	}
	
}

/**
 * Build the eventhandler for ajax cart updates
 */
function initialize_products() {

	$$('span.cart_delete').each(function(cart_delete){
		Event.observe(cart_delete, 'click', deleteProduct);
	});
	
	$$('img.inactive').each(function(img_opacity){
		img_opacity.setStyle({ opacity: '0.3'});
	})
	
	$$('input.quantity_sc').each(function setEventListener(input){
		Event.observe(input, 'keyup', recalculate_products_price);
	});

	$$('a.image_preview img').each(function(elm){
		new ScrollerController(elm);
	})

	$$('img.show_help').each(function(showlink){
		Event.observe(showlink, 'click', show_hide_products_help);
	});

}

/**
 * Check if all images in shopping cart have a image set.
 * @param {Object} my_url
 */
function checkMotive(my_url) {

	// Compare number of products with number of activated images.
	call_function = 'setActiveImage';
	url = 'ajax_functions.php';
	param = '';
	param = 'req_function=' + 'validateMotive';
	var is_ok = false;

	new Ajax.Request(
		url, {
			method: 'post',
			parameters: param,
			onSuccess: function(transport) {
				if(transport.responseText.length>0) alert("Bitte wählen Sie für alle Artikel ein Motiv aus.");
				else window.location.href = my_url;
			}
		}
	);

}

/**
 * Function to active the products image of a given container.
 * @param {Object} img_id
 * @param {Object} image
 */
function activate_products_image(img_id, pimage) {

	call_function = 'setActiveImage';
	url = 'ajax_functions.php';
	param = '';
	param = 'req_function=' + call_function;
	param += '&image_id=' + img_id;
	param += '&active_image=' + pimage;

	new Ajax.Request(
		url, {
			method: 'post',
			parameters: param,
			onSuccess: function(transport) {
				
				$$('div#scroller_' + img_id + ' a.image_preview').each(function(activeImage) {
					
					if(activeImage.rel == pimage) {
						
						$$('div#scroller_' + img_id + ' a.image_preview img').each(function(activeImages) {
							
							if(activeImages.hasClassName('active')) {
								
								activeImages.removeClassName('active');
								activeImages.addClassName('inactive');
								activeImages.up(1).removeClassName('active')
								
								new Effect.Fade(activeImages,{
									duration: 0.3,
									from: 1.0,
									to: 0.3,
									queue: 'end'
								});
							}
						})
						
						activeImage.down().removeClassName('inactive');
						activeImage.down().addClassName('active');

						activeImage.up().addClassName('active');
						
						new Effect.Appear(activeImage.down(), {
							duration: 0.3,
							from: 0.3,
							to: 1.0,
							queue: 'end'
						})
						
						
					}
				})
				
			}
		}
	);
	
}

/**
 * Remove a product from shopping cart
 * @param {Object} e
 */
function deleteProduct(e) {
	
	e = Event.element(e);
	pid = e.alt;
	img_pid = e.up().next().innerHTML;
	
	if (confirm('Diesen Artikel entfernen?')) {
	
		// Compare number of products with number of activated images.
		call_function = 'deleteProduct';
		url = 'ajax_functions.php';
		param = '';
		param = 'req_function=' + call_function;
		param += '&pid=' + pid;
		param += '&img_pid=' + img_pid;
	
		new Ajax.Request(
			url, {
				method: 'post',
				parameters: param,
				onSuccess: function(transport) {
				
					new Effect.Parallel([
						new Effect.Fade(e.up(4)),
						new Effect.Fade(e.up(4).next())
					]);
					
					// Next update the total count column
					get_products_totalprice();					
				
				}
			}
		);
		
	}
}

/**
 * Delete a products image
 * @param {Object} pimage
 */
function delete_products_image(img_id, pimage) {

	if(confirm('Dieses Motiv wirklich entfernen?')) {

		call_function = 'deleteImage';
		url = 'ajax_functions.php';
		param = '';
		param = 'req_function=' + call_function;
		param += '&image_id=' + img_id;
		param += '&image_to_delete=' + pimage;
	
		new Ajax.Request(
			url, {
				method: 'post',
				parameters: param,
				onSuccess: function() {
					
					$$('a.image_preview').each(function(elm){
						if(elm.rel == pimage) new Effect.SwitchOff(elm.up(1));
					})
					carousel_ + img_id.updateSize();							
				}
			}
		);
		
	}
}

/**
 * Show or hide the help
 */
function show_hide_products_help(e) {
	
	if(e == undefined) { is_event = false; }
	else is_event = true;
	
	if (is_event) {
		element = Event.element(e);
		$('upload_help').clonePosition(element, { setTop: true, setLeft: false, setWidth: false, setHeight: false, offsetTop: 130 });
		Effect.Appear($('upload_help'));
	} else {
		Effect.Fade($('upload_help'));
	}
	
}

/**
 * Get the products new price (Eventhandler)
 * @param {Object} e
 */
function recalculate_products_price(e) {

	element = e.element();
	counts = element.value;
	pid = element.next().value;

	call_function = 'updatePrice';
	url = 'ajax_functions.php';
	param = '';
	param = 'req_function=' + call_function;
	param += '&pid=' + pid;
	param += '&counts=' + counts;

	new Ajax.Request(
		url, {
			method: 'post',
			parameters: param,
			onSuccess: function(transport) {
				
				// First update the price column
				element.up().next().innerHTML = transport.responseText;
				
				// Next update the total count column
				get_products_totalprice();
			}
		}
	);
	
}

/**
 * Get the products total price
 */
function get_products_totalprice() {

	call_function = 'getTotals';
	url = 'ajax_functions.php';
	param = '';
	param = 'req_function=' + call_function;

	new Ajax.Request(
		url, {
			method: 'post',
			parameters: param,
			onSuccess: function(transport) {
				$('sc_count_total').innerHTML = transport.responseText;
			}
		}
	);

}

