(function($) {
	$(document).ready(function(){
		var document_height = $('#main').height();
		var sliderCounter = 0;
		var widthPreview = 120;
		$('#menu > li').hover(
			function() {
				if($(this).children('a[title!=""]')) {
					$(this).children('ul').show();
				}
			},
			function() {
				$(this).children('ul').hide();
			}
		)
				
		$('#voteButton').bind('click',function() {
			var data = $('#voteForm').serialize();
			$('#shadow').show();
			$('#main').before('<div id="voteMessage" class="popup_window2"></div>');
			$.ajax({
				url:'/vote/addVote/',
				type:'POST',
				data:data,
				success:function(response) {
					scrollTo(0,0);
					$('#voteMessage').html(response).fadeIn();
					setTimeout(function() {
						$('#voteMessage').fadeOut();
						$('#shadow').hide();
					},3000);
					if(response == '<span class="ok">Спасибо, ваш голос учтен</span>') {
						setTimeout(function() {
							window.location='/vote/';
						},4000)
					}
				}
			})
		})

		$('#rightArrowClients').bind('click',function() {
			if((Math.ceil(($('#scrollClients ul li').length/5))-1) == sliderCounter) sliderCounter = 0;
			else sliderCounter++;
			$('#scrollClients ul').animate({
				marginLeft :'-'+5*(sliderCounter*widthPreview)+'px'
			},400);
		});
		
		$('#leftArrowClients').bind('click',function() {
			if(sliderCounter !== 0) sliderCounter--;
			else sliderCounter = Math.ceil(($('#scrollClients ul li').length/5))-1;
			$('#scrollClients ul').animate({
				marginLeft:'-'+5*(sliderCounter*widthPreview)+'px'
			},400);
		});
		
		$('#myClients a').bind('click',function() {
			var id = $(this).find('img').attr('alt');
			$('#myClients li').removeClass('active');
			$(this).parent().addClass('active');
			$.ajax({
				url: '/pages/getClient/'+id+'/',
				type:'POST',
				success: function(response) {
					$('#clientsInfoBlock').html(response);
				}
			})
		})

		$('h1').bind('click',function() {
			window.location = "/";
		});

		$('#shadow').bind('click',function() {
			$('.popup_window').fadeOut();
			$('.popup_window2').remove();
			$(this).hide();
		})

		$('#shadow').css('height',document_height+'px');
		
		$('#keywords .ajax_link').bind('click',function() {
			$('#keywords_slide').slideToggle('slow');
		});
		var params = {
			wmode:"opaque"
            }
		swfobject.embedSWF("/application/includes/votes/cup.swf", "flashVideo", "220", "220", "9.0.115", false, false, params,false);
	});
	
	check = function(id) {
		value = $('#'+id).val();
		if (value != '') 
			return 1;
		else {
			$('#'+id+'_error').html('Проверьте введенные данные').show();
			return 0;
		}
	}

	send_form = function(name,send) {
		var error = 0;
		var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		$('form[name="'+name+'"] *[required]').each(function() {
			if ($(this).val()=='') {
				error ++;
				$(this).addClass('notValid');
			} else if($(this).attr('name') == 'email' && !reg.test($(this).val())) {
				error++;
				$(this).addClass('notValid');
			} else 
				$(this).removeClass('notValid');
		});
		if(error !== 0) {
			$('form[name="'+name+'"] .error').remove();
			$('form[name="'+name+'"]').prepend('<span class="error">Проверьте введеные данные</span>');
		} else {
			var data = $('form[name="'+name+'"]').serialize();
			scrollTo(0,0);
			if(send == false) {
				$('#shadow').show();
				$('#main').before('<div id="submitForm" class="popup_window2"><img src="/application/includes/images/smallAjaxLoader.gif" /></div>');
				$.ajax({
					url:'/pages/showSubmitForm/',
					type: 'POST',
					data: data,
					success: function(response) {
						$('#submitForm').html(response);
						setTimeout(function() {
							questHeight = $('dd.quest').height();
							var height = parseInt(questHeight) + 180;
							$('#submitForm').css('visibility','visible');
							$('#submitForm').animate({
								'width':'350px',
								'height':height+'px',
								'marginLeft':'-175px'
							});
						},500);
					}
				})
			} else {
				$('#submitForm').remove();
				$('#main').before('<div id="submitForm" class="popup_window2" style="width:32px;"><img src="/application/includes/images/smallAjaxLoader.gif" /></div>');
				$.ajax({
					url:'/pages/feedback/',
					type: 'POST',
					data: data,
					success: function(response) {
						$('#submitForm').html(response).css('visibility','visible');
						$('#submitForm').animate({
								'width':'300px',
								'height':'50px',
								'marginLeft':'-150px'
							});
					}
				})
				setTimeout(function() {
					$('#submitForm').remove();
					$('#shadow').hide();
				},5000)
			}
		}
	}
	
	simple_gallery = function (id,note,width) {
		scrollTo(0,0);
		setTimeout(function() {
			$('#simple_gallery').html('<a href="javascript:void();" onclick="window_hide();">закрыть</a><img src="/application/includes/img/b/'+id+'.jpg" /><p>'+note+'</p>');
			},100);
		$('#simple_gallery').css('margin-left','-'+width/2+'px');
		$('#shadow').show();
		$('#simple_gallery').fadeIn();
	}
	
	window_hide = function() {
		$('.popup_window').fadeOut();
		$('.popup_window2').css('visibility','hidden');
		$('.popup_window2').removeAttr('style');
		$('#shadow').hide();
	}
	
	getFoto = function(id,direction,module_id,gallery_pid) {
		$('#simple_gallery').html('<img src="/application/includes/images/smallAjaxLoader.gif" />').fadeIn();
		$('#shadow').show();
		scrollTo(0,0);
		$.ajax({
			url: '/pages/gallery/'+id+'/'+direction+'/'+module_id+'/'+gallery_pid+'/',
			type: 'POST',
			success: function(response) {
				$('#simple_gallery').html(response);
				var width = $('#simple_gallery #mainImg img').attr('width');
				var height = $('#simple_gallery #mainImg img').attr('height');
				var widthGallery = parseInt(width)+100;
				var heightGallery = parseInt(height)+110;
				$('#simple_gallery #mainImg').css({
					'width':width+'px',
					'height':height+'px'
				});
				$('#simple_gallery').animate({
					'width':widthGallery+'px',
					'height':heightGallery+'px',
					'marginLeft':'-'+parseInt(widthGallery/2)+'px'
				},1500).fadeIn();
			}
		})
	}
	
	getResultVote = function(voteID,object) {
		var top = parseInt($(object).offset().top)-50;
		var left = $(object).offset().left;
		$('#resultVotes').remove();
		$.ajax({
			url:'/vote/getResultVote/'+voteID+'/',
			type:'POST',
			success:function(response) {
				$(object).after('<span id="resultVotes">'+response+'</span>');
				$('#resultVotes').css('display','block');
				/*css({
					'top':top+'px',
					'left':left+'px'
				}).*/
				$('#resultVotes').bind('mouseover',function() {
					$(this).show();
				})
				$('#resultVotes').bind('mouseout',function() {
					$(this).hide();
				})
			}
		})
	}
	
})(jQuery);