// JavaScript Document

//Google Analytics code
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-26059137-1']);
_gaq.push(['_setDomainName', 'erni-baer.de']);
_gaq.push(['_trackPageview']);

(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();

//Site code
var recent_target = '';
var active = false; //verhindert, dass eine neue Ajax-Anfrage abgeschickt wird, während einer vorherige noch gar nicht beendet ist; ansonsten würden z. B. mehrere Inhalte hintereinander geladen, ohne dass zunächst der Vorgänger entfernt würde

$(document).ready(function(){
		
	$('#menu').transBox('#F00', 75).center();
	
	$.history.init(function(hash){
							
		if(hash == "") {
			// initialize your app
		} else {
			
			hashSort(hash);	
			
		}
		
	}, { unescape: ",/" });

	
});



function hashSort(hash){
	
	if(active === false){
			
		if(hash != recent_target){
			
			switch(hash){
				
				case 'aktuell':	$('#menu').getContent(700, 650, 'php/aktuell.php', '');
								break;
								
				case 'biografie':
				case 'konzept':	$('#menu').getContent(700, 650, 'php/getText.php', 'kennung=' + hash);
								break;
								
				case 'links':	$('#menu').getContent(500, 350, 'php/getLinks.php', '');
								break;
								
				case 'werke':	$('#menu').getContent(630, 650, 'php/exponate.php', '');
								break;
								
				case 'nichtvonmir':	$('#menu').getContent(630, 650, 'php/getNVM.php', '');
								break;
								
				case 'gaestebuch':	$('#menu').getContent(570, 450, 'php/guestbook.php', '');
								break;
								
				case 'gb_form':	$('#menu').getContent(570, 450, 'php/gb_form.php', '');
								break;
								
				case 'lang_de':
				case 'lang_en':	$.get('php/lang.php', 'lang=' + hash.substr(5), function(answer){
																						 
									if(answer == 1)	history.back();
									
								});
								break;						
								
				default:	$('#menu').getContent(500, 200, 'php/urc.php', '');
							break;
															
	
							
			}		
			recent_target = hash;
		
		}else{
			
			//alert(hash + ' = '.recent_target);
			
			if($('#menu').children('#content').length > 0) $('#menu').children('#content').detach();
			var new_margin_top = ($(window).height() / 2) - (31 / 2);		
			$('#menu').animate({'height': 31, 'width': 800, 'margin-top': new_margin_top}, 300);
			recent_target = '';
			
		}
	
	}
	
}

$('.thumbnail').live('click', function(){
	
	$('#thumbnailtable').hide();
	$('#content').removeClass('transparent').addClass('green').details($(this));
	
	
});

$('#next').live('click', function(){
	$('#content').details('#thumbnail_' + $(this).data('referendum'));
});

$('#prev').live('click', function(){
	$('#content').details('#thumbnail_' + $(this).data('referendum'));								  
});

$('#back').live('click', function(){

	$('#detail').detach();
	$('#content').removeClass('green').addClass('transparent');	
	$('#thumbnailtable').show();

});

jQuery.fn.details = function(imgObj){
	
	//hängt die Details des Bildes ein, anwendbar für #content
	this.addClass('loading smallgreen');
	(this.children('#detail').length > 0) ? $('#detail').empty() : $('<div id="detail"></div>').appendTo(this);
	
	if($(imgObj).data('titel') != undefined){
		
		$('<h1></h1>').html($(imgObj).data('titel')).appendTo('#detail');
		if($(imgObj).data('jahr') != 0) $('<h2></h2>').html($(imgObj).data('jahr')).appendTo('#detail');
		
	}
	
	$('<img />').attr('id', 'large_' + $(imgObj).data('index')).attr('src', $(imgObj).data('path')).addClass('large').appendTo('#detail').hide();	
	$('#large_' + $(imgObj).data('index')).load(function(){
		
		var m_left = ($('#detail').innerWidth() / 2) - ($('#large_' + $(imgObj).data('index')).outerWidth() / 2);
		$('#large_' + $(imgObj).data('index')).css('margin-left', m_left);
		
		if($(imgObj).data('titel') == undefined){
			
			var m_top = ($('#content').innerHeight() / 2) - ($('#large_' + $(imgObj).data('index')).outerHeight() / 2) - 5;
			$('#large_' + $(imgObj).data('index')).css('margin-top', m_top);
			
		}		
		$('#large_' + $(imgObj).data('index')).show();		
		$('#content').removeClass('loading smallgreen').css('background-image', 'none');
			
	});
	
	here = Number($(imgObj).data('index'));
	prev = here - 1;
	next = here + 1;
	
	if(here > 1) $('<img id="prev" data-referendum="'+prev+'" src="images/pfeil_links.png" />').appendTo('#detail');
	if(here < $('.thumbnail').length) $('<img id="next" data-referendum="'+next+'" src="images/pfeil_rechts.png" />').appendTo('#detail');
	
	$('<a id="back" href="#">zurück</a>').appendTo('#detail');	
	return this;
	
}

jQuery.fn.transBox = function(color, opacity){
	
	//versieht ein div mit einem transparenten Hintergrund

	var op_standard = opacity / 100;
	var op_ie = opacity;
	
	var bb_width = this.outerWidth();
	var bb_height = this.outerHeight();
	
	
	//CSS für das DIV für die Transparenz
	var bb_css = {	'height': '100%',
					'width': '100%',
					'position': 'absolute',
					'top': 0,
					'left': 0,
					'background-color': color,
					'opacity': op_standard,
					'-ms-filter': '"progid:DXImageTransform.Microsoft.Alpha(Opacity=' + op_ie + ')"',
					'filter': 'alpha(opacity=' + op_ie + ')',
					'text-align': 'left',
					'z-index': -1
				};
				
	//CSS für das DIV für den Content
	/*
	cc_css = {	'position': 'absolute',
				'top': 0,
				'left': 0
			};

	var container = $('<div></div>').css(cc_css).html(this.html());
	*/
	var content = this.html();
	var backBox = $('<div></div>').css(bb_css);
	this.empty();
	this.append(content);	
	this.append(backBox);
	this.css('visibility', 'visible');
	return this;
	
}

jQuery.fn.center = function(){
	
	//für ein realtives Element, das bereit per auto in der Horizontalen zentriert wurde
	var top = ($(window).height() / 2) - (this.outerHeight() / 2);
	this.css('margin-top', top);
	return this;
	
}

function gb_form_beforeSubmit(formData, jqForm, options){
	
	var returnValue = true;
	
	jqForm.children('.obligatory').each(function(){
		
		if($(this).val() == ''){
			
			returnValue = false;
			switch($('#gb_form').data('lang')){
				
				case 'de':	var message = 'Feld "' + $(this).prev('label').text() + '" bitte ausfüllen.';
							break;
							
				case 'en':	var message = 'Please fill out field "' + $(this).prev('label').text() + '".';
							break;
							
			}
			alert(message);
			
		}
		
	});
		
	return returnValue;
	
}

function gb_form_success(responseText, statusText, xhr, $form){
	
	document.location.href = '#gaestebuch';
	
}

jQuery.fn.getContent = function(w, h, url, vars){
	
	active = true;
	//wenn die angegebenen Dimensionen größer sind als das Browserfenster, dann weniger
	if(h > $(window).height()) h = $(window).height() - 80;
	if(w > $(window).width()) h = $(window).width() - 20;
	
	//für ein realtives Element, das bereit per auto in der Horizontalen zentriert wurde
	if($('#menu').children('#content').length > 0) $('#menu').children('#content').detach();
	var new_margin_top = ($(window).height() / 2) - (h / 2);
	this.addClass('loading bigred');
	this.animate({'height': h, 'width': w, 'margin-top': new_margin_top}, 300, function(){
		
		$.get(url, vars, function(data){
			
			$('#menu').removeClass('loading bigred');
			$('#menu').append(data);
			$('#content').contentResize(w, h);
			
			//falls Form, dann Formbindung
			if($('#content').children('#gb_form').length > 0){
				
				$('#gb_form').ajaxForm({beforeSubmit: gb_form_beforeSubmit, success: gb_form_success});
				
			}
			
			active = false;
			
		});
		
		
	});	
	return this;
	
}

jQuery.fn.contentResize = function(w, h){
	
	var c_w = w - 80;
	var c_h = h - 100;
	
	this.css({'width': c_w, 'height': c_h});
	return this;
	
}
