$(function(){
	$('#content .inner')._fw({
		uSlider:{
			pagination:'nav li',
			items:'#content .items>li',
			one:function(){
				show_subpages()
			},
			afterChange:function(){
				if(this.currN!=1)
					hide_gallery(),
					hide_content()
				else
					hide_content()				
				$('nav li .butt').trigger('mouseout')				
			},
			beforeAnimation:function(){
				var _=this
				if(_.itmsHTML.find(_.path).is('.popup'))
					_.holder.parent().append($('<a href="#" class="more close" rel="back">x</a>').hide())
				else
					$('.close').remove()
			},
			animationCallback:function(){
				if(this.currN==1)
					show_gallery()
				else
					show_content()
				$('.inner .scroll')._fw({
					cScroll:{
						step:'85px',
						sWidth:'36px',
						sHeight:'72px'
					}
				}).trigger('rcs')
				$('.inner .more,.close').show()._fw({
					sprites:{
						hover:true,
						hoverIn:function(){
							var _=this
							_.holder
								.stop()
								.animate({color:'#fff'},{easing:_.easing,duration:_.duration})
						},
						hoverOut:function(){
							var _=this
							_.holder
								.stop()
								.animate({color:'#000'},{easing:_.easing,duration:_.duration})
						}
					}	
				})
			}
		}
	})
})
$(window).load(function(){   
	$('.butt')._fw({
		sprites:{
			duration:400,
			easing:'easeOutSine',
			pasta:'<span></span>',
			hover:true,
			holdOn:function(){
				if(this.holder.parent().is('.active'))
					return true
			},
			hoverIn:function(){
				var _=this
				_.holder
					.stop()
					.animate({color:'#000'},{easing:_.easing,duration:_.duration})
			},
			hoverOut:function(){
				var _=this
				_.holder
					.stop()
					.animate({color:'#fff'},{easing:_.easing,duration:_.duration})
			}
		}
	})
		
	$('#content')._fw({
		sprites:{
			method:'corners',
			pasta:'<div class="_sprt"></div>',
			add:{
				tb:'images/content-tb-sides-sprite.png',
				lr:'images/content-lr-sides-sprite.png'
			}
		}
	})
		
	
	
	show_splash()
	
	
	$('#galls .thumbs').waitImgLoad(function(){
		var th=$('#galls'),
			thumbs=$('.thumbs',th)
		th.show()
		thumbs._fw({
			vSlider:{
				nextBu:'.gallery .next',
				prevBu:'.gallery .prev',
				mousewheel:true,
				easing:'easeInSine',
				duration:300
			}
		})
		$('>ul>li',thumbs).each(function(){
			var th=$(this),
				span=$('<span class="curr-mask"></span>')
					.css({
						position:'absolute',
						width:'100%',
						height:'100%',
						left:0,
						top:0,
						zIndex:1,
						background:'url(images/gallery-curr.png) 0 0 no-repeat',
						opacity:0
					}),
				tmp
			th.find('a')
				.css({position:'relative',display:'block'})
				.append(tmp=span.clone())
				.bind('mouseenter',function(){
					tmp
						.stop()
						.animate({opacity:1})
				})
				.bind('mouseleave',function(){
					if(th.is('.current'))
						return false
					tmp
						.stop()
						.animate({opacity:0})
				})
				.bind('click',function(){
					th.addClass('current').siblings().removeClass('current')
					.find('a .curr-mask').stop().animate({opacity:0})
					tmp.css({opacity:1})
				})
		})
		
		$('.titems li').each(function(i){
			$('.gallery .pic').eq(i)._fw({
				"_spinnerGallery":{
					pagination:'.titems>li:eq('+i+') .gallery .thumbs li',
					spinnerURL:'images/ajax-loader.gif',
					currentCl:'current'
				}
			})
		})
		
		$('.titems')._fw({
			tabs:{
				tabs:'#galls .tabs li'
			}
		})
		
		th.hide()
	})
	
})

function show_splash(){
	$('#total-spinner').hide()
	var duration=1200,
		easing='easeOutBack',
		nav=$('nav ul li').hide(),
		itms=[$('.itm1'),$('.itm2'),$('.itm3')],
		itmC=[{left:0,top:224},
			  {left:275,top:483},
			  {left:431,top:131}
			 ],
		itmS=[{left:-20,top:30},
			  {left:30,top:20},
			  {left:30,top:-20}],
		glob=$('#glob').css({overflow:'hidden'})
	$('#content').hide()
	itms[0]
		.show()
		.stop()
		.animate(itmC[0],{
			duration:duration,
			easing:easing
		})
	itms[1]
		.show()
		.stop()
		.animate(itmC[1],{
			duration:duration,
			easing:easing
		})
	itms[2]
		.show()
		.stop()
		.animate(itmC[2],{
			duration:duration,
			easing:easing,
			complete:function(){
				if($.browser.msie&&$.browser.version<9)
					nav.show()
				else
					nav.fadeIn()
				var k=241
				$('#main').css({top:(k/(document.body.offsetHeight/100))+'%'})
				$(itms).add(nav).each(function(){
					var th=$(this),
						top=parseInt(th.css('top'))-k
					th.css({top:top})
				})
				$(itmC).each(function(i){
					var tmp=this
					tmp.top=tmp.top-k
					itmC[i]=tmp
				})
				glob.css({overflow:'visible'})
			}
		})
	
	nav.each(function(i){
		$(this)
			.bind('mouseenter',function(){
				itms[i]
					.stop()
					.animate({
						left:itmC[i].left+itmS[i].left,
						top:itmC[i].top+itmS[i].top
					})			
			})
			.bind('mouseleave',function(){
				itms[i]
					.stop()
					.animate({
						left:itmC[i].left,
						top:itmC[i].top
					})			
			})
	})
	$('nav').one('click',function(){show_subpages()})
}

