var unblur = function()
{
	this.blur();	
};

var fixOutlineProperty = function()
{
	$("a, object").each(function() {
		$(this).focus(unblur);							 
	});
};

var hoverTeaserX = function() 
{	
	var $img_fade = 400;
	var text_fade = 300;
	var $teaser_fade = 400;
	
	var i = 0;
	var black_init = 300;
	var sleep_init = 500;
	var sleep = 600;
	
	var $teaser_to_animate = $(".teaser-x-inside");
	
	var teasers = new Array();
	
	var teaser_sleep = 1000;

	$teaser_to_animate.each(function() {
		var $teaser = $(this).addClass("teaser-x-inside-js").css({visibility : "visible"}),
			$teaser_text = $teaser.find(".text").css({height : $teaser.height(), width : $teaser.width(), opacity : 0, "zIndex" : 233}),
			$teaser_text_bg = $teaser.find(".text-bg").css({opacity : 0, "zIndex" : 230}),
			$teaser_pstext = $teaser.find(".ps-text").css({visibility : "visible", opacity : 0});
			teaser_id = $teaser.attr("id"),
			$teaser_link = $teaser.find("a:eq(0)").css({overflow : "hidden"}),
			$img = $teaser.find(".image").css({opacity : 1}),
			teaser = {
				teaser : $teaser,
				timeout : "",
				faded : false
			}
		
		teasers[teaser_id] = teaser;
	
		if($img.find("img").length == 0)
		{  
			$teaser_text.css({opacity : 1});
			$teaser_pstext.css({opacity : 1});
			$teaser_text_bg.css({opacity : 1});
			$img.css({opacity : 0});
		}
		else
		{
			/*
			window.setTimeout(function() {
				$teaser_text.animate({opacity : 1}, 1000, function() {
					teasers[teaser_id].faded = true;
				});
			}, i * black_init);
	
			teasers[teaser_id].timeout = window.setTimeout(function() {
				$img.stop().animate({"opacity" : 1}, $img_fade + 400).css; 
			}, teaser_sleep + $teaser_to_animate.length * (sleep));
	
			teaser_sleep += sleep_init;
			*/
			
			teasers[teaser_id].faded = true;

			$teaser.hover(function(i) {	     
				if(!teasers[teaser_id].faded && false) 
				{	
					$img.stop().animate({"opacity" : 1}, 0);
					window.clearTimeout(teasers[teaser_id].timeout);
				}
				else
				{ 		
					$teaser_text.stop().animate({"opacity" : 1}, text_fade);
					$teaser_pstext.stop().animate({"opacity" : 1}, text_fade - 10);
					$teaser_text_bg.stop().animate({"opacity" : 0.9}, text_fade - 10);
				}
			},
			function() { 
				if(!teasers[teaser_id].faded && false)
				{
					$img.stop().animate({"opacity" : 1}, 0);	
					$teasers[teaser_id].faded = true;
				}
				else
				{
					$teaser_text.stop().animate({"opacity" : 0}, text_fade);	
					$teaser_pstext.stop().animate({"opacity" : 0}, text_fade - 10);
					$teaser_text_bg.stop().animate({"opacity" : 0}, text_fade - 10);
				}
			});
		
			i++;
		}
	});
};

var hoverTeaser = function() 
{
	hoverTeaserX();
	$(".teaser").each(function() {
		var $teaser = $(this);
		if(!$teaser.hasClass("teaser-inside") || $teaser.hasClass("teaser-x-inside") || !$teaser.find("a").length)
			return;
	
		var $teaser_text = $teaser.find(".text");
		if(!$teaser_text.find(".teaser-click").length)
		{
			$teaser_text.css({"bottom" : "-23px"}).append('<span class="teaser-click">' + translations.main.teaser_click + '</span>');
		}
		var css_bottom = $teaser.find(".text").css("bottom");
		var bottom_init_value = 0;
		
		if($.browser.msie && ($.browser.version).indexOf("6.0") > -1)
		{
			bottom_init_value = -1;
		}
		
		$teaser.find("a").hover(function() { $teaser_text.stop().animate({bottom : bottom_init_value}, 100); }, function() { $teaser_text.stop().animate({bottom : css_bottom}, 100); });
	});
};

var hoverProjectTeaser = function()
{
	$teaser = $(".teaser_project a, .teaser_image a, .project-list-imgbox a");
	$teaser.each(function() { 
		if(!$(this).parent().parent().hasClass("teaser-x-inside"))
		{ 
			var $img = $(this).find("img");
			$(this).hover(function() {	
				$img.stop().animate({"opacity" : 0.7}, "fast");	
				if($img.parents(".teaser_news").length)
				{
					$img.parents(".teaser_news").find("h3:eq(0)").addClass("teaser-h3-hover");
				}
			},
			function() { 
				$img.stop().animate({"opacity" : 1}, "fast");
				if($img.parents(".teaser_news").length)
				{
					$img.parents(".teaser_news").find("h3:eq(0)").removeClass("teaser-h3-hover");
				}
			});
		}
	});
	
	$teaser = $(".teaser_news");
	$teaser.each(function() { 
		if(!$(this).parent().parent().hasClass("teaser-x-inside"))
		{ 
			var $img = $(this).find("img");
			if($img.length)
			{
				$(this).hover(function() {	
					$img.stop().animate({"opacity" : 0.7}, "fast");	
					$(this).addClass("teaser-h3-hover");
				},
				function() { 
					$img.stop().animate({"opacity" : 1}, "fast");
					$(this).removeClass("teaser-h3-hover");
				});
			}
		}
	});
}

var fixPNGs = function()
{
	$(".teaser-inside").pngFix({sizingMethod : "scale", selectorOnly : false});
	$("#page-wrapper, #feature-navigation a").pngFix();
};

