/**
 * BLAU.DE Create global and section namespaces.
 */

var BLAU = {};
	BLAU.SERVICE = {};

var SHARED = {};
 	SHARED.PROCESS = {};

/**
 * Blau.Service: initMoreSearchResults Hide more search results info/link and
 * just show them.
 */
BLAU.SERVICE.initMoreSearchResults = function() {
	jQuery(document).ready(function() {
		if (jQuery('#moreSearchResults') && jQuery('#moreSearchResultsLink'))
		{
			jQuery('#allSearchResultsInfo').hide();
			jQuery('#firstSearchResultsInfo').show();
			jQuery('#moreSearchResults').hide();
			jQuery('#moreSearchResultsLink').show();
			
			// Activate button for more search results
			jQuery('#moreSearchResultsLink > a').click(function(event) {
				event.preventDefault();
				
				jQuery('#firstSearchResultsInfo').hide();
				jQuery('#allSearchResultsInfo').show();
				jQuery('#moreSearchResultsLink').hide();
				jQuery('#moreSearchResults').show();
			});
		}
	});
}


BLAU.SERVICE.initTree = function(accordionClassName) {
	$(document).ready(function() {
		// hide all sub items initially.
		$('#' + accordionClassName + ' tr[class != "level0"][id ^= "child"]').hide();
		
		$('#' + accordionClassName + ' tr > td:first-child > a').click(function(event) {
			event.preventDefault();
			
			thisId = $(this).parents('tr').attr('id');
			subItems = $(this).parents('tr').nextAll('tr[id *= "' + thisId + '"]');

			nextClass = $(this).parents('tr').next().attr('class');
			directSubItems = subItems.filter('[class = "'+nextClass+'"]');
			
			if($(this).hasClass('on')){
				subItems.hide();
				subItems.find('a.on').removeClass('on');
			}else{
				directSubItems.show();
			}
			$(this).toggleClass('on');
		});
	});
}

/**
 * Blau.Service: processRating Hide rating form and show "Thank You" message
 * after rating an FAQ question.
 * 
 * @param linkId
 *            The id of the rating link
 */
BLAU.SERVICE.processRating = function(linkId) {
	jQuery('#' + linkId)
		.parents('div.form').hide()
		.next('.thankyou').show();
}



// target name of the window to open
var POPUP_DEFAULT_TARGET = "_blank";
// features, i.e. options of the window to open
var POPUP_DEFAULT_FEATURES = "location=0,menubar=0,resizable=1,scrollbars=1,status=0,titlebar=1,toolbar=0,screenX=0,left=0,screenY=0,top=0";

var POPUP_SIZE_SMALL = new Array(400, 400);
var POPUP_SIZE_MEDIUM = new Array(600, 400);
var POPUP_SIZE_BIG = new Array(800, 600);
var POPUP_SIZE_DEFAULT = POPUP_SIZE_BIG; 
var POPUP_SIZE_XXL = new Array(1024, 768);
var POPUP_SIZE_FAQ = new Array(1000, 600);

/**
 * Opens a window with given url as popup.
 * 
 * @param url
 *            The URL to open.
 * @param windowName
 *            The name of the window.
 * @param width
 *            The width of the window (optional).
 * @param height
 *            The height of the window (optional).
 * @returns The opened window object
 */
function openWindow(url, windowName, width, height) {
	var features = "width=" + width + ",height=" + height + "," + POPUP_DEFAULT_FEATURES;
	var newWin = window.open(url, windowName, features);
	newWin.focus();
	return newWin;
}

/**
 * Opens an anchor as popup window. (aka BlauPopupOpenr) Use this from event
 * handlers inside <a> tags
 * 
 * @param anchorElement
 *            The <a>-element.
 * @param size
 *            The width and height of the window to open as two-dimensional
 *            array (optional)
 * @returns false if window has been opened, true otherwise (value should be
 *          used as return value inside event handler)
 */
function popup(aEl, size) {
	if (size == null) size = POPUP_SIZE_DEFAULT;
	var windowName = aEl.getAttribute("target") ? aEl.getAttribute("target") : POPUP_DEFAULT_TARGET;
	var newWin=openWindow(aEl.getAttribute("href"), windowName, size[0], size[1]);

	return !((newWin != null) && (newWin != "undefined"));
}

/**
 * Force merchant to confirm selection of manual convenience.
 */
function confirmManualConvenience() {
	return true;
}

/**
 * Hides a row with given id.
 * 
 * @deprecated Use new switchElement mechanism instead.
 */
function hiderow(id) {
	document.getElementById(id).className = "hide";
}

/**
 * Shows a row with given id.
 * 
 * @deprecated Use new switchElement mechanism instead.
 */
