// JavaScript Document

// PNG fix
var blank = new Image();
blank.src = 'images/blank.gif';

// DOCUMENT LOAD 
$(function() {
	
	// extrahiere p und a
	var loc=" "+window.location;
	//alert("location = "+window.location+"\rlocation.href = "+window.location.href+"\rlocation.hash = "+window.location.hash);
	if(loc.split("=").length==1){
		p=loc+"?p=home";
	}else{
		p=loc.split("=")[1];
		p=p.split("#")[0];
	}
	var a=loc.split("#")[1];
	if (a)
		a = a.replace( /[^a-z0-9_-]/gi , "" );
		
	
	// navigation home	
	$("#navigation_home > a").hover(function() {
										$(this).find("img").css('backgroundColor','#5bad01');
									},
									function() {
										$(this).find("img").css('backgroundColor','#98d20d');
									}
	);
	
	$("#kontakt_formular").validate({
		rules: {
		  kf_name: "required",
		  kf_vorname: "required",
		  kf_email: {
		  	required: true,
		  	email: true
		  },
		  kf_nachricht: {
			required: true
		  }
		}
  	});

	
	// dropdown	
	$("ul.navigation > li").hover(function() {
	$(this).find("ul.subnavigation").slideDown('fast').show();
	$(this).find("ul.subnavigation li:last a").corner("bottom 10px");

	$(this)	.hover(	function() {}, 
						function(){$(this).find("ul.subnavigation").slideUp('fast'); });})
	

	
	// selected link deaktivieren und subnavi schreiben
	$("ul.navigation a").each(function (i) {
		var myP=$(this).attr('href').split("=")[1];
		if(myP==p){
			// parent weiss färben
			if($(this).parent().parent().hasClass('subnavigation')){
				$(this).parent().parent().prev().css('color','#e3f3bd');
			}
			// falls unterpunkte vorhanden dann nur farbe ändern 
			if($(this).next().hasClass('subnavigation')){
				$(this).css('color','#ffffff');
			}else{
			// sonst entferne link
				var mytext=$(this).text();
				$(this).replaceWith('<span>'+mytext+'</span>');
			}
			
		}
	});
	
	// ändere Reihenfolge der Haupnavigation
	var countli = $('ul.navigation > li').length;
	for (i=1;i<countli;i++) {
        $('ul.navigation > li:eq('+(i-1)+')').before($('ul.navigation > li:last'));
    }

	$('ul.subnavigation').css('display','none');
	
	
	// ANFANG CAROUSEL cccccccccccccccccccccccccccccccccc
	
	// pfeile hover-funktion
	$("#pfeil_li").hover(	function(){$(this).fadeTo(200, 0.6);}	, function(){$(this).fadeTo(200, 1);}	);		
	$("#pfeil_re").hover(	function(){$(this).fadeTo(200, 0.6);}	, function(){$(this).fadeTo(200, 1);}	);
	
	// falls anker gesetz ist, wähle entsprechendes zoom aus
	if(a!=undefined){
		var mytempindex=-1;
		// vergleiche jedes list element
		$('#carousel_ul li').each(function (i){
													// lese source attribut aus dem list element
													mysrcArr=$(this).find('a').find('img').attr("src").split("/");
													// zerlege URL in pfad
													mypath=mysrcArr[mysrcArr.length-2];
													// und datei
													myimg=mysrcArr[mysrcArr.length-1];
													// falls anker und thumbnail übereinstimmen
													if(myimg.indexOf(a)>-1){
														// zoom auswählen
														mytempindex=i;
														zeigeProjekt($(this));
														
													}
													
												});
		// ändere Reihenfolge der listen Elemente bis das Ausgewählte am Anfang steht
		if(mytempindex>-1){
			for (i=0;i<mytempindex;i++) {
        		$('#carousel_ul li:last').after($('#carousel_ul li:first'));
    		}
		}
		
	}
	
	// thumbnail hover-funktion
	$('#carousel_ul li').hover(
	
									function(){
													mysrcArr=$(this).find('a').find('img').attr("src").split("/");
													mypath=mysrcArr[mysrcArr.length-2];
													myimg=mysrcArr[mysrcArr.length-1];
													myimg=myimg.replace('0_', "1_");
													$(this).find('a').find('img').attr("src", mypath+'/'+myimg);
												},
									
									function(){
													mysrcArr=$(this).find('a').find('img').attr("src").split("/");
													mypath=mysrcArr[mysrcArr.length-2];
													myimg=mysrcArr[mysrcArr.length-1];
													myimg=myimg.replace('1_', "0_");
													$(this).find('a').find('img').attr("src", mypath+'/'+myimg);
												}
								
								);
	
	// entferne elemente aus random-karussell
	thumbs = $('#carousel_inner_random #carousel_ul').children().size();
	while (thumbs>6){
								zufall=Math.floor(Math.random()*thumbs);
								mysrc=$('#carousel_inner_random #carousel_ul li:eq('+ zufall +') a img').attr("src");
								// falls anker NICHT vorhanden
								if(  mysrc.indexOf(a)==-1 ){
									$('#carousel_inner_random #carousel_ul li:eq('+ zufall +')').remove();
								}
								thumbs = $('#carousel_inner_random #carousel_ul').children().size();
						}
	
	
	// preload images
	MM_preloadImages('images/carousel_pfeil_l_over.png', 'images/carousel_pfeil_r_over.png');
	
	$('#carousel_ul li').each(function (i){
		mysrcArr=$(this).find('a').find('img').attr("src").split("/");
		mypath=mysrcArr[mysrcArr.length-2];
		myimg=mysrcArr[mysrcArr.length-1];
		myimg=myimg.replace('0_', "1_");
		MM_preloadImages(mypath+'/'+myimg);
		myimg=myimg.substr(2);
		MM_preloadImages(mypath+'/'+myimg);
	});
	
	// entferne Pfeile falls nur 3 thumbnails vorhanden
	if($('#carousel_ul').children().size()<=3){
		$("#pfeil_li").remove();
		$("#pfeil_re").remove();
	}

	
	/* ZOOM */
	
	$('#carousel_ul li').click(
		function(){	
						zeigeProjekt( $(this) );		
					}
	);
	
	function zeigeProjekt(ziel){	
										//alert(myziel);
										// lese source attribut aus dem list element
										mysrcArr=ziel.find('a').find('img').attr("src").split("/");
										// zerlege URL in pfad
										mypath=mysrcArr[mysrcArr.length-2];
										// und datei
										myimg=mysrcArr[mysrcArr.length-1];
										// entferne die 5 ersten Zeichen des Dateinamens ("1_04_")
										myanker=myimg.substr(5);
										// entferne die 4 letzten Zeichen des Dateinamens (".jpg")
										myanker=myanker.substring(0, myanker.length-4);
										window.location="#"+myanker;
										// entferne die 2 ersten Zeichen
										myimg=myimg.substr(2);
										$('#zoom').fadeOut(0);
										$('#zoom_inhalt').css('display','none');
										$('#zoom img').attr("src", mypath+'/'+myimg);
										
										// prüfen ob zoombild popup inhalte anzeigen soll
										myhtml=myimg.replace('.jpg','.html');
										
										$.ajax({
														url:mypath+'/'+myhtml,
														/*type:'HEAD',*/
														dataType: 'text',
														error:
															function(x,e){
																//alert(x.status+" : "+e+" :::  "+mypath+'/'+myhtml+" : File does not exist");
																$('#zoom > a').attr("href", "#");
																$('#zoom > a').css('cursor','default');
																
															},
														success:
															function(x,e){
																//alert(x.status+" : "+e+" :::  "+mypath+'/'+myhtml+" : File does exist. Yahoooo!");
																$('#zoom > a').attr("href", mypath+'/'+myhtml);
																$('#zoom > a').css('cursor','pointer');
															}
													});						
										
										// lade projektbeschreibung
										myhtm=myhtml.replace('.html','.htm');
										$("#projektbeschreibung").load(mypath+'/'+myhtm);
										$('#zoom').fadeIn(300);
									
								 }
								  
   // ENDE CAROUSEL cccccccccccccccccccccccccccccccccc 
	
	
	// prettyPopin
	$("a[rel^='prettyPopin']").prettyPopin();
	
	
	// ANFANG NEWS /////////////////////////////////////
	
	// h1 over	
	$("#news h1").hover(function() {
										$(this).css('backgroundColor','#eee');
									},
									function() {
										$(this).css('backgroundColor','#f7f5f5');
									}
	);
	
	$('#news > div > div').hide();
  	$('#news > div:first > div').show();
	$('#news img:first').attr("src", "images/news_pfeil_u.png");
  	$('#news h1').click(
						function() {
									  if(($(this).next().is('div')) && ($(this).next().is(':visible'))) {
										return false;
										}
									  if(($(this).next().is('div')) && (!$(this).next().is(':visible'))) {
											$('#news > div > div:visible').slideUp('normal');
											$('#news h1 > img').attr("src", "images/news_pfeil_r.png");
											$(this).next().slideDown('normal');
											$(this).find('img').attr("src", "images/news_pfeil_u.png");
											return false;
										}
									  }
						);
	// ENDE NEWS ///////////////////////////////////////
	
   // PNG fix
   var badBrowser = (/MSIE ((5\.5)|6)/.test(navigator.userAgent) && navigator.platform == "Win32");
   if (badBrowser) {
	 // get all pngs on page
	 $('img[src$=.png]').each(function() {
	   if (!this.complete) {
		 this.onload = function() { fixPng(this) };
	   } else {
		 fixPng(this);
	   }
	 });
   }
   


	
});

