namespace("tv.nordisch.View");

/**
* @author Martin Anderle
*/

tv.nordisch.View = function()
{
	//private
	var _ignoreInittChange=true;
	var _imprintToggled=false;
	var _contentChangeSmall=0;
	var _contentChangeAdd=0;
	var _content;
	var _contentIn;
	
	//public
	return {
		
	imprint : function()
	{
		var y;
		var m;
		var t;
		var h;
		var c;
		
		
		if(!_imprintToggled)
		{
			$("#footer").css("height","700px");
			$("#footerContent").css("top","500px");
			
			t=400;
			y=-350;
			m="easeOutQuad";
			c=function(){};
			_imprintToggled=true;
		}
		else
		{
			t=350;
			y=0;
			m="easeQuad";
			
			c=this.imprintComplete;
			_imprintToggled=false;
		}
		
		$("#footerContentWrapper").stop({clearQueue:true});
		
		if(!jQuery.browser.msie && !($.browser.mozilla && parseInt(jQuery.browser.version) < 2))
			$("#footerContentWrapper").animate({top:y,useTranslate3d:true,leaveTransforms:true}, t,m,c);
		else
			$("#footerContentWrapper").animate({top:y}, t,"linear",c);
	},
	
	imprintComplete : function()
	{
		$("#footer").css("height","200");
		$("#footerContent").css("top","0");
	},
		
	toggle : function()
	{
		var time=500;
		
		if($("#content").css("opacity")==1)
		{
			$('#content').stop({clearQueue:true}).animate({opacity:0.0001},time,'linear');
			$('#footer').stop({clearQueue:true}).animate({opacity:0.0001},time,'linear');
			document.getElementById("navBgToggle").firstChild.src="style/navi/plus.png";
		}
		else
		{
			$('#content').stop({clearQueue:true}).animate({opacity:1},time,'linear');
			$('#footer').stop({clearQueue:true}).animate({opacity:1},time,'linear');
			document.getElementById("navBgToggle").firstChild.src="style/navi/close.png";
		}
	},
	
	motionDataComplete: function(site)
	{
		$('#content').stop({clearQueue:true});
		
		var heightAdd=200;
		
		if(site.id=="home")
			heightAdd=400;
		else if(site.id=="about")
			heightAdd=600;
		
		$('#content').css("marginTop",-parseInt($('#content').outerHeight()+heightAdd)+ 'px');
	},
	
	motionIn : function()
	{
		if($("#footer").css("opacity")!=1)
			this.toggle();
		
		view.motionDataComplete(window.next);
		$('#content').stop({clearQueue:true});
		$('#content').animate({ marginTop: '0px'},600);
		
		setTimeout(contentBuildComplete,650);
	},
	
	motionOut : function()
	{
		$('#content').stop({clearQueue:true});
		$('#content').animate({ marginTop: -parseInt($('#content').outerHeight()+200) + 'px'},500);
		
		setTimeout(contentSetData,550);
	},
	
	motionChange : function()
	{
		if($("#footer").css("opacity")!=1)
			this.toggle();
		
		_contentChangeAdd=0;
		_contentChangeSmall=0;	
		var x=-window.contentWidth;
	
		
		if(SWFAddress.getParameter("l")==1)
			x=window.contentWidth;
		
		_contentIn = $('#contentIn');
		_content = $('#content');
		
		$('#contentIn').css("left", -x+ 'px');
		
		if(window.site.id=="clients")
		{
			_content = $('#clients');
		}
		else if(window.next.id=="clients")
		{
			_contentIn = $('#clients');
			_contentChangeAdd = -24;
		}
			
		
		if(_contentIn.outerHeight()>_content.outerHeight()) //NEUER CONTENT GRÖSSER
		{
			_content.css("height",_content.outerHeight()+ 'px');
			_content.animate({height:_contentIn.outerHeight()+ 'px'},300);
			setTimeout(view.motionChangeNext,350);
		}
		else if(_contentIn.outerHeight()<_content.outerHeight()) //NEUER CONTENT KLEINER
		{
			_contentChangeSmall = _contentIn.outerHeight();
			_contentIn.css("height",_content.outerHeight()+_contentChangeAdd+ 'px');
			
			$('#contentSwitcher').stop({clearQueue:true});
			$('#contentSwitcher').animate({marginLeft:x},500);
			
			setTimeout(view.motionChangeNextSmall,550);
		}
		else //NEUER CONTENT GLEICH
			view.motionChangeNext();
	},
	
	motionChangeNext : function()
	{
		var x=-window.contentWidth;
		
		if(SWFAddress.getParameter("l")==1)
			x=window.contentWidth;
			
		_content.stop({clearQueue:true});
		$('#contentSwitcher').stop({clearQueue:true});
		$('#contentSwitcher').animate({marginLeft:x},500);
			
		setTimeout(contentMotionComplete,550);
	},
	
	motionChangeNextSmall : function()
	{
		_contentIn.stop({clearQueue:true});
		_contentIn.animate({height:_contentChangeSmall+ 'px'},300);
		$('html,body').stop({clearQueue:true});
		$('html,body').animate({scrollTop: 0}, 300);
		setTimeout(contentMotionComplete,350);
	},
	
	bgChange : function()
	{
		trace("MAIN_BACKGROUND_CHANGE");
		
		if(_ignoreInittChange)
		{
			_ignoreInittChange=false;
			return;
		}
		
		for(var i=0;i<sites.items.length;i++)
		{
			if(sites.items[i].id==window.site.id)
			{
				siteClear(sites.items[i]);
				
				if(sites.items[i].img==null)
				{
					sites.items[i].img = new Image();
					
					if(window.siteInit)
					{
						if(!jQuery.browser.msie)
							sites.items[i].img.addEventListener("load", view.bgLoadComplete, false);
					}
					
					
					sites.items[i].img.id = sites.items[i].bg[0];
					sites.items[i].img.src = sites.items[i].bg[0];
				}
				else
					view.bgLoadComplete();
			}
		}
	},
	
	bgLoadComplete : function()
	{
		if(!jQuery.browser.msie)
			window.site.img.removeEventListener("load", view.bgLoadComplete,false);
		
		document.getElementById('backgroundImage').appendChild(window.site.img);
		view.bgTransition();
	},
	
	bgClear : function()
	{
		var items = document.getElementById('backgroundImage').childNodes;
		
		for(var i=items.length-1;i>-1;i--)
		{
			if(items[i].src!=null)
			{
				if(window.site.bg[0]!=items[i].id)
					document.getElementById('backgroundImage').removeChild(items[i]);
			}
			else
				document.getElementById('backgroundImage').removeChild(items[i]);
		}
	},

	bgTransition : function()
	{
		trace("VIEW.BG.TRANSITION");
		
		if(!window.isMobile)
			jQuery(window.site.img).stop({clearQueue:true}).css("opacity",.01).animate({opacity:1},1500,'linear',view.bgClear);
		else
			jQuery(window.site.img).stop({clearQueue:true}).css("opacity",.01).animate({opacity:1,avoidTransforms:true},700,'linear',view.bgClear);
	},
	
	reset : function()
	{
		
	}
};};
