var isIE = '\v' === 'v';
function showModalForm(form) {
	var c = $('#container'),
		o = $('#overlay');
	if (!o.length) {
		o = $('<div id="overlay"></div>');
		c.append(o);
	} else {
		o.show()
	}
	if (!isIE) {
		form.fadeIn(450, function() {
			o.click( function() { closeModalForm(form) });
		});
	} else {
		form.show(24, function() {
			o.click( function() { closeModalForm(form) });
		});
	}
}
function closeModalForm(form) {
	var o = $('#overlay');
	if (!isIE) {
		form.fadeOut(450, function() { o.hide(); });
	} else {
		form.hide(24, function() { o.hide(); });
	}
}
$(function() {
	$('#fgtFormLink').click(function() {
		var form = $('#about');
		showModalForm(form);
		$('a.close', form).click(function() {
			closeModalForm(form);
			return false;
		});
		return false;
	});
});


var need_hide = true;
function hiding_pop()
{
	if(need_hide)
	{
		$('.selector_block3').hide();
	}
}

function hide_form()
{
	$('.form_table').hide();
	$('.form_table2').hide();
	$('#frame').attr("src","/?page=video_view");
	$('.fade').hide();
	$('#containerf').hide();
	$('.fade').hide();
}
function show_form()
{
	$('.form_table').show();
	$('.fade').show();
}


var cab_login_entered = false;
var cab_pass_entered = false;

function coloring()
{
	//if( cab_login_entered || cab_pass_entered )
	//	$('#b_cabinet').fadeIn(200);
	//		else $('#b_cabinet2').fadeOut(200);



	if( cab_login_entered && cab_pass_entered )
	{
		$('#b_cabinet2')[0].src = 'img/b_login2.gif';
	}
	else
	{
		$('#b_cabinet2')[0].src = 'img/b_login.gif';
	}

}


function changeImage(src,mainImage)
{
  mainImage.src = src;
}

