var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-90464-5']);
_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);
})();

(function($){
	$(document).ready(function(){
		$("#content").load("content/chronik.php", {}, function() { updateContent(); });
		$("#menu_item_chronik").addClass("active");
		$("[id^='menu_item_']").initMenuItem();
		$.fn.superbgimage.options = {
			z_index: -1,
			randomimage: 1
		};
		$('#background').superbgimage();
		_gaq.push(['_trackEvent', 'page', 'chronik']);
	});
	
	$.fn.initMenuItem = function() {
		this.click(function() {
			$(this).loadContent();
		});
	};
   
	$.fn.loadContent = function() {
		if ($(this).attr('class').toString().substring(0, 12) == "submenu_item") {
			$(".submenu_item").removeClass("active");
			$(this).addClass("active");
			$("#content").load("foto/gallerie.php?folder=" + $(this).attr('id').toString(), {}, function() { updateContent(); });
			_gaq.push(['_trackEvent', 'fotos', $(this).attr('id').toString()]);
		} else {
			var fileName = $(this).attr('id').toString().substring(10);
			if (fileName == 'fotos') {
				$('#submenu').load("foto/menu.php", {}, function() { 
					$(".submenu_item").initMenuItem();
					$(".submenu_item").removeClass("active");
					var firstSubmenuId = $(".submenu_item").attr('id').toString();
					$("#" + firstSubmenuId).addClass("active");
					$('#submenu').show();
					$("#content").load("foto/gallerie.php?folder=" + firstSubmenuId, {}, function() { updateContent(); });
					_gaq.push(['_trackEvent', 'fotos', firstSubmenuId]);
				});
			} else {
				$('#submenu').hide();
				$("#content").load("content/" + fileName + ".php", {}, function() { updateContent(); });
			}
			$("[id^='menu_item_']").removeClass("active");
			$("#" + $(this).attr('id')).addClass("active");
			_gaq.push(['_trackEvent', 'page', fileName]);
		}
		$('#background').nextSlide();
	};
	
	$.fn.sendContactMessage = function() {
		var fieldMissing = false;
		var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		$(this).find('input, textarea').each(function(i) {
			if (this.value.length == 0 || (this.name == 'email' && !filter.test(this.value))) {
				fieldMissing = true;
				$('#' + this.name + '-missing').show();
			} else
				$('#' + this.name + '-missing').hide();
		});
		
		if (!fieldMissing) {
			$(':button').attr({'disabled' : 'disabled', 'value' : 'Sende...' });
			$(':button').text('Sende...');
			$.post($(this).attr('action'), $(this).serialize(), function(result){ $("#content").html(result); });
		}
		return false;
	}

	function updateContent() {
		$("a[rel='lightbox-set']").colorbox({transition:"fade", maxHeight:"100%", current:'{current}/{total}'});
		$("a[id^='menu_item_']").initMenuItem();
		$('#contactForm').submit(function(){ return $(this).sendContactMessage(); });
		$('#skinnerbox').tooltip({tip: '.tooltip', effect: 'fade', position: 'bottom right', offset: [40,200]});
	}
})(jQuery);

