var fl=false
$(document).ready(function() {
	// hover
	
	$('.submenu .img_act').css({opacity:0})

	$('#menu > li').hover(function(){
		$(this).find(' > span').stop().animate({height:50, top:0},'easeOutCirc')
	}, function(){
		if ((!$(this).hasClass('active'))&&(!$(this).hasClass('sfHover'))) {
			$(this).find(' > span').stop().animate({height:0, top:25},'easeOutCirc')
		}
	})
	
	$('.submenu > li').hover(function(){
		$(this).find('.img_act').stop().animate({opacity:1}, function(){$(this).css({opacity:'none'})})
	}, function(){
		if (!$(this).hasClass('active')) {
			$(this).find('.img_act').stop().animate({opacity:0})
		}
	})
	
	$('#icons .img_act').css({opacity:0})
	
	$('#icons a').hover(function(){
		$(this).find('.img_act').stop().animate({opacity:1})						 
	}, function(){
		$(this).find('.img_act').stop().animate({opacity:0})						 
	})
	
	
	$('ul#menu').superfish({
      delay:       600,
      animation:   {height:'show'},
      speed:       600,
      autoArrows:  false,
      dropShadows: false
    });
	$('#gallery1').gallery({
		amount:9
	})
	
	$('#gallery2').gallery({
		amount:6
	})
	
	$('#gallery3').gallery({
		amount:5
	})
	
	$('.gallery img').css({opacity:0})
	$('.gallery').css({display:'none'})
	
	$('.gallery a').hover(function(){
		$(this).parent().find('strong').css({display:'none'})							
		$(this).parent().find('img').stop().animate({opacity:1}, function(){$(this).css({opacity:'none'})})
	}, function(){
		$(this).parent().find('strong').css({display:'block'})						
		$(this).parent().find('img').stop().animate({opacity:0})
	})
	
	$(".gallery a").fancybox({
				'transitionIn'		: 'elastic',
				'transitionOut'		: 'none',
				'titlePosition' 	: 'over',
				'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
					return '<span id="fancybox-title-over">Image ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '</span>';
				}
	});
	
	$('.close strong').css({opacity:0})
	
	$('.close').hover(function(){
		$(this).find('span').stop().animate({opacity:0})					   
		$(this).find('strong').stop().animate({opacity:1}, function(){$(this).css({opacity:'none'})})
	}, function(){
		$(this).find('strong').stop().animate({opacity:0})					   
		$(this).find('span').stop().animate({opacity:1}, function(){$(this).css({opacity:'none'})})
	})
	
	$('#gallery_1 img, .gallery_2 img').each(function(){
		var r= Math.floor(Math.random()*11)+5;
		if (Math.random()-0.5<0) {r=-r}
		$(this).css({rotate:r+'deg'})
	})
	
	$('#gallery_1 img').hover(function(){
		$(this).stop().animate({rotate:'0deg', marginTop:-20})							   
	}, function(){
		var r= Math.floor((Math.random())*11)+5;
		if (Math.random()-0.5<0) {r=-r}
		$(this).stop().animate({rotate:r+'deg', marginTop:0})							   
	})
	
	$('.gallery_2 img').each(function(num){
		$(this).data({num:num}).css({zIndex:$('.gallery_2 img').length-num})
	})
	
	$('.gallery_2 img').hover(function(){
		k=1.2;
		if ($(this).parent().parent().hasClass('first')) {k=-0.5}
		$(this).stop().animate({rotate:'0deg', top:k*10},300, function(){
			$(this).css({zIndex:$('.gallery_2 img').length}).animate({top:0},300)													 
		})							   
	}, function(){
		k=1.2;
		if ($(this).parent().parent().hasClass('first')) {k=-0.5}
		var r= Math.floor((Math.random())*11)+5;
		if (Math.random()-0.5<0) {r=-r}
		$(this).stop().animate({rotate:r+'deg', top:k*10},200, function(){
			$(this).css({zIndex:$('.gallery_2 img').length-$(this).data('num')}).animate({top:0},200)													 
		})								   
	})
	
 });
$(window).load(function() {	
	
	// scroll
	$('.scroll').cScroll({
		duration:700,
		step:63,
		trackCl:'track',
		shuttleCl:'shuttle'
	})	
	
	
	// contact form
	$('#ContactForm').forms({
		ownerEmail:'#'
	})
	//content switch
	var content=$('#content'),
		nav=$('.menu');
	nav.navs({
		useHash:true
	})	
	nav.navs(function(n, _){
		content.cont_sw(n);
		if (_.n!=-1) {
			$('#menu > li').eq(_.n).find(' > span').stop().animate({height:50, top:0}).parent().siblings().find(' > span').stop().animate({height:0, top:25})
		} else {
			$('#menu > li').not('.sfHover').find(' > span').stop().animate({height:0, top:25})
			if (n.slice(0,13)!='#!/page_Works') {
				$('#menu > li').find(' > span').stop().animate({height:0, top:25})
			}
		}
	})
	content.cont_sw({
		showFu:function(){
			var _=this			
			$.when(_.li.find('.pad')).then(function(){
			$.when(_.li).then(function(){
				if (fl) {
					$('.submenu > li').eq(number-1).removeClass('active').find('.img_act').stop().animate({opacity:0}) 
					fl=false;
				}
				if (_.next.attr('id').slice(0,10)=='page_Works') {
					fl=true;
					$('#menu > li').eq(2).addClass('active').find(' > span').stop().animate({height:50, top:0});
					number=_.next.attr('id').slice(-1);
					show_gallery($('#gallery'+number));
					$('.submenu > li').eq(number-1).addClass('active').find('.img_act').stop().animate({opacity:1}, function(){$(this).css({opacity:'none'})})
				} else {
					$('#menu > li').eq(2).removeClass('active').not('.sfHover').mouseleave()
					if (_.next.attr('id')=='page_Contacts') {
						$('#map').html('<iframe width="262" height="262" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.com/maps/ms?msa=0&amp;msid=208689552347407329196.0004b78055ed5448e170d&amp;hl=tr&amp;ie=UTF8&amp;t=m&amp;iwloc=0004b78055f01ec86b466&amp;ll=41.074983,28.894683&amp;spn=0,0&amp;output=embed"></iframe>');
					}
					$.when($('.gallery li')).then(function(){
						_.next.css({display:'block'}).stop().animate({width:'100%', height:'100%', top:0, left:0},400,'easeOutCirc', function(){
							_.next.find('.pad').stop().animate({opacity:1}, function(){$(this).css({opacity:'none'})})																												  
						});	
					});
				}
			});
			});
		},
		hideFu:function(){
			var _=this
			if (fl) {
				hide_gallery($('#gallery'+number));	
			} else {
				_.li.find('.pad').stop().animate({opacity:'0'},600,'easeInCirc', function(){
					_.li.stop().animate({width:0, height:0, top:259, left:475},600,'easeInCirc')
				})	
			}
		},
		preFu:function(){
			var _=this
			_.li.css({position:'absolute', display:'none'});
			$('.gallery > div').css({display:'none'})
		}
	})
	nav.navs(2)
	
	var h_cont=817;
	function centre() {
		var h=$(document).height();
		if (h>h_cont) {
			m_top=~~(h-h_cont)/2;
		} else {
			m_top=0;
		}
		$('.center').stop().animate({paddingTop:m_top})
	}
	centre();
	$(window).resize(centre);
	
	/*$('a').click(function(){
		page = $(this).attr('href').slice(0,13)				  
		if 	(page=='#!/page_works')	{
			num = page.slice(-1);
		}
	})*/
	
})
