$(document).ready(function()
{
 csshelp();
 //csshelp_bottom();
 $(".email").each(function()
 {
  var e = $(this).attr("href") + "@" + $(this).text();
  $(this).text(e);
  $(this).attr("href", "mailto:" + e);
 }
 );	 
 
 $("#blog_create").click( function() {
 	$("#blogloader").html("<div style=\"position:absolute;top:-14px;left:100px;width:16px;height:16px\"><img src=\"/i/ajax/black.gif\" width=\"16\" height=\"16\" alt=\"\" /></div>");
  	$.getJSON(
 	'/blog',
 	{
 	  title:$("#title").get(0).value,
 	  description:$("#description").get(0).value,
	  blog_create: true
 	},
 	function(obj) {
		$("#blogloader").html("<div style=\"position:absolute;top:-14px;left:100px;width:166px;height:16px\">"+obj.html+"</div>");
 	}
 	);	
 });
 
 
 if($("#popupbanner").length) {
 startPos = $("#popupbanner").position().top;
 divHeight = $("#popupbanner").outerHeight();
 $("#placeHolder").css("height", divHeight + "px")
 $(window).scroll(function () { 
 	var scrTop = $(window).scrollTop();
 	var windowheight = $(window).height();	
 	var height = $("#popupbanner").height();
 	var indicatortop = scrTop + windowheight/2-height/2;
 	var normal_position = $(window).height()/2-height/2;
 	
 	normal_position = normal_position <= 20 ? 20:normal_position;
 	
 	
	if ((startPos) < indicatortop) {
		if ($.browser.msie && $.browser.version <= 6 ) {
		topPos = startPos + (indicatortop - startPos);
		
		if (windowheight<=height-40) {
			indicatortop = indicatortop + 40;
			
		}
		$("#popupbanner")	.css("position", "absolute")
						.css("top", indicatortop +"px")
						.css('zIndex', '500')
		}
		else {
		$("#popupbanner")	.css("position", "fixed")
						.css("top", normal_position+"px")
						.css("zIndex", "500")
		}
	}
	else {
		$("#popupbanner")	.css("position", "static")
	}
	
 });
 }
}
);

function change(key) {
	$("input[name='sort_type']").attr("value",key);
 	$("form[name='items']").submit();
}

/*
* функция удаляет якоря из ссылок
*
*/
function delete_anchors(link)
{
 var link = new String(link);
 anchor = link.indexOf("#");
 if (anchor>0) link = link.substring(0,anchor)
 return link;	
}

function csshelp_bottom()
{
 var height = Math.max($("#bottomleft").height(),$("#bottom").height(),$("#bottomright").height());
 $("#bottomleft").css(
 { height: height+"px"}
 );
 $("#bottomright").css(
 { height: height+"px"}
 );
 height = height - 20;
 $("#heightit").css(
 { height: height+"px"}
 );		
}

function csshelp()
{
 var height = Math.max($("#left").height(),$("#center").height(),$("#right").height());
 $("#left").css(
 { height: height+"px"}
 );
 $("#right").css(
 { height: height+"px"}
 );	
 if ($("#center").height()<height)
 {
  $("#center").css(
  { height: height+"px"}
  );	 	
 }
}

function loadform(alias)
{
 $('#'+alias).ajaxForm(
 { 
  dataType:  'json', 
  success:   processJsonForm
 }
 );	
}

function processJsonForm(data)
{
 $("#inputresult").html(data.text);
 if(data.done==1) 
 {
  $("#submit_data").attr("disabled","disabled");
  // ставим задержку, чтобы пользователь мог прочитать надпись
  setTimeout(function(){window.location = data.link;}, 2000);
 }
 else
 {
  $("#submit_data").attr("disabled","");
 }
 $("#formajaxloader").empty();
 csshelp();
}

function loadindicator()
{
 $("#formajaxloader").html("<div style=\"position:absolute;top:5px;left:50%;width:16px;height:16px\"><img src=\"/i/ajax/black.gif\" width=\"16\" height=\"16\" alt=\"\" /></div>");
}

function redraw_calendar(direction)
{
 $("#calendar_snake").append("<img src=\"/i/calendar-snake.gif\" alt=\"\" width=\"16\" height=\"16\" />");
 $("#calendar").load("/calendar/",
 {
  direction: direction
 }
 );
}

function getLogin()
{
 $("#ajaxloader").html("<div style=\"position:absolute;top:67px;left:120px;width:16px;height:16px\"><img src=\"/i/ajax/black.gif\" width=\"16\" height=\"16\" alt=\"\" /></div>");
 $.getJSON(
 '/userlogin',
 {
  login:$("#trylogin").get(0).value,
  password:$("#trypassword").get(0).value,
  remember: $("#remember").get(0).checked ? "Y":"N",
  submit_login: true
 },
 function(obj)
 {
  $("#log_information").empty();
  $("#ajaxloader").empty();
  $("#log_information").append(obj.userstring);
  if(obj.reload==1) 
  {
   // Должны вырезать якорь!
   document.location.href= delete_anchors(document.location.href); 
  } 
  else
  {
   $("#login_result").html("<div style=\"position:absolute;top:-14px;left:55px;color:#9F1D1D;width:200px;font-size:11px;font-weight:bold;z-index:100\">Вы ввели неправильные данные!</div>");
  }
 }
 );
 //$('#loginblock').hide();
}

