/**
 * handles the ajax response codes
 */
function handle_ajax_errors(s_status_code)
{
 
  switch (parseInt(s_status_code)) 
  {

    case 401:
    {
    
      document.location.href = $("#DOMAIN_NAME").val()+ "login/";
      break;
    }//cs
    
    
    case 404:
    {
    
      alert("not found");
      break;
    }//cs
     
         
    default:
    {
    
      //alert("error status:"+ s_status_code);  
      break;
     }//df
  }//sw

  return(false);
}//fun

function detect_browser()
{
  var s_resp = "FF";
  
  if (/MSIE (\d+\.\d+);/.test(navigator.userAgent))
  { 
  	//test for MSIE x.x;
    s_resp = "IE";
  }//if
  return(s_resp);
}//fun


function listener_main_hate_btn()
{
 
  var s_look4 = $("#hate_inp").val();
  if ($.trim(s_look4) != "")
  {
   
    s_look4 = encodeURIComponent(s_look4);
    document.location.href = $("#DOMAIN_NAME").val()+ "search/"+ s_look4+ "/";
  }//if
  else
  {
   
    alert("?");
  }//if
  return(false);
}//fun

function listener_delete_this_response(s_data)
{
 
  if ($.trim(s_data) == "OK")
  {
    
    document.location.href = $("#DOMAIN_NAME").val()+ "home/";
  }//if(s_data);
}//fun

function listener_hate_this_response(s_data)
{
  var a_data = s_data.split(";");
  var s_title = a_data[0];
  var s_msg = a_data[1];
  
  if (s_msg != "OK")
  {
    alert(a_data[2]);
    $("#"+ s_title+ "_hate_btn").css("visibility","visible");
  }//if
  else
  {
   
    alert(s_msg);
    $(".list_hated_comment_outer").html("");
    document.location.reload();
  }//if
  
  $("#"+ s_title+ "_loader").html("");
  
}//fun

function hate_this(s_title)
{
 
  var s_comment_text = $.trim($("#"+ s_title+ "_comment_container textarea").val());
  var s_loader_img = $("#loader_copy").html();
  $("#"+ s_title+ "_loader").html(s_loader_img);
  $("#"+ s_title+ "_hate_btn").css("visibility","hidden");
  
  if (s_comment_text.length > 950)
  {
   
    alert(s_msg_error_comment_too_long);
    $("#"+ s_title+ "_hate_btn").css("visibility","visible");
    //$("#"+ s_title+ "_hate_btn").css("display", "block");
    
    return(false);
  }//if
  
  
  //$("#modal_dialog").jqm({modal: true});
  //$("#modal_text").html(s_msg_wait_loading);
  //$("#modal_dialog").jqmShow();
      
  //send
  var a_data = {"s_title": s_title, "s_comment": s_comment_text};
  var s_url = $("#DOMAIN_NAME").val()+ "ihate/point/";
  
  ajax_petition(a_data, s_url,listener_hate_this_response);
  return(false);    
}//fun


function love_this(s_title)
{
 
  var s_comment_text = $.trim($("#"+ s_title+ "_comment_container textarea").val());
  var s_loader_img = $("#loader_copy").html();
  $("#"+ s_title+ "_loader").html(s_loader_img);
  $("#"+ s_title+ "_love_btn").css("visibility","hidden");
  
  if (s_comment_text.length > 950)
  {
   
    alert(s_msg_error_comment_too_long);
    $("#"+ s_title+ "_love_btn").css("visibility","visible");
    //$("#"+ s_title+ "_hate_btn").css("display", "block");
    
    return(false);
  }//if
  
  //$("#modal_dialog").jqm({modal: true});
  //$("#modal_text").html(s_msg_wait_loading);
  //$("#modal_dialog").jqmShow();
      
  //send
  var a_data = {"s_title": s_title, "s_comment": s_comment_text};
  var s_url = $("#DOMAIN_NAME").val()+ "ilove/point/";
  
  $.ajax({
      url: s_url,
      dataType: "text",
      cache: false,
      type: "POST",
      data: a_data,
      error: function(xmlhttp)
      {
        handle_ajax_errors(xmlhttp.status); 
      },
      success: listener_hate_this_response
    });
   
  return(false);    
}//fun



