jQuery(document).ready(function($){

    $('input.place-holder').each(function() {
      var $input = $(this);
      var value = $input.val();
      $input.focusin(function() {
        if ($input.val() == value) {
            $input.val('').removeClass('ph-active');
        }
      }).focusout(function() {
        if ($input.val() == '') {
            $input.val(value).addClass('ph-active');
        }
      });
      $input.addClass('ph-active');
    });

    $('#content ul[rel="gallery"]').wrap('<div class="gallery"><div></div></div>').closest('div.gallery').prepend('<a href="#" class="link-prev">prev</a>').append('<a href="#" class="link-next">next</a>').galleryScroll({
			step:1,
			duration:450
		});

		jQuery('div.g1').galleryScroll({
			step:1,
			duration:450
		});
		jQuery('div.g2').galleryScroll({
			step:1,
			duration:450
		});
		jQuery('div.g3').galleryScroll({
			step:1,
			duration:450
		});

		jQuery('div.g2 li').click(function(event)
		{
			event.preventDefault();

			jQuery('div.g2 li').removeClass('active')
			jQuery(this).addClass('active')

			jQuery('div.content-box > div[year]').hide();
			jQuery('div.content-box > div[year="'+jQuery(this).find('a').html()+'"]').show();
		});

		jQuery("ul.video-list a").click(function()
		{
			jQuery("#popup-video span").html(jQuery(this).parent().find("span").html());

			jwplayer("jwplayer").setup({
        		flashplayer: "/images/player.swf",
        		file: jQuery(this).attr("vsrc"),
        		height: 300,
       	 		width: 400
    		});

    		jQuery("#popup-video a.close,div.fader").click(function(){
    			jwplayer().stop();
    		});
		});





		jQuery("#director_letter").submit(function(event){
			event.preventDefault();

			if(!jQuery('form#director_letter').data('bValidator').isValid()){
				return false;
			}

			// Собираем все поля формы
			data = new Object();
			jQuery.each(jQuery("form#director_letter").serializeArray(),function(index,element){
				data[element.name] = element.value
			})

			// Отправляем
			jQuery.post("/",data,
			function(data)
			{


            	if(data.ERROR == 0)
            	{



            		jQuery("form#director_letter").slideUp(function(){

//						alert(data.MESSAGE);

						jQuery("div#director_letter div.popup-holder").append("<p class='result_message'>"+data.MESSAGE+"</p>");
            			jQuery("div#director_letter div.popup-holder p.result_message").slideDown();

            			jQuery("div.fader, a.close").click(function(){
            				setTimeout(function(){
            					jQuery("div#popup-2 div.popup-holder .result_message").remove();
            					jQuery("form#director_letter").show();
            					jQuery("form#director_letter input:visible, form#director_letter textarea").val("");
            				},400)

            			})
            		});
            	}

			},"json");
		});
		
		jQuery("#recommend").submit(function(event){
			event.preventDefault();

			if(!jQuery('form#recommend').data('bValidator').isValid()){
				return false;
			}

			// ???????? ??? ???? ?????
			data = new Object();
			jQuery.each(jQuery("form#recommend").serializeArray(),function(index,element){
				data[element.name] = element.value
			})

			// ??????????
			jQuery.post("/",data,
			function(data)
			{


            	if(data.ERROR == 0)
            	{



            		jQuery("form#recommend").slideUp(function(){

//						alert(data.MESSAGE);

						jQuery("div#popup-3 div.popup-holder").append("<p class='result_message'>"+data.MESSAGE+"</p>");
            			jQuery("div#popup-3 div.popup-holder p.result_message").slideDown();

            			jQuery("div.fader, a.close").click(function(){
            				setTimeout(function(){
            					jQuery("div#popup-3 div.popup-holder .result_message").remove();
            					jQuery("form#recommend").show();
            					jQuery("form#recommend input:visible, form#recommend textarea").val("");
            				},400)

            			})
            		});
            	}

			},"json");
		});

		jQuery("form#director_letter").bValidator({
			position: {x:'left', y:'top'},
			showCloseIcon: false,
			offset:{x:0, y:-3}
		});

		jQuery("form#zamer").bValidator({
			position: {x:'left', y:'top'},
			showCloseIcon: false,
			offset:{x:0, y:-3}
		});
		
		jQuery("form#recommend").bValidator({
			position: {x:'left', y:'top'},
			showCloseIcon: false,
			offset:{x:0, y:-3}
		});

		function showResponse(responseText, statusText, xhr, $form)  { 
    	jQuery("form#zamer").slideUp(function(){

    		jQuery("div#popup-1 div.popup-holder").append("<p class='result_message'>"+responseText.MESSAGE+"</p>");
    		jQuery("div#popup-1	div.popup-holder p.result_message").slideDown();

    		jQuery("div.fader, a.close").click(function(){
    			setTimeout(function(){
    				jQuery("div#popup-1 div.popup-holder .result_message").remove();
    				jQuery("form#zamer").show();
    				jQuery("form#zamer input:visible, form#zamer textarea").val("");
    			},400)

    		})
    	});
    	}

        var options = { 
//            beforeSubmit:  jQuery('form#zamer').data('bValidator').isValid(),  // pre-submit callback 
			target:        'div#popup-1 div.popup-holder',
            success:       showResponse,  // post-submit callback 
            dataType:  'json'        // 'xml', 'script', or 'json' (expected server response type) 
     
        }; 
     
        // bind form using 'ajaxForm' 
        jQuery('#zamer').ajaxForm(options); 


    	/*
    	without file

		jQuery("#zamer").submit(function(event){
			event.preventDefault();

			if(!jQuery('form#zamer').data('bValidator').isValid()){
				return false;
			}

			// Собираем все поля формы
			data = new Object();
			jQuery.each(jQuery("form#zamer").serializeArray(),function(index,element){
				data[element.name] = element.value
			})

			// Отправляем
			jQuery.post("/",data,
			function(data)
			{

            	if(data.ERROR == 0)
            	{

	            		jQuery("form#zamer").slideUp(function(){

						jQuery("div#popup-1 div.popup-holder").append("<p class='result_message'>"+data.MESSAGE+"</p>");
            			jQuery("div#popup-1	div.popup-holder p.result_message").slideDown();

            			jQuery("div.fader, a.close").click(function(){
            				setTimeout(function(){
            					jQuery("div#popup-1 div.popup-holder .result_message").remove();
            					jQuery("form#zamer").show();
            					jQuery("form#zamer input:visible, form#zamer textarea").val("");
            				},400)

            			})
            		});
            	}

			},"json");
		});
		*/

        jQuery(".btn-consultant").hover(function(){
        jQuery(".conslink").animate({"width": "+=95px"}, "slow");
        jQuery(this).toggleClass("active"); return false;
        },function(){
        jQuery(".conslink").animate({"width": "-=95px"}, "slow");
        jQuery(this).toggleClass("active"); return false;
        });


});