$(document).ready(function(){

		$.ajax({
			'url':'/',
			'type':'post',
			'dataType':'html',
			'data':'f=addbasket',
			'success':function(data){
				$('.b-personal .b-personal-view .r').html(data);
			}
		});
        //$.ajax({'url':'/data/js/aj.js','type':'get','dataType':'script'});

	$(document).click(function(){
		$('div.contacts-popup').each(function(){
			$(this).css('display', 'none');
		});
	});

	$('.b-personal .b-personal-place .place-item-note p.place-note a[action=addoffer]').click(function(){
		$.ajax({
			'url':'/',
			'type':'post',
			'dataType':'html',
			'data':'f=addoffer',
			'success':function(data){
				$('.b-personal .b-personal-place').find('.place-item:first').append(data);
			}
		});
	});

	$('.view-item p a[action=popup]').click(function(){
		$('#'+$(this).attr('xid')).show(200);
	});

	$('.b-personal-view .view-item p a[action=extend]').click(function(){

		$.ajax({
			'url':'/',
			'type':'post',
			'dataType':'text',
			'data':'f=extend&article='+$(this).attr('article')
		});

		$(this).html('продлено');
	});

	$('.b-personal-view .view-item p a[action=edit]').click(function(){

		$.ajax({
			'url':'/',
			'type':'post',
			'dataType':'html',
			'data':'f=getItem&article='+$(this).attr('article'),
			'success':function(data){
				$('.b-personal .b-personal-edit').html(data);

				$('.b-personal-tab a').each(function(){
					$(this).removeClass('act');
				});

				$('.b-personal-view').hide();
				$('.b-personal-place').hide();
				$('.b-personal-edit').show();
			}
		});
	});

	$('.b-personal-view .view-item p a[action=delete]').click(function(){

		$.ajax({
			'url':'/',
			'type':'post',
			'dataType':'text',
			'data':'f=deleteItem&article='+$(this).attr('article')
		});

		$(this).parent().parent().parent().remove();
	});

	$('.view-item input[type=submit]').live('click',function(){
	   //alert($(this).parent().parent().serialize());
		$.ajax({
			'url':'/',
			'type':'post',
			'dataType':'html',
			'data':$(this).parent().parent().serialize(),
			'success':function(data){
			 //alert(data);
				$('.b-personal .b-personal-view .r').html(data);
                //$.ajax({'url':'/data/js/aj.js','type':'get','dataType':'script'});
                //$('.view-item input[type=submit]').click(function(){alert('111')});
			}
		});
		return false;
	});


	$('.dock').hover(
		function(){
		   if($(this).queue('fx').length > 1)
		    {
		    $(this).dequeue('fx')
		    return;
		    }
var height = $('.dock table')[0].offsetHeight;			$(this).animate({"height":height, "top":"-"+(height+51)}, 600, 'swing');

		},
		function(){

			$(this).animate({"height":"41px", "top":"-98px"}, 600, 'swing');
		}
	);


	$('img').hover(
		function()
		{
			if( (!$(this).hasClass('act')) && ($(this).attr('hover')) )
			{
				tmp = $(this).attr('src');
				$(this).attr('src', $(this).attr('hover'));
			}
		},
		function()
		{
			if( (!$(this).hasClass('act')) && ($(this).attr('hover')) )
				$(this).attr('src', tmp);
		}
	);

	if(typeof $(".popup_hint").tooltip == 'function') $(".popup_hint").tooltip({
		track: true,
		delay: 0,
		showURL: false,
		opacity: 1,
		fixPNG: false,
		showBody: " - ",
		top: 10,
		left: 5,
		maxWidth: 200
	});

	$('.prev_pics img').hover(
		function()
		{
			$(this).animate({"opacity":"1"}, 300, 'swing');
		},
		function()
		{
			if($(this)[0].sel) return;
			$(this).animate({"opacity":"0.3"}, 300, 'swing');
		}
	);

	$('.selector_block2, .selector_block2 div').hover(
		function()
		{
			need_hide = false;
			$('.selector_block3').show();
		},
		function()
		{
			need_hide = true;
			setTimeout( 'hiding_pop();', 100);
		}
	);

	$('.selector_block3').hover(
		function()
		{
			need_hide = false;
		},
		function()
		{
			need_hide = true;
			setTimeout( 'hiding_pop();', 100);
		}
	);


	$('.galery_block .pic_block img').hover(
		function()
		{
			$(this).animate({"opacity":"1"}, 300, 'swing');
			$(this).parent().parent().next().animate({"opacity":"1"}, 300, 'swing');
		},
		function()
		{
			$(this).animate({"opacity":"0.4"}, 300, 'swing');
			$(this).parent().parent().next().animate({"opacity":"0.4"}, 300, 'swing');
		}
	);


	$('.galery_block2 img, div.pic_block img').hover(
		function()
		{
			$(this).animate({"opacity":"1"}, 300, 'swing');
			$(this).parent().parent().next().animate({"opacity":"1"}, 300, 'swing');
		},
		function()
		{
			$(this).animate({"opacity":"0.4"}, 300, 'swing');
			$(this).parent().parent().next().animate({"opacity":"0.4"}, 300, 'swing');
		}
	);

	$('.galery_block3 td').hover(
		function()
		{
			$(this).animate({"opacity":"1"}, 300, 'swing');
		},
		function()
		{
			$(this).animate({"opacity":"0.4"}, 300, 'swing');
		}
	);


	$('#cab_login').keypress(
      function(e)
      {
	e = e ? e : window.Event;
	if(e.keyCode == 9)
	{
	 $('#pas_txt').hide();
	}
      }
);

	$('#cab_login').focus(
		function()
		{
			if( $(this).val() == 'ВВЕДИТЕ ВАШ ЛОГИН' )
			{
				$(this).val('');
				coloring();
			}
		}
	);
	$('#cab_login').blur(
		function()
		{
			if( $(this).val() == '' ) $(this).val('ВВЕДИТЕ ВАШ ЛОГИН');
			coloring();
		}
	);
	$('#cab_login').keyup(
		function()
		{
			if( $(this).val() == '' ) cab_login_entered = false;
				else cab_login_entered = true;
			coloring();
		}
	);



	$('#cab_pas').focus(
		function()
		{
			 $('#pas_txt').hide();
			coloring();
		}
	);
	$('#cab_pas').blur(
		function()
		{
			//if( $(this).val() == '' ) $(this).val('������� ��� ������');
			if( $(this).val() == '' ) $('#pas_txt').show();
			coloring();
		}
	);
	$('#cab_pas').keyup(
		function()
		{
			if( $(this).val() == '' ) cab_pass_entered = false;
				else cab_pass_entered = true;
			coloring();
		}
	);
	$('#cab_pas').keydown(
		function()
		{
			if( $(this).val() == '' ) $(this).attr('type', 'text');
				else $(this).attr('type', 'password');
		}
	);

	$('#pas_txt').click(
		function()
		{
			$(this).hide();
			$('#cab_pas').focus();
		}
	);



	$('#b_cabinet').hover(
		function()
		{
			if($(this).attr('src') == 'img/b_cabinet.gif') $(this).attr('src', 'img/b_cabinet1.gif');
		},
		function()
		{
			if($(this).attr('src') == 'img/b_cabinet1.gif') $(this).attr('src', 'img/b_cabinet.gif');
		}
	);
	$('#b_cabinet2').click(
	  function()
	  {
	      if(this.src.indexOf('b_login2.gif') > -1)
	      $('#partnersForm')[0].submit();
	  }
	)

	if( cab_login_entered && cab_pass_entered )
	{

		$('#b_cabinet').attr('src', 'img/b_cabinet.gif');
	}
	else
	{
		$('#b_cabinet').attr('src', 'img/b_cabinet0.gif');
	}

/*удаление первоначальной надписи-подсказки при выделении поля*/
    $('.txt_field, .txtarea_field').focus(function() {
		if($(this).attr("defvalue")==$(this).val() || $(this).attr("erMsg")==$(this).val()){
			$(this).val("");

		}
			if($(this).parent().hasClass('textarea_box')){
				$(this).removeClass("txt_field_a");
				$(this).parent().removeClass("textarea_box_a");
			}else{
				$(this).removeClass("txt_field_a");
				$(this).parent().removeClass("txt_field_box_a");
			}
    });

	$('.contact_btn').click(function(){
		validate_fields(null,true);
	});

	$('.txt_field, .txtarea_field').keyup(function() {
		var complete=validate_fields(null,false);
			if(complete){
	           $(".contact_btn").addClass("contact_btn_a");
	        }
	        else
	        {
	           $(".contact_btn").removeClass("contact_btn_a");
	        }
	});


    $('.txt_field, .txtarea_field').blur(function(){
       /*Восстановление надписей в полях, если туда ничего не введено.*/
	   name=$(this).attr("name");
	   value=$(this).val();
        if($(this).hasClass("no_active") )
        {
             if($(this).val()=="")
             {
                   if($(this).attr("erMsg")!=undefined){
					$(this).val($(this).attr("erMsg"));
					$(this).addClass('txt_field_a');
					if($(this).parent().hasClass('txt_field_box')) $(this).parent().addClass("txt_field_box_a");
					else if($(this).parent().hasClass('textarea_box')) $(this).parent().addClass("textarea_box_a");
				   }else if($(this).attr("defvalue")!=undefined){
						$(this).val($(this).attr("defvalue"));
				   }
             }
        }

       validate=validate_fields($(this),false);

		if(validate){
           $(".contact_btn").addClass("contact_btn_a");
        }
        else
        {
           $(".contact_btn").removeClass("contact_btn_a");
        }
    });


	$('.b_close_form, .b_close_form2').click(function(){
		hide_form();
	});

	$('.button4 a').click(function(){
		show_form();
		return false;
	});

	$('.check_td input:checkbox').click(function(){
		txt=$(this).parent().next().next().find("input:text");
		if($(this).attr('checked')==true){
			$(txt).attr("erMsg",$(txt).attr("defvalue"));
		}else $(txt).removeAttr("erMsg");

	});

	$('.fade').css('width',$(document).width()+'px');
	$('.fade').css('height',$.getDocHeight()+'px');
	$("#menu2 a.sm").mouseover(function(){
		$('.dropdown').hide();
		oDiv=$('#'+$(this).attr('subMenuID'));
		if(oDiv){
			$(oDiv).css('display','inline');
		}
	});
	$('.dropdown').mouseleave(function(){
		$(this).hide();
	});
	$("#menu2 a.current").click(function(){
		oDiv=$('#'+$(this).attr('subMenuID'));
		if(oDiv){
			if($(oDiv).css('display')=='inline') $(oDiv).hide();
			else $(oDiv).css('display','inline');
		}
		//return false;
	});
});


