// JavaScript Document
/*function $(id)
{
	return document.getElementById(id);
}*/
	
function Page_Load()
{
	WriteAllFlash();
	PositionDivs();
	
	setTimeout(InitCompass, 1000);
	
	MM_preloadImages('images/btn_balsuoti_active.gif');
}

function PositionDivs()
{
	var m = Math.max($("#main").height(), Math.max($("#left").height(), $("#middle").height()));
	$('#middle').height(document.body.clientHeight);
	
	var compos = $("#compos");
	compos.css("top", document.body.clientHeight - compos.height() - 40);
}



var mouseX, mouseY;
function InitCompass()
{
	var comp = GetFlash("komposas");
	
	if(!comp.SetPos)
	{
		setTimeout(InitCompass, 100);
		return;	
	}
//	var comp = GetFlash("komposas");

	var pos = findPos(document.getElementById("compos"));
	comp.SetPos(pos[0],pos[1]);
	
	
	//var h = getwindow();

	document.onmousemove = function(e)
	{
		//seekMouse(e);
		var m = mouseXandY(e);
		comp.OnHtmlMouseMove(m[0], m[1]);
	}
}


function getwindow() {
 var d = document, v = window, w, h, l, t;
 if( typeof v.innerWidth==='number' ) {
  w = v.innerWidth;
  h = v.innerHeight;
  l = v.pageXOffset;
  t = v.pageYOffset;
 } else if( ( v = d.documentElement ) &&
   typeof v.clientWidth==='number' &&
   v.clientWidth !== 0 || ( v = d.body ) ) {
  w = v.clientWidth;
  h = v.clientHeight;
  l = v.scrollLeft;
  t = v.scrollTop;
 }
 return {w: w, h: h, l: l, t: t};
}

function seekMouse( e ) {
  e = e || window.event || {};
  var  w = getwindow(),
   minx = w.l,
   miny = w.t,
   maxx = w.w + w.l,
   maxy = w.h + w.t;
  if( typeof e.pageX==='number' ) {
    mouseX = e.pageX;
    mouseY = e.pageY;
  } else {
    mouseX = e.x + w.l;
    mouseY = e.y;
  }
}


var IE = document.all?true:false;
function getMouseXY(e)
{
	if (IE)
	{ // grab the x-y pos.s if browser is IE
		mouseX = event.clientX + document.body.scrollLeft;
		mouseY = event.clientY + document.body.scrollTop;
	}
	else
	{  // grab the x-y pos.s if browser is NS
		mouseX = e.pageX;
		mouseY = e.pageY;
	}  
	if (mouseX < 0)
	{
		mouseX = 0;
	}
	if (mouseY < 0)
	{
		mouseY = 0;
	}  
}

function mouseXandY(e) { // by Kravvitz of DynamicSiteSolutions.com
  var x=0,y=0;
  e=e||window.event;
  if(e.pageX || e.pageY) {
    x=e.pageX;
    y=e.pageY;
  } else if(typeof(e.clientX)=='number') {
    var dE=document.documentElement;
    x=e.clientX+document.body.scrollLeft+(dE?dE.scrollLeft:0);
    y=e.clientY+document.body.scrollTop+(dE?dE.scrollTop:0);
  }
  return [x,y];
}

if( window.captureEvents ) { window.captureEvents( Event.MOUSEMOVE ); }

function GetFlash(name)
{
	if (navigator.appName.indexOf("Microsoft") != -1)
	{
		return window[name];
	}
	else
	{
		return document[name];
	}
}

function findPos(obj)
{
	var curleft = curtop = 0;
	if (obj.offsetParent)
	{
		do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
	}
	return [curleft,curtop];
}

function openImg(id, isFlv, width, height)
{
	var holder = $("#img_holder");
	
	
	if(width > 518)
	{
		var percent = 518 * 100 / width; //mažinam procentaliai
		width = 518;
		
		height = parseInt(percent * height / 100) + 10;
	}
	
	if(parseInt(width) <= 0 || parseInt(height) <= 0)
	{
		width = 518;	
		height = 334;
	}
		
		
	if(isFlv)
	{
		var ft = new FlashTag("player.swf?film=streamer.php?id=" + id, width, height);
		ft.setParentNode("img_holder");
		ft.write();
	}
	else
	{
		holder.html('<span class="single"><a href="streamer.php?id='+id+'"><img src="streamer.php?id='+id+'" width="'+width+'" height="'+height+'" /></a></span>');
		
		$('.thumbs').piroBox({
			border: 1, 
			mySpeed: 700,
			borderColor : '#444',  
			bg_alpha: 0.5,
			cap_op_start : 0.4,
			cap_op_end: 0.8,
			pathLoader : '#000 url(css_pirobox/ajax-loader.gif) center center no-repeat;', 
			
			single : '.single  a',
			next_class : '.next',
			previous_class : '.previous'
			
		});	
	}
	
}

