function initPage()
{
	clearFormFields({
		clearInputs: true,
		clearTextareas: true,
		passwordFieldText: false,
		addClassFocus: "focus",
		filterClass: "default"
	});
}
function popup() {
	window.open(this.href, 'gc_popup', 'height=690,width=1070,scrollbars=yes').focus();
}

function clearFormFields(o)
{
	if (o.clearInputs == null) o.clearInputs = true;
	if (o.clearTextareas == null) o.clearTextareas = true;
	if (o.passwordFieldText == null) o.passwordFieldText = false;
	if (o.addClassFocus == null) o.addClassFocus = false;
	if (!o.filterClass) o.filterClass = "default";
	if(o.clearInputs) {
		var inputs = document.getElementsByTagName("input");
		for (var i = 0; i < inputs.length; i++ ) {
			if((inputs[i].type == "text" || inputs[i].type == "password") && inputs[i].className.indexOf(o.filterClass) == -1) {
				inputs[i].valueHtml = inputs[i].value;
				inputs[i].onfocus = function ()	{
					if(this.valueHtml == this.value) this.value = "";
					if(this.fake) {
						inputsSwap(this, this.previousSibling);
						this.previousSibling.focus();
					}
					if(o.addClassFocus && !this.fake) {
						this.className += " " + o.addClassFocus;
						this.parentNode.className += " parent-" + o.addClassFocus;
					}
				}
				inputs[i].onblur = function () {
					if(this.value == "") {
						this.value = this.valueHtml;
						if(o.passwordFieldText && this.type == "password") inputsSwap(this, this.nextSibling);
					}
					if(o.addClassFocus) {
						this.className = this.className.replace(o.addClassFocus, "");
						this.parentNode.className = this.parentNode.className.replace("parent-"+o.addClassFocus, "");
					}
				}
				if(o.passwordFieldText && inputs[i].type == "password") {
					var fakeInput = document.createElement("input");
					fakeInput.type = "text";
					fakeInput.value = inputs[i].value;
					fakeInput.className = inputs[i].className;
					fakeInput.fake = true;
					inputs[i].parentNode.insertBefore(fakeInput, inputs[i].nextSibling);
					inputsSwap(inputs[i], null);
				}
			}
		}
	}
	if(o.clearTextareas) {
		var textareas = document.getElementsByTagName("textarea");
		for(var i=0; i<textareas.length; i++) {
			if(textareas[i].className.indexOf(o.filterClass) == -1) {
				textareas[i].valueHtml = textareas[i].value;
				textareas[i].onfocus = function() {
					if(this.value == this.valueHtml) this.value = "";
					if(o.addClassFocus) {
						this.className += " " + o.addClassFocus;
						this.parentNode.className += " parent-" + o.addClassFocus;
					}
				}
				textareas[i].onblur = function() {
					if(this.value == "") this.value = this.valueHtml;
					if(o.addClassFocus) {
						this.className = this.className.replace(o.addClassFocus, "");
						this.parentNode.className = this.parentNode.className.replace("parent-"+o.addClassFocus, "");
					}
				}
			}
		}
	}
	function inputsSwap(el, el2) {
		if(el) el.style.display = "none";
		if(el2) el2.style.display = "inline";
	}
}
if (window.addEventListener)
	window.addEventListener("load", initPage, false);
else if (window.attachEvent)
	window.attachEvent("onload", initPage);

// open-close init
function initOpenClose() {
	jQuery('.login').OpenClose({
		activeClass:'active',
		opener:'a.login-link',
		slider:'div.login-box',
		effect:'slide',
		animSpeed:300
	});
}

// open-close plugin
jQuery.fn.OpenClose = function(_options){
	// default options
	var _options = jQuery.extend({
		activeClass:'active',
		opener:'.opener',
		slider:'.slide',
		animSpeed: 400,
		animStart:false,
		animEnd:false,
		effect:'fade',
		event:'click'
	},_options);

	return this.each(function(){
		// options
		var _holder = jQuery(this);
		var _slideSpeed = _options.animSpeed;
		var _activeClass = _options.activeClass;
		var _opener = jQuery(_options.opener, _holder);
		var _slider = jQuery(_options.slider, _holder);
		var _animStart = _options.animStart;
		var _animEnd = _options.animEnd;
		var _effect = _options.effect;
		var _event = _options.event;
		if(_slider.length) {
			_opener.bind(_event,function(){
				if(!_slider.is(':animated')) {
					if(typeof _animStart === 'function') _animStart();
					if(_holder.hasClass(_activeClass)) {
						_slider[_effect=='fade' ? 'fadeOut' : 'slideUp'](_slideSpeed,function(){
							if(typeof _animEnd === 'function') _animEnd();
						});
						_holder.removeClass(_activeClass);
					} else {
						_holder.addClass(_activeClass);
						_slider[_effect=='fade' ? 'fadeIn' : 'slideDown'](_slideSpeed,function(){
							if(typeof _animEnd === 'function') _animEnd();
						});
					}
				}
				return false;
			});
			if(_holder.hasClass(_activeClass)) _slider.show();
			else _slider.hide();
		}
	});

}

// close login form after click on submit button and reset form
// -> simulate click on element which opens/closes the form
function hideFormPane() {
	jQuery('a.login-link').trigger('click');
	// reset form AFTER submission
	setTimeout(function(){
    	jQuery('form.login')[0].reset();
  	}, 500 );
  	return true;
}