function hate_this1(s_title)
{
 
  $(".list_hate_btn1").css("display", "block");
  $("#"+ s_title+ "_hate_btn1").css("display", "none");
  $("#"+ s_title+ "_love_btn1").css("display", "block");
  
  $(".list_hated_comment_outer").html("");
  var s_comment_div = $("#comment_copy").html();
  $("#"+ s_title+ "_comment_container").html(s_comment_div);
  $("#"+ s_title+ "_comment_container .smallenv1 .yh2_button").attr("id", s_title+ "_hate_btn");
  
  $("#"+ s_title+ "_comment_container .smallenv2").css("display", "none");
  $("#"+ s_title+ "_comment_container .smallenv1").css("display", "block");  
  
  $("#"+ s_title+ "_comment_container textarea").keyup(function(e)
  {
  	$("#"+ s_title+ "_hate_btn span").html(s_btn_hate_value2);
  	listener_comment_textarea(s_title);
  });
  
  
  $("#"+ s_title+ "_hate_btn").click(function(e)
  {

  	hate_this(s_title);
  	return(false);
  });
  
  listener_highlight_inputs(".list_hated_comment_textarea");
  return(false);
}//fun


function love_this1(s_title)
{
 
  //$(".list_hate_btn1").css("display", "block");
  $("#"+ s_title+ "_love_btn1").css("display", "none");
  $("#"+ s_title+ "_hate_btn1").css("display", "block");
  
  //$(".list_hated_comment_outer").html("");
  var s_comment_div = $("#comment_copy").html();
  $("#"+ s_title+ "_comment_container").html(s_comment_div);
  $("#"+ s_title+ "_comment_container .smallenv2 .yh2_button").attr("id", s_title+ "_love_btn");
  
  $("#"+ s_title+ "_comment_container .smallenv1").css("display", "none");
  $("#"+ s_title+ "_comment_container .smallenv2").css("display", "block");
  
  $("#"+ s_title+ "_comment_container textarea").keyup(function(e)
  {
    $("#"+ s_title+ "_love_btn span").html(s_btn_love_value2);
    listener_comment_textarea(s_title);
  });
  
  
  $("#"+ s_title+ "_love_btn").click(function(e)
  {

    love_this(s_title);
    return(false);
  });
  
  listener_highlight_inputs(".list_hated_comment_textarea");
  return(false);
}//fun

function edit_this(s_title)
{
 
  var s_url = $("#DOMAIN_NAME").val()+ "ihate/modh/"+ s_title+ "/data/";
  document.location.href = s_url;
}//fun

function delete_this(s_title)
{
 
  if (confirm(s_msg_delete_question) == false)
  {
  	
  	return(false);
  }//if
  
  var s_url = $("#DOMAIN_NAME").val()+ "ihate/delh/"+ s_title+ "/";
  var a_data = {"auth": "fke"};
  $.ajax({
      url: s_url,
      dataType: "text",
      data: a_data,
      cache: false,
      type: "POST",
      error: function(xmlhttp)
      {
        handle_ajax_errors(xmlhttp.status); 
      },
      success: listener_delete_this_response
    });      
}//fun

function go_mod_media(s_title)
{
  var s_url = $("#DOMAIN_NAME").val()+ "ihate/modh/"+ s_title+ "/media/";
  document.location.href = s_url; 
}//fun

function listener_comment_textarea(s_title)
{
  var s_data = $.trim($("#"+ s_title+ "_comment_container textarea").val());
  
  var n_length = s_data.length;
  
  if (n_length > 950)
  {
   
    alert(s_msg_error_comment_too_long);
  }//if
  
}//fun

function listen_action_key(s_id_element, n_key, s_action_name)
{ 

 $("#"+ s_id_element).keydown(function(e)
 {
   var n_captured_key = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;
   if (n_captured_key == n_key)
   {
    
     eval(s_action_name)();
   }//if
 });  
}//fun