function preInput(obj, val)
{
	if(obj.value == val)
		obj.value = "";
}

function postInput(obj, val)
{
	if(obj.value == "")
	{
		obj.value = val;	
	}	
}

function sendNewsComment(id, default_name, default_email)
{
	var	name = $("#comment_name").val();
	var email = $("#comment_email").val();
	var content = $("#commnet_content").val();
	
	if(!prepareComment(name, email, content, default_name, default_email))
		return;

	$.post("actions.php", {action:"commentNews", newsId:id, name:name, email:email, comment:content}, null, "script");
	//SendNewsComment_onCallBack(name, content);
}

function sendQuestBookComment(num, default_name, default_email)
{
	var	name = $("#comment_name").val();
	var email = $("#comment_email").val();
	var content = $("#commnet_content").val();
	
	if(!prepareComment(name, email, content, default_name, default_email))
		return;
	
	$.post("actions.php", {action:"commentQuestBook", num:num, name:name, email:email, comment:content}, null, "script");
	//SendNewsComment_onCallBack(name, content);
}

function sendDocumentComment(id)
{
	var content = $("#commnet_content").val();
	if(jQuery.trim(content) == "")
	{
		$("#n_c_error").text("Įrašykite komentarą");
		return;	
	}
	
	$("#n_c_error").text("");
	
	$.post("actions.php", {action:"commentDocument", docId:id, comment:content}, null, "script");
	//SendNewsComment_onCallBack(name, content);
}

function sendRegataComment(id, default_name, default_email)
{
	
	var	name = $("#comment_name").val();
	var email = $("#comment_email").val();
	var content = $("#commnet_content").val();
	
	if(!prepareComment(name, email, content, default_name, default_email))
		return;
		
	$.post("actions.php", {action:"commentRegata", newsId:id, name:name, email:email, comment:content}, null, "script");
	//SendNewsComment_onCallBack(name, content);
}

function sendKelioneComment(id, default_name, default_email)
{
	
	var	name = $("#comment_name").val();
	var email = $("#comment_email").val();
	var content = $("#commnet_content").val();
	
	if(!prepareComment(name, email, content, default_name, default_email))
		return;
		
	$.post("actions.php", {action:"commentKelione", newsId:id, name:name, email:email, comment:content}, null, "script");
	//SendNewsComment_onCallBack(name, content);
}

function prepareComment(name, email, content, default_name, default_email)
{
	if(jQuery.trim(name) == "" || name == default_name)
	{
		$("#n_c_error").text("Įveskite vardą");
		return false;	
	}

	
	if(email != default_email && jQuery.trim(email) != "")
	{
		if(!checkEmail(email))	
		{
			$("#n_c_error").text("Neteisingas el. paštas");
			return false;
		}
	}
	else //nevedė emailo
	{
		email = "";	
	}
	
	
	if(jQuery.trim(content) == "")
	{
		$("#n_c_error").text("Įrašykite komentarą");
		return false;	
	}
	
	$("#n_c_error").text("");
	return true;
}

function SendNewsComment_onCallBack(name, comment, date)
{
	$("#comment_name").val("vardas*");
	$("#comment_email").val("el. paštas");
	$("#commnet_content").val("");
	
	var main_div = jQuery('<div class="com"></div>');
	var name_div = jQuery('<div class="name">'+name+'<span class="dt">'+date+'</span></div>');
	var content_div = jQuery('<div>'+comment+'</div>');
	name_div.appendTo(main_div);
	content_div.appendTo(main_div);
	main_div.appendTo("#com_container");
	
	PositionDivs();
}

