$(document).ready( function(){
	
	// фокус и блер
	
	$(".default-field").click( function() {
		 /*$(this).hide();*/
		 var id = new String($(this).find('span').attr('id'));
		 var field = id.replace("_value","");
		 $("#"+field).focus();
	});
	
	$(".form-field").bind({
		focus: function() {
		
		var id = new String($(this).attr('id'));
		$("#"+id+"_value").parents('div.default-field').hide();
		
	},
		blur: function() {
		
		if($(this).attr("value")=='') {
			var id = $(this).attr('id');
			$("#"+id+"_value").parents('div.default-field').show();
		}
		
		}
	});
	
	
	// checkboxes
	
	$(".checkbox, .checkbox-label").click( function() {
		//var parent = $(this).parents("ul.checkboxses");
		var subparent = $(this).parents("li.check_li");
		
		
		//parent.find('li');
		//убираем все отмеченные! - не убираем, так как чекбоксы
		//если была нажата отеченная, то все, если была нажата не отмеченная, то ее отмечаем
		var flag = $(this).hasClass('checkbox-label');
		var attr = flag ? subparent.find('img').attr("src"):$(this).attr("src");
		if(attr=='/i/checkbox_checked.jpg') {
			if(flag) {
				subparent.find('img').attr("src","/i/checkbox.jpg");
			} else {
				$(this).attr("src","/i/checkbox.jpg");
			}
			subparent.find("input[checkbox='true']").attr("value","0");
			subparent.find("dd").removeClass("checked");						
		} else {
			if (flag) {
				subparent.find('img').attr("src","/i/checkbox_checked.jpg");
			} else {
				$(this).attr("src","/i/checkbox_checked.jpg");
			}
			subparent.find("input[checkbox='true']").attr("value","1");
			subparent.find("dd").addClass("checked");
		}
	});
	
	
	//
	
	
	
	$("#count_field .count").bind({
		focus: function() {
		
		var id = $(this).attr("id");
		var value = $(this).attr("value");
		var default_value = $("#default-"+id).html();
		if (value==default_value) $(this).attr("value","");
		
	},
		blur: function() {
		
		var id = $(this).attr("id");
		var value = $(this).attr("value");
		var default_value = $("#default-"+id).html();
		if (value=='') $(this).attr("value",default_value);
		
		}
	});
	
	
	
	$(".idtumba").click( function() {
		//фотки 3!!!
		//так что не надо менять их местами!!!
		//просто подгружаем в главный контейнер!!
		var show_main = $("#show_main").length ? true:false;
		var el = $(this);
		var id = new String(el.attr('id'));
		var source = id.replace("tumba","tumba-source");
		source_img = new String($("#"+source).html());
		var source_link = source_img.replace("m_",""); 
		
		var mainimg_source = $("#mainimg").attr("src");
		var mainimg = new String(mainimg_source);
		mainimg = mainimg.replace("m_","ss_"); 
		
		
		$("#mainimg").attr("src",source_img);
		$("#mainlink").attr("href",source_link);
		
		//$('#mainlink').colorbox();
		
		//el.attr("src",mainimg);
		
		
		//$("#"+source).html(mainimg_source);
	});
	
	
	if($("a[rel='colorbox']").length){
		$('a[rel="colorbox"]').colorbox();
	}

	
	$(".more").click( function() {
		var parent = $(this).parent('div');
		$(this).hide();
		/*
		parent.find('div.hide').addClass('showed').fadeIn('slow', 
  		function() {
  			parent.find('span.close').show();
    	});
    	*/
		parent.find('div.hide').addClass('showed').animate({opacity: "show", height: "show"},'slow', 
  		function() {
  			parent.find('span.close').show();
    	});
	});
	
	$(".close").click( function() {
		var parent = $(this).parent('div');
		$(this).hide();	
		parent.find('div.showed').removeClass('showed').animate({opacity: "hide", height: "hide"},'slow', 
  		function() {
  			parent.find('span.more').show();
    	});	
	});
});

function loadform(alias)
{
 $('#'+alias).ajaxForm(
 { 
  dataType:  'json', 
  beforeSubmit: function(){
  		$("#formajaxloader").html("<img src=\"/i/ajax/orange.gif\" width=\"16\" height=\"16\" alt=\"\" />").show();
 		$("#disabled").show();
  },
  success: function(data) {
  	$("#inputresult").html(data.text);
  	$("#formajaxloader").empty();
  	$("#disabled").hide();
  	
 	
  	if(data.note) $("#inputresult").html(data.note);
  	
 	if(data.done==1) {
  		$("#submit_data").attr("disabled","disabled");
  		// ставим задержку, чтобы пользователь мог прочитать надпись
  		$("#formajaxloader").html("<img src=\"/i/ajax/orange.gif\" width=\"16\" height=\"16\" alt=\"\" />").show();
  		setTimeout(function(){
  			window.location = data.link;
  		}, 5000);
 	}
 	else {
  		$("#submit_data").attr("disabled","");
 	}
  }
 }
 );	
}

function check_length(param)
{
 //var tarea = new String($("#text_area").val());
 // alert(tarea.length);
 var length = $("#text_area").val().length;
 if(length>=param)
 {
  $("#text_area").val($("#text_area").val().substring(0,param));
   $("#ostatok").html("0");
 }
 else
 {
  var ostatok = param - length;
  $("#ostatok").html(ostatok);	
 }
}
