$(document).ready(function()
{
 $('.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);
 }
 else $("#left").css("height",text_height);
 
 /* banner */
 /*
 $("#index_banner").mouseover( function() {
 	var el = $(this);
 	el.removeClass("backgr");
 	el.addClass("backgr180");
 	$("#banner_information").show();
 }).mouseout ( function() {
 	var el = $(this);
 	el.removeClass("backgr180");
 	el.addClass("backgr");
 	$("#banner_information").hide(); 	
 });
 */
 
 $(".span_dialog").mouseover( function () {
 	/*
 	var top =$(this).offset().top - 60;
 	var left =$(this).offset().left - 225;
 	problem with rel if first var!
 	*/
 	var top =$(this).offset().top - 60;
 	var left =$(this).offset().left + 55;
 	$("#dialog_hint").css( {
 		top: top+"px",
    	left:left+"px"
 	}).show();
 }).mouseout( function() {
 	$("#dialog_hint").hide();
 });
 
 $("#see").click( function () {
 	$("#formajaxloader").html("<div style=\"position:absolute;top:0px;left:120px;width:31px;height:31px\"><img src=\"/i/ajax/form.gif\" width=\"31\" height=\"31\" alt=\"\" /></div>");
 	$.getJSON(
 	'/anketa',
 	{
  		show_result: true
 	},
 	function(obj)
 	{
 	 if(obj.done==1) {
 	 	$("#anketa_source").fadeOut("slow", function callback() {
 	 		$("#formajaxloader").empty();
 	 		$("#anketa_source").html(obj.response);
 	 		$("#anketa_source").fadeIn("slow");
		});
 	 }
 	}
 	);
 });
 /* */
}
);


 function loadmap(x,y) {
	var map = new GMap2(document.getElementById("googlemap"));
	map.addControl(new GSmallMapControl());
	map.setCenter(new GLatLng(x,y), 14);

	var blueIcon = new GIcon(G_DEFAULT_ICON);
	blueIcon.image = "/i/redcross.jpg";
	blueIcon.iconSize = new GSize(20, 20);
	markerOptions = { icon:blueIcon };

	var point = new GLatLng(x,y);

	map.addOverlay(new GMarker(point, markerOptions));
 }

/* actions */

 function change(key) {
	$("input[name='sort_type']").attr("value",key);
 	$("form[name='items']").submit();
 }

function loadform(alias)
{
 $('#'+alias).ajaxForm(
 { 
  dataType:  'json', 
  beforeSubmit: function() {
  	$("#disabled div").show();
  },
  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%;margin-left:-15px;width:31px;height:31px\"><img src=\"/i/ajax/form.gif\" width=\"31\" height=\"31\" alt=\"\" /></div>");
  // ставим задержку, чтобы пользователь мог прочитать надпись
  setTimeout(function(){window.location = data.link;}, 3000);
 }
 else
 {
  $("#disabled div").hide();	
  $("#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%;margin-left:-15px;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/bluefon.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("");	
		if($("#imgtitle").length) $("#imgtitle").show();
	} else {
  		$("#imgsrc").attr("src",obj.src).load( function() {
  			$("#imgsrc").show("");	
  			if($("#imgtitle").length) $("#imgtitle").show();
  		});
	}
 });
}

function closeimg() {
	$('#bigimg').hide();
	$('#bigimgfon').hide();
	$('#imgsrc').hide();
}