$(document).ready(function(){
	//Cufon.now();

	$('.pieJs').pie();
	$('input.placeholder, textarea.placeholder').placeholder();
	$('.header td:last-child, .footer .link a:last').addClass('last-child');
	$('.cat_links li:first-child ').addClass('first-child');

	// движение облаков при движении мышкой
	var clouds = $('.clouds');
	$('body').mousemove(function(e){
		var width = $(this).width(),
			pos = (e.pageX-width/2)*100/width;
		clouds.css('background-position',(pos/100*7)+50+'% 100%');
	});

	// эффекты меню
	var active_menu = $('#menu a.active, table.a a.active');
	if (active_menu.length){
		var pos = active_menu.position();
		var ww = active_menu.width()/2+15;
		if(active_menu.parents('.a').length)
			$('#menu_circle').css({top:pos.top-215,left:pos.left+225+ww}).show();
		else
			$('#menu_circle').css({top:pos.top-234,left:pos.left+225}).show();
	}
	// логотип опускается
	$('#logo').css({top:-$(this).height()}).animate({top:0}, 2000, function(){
		// мишень
		$('#header-target').animate({top:0}, 1000, function(){
			i=1;
			target_interval = setInterval(function(){ 
				if (i==13) {
					$('#target-hole-big').show(); 
				}
				if (i==15){
					$('#target-hole0').show(); 
					clearInterval(target_interval);
				}
				$('#target-hole'+i).show(); 
				i++;
			}, 200);
		});
		
	});
	//$('#target-hole3').show(0, function(){});
	/*
	if($.browser.msie)
		$('#header-animte-1').show();
	else
		$('#header-animte-1').fadeIn(2000);
	*/
	// лампочка у спутника
	setInterval(function(){
		$('#header-animte-3_1').show().animate({opacity: 1}, 200, function(){
			$(this).delay(1000).animate({opacity: 0}, 200);
		});
	}, 1500);

	var hdimg2 = $('#header-animte-2 img');
	var hdimg2sp = 1500;
	hdimg2.eq(0).animate({top:0},hdimg2sp,function(){
		hdimg2.eq(1).animate({top:0},hdimg2sp-800,function(){
			hdimg2.eq(2).animate({top:0},hdimg2sp-600,function(){
				hdimg2.eq(3).animate({top:0},hdimg2sp-900);
			});
		});	
	});

	slide();
	
	// пушка облаков
	var dimi = 0;
	var dimi2 = 246;

	setInterval(function(){

		if(dimi >= dimi2)
		{
			dimi2--;
			$('#dim').css('backgroundPosition','0 '+(dimi2-246)+'px');
		} else {
			dimi++;
			$('#dim').css({height:dimi,top:dimi2-dimi});
		}
	},40);

	// воздушные шары

	function balloon1() {
		dimi = 310;
		b1timer = setInterval(function(){
			dimi--;
			$('.balloon1').css({top:dimi+'px'});
			if (dimi == -125)
				clearInterval(b1timer);
		},10);
		/*
		$('.balloon1').animate({top:'-150px'}, 8000, function(){
			$(this).css({top: '325px'});
			balloon2();
		});
		*/
	}
	
	function balloon2() {
		$('.balloon2').animate({top:'-150px'}, 8000, function(){
			$(this).css({top: '325px'});
			balloon3();
		});
	}
	
		function balloon3() {
		$('.balloon3').animate({top:'-150px'}, 8000, function(){
			$(this).css({top: '325px'});
			balloon4();
		});
	}
	
		function balloon4() {
		$('.balloon4').animate({top:'-150px'}, 8000, function(){
			$(this).css({top: '325px'});
			balloon1();
		});
	}
	//balloon1();
	
	var balloon = 0;
	var balloon2 = 310;
	setInterval(function(){

		if(balloon >= balloon2)
		{
			balloon2--;
			$('#balloon').css('backgroundPosition','0 '+(balloon2-310)+'px');
		} else {
			balloon++;
			$('#balloon').css({height:balloon,top:balloon2-balloon});
		}
	},20);
	

	// наружная реклама
	/*
	var logo_blink = 0;
	setInterval(function(){
		if(logo_blink == 0)
			logo_blink = 100;
		else
			logo_blink = 0;
		$('#logo_blink').css({backgroundPosition : '0 '+logo_blink+'%'});
	},1500);
	*/
	logo_blink = function() {
		setInterval(function(){
			$('#logo_blink').animate({opacity: 1}, 400, function(){
				$(this).delay(1000).animate({opacity: 0}, 400);
			});
			$('#logo_shadow').animate({opacity: 0.5}, 400, function(){
				$(this).delay(1000).animate({opacity: 1}, 400);
			});
		}, 1500);
	}
	if ($('.design').length) {
		logo_blink();
	}
	if ($('.production').length) {
		$('#logo_shadow').css("top", "381px").animate({top: '107px'}, 1200);
		$('.production .viveska').animate({top: '46px'}, 1200, function(){
			$('.production .viveska2').animate({top: '0'}, 1200, function(){
				$('.production .viveska3').animate({left: '0'}, 1200, function(){
					$('.production .viveska4').animate({left: '0'}, 1200);
					logo_blink();
				});
			});
		});
	}

	// шестеренки
	var irotate1 = $('#anime .a2,#anime .a3');
	var irotate2 = $('#anime .a4,#anime .a5')
	var irotate3 = $('#anime .a7,#anime .a8')

	setInterval(function () {
		irotate1.animate({rotate: '+=1deg'}, 0);
	},50);

	setInterval(function () {
		irotate2.animate({rotate: '-=1deg'}, 0);
	},25);

	setInterval(function () {
		irotate3.animate({rotate: '+=1deg'}, 0);
	},25);

	// глаза у совы
	var present_eyes = $('.present .eyes, .films .eyes, .videoadv .eyes');
	setInterval(function () {
		present_eyes.animate({rotate: '-=1deg'}, 0);
	},40);

	$('#logo-videoadv, #logo-vacancy').css({top:-$(this).height()}).animate({top:'-59px'}, 2000);

	// шляпа и трость
	$('.firstyle_1').animate({top:0},700,function(){
		setTimeout(function(){
			$('.firstyle_2').animate({top:0},700);
		},500)
	});
	
	// монстры
	var monstr_div = $('.monstr');
	var monstr_img = $('.monstr img');
	var step = 0;
	var monstr_src = '/img/monstr';
	var step_width = 20;
	var back = false;

	setInterval(function(){
		if (step==1){monstr_img.attr('src', monstr_src+'1_1.png');} 
		if (step==2){monstr_img.attr('src', monstr_src+'1.png');} 
		if (step==3){monstr_img.hide(); monstr_div.css('left', function(index, value){ return parseInt(value)+step_width+'px';}); monstr_img.attr('src', monstr_src+'2.png').show();} 
		if (step==4){monstr_img.attr('src', monstr_src+'2_1.png');} 
		if (step==5){monstr_img.attr('src', monstr_src+'2.png');} 
		if (step==6) {
			monstr_img.hide();
			monstr_div.css('left', function(index, value){ return parseInt(value)+step_width+'px';});
			monstr_img.attr('src', monstr_src+'1.png').show();
			if (parseInt(monstr_div.css('left')) < 70 && !back) {
				step_width = 20;
			} else if (parseInt(monstr_div.css('left')) > 30) {
				step_width = -20;
				back = true;
			} else {
				back = false;
			}
			step=0;
		}
		step++;
	}, 400);
	
	// медийная реклама
	function idea1() {
		$('.idea1').animate({top:0}, 1500, function(){
			$(this).delay(3000).animate({top:-$(this).height()}, 1500, function(){ idea2(); });
		});
	}
	
	function idea2() {
		$('.idea2').animate({top:0}, 1500, function(){
			$(this).delay(3000).animate({top:-$(this).height()}, 1500, function(){ idea3(); });
		});
	}
	
	function idea3() {
		$('.idea3').animate({top:0}, 1500, function(){
			$(this).delay(3000).animate({top:-$(this).height()}, 1500, function(){ idea4(); });
		});
	}
	
	function idea4() {
		$('.idea4').animate({top:0}, 1500, function(){
			$(this).delay(3000).animate({top:-$(this).height()}, 1500, function(){ idea5(); });
		});
	}
	
	function idea5() {
		$('.idea5').animate({top:0}, 1500, function(){
			$(this).delay(3000).animate({top:-$(this).height()}, 1500, function(){ idea1(); });
		});
	}
	
	idea1();
	
	// рука
	var rand = function (){
	  return Math.floor(Math.random() * (2000 - 1500 + 1)) + 1500;
	}
	
	for (i=1; i<=21; i++){
		$('.site-star'+i).animate({top:0}, rand());
	}
	
	setTimeout(function(){
		$('.site-hand-dashed').show(50).hide(100, function(){
			$(this).show(10);
			setTimeout(function(){
				$('.site-hand-dashed').hide().show(50).hide(100, function(){
					$(this).show(10);
					setTimeout(function(){
						$('.site-hand').fadeIn(1500);
					}, 500);
				});
			}, 1000);
		});
	}, 3000);
	
	$(".otheritems").smoothDivScroll({ 
		scrollableArea: "ul.scrollableArea",
		visibleHotSpots: "always"
	});
	
	$(".order_form form").submit(function(){
		if ($(".order_form input[name=name]").val() == "")
			$(".order_form td.result").html("Пожалуйста, укажите Ваше имя");
		else if ($(".order_form input[name=city]").val() == "")
			$(".order_form td.result").html("Пожалуйста, укажите Ваш город");
		else if ($(".order_form input[name=phone]").val() == "")
			$(".order_form td.result").html("Пожалуйста, укажите Ваш контактный телефон");
		else {
		
			$.post("/ajax?action=order",
			{ subj: $(".order_form input[name=subj]").val(),
			  name: $(".order_form input[name=name]").val(),
			  company: $(".order_form input[name=company]").val(),
			  city: $(".order_form input[name=city]").val(),
			  phone: $(".order_form input[name=phone]").val(),
			  email: $(".order_form input[name=email]").val(),
			  site: $(".order_form input[name=site]").val(),
			  comment: $(".order_form textarea[name=comment]").val()
			},
			function(data){
				if (data == 'ok'){
					$(".order_form td.result").html("Заявка успешно отправлена");
					$(".order_form form").get(0).reset();
				}
				else
					$(".order_form td.result").html(data);
			});
		}
		
		return false;
	});
});

