$(document).ready(function() 
{
	$('img.hidden').css({visibility:"visible"});
	$('.home_pic').jImagePreloader({imagesPath:'/screenbow/public/images/', defaultWidth:250, defaultHeight:164, imageLoading:'loader_grey.gif'});
	$('.news_pic').jImagePreloader({imagesPath:'/screenbow/public/images/', defaultWidth:130, defaultHeight:85, imageMarginLeft: 12, imageMarginTop: 10});
	$('.projekt_pic').jImagePreloader({imagesPath:'/screenbow/public/images/', defaultWidth:250, defaultHeight:164, imageLoading:'loader_grey.gif'});	
	$('.agentur_pic').jImagePreloader({imagesPath:'/screenbow/public/images/', defaultWidth:250, defaultHeight:164, imageLoading:'loader_grey.gif'});	
	$('.thumb').jImagePreloader({imagesPath:'/screenbow/public/images/', defaultWidth:81, defaultHeight:53, imageMarginLeft: 15, imageMarginTop: 10});	
	
	
	$('.projekt').hover(
      function () {
      	$(this).find(".qtip").show();
      }, 
      function () {
        $(this).find(".qtip").hide();
      }
    );		
    
	$('.pic, .projekt').hover(
      function () {
      	$(this).find(".button #button_inaktiv").hide();
      	$(this).find(".button #button_aktiv").show();      	
      }, 
      function () {
      	$(this).find(".button #button_aktiv").hide();
      	$(this).find(".button #button_inaktiv").show();
      }
    );	
    
    $('.text_block').hover(
      function () {
      	var tmp_id = $(this).attr("id");
      	var id = tmp_id.split('_');
      	$('#homepic_'+id[1]).find(".button #button_inaktiv").hide();
      	$('#homepic_'+id[1]).find(".button #button_aktiv").show();      	
      }, 
      function () {
      	var tmp_id = $(this).attr("id");
      	var id = tmp_id.split('_');      
      	$('#homepic_'+id[1]).find(".button #button_aktiv").hide();
      	$('#homepic_'+id[1]).find(".button #button_inaktiv").show();
      }
    );
    
	$('.projekt_jahre').click(function(){
		menuRollOver();
		$('.projekt_jahre_content').find(".link").hide();
		$('.projekt_jahre').removeClass("aktiv");
		jQuery.each($(".projekt_jahre_content"), function() {
			//$(this).slideUp();
			var block = $(this);
			if(block.css("display") != 'none'){
				var h = block.height();
				var cssHeight=block.css('height');
				block.animate( 
				{ height: '1px' }, "normal", function() { 
				  block.hide();
				  block.height(h);
				  block.css('height',cssHeight);	
				});				
			}
		});			
		
		
		var block = $(this).parent().find('.projekt_jahre_content:first');
		var anchor =$(this).find("a:first").attr("name");
		if(block.css("display") == 'none'){
			$('.projekt_jahre_content').find(".link").show();				  			  
			$(this).addClass("aktiv");		
			block.show();
			var h = block.height();
			block.hide();
			block.height(h);		
			block.animate({"height": "toggle"},"normal",function(){
				menuRollOut();		
				document.location.href = "#"+anchor;
			});						
		}else{		
			$(this).removeClass("aktiv");				
			var h = block.height();
			var cssHeight=block.css('height');
			block.animate( 
			{ height: '1px' }, "normal", function() { 
			  block.hide();
			  block.height(h);
			  block.css('height',cssHeight);
			  $('.projekt_jahre_content').find(".link").show();				  			  
			  menuRollOut();	
			  document.location.href = "#"+anchor;			  
			});			
		}
		
	});
	
	$('.link').hover(
      function () {
      	var position = $(this).position();
      	$(this).parent().parent().find("#qtip_"+$(this).attr("id")).css({left:position.left-15+'px', top:position.top-32-$(this).parent().parent().find("#qtip_"+$(this).attr("id")).height()+'px'});
      	$(this).parent().parent().find("#qtip_"+$(this).attr("id")).show();
      }, 
      function () {
        $(this).parent().parent().find("#qtip_"+$(this).attr("id")).hide();
      }
    );  
    
	
	$('.projekt_detail').hover(
      function () {
      	$(this).find("#button_inaktiv").hide();
      	$(this).find("#button_aktiv").show();      	      
      	var position = $(this).position();
      	$(this).find(".qtip").css({left:-10+'px', top:-72+'px'});
      	$(this).find(".qtip").show();
      }, 
      function () {
      	$(this).find("#button_aktiv").hide();
      	$(this).find("#button_inaktiv").show();      
      	$(this).find(".qtip").hide();
      }
    );	
    
    if($('.projekt_detail').size()<=2){
    	$("#arrow_right").css({visibility:"hidden"});						
    }
    
    if($('.agentur_detail').size()<=3){
    	$("#arrow_right_agentur").css({visibility:"hidden"});						
    }    
    
	function moveLeft(){
		menuRollOver();
    	var position = $('#slider').position();
    	if(position.left<6){
		    $("#arrow_left").unbind('click', moveLeft);    	
			$('#slider').animate({"left": "+=389px"}, "slow", function(){
				$("#arrow_left").bind('click', moveLeft);    	
		    	var pos = $('#slider').position();
		    	if(pos.left>=6){
					if($('.projekt_detail').size()==3){
						$("#arrow_right").css({visibility:"visible"});										
					}			    	
					$("#arrow_left").css({visibility:"hidden"});						
				}else{
					$("#arrow_right").css({visibility:"visible"});										
				}
				menuRollOut();				
			});
		}
    }
        
	function moveRight(){
		menuRollOver();	
    	var position = $('#slider').position();		    
    	if(position.left-1>6-($('.projekt_detail').size()-2)*389){
    		$("#arrow_right").unbind('click', moveRight);    	
			$('#slider').animate({"left": "-=389px"}, "slow", function(){
	    		$("#arrow_right").bind('click', moveRight);    				
		    	var pos = $('#slider').position();
		    	//alert((pos.left-1)+' <= '+(6-($('.projekt_detail').size()-2)*389));
		    	if(pos.left-1<=6-($('.projekt_detail').size()-2)*389){
					$("#arrow_right").css({visibility:"hidden"});
					if($('.projekt_detail').size()==3){
						$("#arrow_left").css({visibility:"visible"});										
					}						
				}else{
					$("#arrow_left").css({visibility:"visible"});										
				}		
				menuRollOut();					
			});
		}
    }
        
    $('#arrow_left').click(moveLeft);
    $('#arrow_right').click(moveRight);    
    
    
	function moveLeftAgentur(){
		menuRollOver();
    	var position = $('#slider').position();
    	if(position.left<6){
		    $("#arrow_left_agentur").unbind('click', moveLeft);    	
			$('#slider').animate({"left": "+=258px"}, "slow", function(){
				$("#arrow_left_agentur").bind('click', moveLeft);    	
		    	var pos = $('#slider').position();
		    	if(pos.left>=6){
					if($('.agentur_detail').size()==4){
						$("#arrow_right_agentur").css({visibility:"visible"});										
					}		    	
					$("#arrow_left_agentur").css({visibility:"hidden"});						
				}else{
					$("#arrow_right_agentur").css({visibility:"visible"});										
				}
				menuRollOut();				
			});
		}
    }
        
	function moveRightAgentur(){
		menuRollOver();	
    	var position = $('#slider').position();		    
    	if(position.left-1>6-($('.agentur_detail').size()-3)*258){
    		$("#arrow_right_agentur").unbind('click', moveRight);    	
			$('#slider').animate({"left": "-=258px"}, "slow", function(){
	    		$("#arrow_right_agentur").bind('click', moveRight);    				
		    	var pos = $('#slider').position();
		    	//alert((pos.left-1)+' <= '+(6-($('.agentur_detail').size()-2)*258));
		    	if(pos.left-1<=6-($('.agentur_detail').size()-3)*258){
					$("#arrow_right_agentur").css({visibility:"hidden"});		
					if($('.agentur_detail').size()==4){
						$("#arrow_left_agentur").css({visibility:"visible"});										
					}									
				}else{
					$("#arrow_left_agentur").css({visibility:"visible"});										
				}		
				menuRollOut();					
			});
		}
    }    
    
    $('#arrow_left_agentur').click(moveLeftAgentur);
    $('#arrow_right_agentur').click(moveRightAgentur);        
    
    $('.ajax_link').click(function(){
    	$('#right_block').load($(this).attr("href"), {ajax: 'true'});
    	return false;
    });
    
	$('<div id="overlay">')
	  .css({
	     position: 'absolute',
	     top: 0,
	     left: 0,
	     height: $(document).height(),
	     width: '100%',
	     opacity: 0.8, 
	     filter: 'alpha(opacity=80)',
	     backgroundColor: 'black',
	     zIndex: 5000  
	  })
	  .appendTo(document.body)
	  .hide(); 
	    
	$('<div id="popup">')
	  .css({
	     position: 'absolute',
	     height: '24px',    
	     width: '24px',
	     zIndex: 5001  
	  })
	  .appendTo(document.body)
	  .hide(); 	  
	  
	//kontakt
	DD_roundies.addRule('input, .textarea, div.selectbox-wrapper', '5px', true);		
	$('select').selectbox();				
	$('.kontaktSubmit').click(function(){
    	var error = 0;
		var checkArray = new Array();			
		
		checkArray['vorname'] = /\S{2,100}/;
		checkArray['name'] = /\S{2,100}/;
		checkArray['nachricht'] = /\S{1,100}/;		
		checkArray['email'] = /^(\w|[-.,]){2,}@((\w|[-.,]){2,}[.]?){1,30}([.][a-z]{2,4})$/;
		
		jQuery.each($("#kontaktForm input"), function() {
	    	if($(this).hasClass("required") && !checkArray[$(this).attr("id")].test($(this).val())){
	    		document.getElementById($(this).attr("id")).style.backgroundColor = "#f09e93";
	    		error = 1;
	    	}else{
	    		document.getElementById($(this).attr("id")).style.backgroundColor = "#f0f1ed";
	    	}
		});	
		
		jQuery.each($("#kontaktForm textarea"), function() {
	    	if($(this).hasClass("required") && !checkArray[$(this).attr("id")].test($(this).val())){
	    		document.getElementById($(this).attr("id")).style.backgroundColor = "#f09e93";
	    		document.getElementById($(this).attr("id")+'Div').style.backgroundColor = "#f09e93";	    		
	    		error = 1;
	    	}else{
	    		document.getElementById($(this).attr("id")).style.backgroundColor = "#f0f1ed";
	    		document.getElementById($(this).attr("id")+'Div').style.backgroundColor = "#f0f1ed";	    			    		
	    	}
		});			
		
		if(error)
			$("#formError").show();
		else{
			$("#formError").hide();
			$("#kontaktForm").submit();
		}				
					
	});	 
	
	$('.kontaktSubmit').hover(
      function () {
      	$("#button_inaktiv").hide();
      	$("#button_aktiv").show();      	
      }, 
      function () {
      	$("#button_aktiv").hide();
      	$("#button_inaktiv").show();
      }
    );
    
    $('#overlay').click(closePopup);		

	if(getFlashVersion() == 0){
		$('#getFlash').show();
	}  
	
});