function showrow(id) {
	document.getElementById(id).className = "";
}

 /**
	 * Initializes elements.
	 * 
	 * @param idCollapsedVersion
	 *            The id of the element of the 'collapsed' version.
	 * @param idExpandedVersion
	 *            The id of the element of the 'expanded' version.
	 * @param idCheckbox
	 *            The id of the checkbox element to get the current state.
	 */
 function initSwitch(idCollapsedVersion, idExpandedVersion, idCheckbox) {
 	if (document.getElementById(idCheckbox).checked) {
 		hide(idCollapsedVersion);
 		show(idExpandedVersion);    
 	} else {
 		hide(idExpandedVersion);
 		show(idCollapsedVersion);
 	}
 }

 /**
	 * Shows an element..
	 * 
	 * Additionally this method allows showing a set of elements, simply use
	 * multiple ids as arguments, e.g. show('thisElement', 'thatElement');
	 * 
	 * @param id
	 *            The id of the element to show.
	 */
 function show(id) {
 	for (var i = 0; i < arguments.length; i++) {
 		var element = document.getElementById(arguments[i]);
 		if (element) {
 			if (element.className == "container") {
 				element.style.display = "block";
 			} else {
 				element.style.display = "inline";
 			}
 		}
 	}
 }

 /**
	 * Hides an element, i.e. sets the class to 'hidden'.
	 * 
	 * Additionally this method allows hiding a set of elements, simply use
	 * multiple ids as arguments, e.g. hide('thisElement', 'thatElement');
	 * 
	 * @param id
	 *            The id of the element to hide.
	 */
 function hide(id) {
 	for (var i = 0; i < arguments.length; i++) {
 		var element = document.getElementById(arguments[i]);
 		if (element) {
 			element.style.display = "none";
 		}
 	}
 }

 /**
	 * Toggles the visibilities sets hidden elements to show and vice versa.
	 * Note that this functionality is based on the "show" and "hide" function
	 * in this script.
	 * 
	 * Additionally this method allows toggling a set of elements, simply use
	 * multiple ids as arguments, e.g. toggleVisibility('thisElement',
	 * 'thatElement');
	 * 
	 * @param id
	 *            The id of the element to toggle visibility.
	 */
 function toggleVisibility(id) {
 	for (var i = 0; i < arguments.length; i++) {
 		var element = document.getElementById(arguments[i]);
 		if (element) {
 			if (element.style.display == "none") {
 				show(element.id);
 			} else {
 				hide(element.id);
 			}
 		}
 	}
 }

 /**
	 * Hides or shows one element using the hide or show function.
	 * 
	 * @param id
	 *            The id of the element to hide/show.
	 * @param shouldShow
	 *            if true shows the element, if false hides it.
	 */
 function showHide(Id, shouldShow) {
 	if (shouldShow) {
 		show(Id);
 	} else {
 		hide(Id);
 	}
 }

 // Stores div IDs for radio group switching
 var switchGroups = new Array();

 /**
	 * Initializes a radio group for switching.
	 */
 function initRadioSwitchGroup(radioGroupForm, radioGroupName) {
 	var radioGroup = radioGroupForm[radioGroupName];
 	var switchGroup = switchGroups[radioGroupName];
 	for (i in switchGroup) {
 		radioButtonId = switchGroup[i][0];
 		if (document.getElementById(radioButtonId).checked) {
 			radioSwitch(radioGroupName, radioButtonId);
 		}
 	}
 }

 /**
	 * Adds an id to switch to a radio group.
	 */
 function addToRadioSwitchGroup(radioGroupName, radioButtonId, expandedFirst, expandedSecond, collapsedFirst, collapsedSecond) {
 	if (switchGroups[radioGroupName] == null) {
 		switchGroups[radioGroupName] = new Array();
 	}
 	var radio = new Array();
 	radio[0] = radioButtonId;
 	radio[1] = new Array(expandedFirst, expandedSecond);
 	radio[2] = new Array(collapsedFirst, collapsedSecond);
 	switchGroups[radioGroupName].push(radio);
 }

 /**
	 * Switches a radio group to the current selected radio button.
	 */
 function radioSwitch(radioGroupName, radioExpandedId) {
 	var radios = switchGroups[radioGroupName];
 	for (i in radios) {
 		var radioId = radios[i][0];
 		var expanded = radios[i][1];
 		var collapsed = radios[i][2];
 		if (radioId == radioExpandedId) {
 			show(expanded[0]);
 			show(expanded[1]);
 			hide(collapsed[0]);
 			hide(collapsed[1]);
 		} else {
 			hide(expanded[0]);
 			hide(expanded[1]);
 			show(collapsed[0]);
 			show(collapsed[1]);
 		}
 	}
 }

 // nationality dependent passport/idCard input fields
 var currentNationality;
 function onNationalityChange(isoCode) {
 	if (isoCode == "DE") {
 		hide("passportDiv", "passportHelpDiv");
 		show("idCardDiv", "idCardHelpDiv");
 	}
 	else {
 		hide("idCardDiv", "idCardHelpDiv");
 		show("passportDiv", "passportHelpDiv");
 	}
 }

 /**
	 * Prevents multiple clicks on a submit element. This should be used in
	 * <code>onclick</code> handler: The submit button is disabled and the
	 * proper form is submitted.
	 */
 function preventMultipleClicks(submitElement) {
 	if (submitElement.internalDisabled) {
 		return false;
 	}
 	else {
 		submitElement.internalDisabled = true;
 		return true;
 	}
 }

 /**
	 * Cross-Browser eventListener by someone guy called Scott Andrew
	 */
 function addEvent(obj, eventType, callFunction, useCaption)
 {
 	if (obj.addEventListener) {
 		obj.addEventListener(eventType, callFunction, useCaption);
 		return true;
 	} else if (obj.attachEvent) {
 		var retVal = object.attachEvent("on"+eventType, callFunction);
 		return retVal;
 	} else {
 		return false;
 	}
 }

 /**
	 * Create cookie
	 */
 function createCookie(name,value,days) {
 	if (days) {
 		var date = new Date();
 		date.setTime(date.getTime()+(days*24*60*60*1000));
 		var expires = "; expires="+date.toGMTString();
 	}
 	else var expires = "";
 	document.cookie = name+"="+value+expires+"; path=/";
 }

 /**
	 * Read cookie
	 */
 function readCookie(name) {
 	var nameEQ = name + "=";
 	var ca = document.cookie.split(';');
 	for(var i=0;i < ca.length;i++) {
 		var c = ca[i];
 		while (c.charAt(0)==' ') c = c.substring(1,c.length);
 		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
 	}
 	return null;
 }
