var Txx = Txx || {};
var comments_count = new Array();
Txx.jsEnabled = document.getElementsByTagName && document.createElement && document.createTextNode && document.documentElement && document.getElementById;


Txx.tooltips = function(what){
	$("#body").append('<div id="tooltips">&nbsp;</div>');
	$(what).mousemove(function(e){
	  var title = this.title;
	  if(title != ""){
		  if(title.search(/&bull;/g) != -1){
			title.replace(/&bull;/g,'<br />');
		  }
		  $("#tooltips").html(this.title);
		  var left  = e.pageX+10;
		  var top 	= e.pageY+10;
		  var div_width = $("#tooltips").width();
		  var div_height = $("#tooltips").height();
		  //alert(win_width + " " + left  + " " + div_width);
		  if( win_width < ( left +  div_width + 10 ) ) {
			  left = left - div_width - 35;
		  }
		  if( win_height < ( top +  div_height + 10 ) ) {
			  top = top - div_height - 40;
		  }
		  $("#tooltips").css({'left' : left+"px", 'top' :top+"px", 'display' : 'block'}).fadeTo("fast",0.85).mouseout(function(){
      	$("#tooltips").fadeOut(5);
    }).click(function(){
      	$("#tooltips").fadeOut(5);
    });
	  }
    });
}

/**
 * Retrieves the absolute position of an element on the screen
 */
Txx.absolutePosition = function (el) {
  var sLeft = 0, sTop = 0;
  var isDiv = /^div$/i.test(el.tagName);
  if (isDiv && el.scrollLeft) {
    sLeft = el.scrollLeft;
  }
  if (isDiv && el.scrollTop) {
    sTop = el.scrollTop;
  }
  var r = { x: el.offsetLeft - sLeft, y: el.offsetTop - sTop };
  if (el.offsetParent) {
    var tmp = Txx.absolutePosition(el.offsetParent);
    r.x += tmp.x;
    r.y += tmp.y;
  }
  return r;
};

/**
 * Return the dimensions of an element on the screen
 */
Txx.dimensions = function (el) {
  return { width: el.offsetWidth, height: el.offsetHeight };
};

/**
 *  Returns the position of the mouse cursor based on the event object passed
 */
Txx.mousePosition = function(e) {
  return { x: e.clientX + document.documentElement.scrollLeft, y: e.clientY + document.documentElement.scrollTop };
};

Txx.ajaxload = function (module, params, id, loadimg){
	if(loadimg == undefined) loadimg = true;
	$.ajax({
      type: "GET",
      url: "/ajax/"+module+"?"+params,
	  data: params,
	  beforeSend: function(){
     	if(loadimg == true) $("#"+id).html("<div style=\"text-align:center;\"><img src=\"/images/loading-white-4AA0E0-big-roller.gif\" alt=\"loading\" />");
		/*$('html, body').animate({
			scrollTop: $("#"+id).offset().top
		}, 500);*/
	   },
      success: function (data) {
		//$("#"+id).css({ display:"none"}).fadeIn(1500);
		$("#"+id).html(data);
		//if(id == "ajaxid") { /*init();*/ Txx.setsize(); }
      },
      error: function (xmlhttp) {
		Txx.showbox('No Network', 'Transport error (#'+ xmlhttp.status +') while retrieving data from endpoint "/ajax/'+module+"?"+params+'": A network error occurred. Check that you are connected to the internet.');
        //$("#"+id).html('An HTTP error '+ xmlhttp.status +' occured.\n');
      }
    });
}

Txx.ajaxpost = function (module, params, id, loadimg){
	if(loadimg == undefined) loadimg = true;
	$.ajax({
      type: "POST",
      url: "/ajax/"+module,
	  data: params,
	  beforeSend: function(){
     	if(loadimg == true) $("#"+id).html("<div style=\"text-align:center;\"><img src=\"/images/loading-white-4AA0E0-big-roller.gif\" alt=\"loading\" />");
	  },
      success: function (data) {
		//$("#"+id).css({ display:"none"}).fadeIn(1500);
		$("#"+id).html(data);
		//if(id == "ajaxid") { /*init();*/ Txx.setsize(); }
      },
      error: function (xmlhttp) {
        Txx.showbox('No Network', 'Transport error (#'+ xmlhttp.status +') while retrieving data from endpoint "/ajax/'+module+'": A network error occurred. Check that you are connected to the internet.');
		//$("#"+id).html('An HTTP error '+ xmlhttp.status +' occured.\n');
      }
    });
}

