jQuery.fn.center = function (absolute) {
	return this.each(function () {
		var t = jQuery(this);

		t.css({
			position:	absolute ? 'absolute' : 'fixed', 
			left:		'50%', 
			top:		'50%', 
			zIndex:		'99'
		}).css({
			marginLeft:	'-' + (t.outerWidth() / 2) + 'px', 
			marginTop:	'-' + (t.outerHeight() / 2) + 'px'
		});

		if (absolute) {
			t.css({
				marginTop:	parseInt(t.css('marginTop'), 10) + jQuery(window).scrollTop(), 
				marginLeft:	parseInt(t.css('marginLeft'), 10) + jQuery(window).scrollLeft()
			});
		}
	});
};

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

function placeLogo(){
	a = findPos(document.getElementById('main_table'));
	l = a[0]+661+'px';
	document.getElementById('zalgiris_logo').style.left = l;
	document.getElementById('zalgiris_logo').style.display = "";
}

function setTitle(title){
  document.title = title;
}

function ajaxFunction(path)
{
var xmlhttp;
if (window.XMLHttpRequest)
  {
  // code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else if (window.ActiveXObject)
  {
  // code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
else
  {
  alert("Your browser does not support XMLHTTP!");
  }
xmlhttp.onreadystatechange=function()
{
if(xmlhttp.readyState==4)
  {
 location.href=path;
 //alert(xmlhttp.responseText);
  }
}
xmlhttp.open("GET","http://www.zalgiris.lt/ajax.php?destroy",true);
xmlhttp.send(null);
}

// Metodai kvieciami is /tvs/modules/pasiulymai.php
// ajax'ed comment's and suggestion's.

function sugg_vote(id, pos){
  var res = $.ajax({
   type: "POST",
   url: "/tvs/ajax/pasiulymai_vote.php",
   data: "id="+id+"&pos="+pos, async: false }).responseText;
 $("#sugg_rating_"+id).html(res);
}

function sugg_comment(id){
  var comment = $("#sugg_comment_form_"+id).val();
  $("#sugg_comment_form_button_"+id).val("Prašome palaukti");
    
  $.post("/tvs/ajax/pasiulymai_comment.php", 
           { id: id, 
             comment : comment
           },
           function(res)
           {
              $("#sugg_comments_list_"+id).prepend(res);
              $("#sugg_comment_form_"+id).val("");
              $("#sugg_comment_form_button_"+id).val("Komentuoti");
           } 
      ); 
}

// Metodai kvieciami is /tvs/modules/nariu-diskusijos.php
function discussion_comment(id, pagesize, currpage, link){
  var comment = $("#discussion_comment_form_"+id).val();
  //alert(comment);
  $("#discussion_comment_form_button_"+id).val("Prašome palaukti");
  
  var mid = $("#discussion_comment_form_button_"+id).attr("message_id");
  
  if($("#discussion_comment_form_button_"+id).hasClass('edit'))
  {
     $.post("/tvs/ajax/discussion_comment.php?action=edit", 
         { 
           id: id,
           mid: mid, 
           comment : comment
         },
         function(data)
         {
           var obj = $.parseJSON(data);

           $("#message_"+mid+" div").html(obj.message);
           $("#message_"+mid+" div.encoded").html(obj.encoded);
           
           $("#discussion_comment_form_"+id).val("");
           $("#discussion_comment_form_button_"+id).val("Komentuoti");
           $("#discussion_comment_form_button_"+id).removeClass("edit");
           $("#discussion_comment_form_button_"+id).removeAttr("message_id");
         } 
      );
  }
  else
  {
    $.post("/tvs/ajax/discussion_comment.php?action=new", 
         { id: id, 
           pagesize : pagesize,
           page : currpage,
           link : link,
           comment : comment
         },
         function(data)
         {
           $("#discussion_comments_list_"+id).append(data);
           $("#discussion_comment_form_"+id).val("");
           $("#discussion_comment_form_button_"+id).val("Komentuoti");
         } 
      );
  } 
}

function disscussion_edit(tid, mid)
{
  //var author = $("#message_"+mid+" h2 b").html();
  var message= $("#message_"+mid+" div.encoded").html();
  
  $("#discussion_comment_form_"+tid).val(message);
  $("#discussion_comment_form_"+tid).focus();

  $("#discussion_comment_form_button_"+tid)
        .val("Išsaugoti pakeitimus")
        .addClass("edit")
        .attr("message_id", mid);
}

function disscusion_quote(tid, mid){
  var author = $("#message_"+mid+" h2 b a").html();
  var message= $("#message_"+mid+" div.encoded").html();
  var string = "[quote="+author+"]"+message+"[/quote] ";
  var old_value = $("#discussion_comment_form_"+tid).val();
  $("#discussion_comment_form_"+tid).val(old_value+string);
  $("#discussion_comment_form_"+tid).focus();
}
// end.

function overlay(url)
{
  var overcontainer = $("#lighbox");   // our magic popup container!
  overcontainer.unbind("click");       // We don't like to user 
                                       // be able to close popup before it loads..
  
  var loader = new Image();            // lets show our popup is loading...
  $(loader).load(function(){           // Loader is loaded
    overcontainer.show();              // show a dark background
    overcontainer.empty();             // clear all other images (old ones). Just in case.
    overcontainer.append($(loader));   // Show a loader (loading bar image)
    $(this).center();                  // in the center of page.
    
    var image = new Image();           // lets load our nasty popup
    $(image).load(function(){          // it's loaded (happy)
      overcontainer.empty();           // clear old content (loading bar)
      overcontainer.append($(this));   // and show it (popup) !
      
      $(this).center();                // oh, we forgot, move it to the center;
      
      overcontainer.click(function(){  // now we can let user to close a popup :D
        overcontainer.hide(); 
      });
      
      
    }).click(function(){               // and if user click on the image ( not the left black space )
      window.location = "http://www.zalgiris.lt/LT/bilietai/abonementai/"; // we love to redirect him :D
    }).attr("src", url);               // popup image source.
  }).attr("src", "/ajax.gif");         // loading bar source.
}


$(document).ready(function() {
  $(".hint").each(function(index){
    $(this).tipsy({gravity: 'w'});
  });
  
  // galleries ;)
  $("a.grouped_elements").fancybox({titlePosition: "over", padding: 0, overlayColor: "#000000", overlayOpacity: 0.3});
});
	