// JavaScript Document

$(document).ready(function() {
    /*
	CONTENT FADE IN
	aBgImg = $("#contentwrap").css('background-image').split('"')
	$("#contentwrap").after("<img id='contentfade' src='"+ aBgImg[1] +"' alt='' style='position:absolute; top:0px; left:0px; display:block;'>");
	$("#contentwrap").children().css({'display' : 'none'});
	$("#contentfade").remove();
	$("#contentwrap").children().fadeIn(300);
	*/
	
	function hoverIn(menuObj)
	{
		menuObj.find("ul").css({'height' : 'auto'});
		menuObj.find("ul").slideDown("normal");
		menuObj.find("ul").css({'height' : 'auto'});
	}
 
	function hoverOut(menuObj)
	{
		menuObj.find("ul").stop().slideUp(250);
		menuObj.find("ul").css({'height' : 'auto'});
	}
 
	/* HOME - Hover */	
		$("#home").hover(function(){
				$(this).css("background-position", "left bottom");
				hoverIn($(this));
			},
			function(){
				$(this).css("background-position", "left top");
				hoverOut($(this));
			}
		);	
		
	/* REFERENZEN - Hover */	
		$("#referenzen").hover(function(){
				$(this).css("background-position", "-159px bottom");
				hoverIn($(this));
			},
			function(){
				$(this).css("background-position", "-159px top");
				hoverOut($(this));
			}
		);	
		
	/* STUDIEN - Hover */	
		$("#studien").hover(function(){
				$(this).css("background-position", "-398px bottom");
				hoverIn($(this));
			},
			function(){
				$(this).css("background-position", "-398px top");
				hoverOut($(this));
			}
		);		
		
	/* INFO - Hover */	
		$("#info").hover(function(){
				$(this).css("background-position", "-567px bottom");
				hoverIn($(this));
			},
			function(){
				$(this).css("background-position", "-567px top");
				hoverOut($(this));
			}
		);			

	/* KONTAKT - Hover */	
		$("#kontakt").hover(function(){
				$(this).css("background-position", "-666px bottom");
				hoverIn($(this));
			},
			function(){
				$(this).css("background-position", "-666px top");
				hoverOut($(this));
			}
		);			
		
	

	// E-Mail Adresse verschlüsseln
	$("#impressumsmail").html("<a href=\"mailto:info@kreutzbergconcepts.de\" title=\"Katarina Kreutzberg eine E-Mail schicken\">info@kreutzbergconcepts.de</a>");
	
	
	/* CAPTCHA RELOAD */
	$("#captchaimg").click(function() {
		$('#captchaimg').attr('src','/manager/includes/veriword.php?rand='+new Date().getTime());
	});	

	/*
	CONTENT FADE OUT
	var oFadingImg = new Object();
	$("#menu").find("a").each(function (){
			if ($(this).attr("fade") == undefined){
				oFadingImg[$(this).parent().attr("id")] = '/assets/templates/kconcepts/bilder/jquery-fade/kconcepts.gif';
			}else{	
				oFadingImg[$(this).parent().attr("id")] = '/assets/templates/kconcepts/bilder/jquery-fade/' + $(this).attr("fade") + '.gif';
			}	
	});
	
	$("#menu").find("a").click(function() {
		var sParentId = $(this).parent().attr('id');
		var sHref = $(this).attr('href');
		
		$(this).mouseleave();
		
		$("#contentwrap").after("<img id='contentfade' src='"+ oFadingImg[sParentId] +"' alt='' style='position:absolute; top:0px; left:0px; display:none;'>");
		$("#contentwrap").children().fadeOut(500);
		$("#contentfade").fadeIn(600, function (){
			// Callback Function nach fade
			$("#contentfade").remove();
			window.location.href = sHref;

		});
		return false;
	});
	*/
	
	// Externe Links tracken
	$("#wrapper a:not([href^='http://www.kreutzbergconcepts.de'])[href^='http']").click(function (){
		_gaq.push(['_trackEvent', 'externer Link', 'click', $(this).attr('href')]);
	});

	
	/* SLIDESHOW */
		(function ()
			{	
				var SlideShow =
				{
					_uiIndex: -1,
					_runShow: true,
					_imagesTotal: $("#slideshow-content").find('a').length,
					_arrTimings: new Array(600, 3000, 500, 2000), // FadeIn, Anzeigedauer, FadeOut, Zeit bis zum Restart (kann nach belieben angepasst werden)
					_noAnimation: false,
					
					init: function(uiIndex)
					{
						arrNavImages = new Array('/assets/templates/kconcepts/bilder/l.png', '/assets/templates/kconcepts/bilder/r.png');

						$("#slideshow-content").css({position: 'relative', 'background-image' : 'none'});
						$("#slideshow-content").find('a').css({ display : 'none' });
						$("#slideshow-content").find('a').each(function() {
							$("#slideshow-content").append('<img src="'+ $(this).attr('href') +'" alt="'+ $(this).attr('title') +'">');
							arrNavImages.push($(this).attr('href'));
							$(this).remove();
						});
						$("#slideshow-content").find('img').css({ display : 'none' });
						
						$("#slideshow-content").append("<div id=\"slideshow-loader\">&nbsp;</div>");
						
						SlideShow.preload(arrNavImages, function () { 
							$("#slideshow-loader").remove();
							// Navigation einbinden
							$("#slideshow-content").append("<div id=\"slideshow-nav\"></div>");
							$("#slideshow-content").mouseleave(function() {
								if (SlideShow._runShow == false){
									SlideShow.restart();
								}	
							});
							$("#slideshow-nav").html("<div id=\"previmg\"></div> <div id=\"nextimg\"></div>");
							SlideShow.show(uiIndex);
						});
					},

					show: function(uiIndex)
					{

						uiIndex = (uiIndex+this._imagesTotal)%this._imagesTotal;

						// Remove Loader, falls noch nicht geschehen
						$("#slideshow-loader").remove();
						
						$("#previmg").unbind('click').click(function() {
							if (SlideShow._noAnimation){
								$("#slideshow-content").find('img').stop(true,true);
							}
							$("#slideshow-content img:eq(" + uiIndex +")").stop();
							$("#slideshow-content img:eq(" + uiIndex +")").css({opacity: null, display: 'none'});
							SlideShow._runShow = false;
							SlideShow._noAnimation = true;
							SlideShow.show(uiIndex-1);
						});

						$("#nextimg").unbind('click').click(function() {
							if (SlideShow._noAnimation){
								$("#slideshow-content").find('img').stop(true,true);
							}
							$("#slideshow-content img:eq(" + uiIndex +")").stop();
							$("#slideshow-content img:eq(" + uiIndex +")").css({opacity: null, display: 'none'});
							SlideShow._runShow = false;
							SlideShow._noAnimation = true;
							SlideShow.show(uiIndex+1);
						});

						if (!SlideShow._runShow){
							SlideShow.hide();
							if (SlideShow._noAnimation){
								$("#slideshow-content img:eq(" + uiIndex +")").delay(SlideShow._arrTimings[2]);
							}
							$("#slideshow-content img:eq(" + uiIndex +")").fadeIn(SlideShow._arrTimings[0]);

						}else{
							$("#slideshow-content img:eq(" + uiIndex +")").fadeIn(SlideShow._arrTimings[0], function (){
								$(this).delay(SlideShow._arrTimings[1]);
								SlideShow.hide();
							});
						}
						
						SlideShow._uiIndex = uiIndex;
					},
					
					hide: function()
					{
						if (this._uiIndex != -1){
							$("#slideshow-content img:eq(" + this._uiIndex +")").fadeOut(SlideShow._arrTimings[2], function(){
								if (SlideShow._runShow){
									SlideShow.show(SlideShow._uiIndex + 1);
								}else{
									//do nothing
								}
							});
						}
					},

					restart: function()
					{
						restart = window.setTimeout(restartShow, this._arrTimings[3]);
						
						$("#slideshow-content").mouseenter(function(){
							window.clearTimeout(restart)
						});
						
						function restartShow()
						{
							SlideShow._runShow = true;
							SlideShow.hide();
						}
					
					},
					
					preload: function(images, callback) 
					{
						 var cache = [];
						//convert to array if needed so rest of script works
						if (!(images instanceof Array)) {
							images = [images];
						}
						
						var imagesLength = images.length;
						var loadedCounter = 0;
						
						for (var i = imagesLength; i--;) {
							var cacheImage = document.createElement('img');
							//set the onload method before the src is called otherwise will fail to be called in IE
							cacheImage.onload = function(){
								loadedCounter++;
								if (loadedCounter >= imagesLength) {
									if (typeof callback == 'function') {
										callback();
									}
								}
							}
							cacheImage.src = images[i];
							cache.push(cacheImage);
						}
					}					
				};
			
			jQuery.fn.mySlide = function(startImage){
				SlideShow.init(startImage);
			}
			
			})()
			
			$("#slideshow-content").mySlide(0);
		
}); 






