// ofxOnloadFunction.js
$(document).ready(function() {
	$.extend({
		getUrlVars: function(){
			var vars = [], hash;
			var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
			for(var i = 0; i < hashes.length; i++)
			{
				hash = hashes[i].split('=');
				vars.push(hash[0]);
				vars[hash[0]] = hash[1];
			}
				return vars;
			},
				getUrlVar: function(name){
				return $.getUrlVars()[name];
		}
	});	
	// FORM CHECKER
	function validate(formData, jqForm, options) { 
		for (var i=0; i < formData.length; i++) { 
			if (!formData[i].value) { 
				$('#formStatus').css('visibility', 'visible');
				$('#formStatus').css('color', 'red');
				$('#formStatus').html('Bitte alle Pflichtfelder ausf&uuml;llen.'); 
				delay2(function(){
					$('#formStatus').css('visibility', 'hidden');
					//$('#formStatus').css('color', 'white');
					}, 2000);
				return false;
			}
		} 
		$('#formStatus').css('visibility', 'visible');
		$('#formStatus').css('color', 'green');
		$('#formStatus').html('Ihre Nachricht wurde erfolgreich verschickt.');
		delay2(function(){
			$('#formStatus').css('visibility', 'hidden');
			//$('#formStatus').css('color', 'white');
		}, 2000);
	}
    // FORM CHECKER END
	var defaultJson = 'data3.json';
	var defaultLang;
	var defaultcontentsrc;
	var selectedLang;
	var selectedItem;
	var logoLink;
	var selectedLabel;
	var sel = '.';
	var console;
	var menuItem;
	var menuTitles;
	var menuContent = 'menuContent';
	var jsonSelect;
	var closed;
	var windowHeight = $(window).height();
	var windowWidth = $(window).width();
	var windowLimitTop = $('#borderTop').height();
	var windowLimitBottom = $('#borderBottom').height();
	var showtitle_opened = $(window).height() - (windowLimitTop+windowLimitBottom+2);
	var showtitle_count = 5;
	var showtitle_closed = $('.showtitle').css('height');
	var showtitle_maxheight = $('#borderTop').css('height');
	var subMenus = [];

	$.ofxInit = function(clicked){
		var ids =[];
		var labels = [];
		var imgs = [];
		var subs = [];
		var menuEntries = [];
		var menuBottomEntries = [];
	
		$.getJSON('data3.json', function(data) {
			if (clicked == false){
				defaultLang = data['defaults'].startlang;
				selectedItem = data['defaults'].startpage;
				defaultcontentsrc = data[defaultLang].contentsrc;
			} else if (clicked == true){
				defaultLang = selectedLang;
				defaultcontentsrc = data[defaultLang].contentsrc;
			}
			logoLink = data['defaults'].startpage;
			if (defaultLang == 'de'){
				$('#flag_de').css('background-image', 'url(img/flag_de_active.png)');
				$('#flag_en').css('background-image', 'url(img/flag_gb.png)');
			} else if (defaultLang == 'en') {
				$('#flag_en').css('background-image', 'url(img/flag_gb_active.png)');
				$('#flag_de').css('background-image', 'url(img/flag_de.png)');
			}
			$.each(data[defaultLang]['menuTitles'], function(entry){
				menuEntries.push('<li id="' + data[defaultLang]['menuTitles'][entry]['id'] + '" img="' + data[defaultLang]['menuTitles'][entry]['img'] + '" sub="' + data[defaultLang]['menuTitles'][entry]['sub'] + '" label="' + data[defaultLang]['menuTitles'][entry]['label'] +'">'+ data[defaultLang]['menuTitles'][entry]['label'] + '</li>');
			});	
			if ($(this).attr('sub') != 'menuBottom'){
  				$('#menu').html('<ul>' +  menuEntries.join('') + '</ul>');
			}
		});
	}
	var delay = (function(){
		var timer = 0;
		return function(callback, ms){
			clearTimeout (timer);
			timer = setTimeout(callback, ms);
		};
	})();
		var delay2 = (function(){
		var timer = 0;
		return function(callback, ms){
			clearTimeout (timer);
			timer = setTimeout(callback, ms);
		};
	})();
	$(window).resize(function() {
		windowHeight = $(window).height();
		windowWidth = $(window).width();
		showtitle_opened = $(window).height() - (windowLimitTop+windowLimitBottom+2);
		delay(function(){
		if (closed=='false'){
			$('.showtitle').stop().animate({ height: showtitle_opened }, 'fast');
		}
    }, 200);
	});
	$('.alert-content').css('display', 'none');
	console = $.getUrlVar('con');
	
	if (console == 'true'){
		$('.alert-content').css('display', 'block');
	} else if (console == 'false') {
		$('.alert-content').css('display', 'none');
	}
	
	$.getSelectedItem = function(content){
			$('#dataset').load(defaultcontentsrc, function() {
			$('#output1').load(defaultcontentsrc+ ' .' + content);
			$('.showsub').load(defaultcontentsrc+ ' .' + content + '_sub');
		});
	}
	// COLOR TWEENER
	jQuery.fn.showtitleAnim = function() {
	if (closed == 'true'){		
			$(".showtitleBtn").animate( { backgroundColor: 'red' }, 900, function(){
				$(".showtitleBtn").animate( { backgroundColor: 'white' }, 1400)
				.animate( { backgroundColor: 'red' }, 400)
				.animate( { backgroundColor: 'white' }, 400)
				.animate( { backgroundColor: 'red' }, 320)
				.animate( { backgroundColor: 'white' }, 110);
				});
			} else {
				closed = 'true';
			}
		}
	$('.showtitleBtn').showtitleAnim();
	
	$.ofxAlert = function(param1,param2){
		//alert("param1: " + param1 + " - param2: " + param2);
	}

	$('#subMenu1').css('display', 'none');
	$.backstretch("img/close_up2.jpg", {speed: 250});
	$('.showtitleBtn').css('background-image', 'url(img/show_info_closed.png)');
	
	// MENU FUNCTIONS
	$('#logoSushi').live('click', function(event){		
		window.location = 'index.php';
	});
	$('#menu UL LI').live('click', function(event){				
		$('#backstretch').remove();
		$.backstretch($(this).attr('img'),{speed:250});
		
		if($(this).attr('sub') == 'true'){
			$(".sub").fadeIn(250);
		}
		if ($(this).attr('sub') == 'false'){
			$(".sub").fadeOut(250);
		}
		if ($(this).attr('sub') == 'nochange'){
		}
		selectedItem = $(this).attr('id');
		selectedLabel = $(this).attr('label');
		
		if(selectedItem == 'contact'){
			$('#validationForm').css('display', 'block');
		} else {
			$('#validationForm').css('display', 'none');
		}		
		$("div.showtitle").scrollTop(0);
		$.getSelectedItem(selectedItem);
		$('.showtitleBtn').showtitleAnim();
		$('.showtitleBtn').html('<h2>' + selectedLabel + '</h2>');
		$('#validationForm').ajaxForm( { beforeSubmit: validate } ); 	
	});
	// MENU FUNCTIONS
	$('.showsub UL LI').live('click', function(event){				
			$('#backstretch').remove();
			$.backstretch($(this).attr('img'),{speed:250});
			selectedItem = $(this).attr('info');
		if(closed=='true'){
			//$('.showtitle').showtitleAnim();
		}
		if(selectedItem != ""){
			//alert(selectedItem);
			$('#output1').html('<h2>' + selectedItem + '</h2>');
			$('.showtitleBtn').html('<h2>' + selectedItem + '</h2>');
		}
		
		$("div.showtitle").scrollTop(0);
	});
	$('ul.flags LI').live('click', function( event ) {
		selectedLang = $(this).attr('id');
		switch(selectedLang) {
			case 'flag_de': defaultLang = 'de';
			break;
			case 'flag_en': defaultLang = 'en';
			break;
		default: defaultLang = defaultLang;
		}
		selectedLang = defaultLang;
		$.getSelectedItem(selectedItem);
		$.ofxInit(true);
	});
	$('.showtitleBtn').click(function() {
		if (closed=='true'){
				$('.showtitle').stop().animate({ height: showtitle_opened }, 'fast');
				$('.showtitleBtn').css('background-image', 'url(img/show_info_opened.png)');
				closed='false';
			} else if (closed=='false'){
				$("div.showtitle").scrollTop(0);
				$('.showtitle').stop().animate({ height: showtitle_closed }, 'fast');
				$('.showtitleBtn').css('background-image', 'url(img/show_info_closed.png)');
				closed='true';
			}
	});
		jQuery.fn.idle = function(time){
		var i = $(this);
		i.queue(function(){
			setTimeout(function(){
				i.dequeue();
			}, time);
		});
	};
	$.ofxInit(false);
	$.ofxConsole = function(msg){
		$('.alert-content p').html(msg);
	}
	/* SPECIAL IPAD FEATURES 
	jQuery.fn.oneFingerScroll = function() {
		var scrollStartPos = 0;
		$(this).bind('touchstart', function(event) {
			// jQuery clones events, but only with a limited number of properties for perf reasons. Need the original event to get 'touches'
			var e = event.originalEvent;
			scrollStartPos = $(this).scrollTop() + e.touches[0].pageY;
			e.preventDefault();
		});
		$(this).bind('touchmove', function(event) {
			var e = event.originalEvent;
			$(this).scrollTop(scrollStartPos - e.touches[0].pageY);
			e.preventDefault();
		});
			return this;
		};


	$(function() {
		$('.showtitle').oneFingerScroll();
	});
	*/
});
