/**
 * @author Jan
 */


$(document).ready(function(){
	initMenu();
	initHome();
	initProductDetail();
	initForms();
	initMaps();
	initSlideshow();
	$("a[rel^='prettyPhoto']").prettyPhoto();
});

function initMenu(){
	if(jQuery.browser.msie){
		$("#top_nav li").hover(function(){
			$(this).addClass("hover");
		},function(){
			$(this).removeClass("hover");
		});
	}
}

function initHome(){
	if($("#slideshow_home").size()){
		/*$("#slideshow_home li").hide().first().show();
		
		var buttons = $("<ul id='slideshow_buttons'></ul>");
		$("#slideshow_home").after(buttons);
		var count = 1;
		$("#slideshow_home li").each(function(){
			$(this).attr("id", "slide_" + count);
			buttons.append("<li><a href='#slide_"+count+"'>"+count+"</a></li>");
			count++;
		});

		
		setInterval("doSlideshowHome()", 5000);
		*/
		
		$("#slideshow_home").slideshow();
	}
}

var prevImage = 1;
var currentSlide = 1;

function doSlideshowHome(){
	/*
	prevImage = currentSlide;
	$("#slideshow_home li").css("z-index", "1");
	currentSlide++;
	if($("#slideshow_home li").size() < currentSlide){
		currentSlide = 1;
	}
	$("#slideshow_buttons li[href=#]")
	$("#slideshow_home li").stop();
	$("#slideshow_home li:nth-child("+currentSlide+")").css({"z-index":"2", "margin-left":"960px"});
	$("#slideshow_home li:nth-child("+currentSlide+")").animate({"margin-left":"0"}, {duration:300, complete:function(){
		$("#slideshow_home li").hide();
		$("#slideshow_home li:nth-child("+currentSlide+")").show();
	}});
	$("#slideshow_home li:nth-child("+prevImage+")").animate({"margin-left":"-960px"}, {duration:300});
	$("#slideshow_home li:nth-child("+currentSlide+")").show();
	*/
}

function initProductDetail(){
	if($("#moreinfo").size()){
		$("#moreinfo").hide();
		$("#moreinfobutton").click(function(){
			$("#moreinfobutton").hide();
			$("#moreinfo").slideDown();
			return false();
		});
	}
}