function listen_response_comments()
{
 
  $(".post_resp_btn a").click(function()
  {
  	var s_reply = $(this).attr("id");
  	a_reply = s_reply.split("_");
  	var s_idx = a_reply[1]+ "_"+ a_reply[2];
  	var s_fill_reply = '<textarea rows="3" cols="80"></textarea><a id="send_reply_btn_'+ a_reply[1] +'_'+ a_reply[2] +'" class="yh_button" href="#" onclick="send_reply(\''+ a_reply[1] +'\',\''+ a_reply[2] +'\'); return(false);" ><span>'+ s_send_btn+ '</span></a>';
  	$(".respan").css("display", "none");
  	$("#"+ "respan_"+ s_idx).html(s_fill_reply);
  	$("#"+ "respan_"+ s_idx).css("display", "block");
  	return(false);
  });  
}//fun

function send_reply_response(s_data)
{
  if ($.trim(s_data) == "OK")
  {
    document.location.reload();
  }//if
  else
  {
    alert(s_data);
  }//if
  
}//fun

function send_reply(nx,ny)
{
	var b_ok = true;
	var s_msg = "";
	//alert($("#send_reply_btn_"+ nx+ "_"+ ny).html());
  $("#send_reply_btn_"+ nx+ "_"+ ny).parent().css({'display':'none'});
  
  var s_com = $.trim($("#respan_"+ nx+ "_"+ ny+ " textarea").val());
  if (s_com == "")
  {
  	b_ok = false;
  }//if
  else if (s_com.length > 950)
  {
    s_msg = s_msg_error_comment_too_long;
    b_ok = false;
  }//if 
  
  if (b_ok == false)
  {
   
    if (s_msg != "")
    {
     
      alert(s_msg);
    }//if
    $("#send_reply_btn_"+ nx+ "_"+ ny).parent().css("display", "block"); 
    return(false);
  }//if
  
  ajax_petition({"s_com": s_com, "nx":nx, "ny":ny}, $("#DOMAIN_NAME").val()+ "ihate/reply/", send_reply_response);
}//fun

function listen_users_private_menu() 
{
  
	$(".menupu").click(function()
	{
		var s_name = $(this).attr("name");
		document.location.href = s_name;
		return(false);
	});
}//fun

function avoid_back_flickr()
{
	try {
  document.execCommand("BackgroundImageCache", false, true);
	} catch(err) {}
}//fun

function listener_highlight_inputs(s_identificator) 
{
  $(s_identificator).focus(function()
	{

		$(this).css({'background-color' : '#FFFEC0', 'font-weight' : 'normal', 'border': '2px solid #F5E0A2'});
	});
	$(s_identificator).blur(function()
	{

		$(this).css({'background-color' : 'white', 'font-weight' : 'normal', 'border': '1px solid #5D86BA'});
	});
	
}//fun

function ajax_petition(a_data, s_url, s_responsef)
{
  $.ajax({url: s_url, dataType: "text", cache: false, type: "POST",
    data: a_data, error: function(xmlhttp){handle_ajax_errors(xmlhttp.status); 
    },success: eval(s_responsef)});
}//fun

function punch_response(s_data)
{
  s_data = $.trim(s_data);
  $(".explo_icon").attr("class", "punch_icon");
  alert(s_data); 
}//fun

function favpost_response(s_data)
{
 
  alert(s_data);
}//fun

function punch_hater(s_id, s_x)
{
	$("#" +s_id).attr("class", "explo_icon");
	$("#" +s_id).fadeOut("slow");
  ajax_petition({"s_x":s_x},$("#DOMAIN_NAME").val()+ "ihate/punch/", punch_response);
  
  $("#" +s_id).fadeIn("slow");
}//fun

function favpost()
{
  ajax_petition({"z":"z"},$("#DOMAIN_NAME").val()+ "ihate/favpost/", favpost_response);
}//fun

function create_ban_text()
{
  if (window.s_msg_show_banned)
  {
    $(".ban_text").html(s_msg_show_banned);
    listen_banlinks();
  }//if
}//fun


function karmadetail_response(s_data)
{
  if ($.trim(s_data) != "")
  {
  }//if
}//fun

function getban_response(s_data)
{
  if ($.trim(s_data) != "")
  {
    var a_data = eval(s_data);
    $(".ban_text[name='"+ a_data[0]["s_id"]+ "']").html(a_data[0]["s_text"]);
    $(".ban_text[name='"+ a_data[0]["s_id"]+ "']").unbind("click");
  }//if
}//fun