function validate_fields(obj,all){
	var validate=1;
      /*подсвечивание неверно заполненных полей*/
	 if(obj!=null){
		name=$(obj).attr("name");
		value=$(obj).val();
		if($(obj).attr('stype')=='num'){
		tst=/^([\d]+)$/.test(value);
		 if(tst!=true && $(obj).attr("erMsg")!=undefined)
         {
           $(obj).addClass("txt_field_a");
		   if($(obj).attr('erMsg')!=undefined) $(obj).val($(obj).attr('erMsg'));
		   if($(obj).parent().hasClass('txt_field_box')) $(obj).parent().addClass("txt_field_box_a");
		   validate=0;
         }
	  }



       if((name=="phone" || $(obj).attr('stype')=='phone') && $(obj).attr("erMsg")!=undefined)
       {
		tst=/^([\d\+\-\s\(\)]+)$/.test(value);
         if(tst!=true || value=="" || value==" " || $(obj).val()==$(obj).attr('erMsg'))
         {
           $(obj).addClass("txt_field_a");
		   if($(obj).attr('erMsg')!=undefined) $(obj).val($(obj).attr('erMsg'));
		   if($(obj).parent().hasClass('txt_field_box')) $(obj).parent().addClass("txt_field_box_a");
		   validate=0;
         }
         else
         {
           $(obj).removeClass("txt_field_a");
         }
       }
       if(name=="mail" || $(obj).attr('stype')=='email')
       {
		tst=/^([a-z_\-\.\d]+)+@+([a-z\-\d]+)+\.+([a-z]{2,4})$/i.test(value);
          if(tst!=true || value=="" || value==" " || $(obj).val()==$(obj).attr('erMsg'))
          {
            $(obj).addClass("txt_field_a");
			if($(obj).attr('erMsg')!=undefined) $(obj).val($(obj).attr('erMsg'));
			if($(obj).parent().hasClass('txt_field_box')) $(obj).parent().addClass("txt_field_box_a");
			validate=0;
          }
          else
          {
           $(obj).removeClass("txt_field_a");
          }
       }
       if(name=="name")
       {
          if(value=="" || value==" " || $(obj).val()==$(obj).attr('erMsg'))
          {
            $(obj).addClass("txt_field_a");
			validate=0;
          }
          else
          {
           $(obj).removeClass("txt_field_a");
          }
       }
	   }

	   /* testing fields */
	   $('.txt_field, .txtarea_field').each(function(){
				if(($(this).val()=="" && $(this).css("display")!="none" && $(this).attr('erMsg')!=undefined) ||
				$(this).val()==$(this).attr('erMsg') ||
				($(this).val()==$(this).attr('defvalue') && $(this).attr('erMsg')!=undefined)) {
					if(all==true){
						if($(this).parent().hasClass('textarea_box')){
							$(this).parent().addClass("textarea_box_a");
						}else{
							$(this).addClass("txt_field_a");
							$(this).val($(obj).attr('erMsg'));
							if($(this).parent().hasClass('txt_field_box')) $(this).parent().addClass("txt_field_box_a");
						}
					}
					validate=0;
				}else if(all==true && $(this).attr("name")!=name){
					if($(this).parent().hasClass('textarea_box')){
						$(this).removeClass("txt_field_a");
						$(this).parent().removeClass("textarea_box_a");
					}else{
						$(this).removeClass("txt_field_a");
						$(this).parent().removeClass("txt_field_box_a");
					}
				};

			if(all==true && $('.contact_btn').hasClass('contact_btn_a') && $(this).val()==$(this).attr('defvalue') && $(this).attr('erMsg')==undefined) $(this).val('');//cleaning
			});

	return validate;
}

$.getDocHeight = function(){
     var D = document;
     return Math.max(Math.max(D.body.scrollHeight,    D.documentElement.scrollHeight), Math.max(D.body.offsetHeight, D.documentElement.offsetHeight), Math.max(D.body.clientHeight, D.documentElement.clientHeight));
};


function showVideo(src,title){
if(title!=undefined) $('#video_title').html(title);
	$('#frame').attr("src",src);
	$('#containerf').css({"left": ($(window).width() / 2 - 320 ) + "px"});
	$('#containerf').css({'top' : ($(document).scrollTop() + 200)+'px'});
	$('.fade').show();
	$('#containerf').show();
}
function hideVideo(){
	$('#frame').attr("src","/?page=video_view");
	$('.fade').hide();
	$('#containerf').hide();
}

