/**
 * jQuery Init.
 * 
 * Wird von jQuery aufgerufen, wenn das Document Model (DOM) vollstaendig geladen wurde.
 * jQuery Plugins, Funktionen etc. sollten erst dann verwendet werden wenn der DOM 
 * vollstaendig geladen wurde.
 */
function init() {

	initFCEproductShow();	
 	initFCEproductHead();	
	initFCEproductOverview();

	// Event Handler fuer Suchbox (Clear)
	jQuery('#toolbar .searchbox input.text').click(function() {
		this.value = '';
	});	

}

// Initial Tab for Productshow
var runFCEproductShow = 1;
var runFCEproductShow_interval = 8000;

// Initial Image for Productshow
var runFCEproductHead = 0;
var runFCEproductHead_interval = 8000;

function initFCEproductShow() {

//	if(!jQuery('.fce-productshow .panes > div')) 
//		return;

	var panes = jQuery('.fce-productshow .panes > div').length;

	// Abbruch, wenn weniger als 2 Tabs da sind
	if(panes <= 1)
		return;

	if(panes > 5)
		panes = 5;

	for(var i = 0; i < panes; i++) {
		var selectorImage = 'fileadmin/templates/images/fce-productshow/tab_' + (i+1) + '.jpg';

		jQuery('.fce-productshow .tabs > ul').append('<li class="tab-image tab-image' + (i+1) + '"><a href="#" onfocus="if(this.blur()){this.blur();}"></a></li>');
	}


	jQuery('.fce-productshow .tabs').tabs('.fce-productshow .panes > div',
		{
			effect: 'slide',
			fadeOutSpeed: 'slow',

			rotate: true,

			onClick: function() {
 				if(runFCEproductShow) {
					jQuery('.fce-productshow .tabs').tabs().stop();
					runFCEproductShow = 0;
				}
			}

		}).slideshow(
		{ 
			interval: runFCEproductShow_interval,
			clickable: false
		});

	// Start slideshow...
	window.setTimeout("jQuery('.fce-productshow .tabs').tabs().play()", runFCEproductShow_interval);
}


function initFCEproductHead() {


	jQuery('.fce-producthead .thumbnails .thumbnail a').click(function(e) {

		e.preventDefault();
	
		this.blur();

		// clicked thumbnail
		var clickedThumb = jQuery(this);

		// display area
		var displayImage = jQuery(this).parent().parent().parent().find('.image');

		// check if there is already a image
		var prevImage = displayImage.find('img');

		if(prevImage) {
			displayImage.css('background-image', "url('" + prevImage.attr('src') + "')");
			displayImage.html('');
		}


		var image = jQuery('<img />');
		image.attr('src', jQuery(this).attr('rel'));
		image.hide();

		var link = jQuery('<a></a>');
		link.attr('href', jQuery(this).attr('href'));
		link.attr('title', jQuery(this).find('img').attr('title'));
		link.append(image);
		
		displayImage.append(link);
		
		image.fadeIn(800);

		displayImage.find('a').fancybox();
	
	});

	jQuery('.fce-producthead .thumbnails .thumbnail:first a').trigger('click');

	// Start sliding
	window.setTimeout("nextFCEproductHead()", runFCEproductHead_interval);

}

function nextFCEproductHead() {
	
	if(runFCEproductHead_interval < 0)
		return;

	var max = jQuery('.fce-producthead .thumbnails > .thumbnail').length;

	// Turn around
	runFCEproductHead++;

	if(runFCEproductHead > max)
		runFCEproductHead = 0;

	
	jQuery('.fce-producthead .thumbnails .thumbnail:eq(' + runFCEproductHead + ') a').trigger('click');

	window.setTimeout("nextFCEproductHead()", runFCEproductHead_interval);

}


function initFCEproductOverview() {

	jQuery('.fce-productoverview .image img').fadeTo('fast', 1.0);

	jQuery('.fce-productoverview').hover(
		// OnMouseOver
		function () {
//			jQuery('.fce-productoverview .image img').fadeTo('fast', 1.0);
/*			jQuery(this).find('.image img:not(:animated)').animate({
				width: "+=30px",
				height: "+=30px",
				right: "-=15px",
				top: "-=30px"
			}, {
				queue: false,
				duration: 350
			});*/

			jQuery(this).find('.image img').animate( {
				opacity: 0.5
			}, {
				queue: false,
				duration: 350
			});
		},

		// OnMouseOut
		function () {
			//jQuery('.fce-productoverview .image img').stop().attr('style', '');

			jQuery(this).find('.image img').stop().animate( {
				opacity: 1.0
			}, {
				queue: false,
				duration: 650
			});
		});

}


// Init Funktion aufrufen.
jQuery(document).ready(init);

