$(document).ready(function()
{		
	// video
	$("img[title='video']").parent().fancybox({
		'hideOnContentClick':false,
		'frameWidth':500,
		'frameHeight':380
	});
	   
	//fancybox
	$("a.show_img").fancybox({ 'overlayShow' : true });

	//ajax stop for gallery
	$("#ajax_gallery").ajaxStop(function(){
		$("a.show_img").fancybox({ 'overlayShow' : true });
		});	

	// Меням размер объектов в случае, если разрешение экрана по ширине меньше либо равно 1024px
	function resize()
	{
		var bodyWidth = $('html').width();
		//function obj_size 
		if(bodyWidth <= 1280) 
		{
			$("img[id='dynamic_img']").css({'width':'350px'});			
		}
	}
	// Стартуем функцию ресайза объектов
	resize();
	
	//$("a.show_img").fancybox({ 'overlayShow' : true });	
	// Page Limiter
	$("a.page_num").click(function()
	{
		// Обнуляем CSS для стилей кнопок страниц
		$(".page_selected").removeClass("page_selected");
		// Меняем стиль
		 $(this).parent().addClass("page_selected");
		//Если уходим с первой странице, то возвращаем ее линку правильный стиль :-)
		if(this.id > 1)
		{
			$("span:has(a[id='1'])").addClass("page");
		}
		//ajax-запрос
		var album_id=$("#album_id").val();					
		$("#ajax_gallery").load('/mod/photo_gallery/ajax_gallery.php?ajax=do&id='+album_id+'&page='+this.id);
		return false;
	});	
	
	// contact form
	var form_exists = $('#contact_form_container').size() == 1 ? true : false;
	if(form_exists){
		// validate
		var form = $('.contact_form');
		validateContactForm(form);
		$('.contact_message').keyup(function(){
			$('.person_panel').slideDown(300);									 
		});
		
		/* tomato joke :-) */
		preloadContactImages();
		
		$('.action_director').click(function(){
			$('.director').attr('src',ContactImg["director_in_action"].src);								   
		});
		
		$('.action_education_head').click(function(){
			$('.education_head').attr('src',ContactImg["education_head_in_action"].src);									   
		});
		
		// Tomato counter :D
		var tomato_counter = [];
		tomato_counter[1] = 0; // director
		tomato_counter[2] = 0; // education head
		
		$('.throw_tomato').click(function(){
			if(tomato_counter[1] >= tomato_counter[2]){
				$('.action_education_head').trigger('click');
				tomato_counter[2] += 1;
				
			}else{
				$('.action_director').trigger('click');
				tomato_counter[1] += 1;
			}
			return false;
		});
	}	
});

/* Валидатор формы */
function validateContactForm(form)
{	
	$.validator.methods.equal = function(value, element, param) {
		return value == param;
	};

	form.validate({
		submitHandler: function(form) 
		{
			var message = $(".contact_message").val();
			var name = $(".input_contact_name").val();
			var contact = $(".input_contact_area").val();
									  
			$.post("/mod/contact/send_message.php",{'message':message,'name':name,'contact':contact},function(result){
				// rest form
				$(".contact_message,.input_contact_area,.input_contact_name").val('');
				$('.answer').html('<div class="answer_good">Спасибо! Ваше сообщение отправлено :-)</div>');
				$('.answer').slideDown(300);
			});
		},					   
		rules : {
			contact_message : {required : true, minlength: 5, maxlength: 1000},
			name : {required : true, minlength: 2, maxlength: 50},
			contact_area : {required : true, maxlength: 100, email:true}
			},
				// Сообщения
		messages : {
			contact_message : {required : "Хммм... а текст? :-)",minlength : "Слишком мало букв :-)",maxlength : "Слишком много букв 0_O"},
			name : {required : "*",minlength : "*",maxlength : "*"},
			contact_area : {required : "*",maxlength : "*", email : '*'}
			},
		errorPlacement: function(error, element)
		{
			var er = element.attr("name");
			error.appendTo(element.parent().find("label[for='" + er + "']").find("em"));
			$('.answer').css('display','none');
		}
	});
}

function preloadContactImages()
{
	if (document.images) {	
		ContactImg = new Object();
		ContactImg["director_in_action"] = new Image(120, 160);	
		ContactImg["director_in_action"].src = "/userfiles/director_tomato.gif";	
		ContactImg["education_head_in_action"] = new Image(120, 160);	
		ContactImg["education_head_in_action"].src  = "/userfiles/education_head_tomato.gif";	
	}	
}