function initForms(){
	if($("form").size() > 0){
		$("input[type=text], textarea").each(function(){
			if($(this).val() != ""){
				$(this).parent().find("label").hide();
			}
		});
		$("input[type=text], textarea").focus(function(){
			$(this).parent().find("label").hide();
		});
		$("input[type=text], textarea").blur(function(){
			if($(this).val() == ""){
				$(this).parent().find("label").show();
			}
		});
		$(".error").hide();
		$(".necessary").blur(function(){
			if($(this).val().length < 2){
				$(this).parent().find(".error").fadeIn();
			}
			if($(this).attr("id") == "mail"){
				var mailpattern = /[a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/;
				if($("#mail").val().search(mailpattern) == -1){
					$(this).parent().find(".error").fadeIn(200);
	            }
			}
		});
		$(".necessary").keyup(function(){
			if($(this).val().length > 2 && $(this).attr("id") != "mail"){
				$(this).parent().find(".error").fadeOut();
			}
			if($(this).attr("id") == "mail"){
				var mailpattern = /[a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/;
				if($("#mail").val().search(mailpattern) != -1){
					$(this).parent().find(".error").fadeOut(200);
	            }
			}
		});
		$("#contactform").submit(function(){
			var error = false;
			$(".necessary").each(function(){
				if($(this).val().length < 2){
					$(this).parent().find(".error").fadeIn();
					error = true;
				}
				if($(this).attr("id") == "mail"){
					var mailpattern = /[a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/;
					if($("#mail").val().search(mailpattern) == -1){
						$(this).parent().find(".error").fadeIn(200);
						error = true;
		            }
				}
			});
			if(error){
				return false;
			}
		});
	}
}

function initSlideshow(){
	if($(".gallery li").size() > 1){
		$(".gallery li").hide();
		$(".gallery li").first().show();
		setInterval("doSlideShow()", 3000);
	}
}

var prevImage = 0;
var currentImage = 1;
function doSlideShow(){
	prevImage = currentImage++;
	if(currentImage > $(".gallery li").size()){
		currentImage = 1;
	}
	$(".gallery li").css({"z-index":"1"});
	$(".gallery li:nth-child("+currentImage+")").css({"z-index":"2"}).fadeIn(500, function(){$(".gallery li:nth-child("+prevImage+")").hide();});
}



function initMaps(){
	
	if($("#parntercontainer").size()){
		$("#parntercontainer h3").click(function(){
			$(".partnerlist").slideUp(200);
			if(!$(this).next().is(":visible")){
				$(this).next().slideDown(200);
			}
		});
		$(".partnerlist").hide();
		/*
		var europe = new google.maps.LatLng(52,15.000000);
		var asia = new google.maps.LatLng(60.363882,95.044922);
		var northam = new google.maps.LatLng(50.363882,-100.044922);
		var southam = new google.maps.LatLng(-3.363882,-58.044922);
		var pacific = new google.maps.LatLng(-22.363882,131.044922);
		
		var latlng = new google.maps.LatLng(28, 0);
	    var myOptions = {
	      zoom: 1,
	      center: latlng,
	      disableDefaultUI: true,
	      scrollwheel: false,
	      draggable: false,
	      mapTypeId: google.maps.MapTypeId.ROADMAP
	    };
	    
	    var map = new google.maps.Map(document.getElementById("map_canvas"),
		        myOptions);
	    
	    for(var i = 0; i < pointsobj.length; i++){
	    	var marker = new google.maps.Marker({
		        position: new google.maps.LatLng(pointsobj[i]["lat"],pointsobj[i]["long"]), 
		        map: map,
		        id: pointsobj[i]["id"]
		    });
	    	google.maps.event.addListener(marker, 'click', function(){
		    	$(window).scrollTo("#" + this.id, 300);
		    });
	    }
		*/
	}
}

$.fn.slideshow = function(){
	  
	  var slides=this.children('li');
	  var count=slides.length;  
	  var $this = this;
	  
	  slides.each(function(index){
	     var $this=$(this);
	   //$this.append('<a class="filter" href="'+$this.find("a").first().attr("href")+'"></a>'); 
	   $this.css( { 
	    zIndex: count-index,
	    display: ( index==0 ) ? 'block' : 'none'
	  } );
	  	if(index == 0) {
	  		$this.addClass("active");
	  	}
	  });
	  
	  $('a.filter').click( function(e){
	    e.preventDefault();
	    if ( $(this).attr('href')!='#' )
	    {
	      open( $(this).attr('href') );
	    }
	  } );
	  
	  var startSlideshow = function(){
	      return setTimeout( function(){
	            doSlideshow()
	      }, 5000 );
	  }
	  
	  var doSlideshow = function(next){
//	      console.log( next );
	      clearTimeout(timeout);
	      var current= $this.children('li.active');     
	      if(next == undefined){
	        var next = current.next();
	          if ( next.length==0 )
	        {
	          next= slides.first();           
	        }
	      }
	      else
	      {
	        next = $(next);
	      }
	        

	        current.stop(true, true).fadeOut( 1000 ).removeClass('active');
	        next.stop(true, true).fadeIn( 1000 ).addClass('active');
	        $("#slideshow_buttons li").removeClass("active");
	        $("#slideshow_buttons li:nth-child("+(next.index() + 1)+")").addClass("active");
	        timeout = startSlideshow();
	  }
	  
	  var numbers = $('<ul id="slideshow_buttons" />');
	  for ( var i=0;i<count;i++ )
	  {
	      var li = $('<li><a href="#slide'+(i+1)+'">'+(i+1)+'</a></li>');
	      if(i == 0){
	          li.addClass("active");
	      }
	      li.find("a").click(function(e){
	          e.preventDefault();
	          doSlideshow( slides[ $(this).parent().index() ] );
	      });
	      numbers.append(li);
	  }
	  
	  this.after( numbers );
	  
	  var timeout = startSlideshow();

	  return this;

	}