function bigImage(url, text){
	menuRollOver();
	$('#popup').html('');
	$('#overlay').fadeIn("def",function(){
		$('#popup').show().centerInClient();
		$('#popup').load('picture', {url:url, text:text}, function(){		
			$('#textClose').hover(
		      function () { $(this).addClass('closeAktiv');}, 
		      function () { $(this).removeClass('closeAktiv');}
		    ); 		
			$('#bigImg').load(function(){
				$('#picturePopup').css({visibility:"visible"}).css({
					marginLeft: '-'+$('#picturePopup').width()/2+'px',
					marginTop: '-'+$('#picturePopup').height()/2+'px'
				});	
				var position = $('#picturePopup').position();	
				if($('#picturePopup').height()/2 > $(window).height()/2){
					$('#picturePopup').css({
						marginTop: '-'+(($(window).height()/2)-20)+'px'
					});					
				}							
				$('#textClose').css({
					marginLeft: $('#picturePopup').width()-15+'px'
				});					
			});
			
		});						
	});
}

function closePopup(){
	$('#popup').load('blank');
	$('#overlay').fadeOut("def",function(){
		menuRollOut();	
	});
	$('#popup').hide();	
}

function movie(url, text){
	menuRollOver();
	$('#popup').html('');
	$('#overlay').fadeIn("def",function(){
		$('#popup').show().centerInClient();	
		$('#popup').load('movie', {url:url, text:text}, function(){		
		
			$('#textClose').hover(
		      function () { $(this).addClass('closeAktiv');}, 
		      function () { $(this).removeClass('closeAktiv');}
		    ); 		 	
    		
			$('#picturePopup').css({visibility:"visible"}).css({
				marginLeft: '-'+$('#picturePopup').width()/2+'px',
				marginTop: '-'+$('#picturePopup').height()/2+'px'
			});	
			$('#textClose').css({
				marginLeft: $('#picturePopup').width()-15+'px'
			});					

		});						
	});
}  
/*
jQuery.fn.fadeIn = function(speed, callback) { 
    return this.animate({opacity: 'show'}, speed, function() { 
        if (jQuery.browser.msie)  
            this.style.removeAttribute('filter');  
        if (jQuery.isFunction(callback)) 
            callback();  
    }); 
}; 
 
jQuery.fn.fadeOut = function(speed, callback) { 
    return this.animate({opacity: 'hide'}, speed, function() { 
        if (jQuery.browser.msie)  
            this.style.removeAttribute('filter');  
        if (jQuery.isFunction(callback)) 
            callback();  
    }); 
}; 
 
jQuery.fn.fadeTo = function(speed,to,callback) { 
    return this.animate({opacity: to}, speed, function() { 
        if (to == 1 && jQuery.browser.msie)  
            this.style.removeAttribute('filter');  
        if (jQuery.isFunction(callback)) 
            callback();  
    }); 
}; 
*/
function getFlashVersion(){ 
  // ie 
  try { 
    try { 
      // avoid fp6 minor version lookup issues 
      // see: http://blog.deconcept.com/2006/01/11/getvariable-setvariable-crash-internet-explorer-flash-6/ 
      var axo = new ActiveXObject('ShockwaveFlash.ShockwaveFlash.6'); 
      try { axo.AllowScriptAccess = 'always'; } 
      catch(e) { return '6,0,0'; } 
    } catch(e) {} 
    return new ActiveXObject('ShockwaveFlash.ShockwaveFlash').GetVariable('$version').replace(/\D+/g, ',').match(/^,?(.+),?$/)[1]; 
  // other browsers 
  } catch(e) { 
    try { 
      if(navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin){ 
        return (navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"]).description.replace(/\D+/g, ",").match(/^,?(.+),?$/)[1]; 
      } 
    } catch(e) {} 
  } 
  return '0'; 
} 