// kunden
$(document).bind('mousemove',function(e){ 
      var relX=(e.pageX-$('#inhalt').offset().left);
	  var relY=(e.pageY-$('#inhalt').offset().top);
	  var myTop=122*Math.floor(relY/122);
	  var myRight=196*(1+Math.floor(relX/196));
	  var myBottom=122*(1+Math.floor(relY/122));
	  var myLeft=196*Math.floor(relX/196);
	  $("#kunden").css('clip','rect('+myTop+'px, '+myRight+'px, '+myBottom+'px, '+myLeft+'px)');
});


// FUNKTIONEN //////////////////////////////////////////////////////////////////////


// PNG fix function
function fixPng(png) {
   // get src
   var src = png.src;
   // set width and height
   if (!png.style.width) { png.style.width = $(png).width(); }
   if (!png.style.height) { png.style.height = $(png).height(); }
   // replace by blank image
   png.onload = function() { };
   png.src = blank.src;
   // set filter (display original image)
   png.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "',sizingMethod='scale')";
}
 
 //slide function  
function slide(where){
	
			//get the item width
			var item_width = $('#carousel_ul li').outerWidth() + 1;
			
			/* using a if statement and the where variable check 
			we will check where the user wants to slide (left or right)*/
			if(where == 'left'){
				//...calculating the new left indent of the unordered list (ul) for left sliding
				$('#carousel_ul').css({'left' : -item_width+'px'});
				$('#carousel_ul li:first').before($('#carousel_ul li:last'));
				var left_indent = parseInt($('#carousel_ul').css('left')) + item_width;
			}else{
				//...calculating the new left indent of the unordered list (ul) for right sliding
				var left_indent = parseInt($('#carousel_ul').css('left')) - item_width;
			}
			
			//make the sliding effect using jQuery's animate function... '
			$('#carousel_ul:not(:animated)').animate({'left' : left_indent},300,function(){    
				
				/* when the animation finishes use the if statement again, and make an ilussion
				of infinity by changing place of last or first item*/
				if(where == 'left'){
					//...and if it slided to left we put the last item before the first item
					//$('#carousel_ul li:first').before($('#carousel_ul li:last'));
					
				}else{
					//...and if it slided to right we put the first item after the last item
					$('#carousel_ul li:last').after($('#carousel_ul li:first'));
					$('#carousel_ul').css({'left' : '0px'});//-196
				}
				
				//...and then just get back the default left indent
				
			});			
			
}

// Bilder vorladen
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