Txx.ajaxbox = function(title,module,params){
	Txx.showbox(title,"<div style=\"text-align:center;\"><img src=\"/images/loading-white-4AA0E0-big-roller.gif\" alt=\"\" /></div>",true);
	Txx.ajaxload(module, params, "dialog", false);
}

Txx.panelajax = function(){
	$.ajax({
      type: "GET",
      url: "/ajax/komunita/?action=ajax",
      success: function (data) {
		$("#komunitypaneltemp").html(data);
  }});
}

Txx.delfriend = function(id,usernick){
	$("#dialog").attr('title','Zmazanie priateľa').html('Skutočne chete vymazať <b>'+usernick+'</b> s priateľov?').dialog({
			bgiframe: true,
			modal: true,
			resizable: false,
			draggable: false,
			width: 640,
			close: function(event, ui) { $(this).dialog('destroy'); },
			buttons: {
				'Nie': function() {
					$(this).dialog('destroy');
				},
				'Áno': function() {
					$.get("/ajax/profile",{ action:'delfriend', user:id } ,function(data)
						{
							$("#friend_"+id).fadeOut(1000);
							$("#friend_m_"+id).fadeOut(1000);
						}
					);
					$(this).dialog('destroy');
				}
			}

	});		
}

Txx.showbox = function(title, message){
	$("#dialog").attr('title',title).html(message).dialog({
			bgiframe: true,
			modal: true,
			resizable: false,
			draggable: false,
			width: 640,
			close: function(event, ui) { $(this).dialog('destroy'); },
			buttons: {
				'Zavrieť': function() {
					$(this).dialog('destroy');
				}
			}

	});
}

Txx.closebox = function(){
	$("#body").removeClass("overed");
	$("#shader").css("display","none");
	$("#box").css("display","none");
	//Txx.setsize();
}
Txx.editableArea = function(where,act){
	$('.editableArea').click(function (){
		var text = $(this).html();
		var pred = '<img src="/images/smilies/';	var za = '.gif">';
		text = text.replace(pred+'2'+za,':)');
		text = text.replace(pred+'1'+za,':D');
		text = text.replace(pred+'5'+za,':(');
		text = text.replace(pred+'9'+za,':P');
		text = text.replace(pred+'4'+za,';)');
		text = text.replace(pred+'115'+za,':-*');
		try {
			var lineb = text.match(/[^\n]*\n[^\n]*/gi).length;
		} catch(e) {
			var lineb = 0;
		}
		var id = $(this).attr('id');
		if(!lineb) lineb = 1;
		if(($(this).width()*0.98) < 25) {
			width = 26;
		} else {
			width = $(this).width()*0.98;
		}
		if($(this).children('textarea').length == 1) {
		} else if($(this).children('input').length == 1) {
		} else {
			if($(this).hasClass('line')){
				$(this).html('<input class="ui-state-default ui-corner-all ui-button"'+
				' id="area'+id+'" style="width:'+(width)+'px;" value="'+text+'" />');			
			} else {
				$(this).html('<textarea class="ui-state-default ui-corner-all ui-button"'+
				'id="area'+id+'" style="height:'+(lineb*30)+'px;'+
				'width:'+(width)+'px;">'+text+'</textarea>');
				//$('#area'+id).resizable();
			}
		}
			$(this).bind('dblclick',function() {
				var id = $(this).attr('id');
				var text = $('#area'+id).val();
				text = text.replace('\n','<br />');
				Txx.ajaxpost(where, "action="+act+"&id="+id+"&text="+encodeURIComponent(text), id);
				//$('#'+id).html(text);
				$(this).unbind('dblclick');
			});
			$('#area'+id).focus();
	});
}
Txx.komunitypanel = function(modul){
	//alert(modul);
	$.ajax({
      type: "GET",
      url: "/ajax/komunita/?action=panel&modul="+modul,
	  beforeSend: function(){
		  $('#komunitypas').slideUp('fast');
		  $("#komunitypanelloading").show();
		  $('#komunitypanelcontent').show();
	   },
      success: function (data) {
		$("#komunitypanelloading").hide();
		$('#komunitypas').slideDown('slow');
		$("#komunity-panel-minimize").hide('fast');
		$("#komunitypanelcontent").html(data);
      },
      error: function (xmlhttp) {
	    $("#komunitypanelloading").hide();
        //alert('An HTTP error '+ xmlhttp.status +' occured.\n');
		Txx.showbox('No Network', 'Transport error (#1001) while retrieving data from endpoint "/ajax/'+module+"?"+params+'": A network error occurred. Check that you are connected to the internet.');
      }
    });
}