$.fn.placeholder = function(){
	this.each(function(){
		$(this).attr('defaultvalue',$(this).val()).focus(function(){
			if($(this).val() == $(this).attr('defaultvalue'))
				$(this).val('');
		}).blur(function(){
			if($(this).val() == '')
				$(this).val($(this).attr('defaultvalue'));
		});
	});
}

$.fn.pie = function(a){
	if (window.PIE && $.browser.msie) {
		if(a === 'remove')
		{
			this.each(function() {
				PIE.detach(this);
			});
		} else
		{
			this.each(function() {
				PIE.attach(this);
			});
		}
	}
	return this;
}

function overlay(act){
	var overlay = $('#sm_overlay');
	if(act === 'hide'){
		overlay.hide();
		return false;
	}
	if(!overlay.length)
	{
		$('<div id="sm_overlay" class="sm_overlay"></div>').appendTo('body');
		overlay = $('#sm_overlay');
	}
	overlay.css({height:$('body').height(),opacity:0.5}).show();
}

function slide(){
	var slide = $('.slide_wrap'),
		prev = slide.find('.left'),
		next = slide.find('.right'),
		ul = slide.find('ul'),
		li  = slide.find('li'),
		width = null,
		speed = 1000,
		gint = null;
		
		li.each(function(){
			width += $(this).width();	
		});
		
		next.mousedown(function(){
			ul.animate({marginLeft:700-width},speed);
			return false;
		}).click(function(){
			return false;	
		}).mouseup(function(){
			ul.stop();	
		});
		
		prev.mousedown(function(){
			ul.animate({marginLeft:0},speed);
			return false;
		}).click(function(){
			return false;	
		}).mouseup(function(){
			ul.stop();	
		});
}

function em(box, server, zone, img)
{
 var at = String.fromCharCode(64);
 if (img == 1)
 	return '<a href="'+'mai'+'lto'+':'+box+at+server+'.'+zone+'">';
 else
 	return '<a href="'+'mai'+'lto'+':'+box+at+server+'.'+zone+'">'+box+at+server+'.'+zone+'<'+'/a>';
}
