$(document).ready(function()
{
 var text = 'Поиск по сайту';
 $('#searchfield').focus(function() 
 {
  if($(this).val() == text) $(this).val('');
 }
 ).change(function() 
 {
  if($(this).val() == '') $(this).val(text);
 }
 ).change();	
	
 $('.tumbhail').live("click",function(){
  var el = this;
  var params = $(el).attr('id').toString();
  params = params.split("_");
  var table = params[0];
  var id = params[1];
  
  if($(".prev_photo").length)  $(".prev_photo").hide();
  if($(".next_photo").length)  $(".next_photo").hide();
  
  show_photo(table,id);
  return false; //  Unobtrusive JavaScript
 });
 
 $("#bigimgbox").live("mouseover", function () { 
 		if($(".prev_photo").length)  $(".prev_photo").show();
 		if($(".next_photo").length)  $(".next_photo").show();
 }); 
 
 $("#bigimgfon").live("mouseover", function () { 
 		if($(".prev_photo").length)  $(".prev_photo").hide();
 		if($(".next_photo").length)  $(".next_photo").hide();
 });	
	
	
 $(".email").each(function()
 {
  var e = $(this).attr("href") + "@" + $(this).text();
  $(this).text(e);
  $(this).attr("href", "mailto:" + e);
 }
 );

 var left_height = $("#left").height();
 var text_height = $("#text").height();
 if(left_height>text_height) {
 	$("#text").css("height",left_height);
 	$("#right").css("height",left_height);
 }
 else {
 	$("#left").css("height",text_height);
 	$("#right").css("height",text_height);
 }
 
}
);

/* actions */

 function change(key) {
	$("input[name='sort_type']").attr("value",key);
 	$("form[name='items']").submit();
 }

function loadform(alias)
{
 $('#'+alias).ajaxForm(
 { 
  dataType:  'json', 
  success:   processJsonForm
 }
 );	
}

function processJsonForm(data)
{
 var style = data.style ? data.style:"";	
 $("#formajaxloader").empty();	
 $("#submit_data").attr("disabled","disabled");
 $("#inputresult").html("<div"+style+">"+data.text+"</div>");	
 if(data.done==1) 
 {
  $("#formajaxloader").html("<div style=\"position:absolute;top:10px;left:50%;width:31px;height:31px\"><img src=\"/i/ajax/form.gif\" width=\"31\" height=\"31\" alt=\"\" /></div>");
  // ставим задержку, чтобы пользователь мог прочитать надпись
  setTimeout(function(){window.location = data.link;}, 3000);
 }
 else
 {
  $("#submit_data").attr("disabled","");
  /*
  $("div#formdiv input:text").addClass("inputalert");
  $("div#formdiv input:password").addClass("inputalert");
  $("div#formdiv label").addClass("alert");
  */	
 }
 
}

function pollindicator()
{
 $("#pollajaxloader").html("<div style=\"position:absolute;top:-19px;left:120px;width:31px;height:31px\"><img src=\"/i/ajax/form.gif\" width=\"31\" height=\"31\" alt=\"\" /></div>");
}

function loadindicator()
{
 $("#inputresult").empty();	
 $("#formajaxloader").html("<div style=\"position:absolute;top:10px;left:50%;width:31px;height:31px\"><img src=\"/i/ajax/form.gif\" width=\"31\" height=\"31\" alt=\"\" /></div>");
 /*
 $("div#formdiv input:text").removeClass("inputalert");
 $("div#formdiv input:password").removeClass("inputalert");
 $("div#formdiv label").removeClass("alert");
 */
}

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));
 }
 else
 {
  var ostatok = param - length;
  $("#ostatok").html(ostatok);	
 }
}

function show_photo(table,id)
{
 var top =$(window).scrollTop();
 var windowheight = $(window).height();	
 var indicatortop = top - 16 + windowheight/2;
 // написать плагин для этого дела	
 var height = $("#container-page").height();	
 var width = $("#container-page").width();	
 $("#bigimgfon").css( {
 	width: width+"px",
    height:height+"px"
 });
 $("#bigimgfon").show();

 
 
 $("#bigimgbox").css("top",indicatortop);
 $("#bigimg").show();
 $("#bigimg").html("<div class=\"bigimgbox\" style=\"top:"+indicatortop+"px;background:none;\"><img src=\"/i/ajax/loadimg.gif\" alt=\"\" width=\"32\" height=\"32\" /></div>");
 $.getJSON(
 '/gallery',
 {
  id: id,
  table: table,
  top: top,
  height: windowheight,
  show_img: true
 },
 function(obj)
 {	
  $("#bigimg").html(obj.html);

  
  if ($.browser.msie && $.browser.version <= 6 ) {
  		$("#imgsrc").attr("src",obj.src)
		$("#imgsrc").show("");	
	} else {
  		$("#imgsrc").attr("src",obj.src).load( function() {
  			$("#imgsrc").show("");	
  		});
	}
 });
}

function closeimg() {
	$('#bigimg').hide();
	$('#bigimgfon').hide();
	$('#imgsrc').hide();
}

function redraw_calendar(direction)
{
 $("#calendar_snake").html("<img src=\"/i/ajax/small.gif\" alt=\"\" width=\"32\" height=\"32\" />");
 $("#calendar").load("/calendar/",
 {
  direction: direction
 }
 );
}
