/*
 * Javascript Functions
 *
 * Copyright (c) CLICKSPORTS
 * $Rev: 13 $
 * $Author: svn-cs $
 * $Date: 2008-07-16 14:41:01 +0200 (Mi, 16 Jul 2008) $
 *
 */

function yesorno (question, f) {

    ack = confirm(question);
    //if (ack) f.submit();
    if (ack) document.location.href=f;
}


/* Load required javascript objects as soon as the dom becomes available */
function load_subpage(color, tab) {

	// Check if we got the startpage or a subpage
	if(tab.length > 2) {
		is_startpage = false;
	} else {
		is_startpage = true;
		tab = 'factory';
	}

	// Hide the subnavigation-tabs by default
	$$('li.collection_hidden ul').each(function(li){
		li.setStyle({ display: 'none' });
	})

	// Build the merchant search
	$('merchant_country').observe('click', showCountryDropdown);
	$('country_dropdown').descendants().each(function(country){ country.observe('click', switchMerchantCountry) });

	// Configure Tabs
	var navigation_tabs = new ProtoTabs('navigation', { defaultPanel: 'sub_' + tab });

	if(tab=='collection') { $('sub_collection').hide(); }

	// Create the mouseover effects.
	createMainNavigationHandler();

	// Set the event listener to the navigation element
	//Event.observe('collection_link', 'click', showFlashNavigation);
	//Event.observe('collection_link', 'mouseover', showFlashNavigation);

	// Set the default bgColor for all needed elements.
	if($('heading_box')) $('heading_box').setStyle({backgroundColor: color});
	
	// Create the scroller if possible.
	if ($('image_scroller_wrapper')) {
	
		$$('div.previous_button').each(function(scroller){
			scroller.setStyle({
				backgroundColor: color
			});
		});
		
		$$('div.next_button').each(function(scroller){
			scroller.setStyle({
				backgroundColor: color
			});
		});
		
		$$('div.container img').each(function(li){
			li.setStyle({
				borderColor: color
			});
		});
		
		// Load the product carousel
		new UI.Carousel("image_scroller");

		// Event observer for Carousel links
		$$('a.teaser_preview').each(
			function(clink) {
				clink.observe('click', switchTeaser);
			}
		);		
		
	}
	
	// Set MouseX and MouseY per default.
	MouseX = 0;
	MouseY = 0;

	// Needed to get the mouse coordinates
	Event.observe(document, 'mousemove', getMouseCords);
	
	// Check for the hover state all n seconds
	new PeriodicalExecuter(checkMousePosition, 4);
}

/**
 * Create the navigation eventhandlers for
 * automatically changing images
 */
function createMainNavigationHandler() {
	
	//descendants
	$('navigation').childElements(0).each(function(link){
		
		link.down(2).observe('mouseover', switchNavigationElement);
		link.down(2).observe('mouseout', switchNavigationElement);
		
	});
	
}

/**
 * Change the image-tags src to the 
 * alternating hover or regular state.
 * @param {Object} event
 */
function switchNavigationElement(event) {
	
	element = event.element();

	// Alter the image only if it
	// is not the active tab.
	// (set in prototabs.js).
	if (!element.hasClassName('active')) {
		ftype = '.gif';
		src = element.src;
		src = src.replace(ftype, '')
		link_suffix = '_active';
		
		
		// Check if we have the suffix.
		if (src.endsWith(link_suffix)) {
			src = src.replace(link_suffix, '');
			src = src + ftype;
		}
		else {
			src = src + link_suffix + ftype;
		}
		
		element.src = src;
		
	}

}

/**
 * Change the background-color of scroller object
 * @param {Object} id
 */
function scroller_change_bgcolor(id, animate) {

	my_element = $('collections_' + id);
	my_element_bgcolor = collection_bgcolor[id];

	// Hide all other open elements
	$$('li.active').each(function(li){
		li.removeClassName('active');
		
		if (animate == 1) {
			li.down().morph('background-color: #44484B');
		} else {
			li.down().setStyle({ backgroundColor: '#44484B' });
		}
		
		if(li.down(1)) li.down(1).setStyle({ backgroundColor: '#44484B' }).hide();
	});
	
	my_element.addClassName('active');
	
	if (animate == 1) {
		my_element.down().morph('background-color:' + my_element_bgcolor);
		if(my_element.down(1)) my_element.down(1).morph('background-color:' + my_element_bgcolor).show();
	} else {
		my_element.down().setStyle({ backgroundColor: my_element_bgcolor });
		if(my_element.down(1)) my_element.down(1).setStyle({ backgroundColor: my_element_bgcolor }).show();
	}

}

/**
 * Display an arrow on the active element
 */
function scroller_change_arrow(id) {
	
	if (id > 0) {
		my_element = $('collections_' + id);
		
		// Remove all elements with arrow class
		$$('li.arrow').each(function(li){
			li.removeClassName('arrow');
		})
		
		my_element.addClassName('arrow');
	}
}

// Minifunctions for country dropdown show / hide
function showCountryDropdown() { new Effect.Appear($('country_dropdown')); }
function hideCountryDropdown() { new Effect.Fade($('country_dropdown')); }

// Minifunctions for flash show / hide
function showFlashNavigation() { $('sub_collection').show(); /*new Effect.Move($('sub_collection'), { x: 0, y: 0, mode: 'absolute' })*/}
function hideFlashNavigation() { $('sub_collection').hide(); /*new Effect.Move($('sub_collection'), { x: 0, y: -230, mode: 'absolute' })*/}

// Check if the mouseposition is inside the flashmovie
function checkMousePosition() {
	if(MouseX < 620 && MouseY < 205) return true;
	else hideFlashNavigation();
}

// Get the mouseposition
function getMouseCords(e) {
	MouseX = Event.pointerX(e);
	MouseY = Event.pointerY(e);
}

function switchMerchantCountry(event) {
	
	element = event.element();
	$('merchant_country').value = element.title;
	hideCountryDropdown();
	
}

// Function to load pictures from the carousel component to #teaser
function switchTeaser(event) {

	element = event.element();
	plink = element.up();

	// Get the required attributes
	rel = plink.rel;
	file = plink.href;

	// Remove the old tag.
	$('teaser').down().remove();

	// get the type of the file and create a image or swf
	if (rel == "teaser_image") {

		// Create the new image.
		$('teaser').insert({ top: '<img src="' + file + '" alt="' + rel + '" id="teaser_image" style="display: none;" />' });
		
	} else if (rel == "teaser_swf") {
		
		// We need to create a flash show
		$('teaser').insert({ top: '<object id="teaser_image" width="620" height="305" type="application/x-shockwave-Flash" data="' + file + '"><param name="movie" value="' + file + '" /><param name="quality" value="high" /><param name="wmode" value="opaque" /></object>' });
		
	} else return false;

	// If we successfully created our element, lets show it
	new Effect.Appear('teaser_image', { duration: 1.5 });
	
}

function switchFlashnavi(startsegment) {

	fn=$('flashnavi');
	fn.SetVariable("/:startsegment", startsegment);
	fn.SetVariable("/:animate", 1);
}

function emptyField (field, form) { if (form.elements[field].value==original[field]) form.elements[field].value=''; }
function fillField (field, form) { if (form.elements[field].value=="") form.elements[field].value=original[field]; }