function SendQuestBookComment_onCallBack(num, name, comment)
{
	
	$("#comment_name").val("vardas*");
	$("#comment_email").val("el. paštas");
	$("#commnet_content").val("");
	
	if(num != 1)
		return;
		
	var main_div = jQuery('<div class="com"></div>');
	var name_div = jQuery('<div class="name">'+name+'<span class="dt">'+date+'</span></div>');
	var content_div = jQuery('<div>'+comment+'</div>');
	name_div.appendTo(main_div);
	content_div.appendTo(main_div);
	main_div.prependTo("#com_container");
	
	PositionDivs();
}

function SendDocumentComment_onCallBack(name, comment)
{
	$("#commnet_content").val("");
	
	var main_div = jQuery('<div class="com"></div>');
	var name_div = jQuery('<div class="name">'+name+'<span class="dt">'+date+'</span></div>');
	var content_div = jQuery('<div>'+comment+'</div>');
	name_div.appendTo(main_div);
	content_div.appendTo(main_div);
	main_div.appendTo("#com_container");
	
	PositionDivs();
}

function SendRegataComment_onCallBack(name, comment)
{
	$("#comment_name").val("vardas*");
	$("#comment_email").val("el. paštas");
	$("#commnet_content").val("");
	
	var main_div = jQuery('<div class="com"></div>');
	var name_div = jQuery('<div class="name">'+name+'<span class="dt">'+date+'</span></div>');
	var content_div = jQuery('<div>'+comment+'</div>');
	name_div.appendTo(main_div);
	content_div.appendTo(main_div);
	main_div.appendTo("#com_container");
	
	PositionDivs();
}


function SendKelioneComment_onCallBack(name, comment)
{
	$("#comment_name").val("vardas*");
	$("#comment_email").val("el. paštas");
	$("#commnet_content").val("");
	
	var main_div = jQuery('<div class="com"></div>');
	var name_div = jQuery('<div class="name">'+name+'<span class="dt">'+date+'</span></div>');
	var content_div = jQuery('<div>'+comment+'</div>');
	name_div.appendTo(main_div);
	content_div.appendTo(main_div);
	main_div.appendTo("#com_container");
	PositionDivs();
}



function checkEmail(email)
{
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	return filter.test(email) ? true : false;
}

var Vote = function()
{
	var _questions = new Array();
	var _img = "images/chBox.gif";
	var _imgChecked= "images/chBox_checked.gif";
	
	return{
				AddQuestion:function(id)
				{
					_questions.push($(id));
				},
				
				Select:function(obj)
				{
					if(obj.getAttribute("checked") == true)
					{
						return;
					}
					else
					{
						var found = false;
						for(var i = 0; i < _questions.length; i++)
						{
							if(_questions[i] == obj)	
								found = true;
							
							_questions[i].setAttribute("checked", 0);
							_questions[i].setAttribute("src", _img);
						}
						obj.setAttribute("checked", 1);
						obj.setAttribute("src", _imgChecked);
						
						if(!found)
							_questions.push(obj);
					}
				}, 
				Vote:function(id)
				{
					
					for(var i = 0; i < _questions.length; i++)
					{
						if(_questions[i].getAttribute("checked") == 1)	
						{
							$.post("actions.php", {action:"vote", a_id:id, item_id:_questions[i].getAttribute("id").substr(6)}, null, "script");
							return;	
						}
					}
					$("#vote_error").text("Pažymėkite kurį nors atsakymą");
				}, 
				
				Vote_onCallBack:function(href)
				{
					document.location = href;
				}
		}	
}();


function login()
{
	//alert($("#login_name"));
	var username = $("#login_name").val();
	var password = $("#login_pass").val();
	
	if(jQuery.trim(username) == "" || jQuery.trim(password) == "")
	{
		$("#login_error").text("vartotojo vardas arba slaptažodis negali būti tusčias");	
		return;
	}
	
	$("#login_error").text("");	
	$.post("actions.php", {action:"login", user:username, pass:password}, null, "script");
	//$.post("actions.php", {action:"login", user:username, pass:password}, function(data){alert(data)});
}

function login_onCallBack(success, name)
{
	$("#login_name").val("");
	$("#login_pass").val("");
		
	if(success)
	{
		/*$("#login_box").css("display", "none");
		$("#user").css("display", "block");
		
		$("#user > a").text(name);*/
		document.location = document.location;
	}
	else
	{
		$("#login_error").text("neteisingas vartotojo vardas arba slaptažodis");	
	}
}

function logout()
{
	$.post("actions.php", {action:"logout"});
	
	$("#login_box").css("display", "block");
	$("#user").css("display", "none");
}

/*MM*/
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}