function listen_banlinks(s_data)
{
  $(".ban_text").click(function(){
    ajax_petition({"s_id": $(this).attr("name")}, s_domain+ "ihate/get_banned_content/", getban_response);
  });
}//fun

function karmaset_response(s_data)
{
  if ($.trim(s_data) != "OK")
  {
    alert(s_data);
  }//if
}//fun

function listen_karmalinks()
{
  
  $(".karmaicon").click(function(){
    //ajax_petition({"s_id": $(this).parent().attr("id")}, s_domain+ "ihate/get_karmavotes/", karmadetail_response);
  });
  $(".karmaiconvote div").click(function(){
    var s_new_class = "hand_top2_btn";
    var s_erase_class = "hand_flop_btn";
    if ($(this).attr("name") == "n")
    {
      s_new_class = "hand_flop2_btn";
      s_erase_class = "hand_top_btn";  
    }//if
    
    var s_cont_id = $(this).parent().parent().attr("id");
    $("#"+ s_cont_id+ " .karmaiconvote "+ " ."+ s_erase_class).remove();
    $(this).hide();
    
    $(this).attr("class", s_new_class);
    $(this).fadeIn(800);
    
    ajax_petition({"s_id": s_cont_id, "s_t":$(this).attr("name")}, s_domain+ "ihate/set_karmavote/", karmaset_response);
  });  
  
}//fun

function round_corners(a_class, b_auto_pad)
{
  if (window.curvyCorners == null)
  {   
    return(false);
  }//if
  
  if (!b_auto_pad)
  {
   
    b_auto_pad = false;
  }//if
 
  var a_settings = {
	          tl: { radius: 10 },
	          tr: { radius: 10 },
	          bl: { radius: 10 },
	          br: { radius: 10 },
	          antiAlias: true,
	          autoPad: b_auto_pad
	      };
  $.each(a_class, function()
  {
    var s_class = $.trim(this);
    var myBoxObject = new curvyCorners(a_settings, s_class);
    myBoxObject.applyCornersToAll();  
  });
}//fun

function listen_logo()
{
  if (parseInt($("#B_CUSTOMIZED").val()) == 1)
  {
    return(false);
  }//if
  
  $("#logo").hover(
    function()
    {
      $(this).css("background-position", "0 -91px");
      $(this).animate({ opacity: 0.2 }, 400 );
    },
    function()
    {
      $(this).css("background-position", "0 0");
      $(this).animate({ opacity: 1.0 }, 300 );
    }
  );
}//fun

function listen_ajax_pager_response(s_data)
{
  $("#right_column_hates_module").html(s_data);
}//fun

function listen_ajax_pager(s_id, n_pag)
{
  ajax_petition({"n_pag":n_pag, "s_id":s_id, "s_par":$("#"+ s_id).attr("name")},
   s_domain+ "service/getdata4page/", listen_ajax_pager_response);
   $("#"+ s_id).parent().html("<div class='loader_cover'></div>");
}//fun

function listen_blog_comment_response(s_data)
{
  if ($.trim(s_data) == "OK")
  {
    document.location.reload();
  }//if
  else
  {
    alert(s_data);
  }//if
  $("#comment_blog_post_btn").css("visibility", "visible");
}//fun

function listen_blog_comment()
{
  $("#comment_blog_post_btn").click(function()
  {
    $(this).css("visibility", "hidden");
    var t = $("#comment_blog_post_text").val();
    var n = $("#comment_blog_post_text").attr("name");
    ajax_petition({"n":n, "t":t}, s_domain+ "service/blog_post_comment/", listen_blog_comment_response);    
  });
}//fun

var s_domain = "";
//---- PROGRAM ----
$(document).ready(function()
{
	
	s_domain = $("#DOMAIN_NAME").val();
	$("#hate_btn").bind("click", listener_main_hate_btn);
	avoid_back_flickr();
	listen_action_key("hate_inp", 13, "listener_main_hate_btn");
	listen_users_private_menu();
	listener_highlight_inputs(".input4form");
	listen_response_comments();
	create_ban_text();//listen inside
	listen_karmalinks();
	listen_logo();
	listen_blog_comment();
	
	round_corners(["activeicons", "titalert", "event_info"], true);
	
	//$(".left_module_cont").css("opacity", "0.7");	
	
});