Txx.komunitypanelclose = function (){
	$('#komunitypas').slideUp('fast');
	$("#komunity-panel-minimize").hide('fast');
}

Txx.komunitypanelmini = function (){
	$('#komunitypanelcontent').toggle('slow');
	$("#komunity-panel-minimize").toggle('fast').html("<b>Minimalizovné:</b> " + $('#komunitypanelcontent > h2').text());
}

Txx.panelresize = function(){
	$('#komunitypas').css('width',($('#komunitypanel').width()+10)+'px');
}
Txx.komunity_load = function(params){
	clearInterval( statusrefresh ) ;
	last_modul = params;
	Txx.ajaxload("komunita", "action=site&modul="+params, "komunity_content");
	Txx.komunity_scroll_to("komunity_content");
}

Txx.komunity_load_new_statuses = function(){
	$.get("/ajax/komunita",{ action:"site", modul:"new_statuses", load_new:true } ,function(data){
	  if(last_modul == "home"){
		$("#new_statuses").prepend('<div class="new">'+data+'</div>');
		$("#new_statuses > .new").fadeIn("slow").removeClass('new');
	  }
	});
}

Txx.komunity_load_old_statuses = function(){
	$("#lista_down_icon").html("<img src=\"/images/komunity-status-saving.gif\" alt=\"loading\" />");
	$.get("/ajax/komunita",{ action:"site", modul:"old_statuses", load_new:false } ,function(data){
	  if(last_modul == "home"){
		$("#old_statuses").append('<div class="old">'+data+'</div>');
		$("#old_statuses > .old").fadeIn("slow").removeClass('old');
		$("#lista_down_statuses").remove();
		$("#lista_down_icon").html("");
	  }
	});
}

Txx.komunity_set_status = function(datas){
	$.post('/ajax/komunita?action=panel&modul=newstatus&komunita=yes', datas, function(data){
		$('#id_status').val('');
		$('#temp_id').html(data);
		Txx.komunity_load_new_statuses();
	});
}

Txx.komunity_load_comments = function(id,user_id){
	$('#komunity_div_'+id).html("<div style=\"text-align:center;\"><img src=\"/images/loading-white-4AA0E0-big-roller.gif\" alt=\"loading\" />");
	$.get("/ajax/komunita",{ action:"site", modul:"load_comments", thread_id:id, uid:user_id } ,
	function(data){
		$('#komunity_div_'+id).html(data);
		Txx.komunity_scroll_to('komunity_div_'+id);
	});
}

Txx.komunity_load_thread = function(id){
	Txx.komunity_scroll_to("komunity_content");
	Txx.ajaxload("komunita", "action=site&modul=load_thread&thread_id="+id, "komunity_content");
	return false;
}

Txx.komunity_load_app = function(id){
	Txx.ajaxload("komunita", "action=site&modul=load_app&app_id="+id, "komunity_content");
	Txx.komunity_scroll_to("komunity_content");
}

Txx.komunity_load_notify = function(){
	$.getScript("/ajax/komunita?action=site&modul=push_notify");
}

Txx.komunity_post_thread = function(app,text,data,date,parm1,id){
	$.post("/ajax/komunita&action=site&modul=post_thread",{ 
		   action:"app",
		   app:app,
		   text:text,
		   data:data,
		   date:date,
		   parm1:parm1,
		   id:id},
		  	function(data){
				Txx.showbox("Bolo pridané na Vašu nástenku", "Vaša správa bola zverejnená");
			}
	);
}