var styleForms = function()
{

};

var initFancyBox = function()
{
	return;
	$("a.overlay").fancybox({
		'overlayShow' : true,
		'overlayOpacity' : 0,
		'zoomSpeedIn' : 250,
		'zoomSpeedOut' : 200
	});
};

var initFacebookFanBox = function(invert) 
{  
	var attr = "src";
	$(".FB_SERVER_IFRAME, .FB_fan").each(function() {
		if($(this).hasClass("FB_fan"))
		{
			attr = "css";
			if(invert)
				$(this).attr("css", "fb_invert.css?ms=" + new Date().getTime());
			else
				$(this).attr("css", "fb.css?ms=" + new Date().getTime());
		}
		else
		{
			attr = "src";
			if(invert)
			{
				if($(this).attr(attr).indexOf("fb_invert.css") == -1)
				{
					$(this).attr(attr, $(this).attr(attr).replace("fb.css", "fb_invert.css"));	
				}
			}
			else
			{
				if($(this).attr(attr).indexOf("fb.css") == -1)
				{
					$(this).attr(attr, $(this).attr(attr).replace("fb_invert.css", "fb.css"));	
				}
			}
		}
	});
};

var initPageSettings = function(force)
{	
	/* main */		
	if(force == undefined || force.styleForms != undefined)
		styleForms();
		
	if(force == undefined || force.fixOutlineProperty != undefined)
		fixOutlineProperty();
		
	if(force == undefined || force.fixPNGs != undefined)
		fixPNGs();
	
	if(force == undefined || force.initFancyBox != undefined)
		initFancyBox();
		
	if(force == undefined || force.initToolTip != undefined)
		initToolTip();

	if(force == undefined || force.hoverTeaser != undefined)
		hoverTeaser();

	if(force == undefined || force.initToolTip != undefined)
		hoverProjectTeaser();
		

	if(force == undefined || force.initFacebookFanBox != undefined)
	{
		initFacebookFanBox(!($("link[href=css/invert.css]").length > 0));
		initFacebookFanBox($("link[href=css/invert.css]").length > 0);
	}
};

var initToolTip = function(Selector, OffsetX, OffsetY) 
{
	var ToolTip = {
		Selector : Selector == undefined ? "a.tooltip" : selector,
		OffsetX : OffsetX == undefined ? 15 : OffsetX,
		OffsetY : OffsetY == undefined ? -60 : OffsetY
	};
	
	$(ToolTip.Selector).hover(function(e) {	
				var $tooltip = $("#tooltip");
				var pos_left = 0;
				var pos_top = 0;
				var tooltip_class = "tooltip-right";
				this.Title = this.title;
				if(this.title == undefined)
					return;
				
				this.title = "";	
					
				if($tooltip.length)
					$tooltip.stop().remove();
				$("body").append('<div id="tooltip"><span class="tooltip-top"><!-- --></span><span class="tooltip-content">' + this.Title + '</span><span class="tooltip-bottom"><!-- --></span><span class="tooltip-arrow"><!-- --></span></div>');
				$tooltip = $("#tooltip");
													
				try {
					pos_left = $(this).offset().left + 50 + $tooltip.width();
					pos_top = $(this).offset().top + 54 - $tooltip.height();
					tooltip_class = "tooltip-right";
					if($(window).width() < pos_left)
					{
						tooltip_class = "tooltip-left";
						pos_left = e.pageX - ToolTip.OffsetX - $tooltip.width();
					}
					else
					{
						pos_left -= $tooltip.width();
					}
	
					if($tooltip.height() < 50)
					{
						pos_top -= 7;
						$tooltip.find(".tooltip-arrow").css({top : "14px"});
					}
					
					$tooltip.css({left : pos_left + "px", top : pos_top + "px"}).addClass(tooltip_class);
					
					$(".tooltip-content").pngFix({sizingMethod : "scale"});
					$(".tooltip-end").pngFix();
					$tooltip.pngFix();

					if(!$.browser.msie)
						$tooltip.fadeIn("fast");
					else
					{
						$tooltip.show();
					}				
				}
				catch(exception) {}
				
			},
			function(){
				this.title = this.Title;
				$("#tooltip").stop();
		
				if(!$.browser.msie)
					$("#tooltip").fadeOut("fast", function() { $(this).remove(); });
				else
					$("#tooltip").remove();					
			})
		.mousemove(function(e) {	
			return;
			try {
				$tooltip = $("#tooltip");
				
				pos_left = e.pageX + ToolTip.OffsetX + $tooltip.width();
				if($(window).width() < pos_left)
				{
					pos_left = e.pageX - ToolTip.OffsetX - $tooltip.width();
				}
				else
				{
					pos_left -= $tooltip.width();
				}
			
				$tooltip.css({"left" : pos_left + "px", "top" : (e.pageY + ToolTip.OffsetY) + "px"});
			}
			catch(exception) {}
		}
	);
}

var initPageSettingsOnAJAXCall = function(force)
{
	ajax_force = force;
	if(ajax_force == null)
	{
		ajax_force = new Object();		
	}
	
	ajax_force.fixOutlineProperty = true;
	ajax_force.styleForms = true;
	ajax_force.fixPNGs = true;
	ajax_force.initFancyBox = true;
	
	initPageSettings(ajax_force);
};

var fixBackground = function()
{
	$("#feature-wrapper").css({"width" : $(window).width() <= 946 ? "946px" : "100%", "overflow" : $(window).width() <= 1024 ? "hidden" : "visible"});	
}

$(document).bind("ready", function() { initPageSettings(); fixBackground(); });

$(window).bind("scroll", function() { fixBackground(); });
$(window).bind("resize", function() { fixBackground(); });