// Change logos, Big-O and texts depending on the current slide
function changeContensDependingOnCurrentSlide () {

	current = 1;

	// Define Container img.src
	currentSrc = $('.rm_show_1 img').attr('src');

	//I'm sorry. I couldn't find any other reliable way to determine which is the current slide
	if (currentSrc == "/static/img/keyvisuals_start/page-1-1.jpg") {
		current = 1;
	}

	else if (currentSrc == "/static/img/keyvisuals_start/page-2-1.jpg") {
		current = 2;
	}

	else if (currentSrc == "/static/img/keyvisuals_start/page-3-1.jpg") {
		current = 3;
	}

	else if (currentSrc == "/static/img/keyvisuals_start/page-4-1.jpg") {
		current = 4;
	}

	// Change Logos
	$('div.holder.bottom ul').removeClass('current');
	$('div.holder.bottom ul.page'+ current).addClass('current');

	// Change Nav
	$('.teaserNav a').removeClass('current');
	$('.teaserNav a.page'+ current).addClass('current');

	// Change text
	$('.start .visual div').removeClass('current');
	$('.start .visual .page'+ current).addClass('current');

	// Rotate Big-O
	$('.rm_wrapper .big-o').removeClass('page1');
	$('.rm_wrapper .big-o').removeClass('page2');
	$('.rm_wrapper .big-o').removeClass('page3');
	$('.rm_wrapper .big-o').removeClass('page4');
	$('.rm_wrapper .big-o').addClass('page'+current);
}

function prepareRatateimageMenu() {
			$('#rm_next').show();
			$('#rm_prev').show();
	
}

function prepareMouseOverMenuItmes() {
	var visual = $('#main').find('.visual');
	var teaserNav = $('#main').find('.teaserNav');
	var pages = $(visual).children();
	var holderBottom = $(visual).find('.bottom');
	var teaserNavListElements = teaserNav.children();
	var listElements = $('#first').children();
		$($(listElements[0]).children()[0]).attr('src', '/static/img/holztafel.jpg');
		$($(listElements[1]).children()[0]).attr('src', '/static/img/sitzkreis.jpg');
		$($(listElements[2]).children()[0]).attr('src', '/static/img/daumenkind.jpg');
		$($(listElements[3]).children()[0]).attr('src', '/static/img/eis.jpg');
	
	for( var i=0; i<teaserNavListElements.length; i++ ) {
		var link = $(teaserNavListElements[i]).children()[0];
		$(link).mouseover(function(){
			for( var j=0; j<teaserNavListElements.length; j++ ) {
				$($(teaserNavListElements[j]).children()[0]).removeClass('current');
			}
			$(this).addClass('current');
			for( var j=0; j<pages.length; j++ ) {
				$(pages[j]).removeClass('current');
			}
			$(visual).find('.'+$(this).attr('class').split(' ')[0]).addClass('current');
			for( var k=0; k<holderBottom.children().length; k++ ) {
				$(holderBottom.children()[k]).removeClass('current');
			}
			holderBottom.find('.'+$(this).attr('class').split(' ')[0]).addClass('current');
		});
	}
}

$(document).ready(function() {

	initOpenClose();

	// carousel for team-page
	if (typeof($('.carouselPanel').jcarousel) == 'function'){
		$('.carouselPanel').jcarousel({
			wrap:'circular',
			scroll:1,
			buttonNextHTML:'<a class="carousel_next"></a>',
			buttonPrevHTML:'<a class="carousel_prev"></a>'
		});
		$('.gallery').jcarousel({
			wrap:'circular',
			scroll:1,
			buttonNextHTML:'<a class="carousel_next"></a>',
			buttonPrevHTML:'<a class="carousel_prev"></a>'
		});
	}

	if($.browser.msie && parseInt($.browser.version) <= 8) {
		// init mouse over
		prepareMouseOverMenuItmes();
	}
	else {
		// prepare and init rotateImageMenu
		prepareRatateimageMenu();
		
	}

	// populate zoom icon with correct link only on livesite
	if ($('#editor_pane').length == 0) {
		/*get the element zoom*/
		var zoom = $('.img-holder.fancybox .zoom');
		/*do everything for each element found on the page*/
		zoom.each(function(index, elem){
			/*get the jquery-element of elem*/
			elem = $(elem);
			/*save the sibling-element of zoom --> a*/
			var sibling = elem.siblings('a');
			/*clone the a */
			var clone = $(sibling[0]).clone();
			/*append to parent (.img-holder div)*/
			elem.parent().append(clone);
			/*append elem to cloned a*/
			clone.append(elem);
			/*remove the img from cloned a*/
			clone.find('img').remove();
		});
	}

	//Fancybox
	if (typeof $.fn.fancybox != 'undefined') {
	    $(".fancybox a").fancybox({
		'transitionIn'	:	'elastic',
		'transitionOut'	:	'elastic',
		'speedIn'	:       600,
		'speedOut'	:       200,
		'centerOnScroll':	true,
	    'titlePosition' :	'over' //ggf auch 'inside'
	    });
	}
	
	//Open Popup
	$("#menu-popup").click(function (e) {
		window.open($("#menu-popup").attr('href'), 'gc_chart', 'height=600,width=800,scrollbars=yes').focus();
		e.preventDefault();
	});
});