function show_subpages(){
	var duration=600,
		easing='easeOutBack',
		itms=[$('.itm1'),$('.itm2'),$('.itm3')],
		itmC=[{left:'-70px',top:'-200px',opacity:0},
			  {left:'1200px',top:'-200px',opacity:0},
			  {left:'-100%',top:'-100%',opacity:0}],
		glob=$('#glob'),
		nav=$('nav li')
		
		nav.unbind('mouseenter mouseleave')
		glob.css({overflow:'hidden'})
		$(itms).each(function(i){
			$(this)
				.stop()
				.animate(itmC[i],{duration:300,complete:function(){$(this).hide()}})
		})
		
		var prev=70,
			navMenu=$('nav'),
			offset=navMenu.offset(),
			prev=0,
			wdths=[],
			tmp
		navMenu
			.appendTo(glob)
			.css({
				left:offset.left,
				top:offset.top
			})
		nav.each(function(i){
			var th=$(this)
			wdths[i]=th.outerWidth()
			th
				.stop()
				.animate({
					top:0
				},{
					duration:duration,
					complete:function(){
						th
							.animate({
								left:prev
							},{
								easing:easing,
								duration:duration
							})
						prev+=wdths[i]
					}
				})
		})
		tmp=wdths[0]+wdths[1]+wdths[2]
		navMenu
			.stop()
			.width(tmp)
			.animate({
				top:60,
				left:document.body.offsetWidth-tmp-70
			},{
				duration:duration,
				complete:function(){
					navMenu
						.css({
							left:'auto',
							right:'70px'
						})
				}
			})		
}

function show_content(){
	var content=$('#content'),
		st={width:88,height:88,opacity:0},
		w=content.data('width')||content.width(),
		h=content.data('height')||content.height()
	content.data({width:w,height:h})
	$('#content .inner').show()
	$('#galls').hide()
	content
		.css(st)
		.show()
		.stop()
		.animate({
			width:w,
			height:h,
			opacity:1
		},{
			step:function(){
				content.css({
					marginLeft:'-'+(content[0].offsetWidth/2)+'px',
					marginTop:'-'+(content[0].offsetHeight/2-40)+'px'
				})
			},
			easing:'easeOutBack',
			duration:600
		})
}

function hide_content(){
	var content=$('#content'),
		st={width:88,height:88,opacity:0},
		w=content.data('width')||content.width(),
		h=content.data('height')||content.height()
	content.data({width:w,height:h})
	$('#content .inner').hide()
	content
		.show()
		.stop()
		.animate(st,{
			step:function(){
				content.css({
					marginLeft:'-'+(content[0].offsetWidth/2)+'px',
					marginTop:'-'+(content[0].offsetHeight/2-40)+'px'
				})
			},
			easing:'easeOutBack',
			duration:600
		},{
			complete:function(){
				$(this).hide()
				$('#galls').hide()
			}
		})
}

function show_gallery(){
	var gall=$('#galls'),
		st={width:88,height:88,opacity:0},
		w=gall.data('width')||gall.width(),
		h=gall.data('height')||gall.height()
	gall.data({width:w,height:h})
		.css(st)
		.show()
		.stop()
		.animate({
			width:w,
			height:h,
			opacity:1
		},{
			step:function(){
				gall.css({
					marginLeft:'-'+(gall[0].offsetWidth/2)+'px',
					marginTop:'-'+(gall[0].offsetHeight/2-40)+'px'
				})
			},
			easing:'easeOutBack',
			duration:600
		})
}

function hide_gallery(){
	var gall=$('#galls'),
		st={width:88,height:88,opacity:0},
		w=gall.data('width')||gall.width(),
		h=gall.data('height')||gall.height()
	gall.data({width:w,height:h})
	gall
		.show()
		.stop()
		.animate(st,{
			step:function(){
				gall.css({
					marginLeft:'-'+(gall[0].offsetWidth/2)+'px',
					marginTop:'-'+(gall[0].offsetHeight/2-40)+'px'
				})
			},
			easing:'easeOutBack',
			duration:600,
			complete:function(){
				$(this).hide()
			}
		})
}