Txx.komunity_play_video = function(div_id,video_id){
	$("#vid_"+div_id).html('<object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/'+video_id+'&hl=cs&fs=1&color1=0x006699&color2=0x54abd6"></param><param NAME="wmode" VALUE="transparent"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/'+video_id+'&hl=cs&fs=1&color1=0x006699&color2=0x54abd6" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" wmode="transparent" width="425" height="344"></embed></object></div>');
}

Txx.komunity_scroll_to = function(id){
	$('html').animate({
		scrollTop:200
	}, 1000);
}

Txx.komunity_actions = function(){
	$(".ui-komunita-activity.me").unbind('mouseenter mouseleave').hover(
      function () {
		 var kom_ac_text = '<div class="ui-komunita-actions">'+
		'<a href="#" onclick="Txx.ajaxbox(\'Zmazať vlákno\',\'komunita\',\'action=deltheard&id='+this.id+'\');return false;">'+
		'<span class="ui-icon ui-icon-circle-close" style="float: right; margin-right: .3em;"></span></a>'+
		'</div>';
		  //komunity259 alert(this.id);
        $(this).append(kom_ac_text);
      }, 
      function () {
        $(this).children('.ui-komunita-actions').remove();

      }
    );
	$(".ui-photo-micro").unbind('mouseenter mouseleave').hover(
      function () {
		 var kom_ut_text = '<div class="ui-komunita-usertab ui-corner-all ui-state-active">'+
		 '<img src="'+$(this).attr('src')+'" style="padding-right:5px;" align="left" /><div style="float:left;">'+
		 '<div class="action_utab"><a href="/user/'+$(this).attr('title')+'">'+$(this).attr('title')+'</a></div>'+
		 '<div><a href="/komunita/mail/new/'+$(this).attr('title')+'">Napísať správu</a></div>'+
		 '<div><a href="/komunita/wall/'+$(this).attr('title')+'">Napísať na nástenku</a></div>'+
		 '<div><a href="/user/'+$(this).attr('title')+'">Zobraziť profil</a></div>'+
		 '</div></div>';
		  //komunity259 alert(this.id);
        $(this).parent().parent().append(kom_ut_text).mouseleave(function() { $('.ui-komunita-usertab').remove(); });
      }, 
      function () {
        

      }
    );
}


Txx.komunityactionpanel = function() {
	$(".ui-dock-item").hover(
		function () {
			$(this).children('span').css('display',"block");
			$(this).children('img').animate({width:'60px'},200);
		}, 
		function () {
			$(this).children('span').css('display',"none");
			$(this).children('img').animate({width:'100%'},200);
		}
	);

}

$(document).ready(function() {
	(function() {
		//settings
		var fadeSpeed = 200, fadeTo = 0.5, topDistance = 30;
		var topbarME = function() { $('#headpanel').fadeTo(fadeSpeed,1); }, topbarML = function() { $('#headpanel').fadeTo(fadeSpeed,fadeTo); };
		var inside = false;
		//do
		$(window).scroll(function() {
			position = $(window).scrollTop();
			if(position > topDistance && !inside) {
				//add events
				topbarML();
				$('#headpanel').bind('mouseenter',topbarME);
				$('#headpanel').bind('mouseleave',topbarML);
				inside = true;
			}
			else if (position < topDistance){
				topbarME();
				$('#headpanel').unbind('mouseenter',topbarME);
				$('#headpanel').unbind('mouseleave',topbarML);
				inside = false;
			}
		});
		$('.ui-state-default').hover(
			function(){$(this).addClass('ui-state-hover');},
			function(){$(this).removeClass('ui-state-hover');}
		)
		.mousedown(function(){$(this).addClass('ui-state-active');})
		.mouseup(function(){$(this).removeClass('ui-state-active');})
		.mouseout(function(){$(this).removeClass('ui-state-active');});
	})();
	
});

// Global Killswitch on the <html> element
if (Txx.jsEnabled) {
  document.documentElement.className = 'js';
}