function getLogout()
{
 var html = $("#log_information").html();	
 $("#log_information").html(html + "<div style=\"position:absolute;top:89px;left:120px;width:16px;height:16px\"><img src=\"/i/ajax/orange.gif\" width=\"16\" height=\"16\" alt=\"\" /></div>");
 $.getJSON(
 "/userlogin/",
 {
  submit_logout: true
 },
 function(obj)
 {
  $.cookie("id_user_cookie", null);
  $.cookie("password_cookie", null);
  document.location.href=delete_anchors(document.location.href);	
 }
 );
}

function remind_password()
{
 $("#loginblock").animate({height: "hide"}, 300);
 $("#loginblock").load("/userlogin/",
 {
  remind_password: true
 }
 );
 $("#loginblock").animate({height: "show"}, 300);
}

function send_new_password()
{
 $("#ajaxloader").html("<div style=\"position:absolute;top:27px;left:125px;width:16px;height:16px\"><img src=\"/i/ajax/black.gif\" width=\"16\" height=\"16\" alt=\"\" /></div>");
 $.getJSON(
 '/userlogin',
 {
  login:$("#login_for_remind").get(0).value,
  email_password: true
 },
 function (obj)
 {
  alert(obj.note);
  $("#ajaxloader").empty();
  if (obj.send==1) $("#loginblock").hide();
 }
 );
}

function close_remainig()
{
 $("#loginblock").hide();
 $("#loginblock").load("/userlogin/",
 {
  reload_login_form: true
 }
 );
}

function anons_vote(param,id,type)
{
 $.getJSON(
 '/anons',
 {
  param: param,
  id: id,
  type: type,
  submit_vote: true
 },
 getvote
 );
}

function getvote(obj)
{
 if (obj.done==1)
 {	
  $("#raiting"+obj.id).html('<b>' + obj.rating + '</b>' + obj.text);
  //$("#onegalleryimg").html("<img src=\"/i/calendar-snake.gif\" alt=\"\" width=\"16\" height=\"16\" />");	
 }
 else
 {
  alert(obj.text);	
 }
}

function showgallery(id,number)
{
 var top =$(window).scrollTop();
 var windowheight = $(window).height();	
 var indicatortop = top - 16 + windowheight/2;
 var height = $("#container-page").height();	
 var width = $("#container-page").width();
 height = (height<top+700) ? top+700:height;
 // 700 - высота блока с фоткой
 $("#bigimgfon").css( {
 	width: width+"px",
    height:height+"px"
 });
 $("#bigimgfon").show();
 $('#ajaxgallery').show();
 $('#ajaxgallery').css("top",top+"px");
 $("#galleryajaxloader").html("<div style=\"position:absolute;top:200px;left:345px;width:16px;height:16px\"><img src=\"/i/ajax/orange.gif\" width=\"16\" height=\"16\" alt=\"\" /></div>");
 $.getJSON(
 '/gallery',
 {
  id: id,	
  number: number,
  show_img: true
 },
 function(obj) {
  	$("#onegalleryimg").html(obj.note);
 	$("#gallerynavy").html(obj.navy);	
 	$("#galleryarrows").html(obj.arrows);
 	$("#galleryajaxloader").empty();	
 });	
}

function close_img() {
	$('#ajaxgallery').hide();
	$("#bigimgfon").hide();
	$('#onegalleryimg').empty();
	$('#ajaxloader').empty();
}

function movegallery(id)
{
 $.getJSON(
 '/gallery',
 {
  id: id,	
  show_img: true
 },
 function(obj) {
  	$("#onegalleryimg").html(obj.note);
 	$("#gallerynavy").html(obj.navy);	
 	$("#galleryarrows").html(obj.arrows);
 	$("#galleryajaxloader").empty();	
 }
 );		
}

function movement(direction)
{
 // $("#testdiv").scrollLeft() - плагин!
 delta = $("#box").width()-$("#move").width();
 delta = delta<0 ? delta:0; 
 $("#move").animate({left : direction=='left' ? delta:0}, 1000);
}

function stopmovement()
{
 $("#move").stop();
}

