function fadeIn($selector){
	$selector.parent().fadeIn(1500);
} 

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();  
    }); 
}; 

$(document).ready(function () {
	
	//Fade In Main Logo
	$('#logo_main img').onImagesLoad({ 
		selectorCallback: fadeIn 
	});
	
	$(this).contextMenu('context', {
        menuStyle: {
          border: '0px',
		  width: '200px'
        },
        itemStyle : {
          fontFamily: 'verdana',
          backgroundColor: '#FFF',
          color: '#838282',
          border: 'none',
          padding: '3px',
		  border: '0px',
		  fontSize: '9px'
        },
        itemHoverStyle: {
          backgroundColor: '#eeeeee',
          color: '#838282',
          border: 'none',
          padding: '3px',
		  border: '0px'
        },
		bindings: {
			'c_ask': function(t) {
			  $('#menu_contact').click();
			},
			'c_copy': function(t) {
			  $('#menu_contact').click();
			}
		}
    });
	
	//Initiate Submenu Height
	var sub_height = $('#sub_bar').height();
	var sub_timer;
	var is_sub = 0;

	$('#menu a').hover(
		function () {
			if(!$(this).hasClass('act'))
				$(this).stop().animate({ color: "#4f5e61" }, 200);
		}, 
		function () {
			if(!$(this).hasClass('act'))
				$(this).stop().animate({ color: "#838282" }, 300);
		}
	);

	
	//MAIN MENU Click
       // Einbau von Moritz: Menüpunkt "Blog" soll kein Ajax-Aufruf starten sondern einfach ein neues Fenster öffnen mit dem Link
	$('#menu a[href^="content"]').click(function(){
		$('#loading').css({display: "block"});
		$('#menu a').each(function(){
			$(this).removeClass('act');
			$(this).stop().animate({ color: "#838282" }, 300);
		});
		$(this).addClass('act');
		$(this).stop().animate({ color: "#4f5e61" }, 200);
		href = $(this).attr('href');
		rel = $(this).attr('rel');
		title = $(this).html();
		$('#sub_bar').stop().animate({'height':0},500);
		switch(rel){
			case 'sub':
				is_sub = 1;
				$.timer(600,function(){
					$('#sub_bar').load(href, function(){
						$('#loading').css({display: "none"});
						sub_height = $('#sub_bar').find(':first').outerHeight();
						$('#sub_bar').stop().animate({'height':sub_height},500);
					});
				});
			break;
			case 'main':
				is_sub = 0;
				//CONTENT FADE
				$('#image_cont').children().each(function(){
					$(this).fadeTo(500,0);
				});
				//LOGO FADE
				$('#logo_main').fadeOut(500);
				if(parseInt($('#side').css('margin-left')) < 0) {
					$('#side').css('margin-left',($('#side').width())*(-1)).animate({marginLeft:0},500);
				}
				//TOGGLE FAID
				$('.img_nav').fadeOut(500);
				
				//LOAD CONTENT
				$.timer(600,function(){
					$('#loading').css({display: "none"});
					$('#image_cont').html('');
					$('#sub_bar').html('');
					$('#image_cont').load(href, function(){
						content = $('#image_cont div.content');
						content.fadeTo(0,0);
						
						//TITLE TEXT UPDATE
						if($('#to_header').html() != '') {
							header = $('#to_header').html();
						}
						$('#img_header').stop().html(header).jTypeWriter({duration:1});
						
						if($('#to_title').html() != '') {
							title = $('#to_title').html();
						}
						$('#img_title').stop().html(title).jTypeWriter({duration:1});
						
						//height = content.outerHeight();
						//alert('-' + parseInt(height/2) + 'px');
						//content.css('margin-top', parseInt(height/2)*(-1));
						content.stop().fadeTo(500,1);
					});
				});
			break;
		}
		return false;
	});
	
	//Submenu Gallery Click
	$('#sub_bar a[rel="gal"]').live('click', function() {
		is_sub = 1;
		href = $(this).attr('href');
		$('#sub_bar').stop().animate({'height':0},500);
		$.timer(600,function(){
			$('#sub_bar').load(href, function(){
				sub_height = $('#sub_bar').find(':first').outerHeight();
				$('#sub_bar').stop().animate({'height':sub_height},500);
				$.timer(700,function(){
					initGallery();
				});
			});
		});
		return false;
	});
	
	//Submenu Video Click
	$('#sub_bar a[rel="vid"]').live('click', function() {
		var video = $(this).attr('href');
		var size = $(this).attr('rev');
		var title = $(this).find('img').attr('alt');
		var sizeArr = size.split('x');
		var width = parseInt(sizeArr[0]);
		var height = parseInt(sizeArr[1]);
		var ratio = height/width;
		var browserwidth = $(window).width();
		var browserheight = $(window).height();
		
		$(this).parent().children().each(function(){
			$(this).removeClass('active');
		});
		$(this).parent().addClass('active');
		
		width = browserwidth;
		height = browserheight;
		
		//TITLE TEXT UPDATE
		$('#img_header').stop().html(title).jTypeWriter({duration:1});
		$('#img_title').stop().html('');
		
		$('#logo_main').fadeOut(500);
		if(parseInt($('#side').css('margin-left')) < 0) {
			$('#side').css('margin-left',($('#side').width())*(-1)).animate({marginLeft:0},500);
		}
		$('.img_nav').fadeOut(500);
		
		$.fn.media.defaults.flvPlayer = 'mediaplayer/player.swf';
		$.fn.media.defaults.flashvars = {skin:'mediaplayer/skins/slim.swf'};
		$('div#image_cont').fadeOut(700);
		
		$.timer(800,function(){
			$('div#image_cont').html('');
			$('div#image_cont').removeAttr('style');
			$('div#image_cont').removeClass('imgsize');
			//cssObject.marginTop = '-' + (parseInt(height/2)+45) + 'px';
			//cssObject.marginLeft = '-' + parseInt(width/2) + 'px';
			var video_cont = $(document.createElement('div')).attr('id', 'video');
			var video_link = $(document.createElement('a')).attr('href', video);
			video_cont.append(video_link);
			$('#image_cont').append(video_cont);
			
			video_link.media({ 
				width:     	width, 
				height:    	height,
				autoplay:	1,
				flashvars:     { 
					file:  			'../'+video,
					autostart:		true,
					backcolor: 		"#f3f3f3",
					frontcolor: 	"#f3f3f3",
					lightcolor: 	"#ef1c24",
					screencolor: 	"#f3f3f3"
				}
			});
			
			$('div#image_cont').fadeIn(700)
		});
		
		return false;
	});

	//hover submenu
	$('#bottom').mouseenter(function() {
		if(is_sub == 1){
			$.clearTimer(sub_timer);
			$('#sub_bar').stop().animate({'height':sub_height},800);
		}
	});
	$('#bottom').mouseleave(function () {
		if(is_sub == 1){
			sub_timer = $.timer(4000, function() {
					$('#sub_bar').stop().animate({'height':0},800);
			});
		}
	});
	
	$('.more_open').live('click', function() {
		if($('.more_open').hasClass('open')){
			$('.more').slideUp(400);
			$('.more_open').removeClass('open');
		} else {
			$('.more').slideDown(400);
			$('.more_open').addClass('open');
		}
	});
	
	$(window).resize(function() {
	  $('embed').width($(window).width());
	  $('embed').height($(window).height());
	});
	
	//SHOW MENU
	$('#menu_bar').slideDown(500);
	$('#menu_shade').animate({'margin-bottom':35},500);
	
});