function showsubmenu(id)
{
 if($("#submenu"+id).get(0).style.display=='block')
 {
  closeall();	
  //$("#li"+id).removeClass("selected");
  $("#submenu"+id).css(
  {
   display: 'none'	
  }
  );
 }
 else
 {
  closeall();	
  //$("#li"+id).addClass("selected");
  //alert($("#parent"+id).offset().left);
  var w = $(window);
  var left = $("#parent"+id).offset().left+15;
  var top =  $("#parent"+id).offset().top+25;
  if(w.width() < left + 300) left = w.width()-300; 
  $("#submenu"+id).css(
  {
   display: 'block',
   left: left+"px",
   top: top+"px"
  }
  ); 	
 }
}

function showrightmenu(id)
{
 if($("#rightmenu"+id).get(0).style.display=='block')
 {
  closeall();	
  //$("#li"+id).removeClass("selected");
  $("#rightmenu"+id).css(
  {
   display: 'none'	
  }
  );
 }
 else
 {
  //closeall();	
  //$("#li"+id).addClass("selected");
  //alert($("#parent"+id).offset().left);
  var left = $("#rparent"+id).offset().left-280;
  var top =  $("#rparent"+id).offset().top+0;
  $("#rightmenu"+id).css(
  {
   display: 'block',
   left: left+"px",
   top: top+"px"
  }
  ); 	
 }
}

function closeall()
{
 //$("li.selected").removeClass("selected");	
 $("div.topsubmenu").css(
 {
  display: 'none'	
 }
 );
}

function unique_login()
{
 $.getJSON(
 '/registration',
 {
  login:$("#login").get(0).value,
  check_login: true
 },
 getloginunique
 );
}

function getloginunique(obj)
{
 $("#checklogin").empty();
 if(obj.param==1)
 {
  $("#submitregistration").attr("disabled","");
  $("#checklogin").append("<span style=\"color:green\">ок</span>");
 }
 else
 {
  $("#submitregistration").attr("disabled","disabled");
  $("#checklogin").append("<span style=\"color:red\">логин занят</span>");
 }
}

function oldpassword_enter()
{
 $("#oldpassword_result").empty();
 $("#oldpassword_result").append("<img src=\"/i/loader.gif\" alt=\"\" width=\"16\" height=\"16\" />");
 $("#oldpassword_result").load("/users/",
 {
  oldpassword:$("#oldpassword").get(0).value,
  check_oldpassword: true
 }
 );
}

function passwords_compare()
{
 //$("#passwords_compare").empty();
 //$("#passwords_compare").append("<p>1</p>");
}

/* функции для модерации сообщений  + блог */ 
function editmessage(id) {
	
}

function delete_blog_note(id) {
	$("#delete_dialog"+id).toggle();
}

function delete_blog (id)
{
 $.getJSON(
 '/blog',
 {
  id: id,	
  delete_blog: true
 },
 function (obj)
 {
  if(obj.done==1) document.location.href=delete_anchors(document.location.href);  
  else {
   alert(obj.alert); 	
  }
 }
 );		
}

function deletemessage(id)
{
 $.getJSON(
 '/comments',
 {
  id: id,	
  delete_message: true
 },
 function (obj)
 {
  if(obj.done==1) document.location.href=delete_anchors(document.location.href);  
  else {
   alert(obj.alert); 	
  }	
 }
 );		
}
function trashmessage(id)
{
 $.getJSON(
 '/comments',
 {
  id: id,	
  trash_message: true
 },
 function (obj)
 {
  if(obj.done==1) document.location.href=delete_anchors(document.location.href);  
  else {
   alert(obj.alert); 	
  }
 }
 );		
}
/* */

function CountClick(id,link)
{
 $.getJSON(
 '/',
 {
  id: id,
  link: link,
  count_clicks: true
 },
 function(obj)
 {
  window.location = obj.result;
 }
 );	
}

/* */
function loadpopupbanner()
{
 $.getJSON(
 '/',
 {
  show_popup: true
 },
 function(obj)
 {
  if(obj.done)
  {
   $("#popupbanner").html(obj.banner);
   //var margin = (obj.width/2).toFixed(2);
   var w = $(window).width();
   var h = $(window).height();
   var left=((w-obj.width)/2).toFixed(0);
   var top=((h-obj.height)/2).toFixed(0);
   var html=$("#popupbanner").html(); 
   $("#popupbanner").css(
   {
    display: 'block',
    width: obj.width+"px",
    height: obj.height+"px",
    left: left+"px",
    top: top+"px"
   }
   );
   // спустя пару секнд добавим кнопку закрытия
   setTimeout(function(){
   $("#popupbanner_close_button").html(obj.close);
   var left = obj.width-61;
   $("#popupclose").css(
   {
    left: left+"px"
   }
   ) 
   }, 2000);
   //
  }
 }
 );	
}

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 vote(poll,punkt)
{
 $.getJSON(
 '/poll',
 {
  poll: poll,
  punkt: punkt,
  submite_vote: true
 },
 function (obj)
 {
  if(obj.done==1) document.location.href=delete_anchors(document.location.href);  
  else 
  {
   alert(obj.alert); 	
  }
 }
 );			
}