// JavaScript Document
//utf8 УТФ8 кодировка lietuviškai ąčęėįšųūž

function _obj(id)
{
	if ((id != undefined)&&(id != '')) { return document.getElementById(id); } else { return ''; }
}

function _url(path)
{
	var real_link = path;
	real_link = real_link.replace(/&amp;/g,'&');
	window.location = real_link;	
}


//======================
// KOMENTARAI
//======================
var ajax = null;
ajax = new GLM.AJAX();

function comments_loading()
{
	_obj('comments_cont').innerHTML = '<table cellspacing="5" cellpadding="0" width="100%"><tr><td align="center">'+lng_loading+'</td></tr><tr><td align="center"><img src="admin/images/loading2.gif" alt="" /></td></tr></table>';
}

function show_comments(id,sub1,sub2,page)
{
	ajax = new GLM.AJAX();
	comments_loading();
	setTimeout("get_comments("+id+","+sub1+","+sub2+","+page+");",1000);
}

function get_comments(id,sub1,sub2,page)
{
	ajax.callPage('komentarai.php?act=list&id='+id+'&sub='+sub1+'&sub2='+sub2+'&page='+page, echo_comments);
}

function echo_comments(responce)
{
	_obj('comments_cont').innerHTML = responce;
	_obj('links_cont').innerHTML = '&nbsp;&nbsp;&nbsp;<a href="javascript:scroll(0,0);">'+lng_top+'</a>&nbsp;&nbsp;|&nbsp;&nbsp;<a href="javascript:history.go(-1);">'+lng_atgal+'</a>';
}






//==========ENLARGER=============
var myWidth = 0, myHeight = 0;
function getClientSize() {
	if( typeof( window.innerWidth ) == 'number' ) {
		myWidth = window.innerWidth; myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth ||document.documentElement.clientHeight ) ) {
		myWidth = document.documentElement.clientWidth; myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		myWidth = document.body.clientWidth; myHeight = document.body.clientHeight;
	}
}
var scrOfX = 0, scrOfY = 0;
function getScrollXY() {
	if( typeof( window.pageYOffset ) == 'number' ) { scrOfY = window.pageYOffset; scrOfX = window.pageXOffset; } 
	else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) { scrOfY = document.body.scrollTop; scrOfX = document.body.scrollLeft; } 
	else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) { scrOfY = document.documentElement.scrollTop; scrOfX = document.documentElement.scrollLeft; }
}

function enlarge_thumb(obj)
{
	_obj('bdy').style.overflow = 'hidden';

	getScrollXY();
	scroll(scrOfX+1,scrOfY+1);
	scroll(scrOfX,scrOfY);

	getClientSize();
	getScrollXY();
	//myWidth = myWidth - 30;
	
	var img = obj.src.replace(/_small100/g,'');
	last_img_src = img;

	_obj('img_enlarge_bg').style.top = (scrOfY + 'px');
	_obj('img_enlarge_bg').style.left = (scrOfX + 'px');
	_obj('img_enlarge_bg').style.width = (myWidth + 'px');
	_obj('img_enlarge_bg').style.height = (myHeight + 'px');


	_obj('img_enlarge').style.top = (scrOfY + 'px');
	_obj('img_enlarge').style.left = (scrOfX + 'px');
	_obj('img_enlarge').style.width = (myWidth + 'px');
	_obj('img_enlarge').style.height = (myHeight + 'px');

	_obj('img_enlarge_bg').style.display = 'block';

	var data = '<table style="cursor:pointer;" width="100%" height="'+myHeight+'" onclick="hide_enlarger();"><tr><td align="center" valign="middle"><img class="img_enl_border" onclick="hide_enlarger();" src="'+img+'" alt="" /></td></tr></table>';

	_obj('img_enlarge').innerHTML = data;
	_obj('img_enlarge').style.display = 'block';
	
}

function enlarge_img(path)
{
	_obj('bdy').style.overflow = 'hidden';

	getScrollXY();
	scroll(scrOfX+1,scrOfY+1);
	scroll(scrOfX,scrOfY);

	getClientSize();
	getScrollXY();
	//myWidth = myWidth - 30;
	
	_obj('img_enlarge_bg').style.top = (scrOfY + 'px');
	_obj('img_enlarge_bg').style.left = (scrOfX + 'px');
	_obj('img_enlarge_bg').style.width = (myWidth + 'px');
	_obj('img_enlarge_bg').style.height = (myHeight + 'px');


	_obj('img_enlarge').style.top = (scrOfY + 'px');
	_obj('img_enlarge').style.left = (scrOfX + 'px');
	_obj('img_enlarge').style.width = (myWidth + 'px');
	_obj('img_enlarge').style.height = (myHeight + 'px');

	_obj('img_enlarge_bg').style.display = 'block';

	var data = '<table style="cursor:pointer;" width="100%" height="'+myHeight+'" onclick="hide_enlarger();"><tr><td align="center" valign="middle"><img class="img_enl_border" onclick="hide_enlarger();" src="'+path+'" alt="" /></td></tr></table>';

	_obj('img_enlarge').innerHTML = data;
	_obj('img_enlarge').style.display = 'block';
	
}

function hide_enlarger()
{
	_obj('img_enlarge_bg').style.display = 'none';
	_obj('img_enlarge').style.display = 'none';
	_obj('bdy').style.overflow = 'auto';
	getScrollXY();
	scroll(scrOfX+1,scrOfY+1);
	scroll(scrOfX,scrOfY);
}



//drop footer
function adjust_footer_position(footer_height)
{
	_obj('footer_cont').style.position = 'relative';
	_obj('footer_cont').style.top = '0px';
	var windowY = (document.documentElement && document.documentElement.clientHeight) || window.innerHeight || self.innerHeight || document.body.clientHeight; 
	var pageHeight = 0;
	if( window.innerHeight && window.scrollMaxY ) { pageHeight = window.innerHeight + window.scrollMaxY; }
	else if( document.body.scrollHeight > document.body.offsetHeight ) { pageHeight = document.body.scrollHeight; }
	else { pageHeight = document.body.offsetHeight + document.body.offsetTop; }

	var b=document.body, e=document.documentElement;
	var esw=0, eow=0, bsw=0, bow=0, esh=0, eoh=0, bsh=0, boh=0;
	if (e) { esh = e.scrollHeight; eoh = e.offsetHeight; }
	if (b) { bsh = b.scrollHeight; boh = b.offsetHeight; }

	if (windowY > pageHeight)
	{
		_obj('footer_cont').style.position = 'absolute';
		_obj('footer_cont').style.top = (windowY-67)+'px';
	}
	else if (bsh > boh) 
	{
		_obj('footer_cont').style.position = 'absolute';
		_obj('footer_cont').style.top = (bsh-67)+'px';
	}
	//else { alert('not changed' + windowY + ' ' + pageHeight); }
}




//===============================
//   TOP MENU
//===============================

var active_top_menu = -1;
var last_shown_active_top_menu = -1;
//top menu
function activate_top_mp(num)
{
	if ((active_top_menu > 0)&&(num != active_top_menu)) { disactivate_top_mp(active_top_menu); }
	last_shown_active_top_menu = num;
	if (_obj('tm_vh_'+(num))) { _obj('tm_vh_'+(num)).style.visibility = 'hidden'; }
	if (_obj('tm_vh_'+(num+1))) { _obj('tm_vh_'+(num+1)).style.visibility = 'hidden'; }
	//_obj('tmc_'+(num)).className = 'tmp_act';
	colorFade('tmc_'+(num),'background','525252','ccde02',10,20);
	//colorFade('tmc_'+(num),'color','ffffff','000000',10,20);
}

function disactivate_top_mp(num)
{
	//_obj('tmc_'+(num)).className = 'tmp';
	colorFade('tmc_'+(num),'background','ccde02','525252',40,20);
	//colorFade('tmc_'+(num),'color','000000','ffffff',10,20);
	if (_obj('tm_vh_'+(num))) { _obj('tm_vh_'+(num)).style.visibility = 'visible'; }
	if (_obj('tm_vh_'+(num+1))) { _obj('tm_vh_'+(num+1)).style.visibility = 'visible'; }

	if ((active_top_menu > 0)&&(num != active_top_menu)) { activate_top_mp(active_top_menu); }
}


// main function to process the fade request //
function colorFade(id,element,start,end,steps,speed) {
  var startrgb,endrgb,er,eg,eb,step,rint,gint,bint,step;
  var target = document.getElementById(id);
  steps = steps || 20;
  speed = speed || 20;
  clearInterval(target.timer);
  endrgb = colorConv(end);
  er = endrgb[0];
  eg = endrgb[1];
  eb = endrgb[2];
  if(!target.r) {
    startrgb = colorConv(start);
    r = startrgb[0];
    g = startrgb[1];
    b = startrgb[2];
    target.r = r;
    target.g = g;
    target.b = b;
  }
  rint = Math.round(Math.abs(target.r-er)/steps);
  gint = Math.round(Math.abs(target.g-eg)/steps);
  bint = Math.round(Math.abs(target.b-eb)/steps);
  if(rint == 0) { rint = 1 }
  if(gint == 0) { gint = 1 }
  if(bint == 0) { bint = 1 }
  target.step = 1;
  target.timer = setInterval( function() { animateColor(id,element,steps,er,eg,eb,rint,gint,bint) }, speed);
}

// incrementally close the gap between the two colors //
function animateColor(id,element,steps,er,eg,eb,rint,gint,bint) {
  var target = document.getElementById(id);
  var color;
  if ((target.step == 1)&&(er == 82)) { target.style.borderBottom = '2px solid #262626';  }
  if(target.step <= steps) {
    var r = target.r;
    var g = target.g;
    var b = target.b;
    if(r >= er) {
      r = r - rint;
    } else {
      r = parseInt(r) + parseInt(rint);
    }
    if(g >= eg) {
      g = g - gint;
    } else {
      g = parseInt(g) + parseInt(gint);
    }
    if(b >= eb) {
      b = b - bint;
    } else {
      b = parseInt(b) + parseInt(bint);
    }
    color = 'rgb(' + r + ',' + g + ',' + b + ')';
    if(element == 'background') {
      target.style.backgroundColor = color;
    } else if(element == 'border') {
      target.style.borderColor = color;
    } else {
      target.style.color = color;
    }
    target.r = r;
    target.g = g;
    target.b = b;
    target.step = target.step + 1;
  } else {
    clearInterval(target.timer);
    color = 'rgb(' + er + ',' + eg + ',' + eb + ')';
    if(element == 'background') {
      target.style.backgroundColor = color;
    } else if(element == 'border') {
      target.style.borderColor = color;
    } else {
      target.style.color = color;
    }
	if (er == '204') { target.className = 'tmp_act'; target.style.borderBottom = '2px solid #ccde02'; }
	if (er == '82') { target.className = 'tmp'; }
  }
}

// convert the color to rgb from hex //
function colorConv(color) {
  var rgb = [parseInt(color.substring(0,2),16), 
    parseInt(color.substring(2,4),16), 
    parseInt(color.substring(4,6),16)];
  return rgb;
}




//-------------------------
//smooth_scroll
//-------------------------
function currentYPosition() 
{
	// Firefox, Chrome, Opera, Safari
	if (self.pageYOffset) return self.pageYOffset;
	// Internet Explorer 6 - standards mode
	if (document.documentElement && document.documentElement.scrollTop)
	return document.documentElement.scrollTop;
	// Internet Explorer 6, 7 and 8
	if (document.body.scrollTop) return document.body.scrollTop;
	return 0;
}

function elmYPosition(eID) 
{
	if (!document.getElementById(eID)) { return 0; }
	else 
	{
		var elm = document.getElementById(eID);
		var y = elm.offsetTop;
		var node = elm;
		while (node.offsetParent && node.offsetParent != document.body) 
		{
			node = node.offsetParent;
			y += node.offsetTop;
		} 
		return y;
	}
}

function smoothScroll(eID) 
{
	var Y = currentYPosition();
	var stopY = elmYPosition(eID);
	var distance = Y - stopY;
	var steps = 5; //30;
	var time = 50;
	var x = 0;
	
	while (true)
	{
		if (  (Y - stopY)  <=  (distance / steps)  )
		{
			setTimeout("scroll(0, "+stopY+");", x);
			return;
		}
		else
		{
			Y = Y - Math.round(distance / steps);
			setTimeout("scroll(0, "+Y+");", x);
			x += time;
		}
	}
}

function page_refresh()
{
	//var sURL = unescape(window.location.pathname);
	//_url(sURL);
	//location.reload(true);
	_url(current_page_url);
}


function nustatyti_rusiavima(mp_id, budas)
{
	var key = 'rusiavimas_'+mp_id;
	var rvalue = readCookie(key);
	if (rvalue != budas)
	{
		createCookie(key,budas,7);
		page_refresh();
	}
}


function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}





function adjust_main_cont_margin()
{
	getClientSize();
	var left_pos = ((myWidth - 990-24)/2);
	if (left_pos < 0) { left_pos = 0; }
	if (_obj('top_banner')) { _obj('top_banner').style.margin = '0px auto 0px '+left_pos+'px'; }
	_obj('main_cont').style.margin = '0px auto 0px '+left_pos+'px';
}


function isInteger(s) { return (s.toString().search(/^-?[0-9]+$/) == 0); }


function ajax_responce_eval(responce)
{
	//alert(responce);
	eval(responce);	
}







function show_modale(url)
{
	_obj('bdy').style.overflow = 'hidden';
	getScrollXY();
	scroll(scrOfX+1,scrOfY+1);
	scroll(scrOfX,scrOfY);
	getClientSize();
	getScrollXY();
	
	_obj('img_enlarge_bg').style.top = (scrOfY + 'px');
	_obj('img_enlarge_bg').style.left = (scrOfX + 'px');
	_obj('img_enlarge_bg').style.width = (myWidth + 'px');
	_obj('img_enlarge_bg').style.height = (myHeight + 'px');

	_obj('img_enlarge').style.top = (scrOfY + 'px');
	_obj('img_enlarge').style.left = (scrOfX + 'px');
	_obj('img_enlarge').style.width = (myWidth + 'px');
	_obj('img_enlarge').style.height = (myHeight + 'px');

	_obj('img_enlarge_bg').style.display = 'block';

	//var h = Math.round(myHeight/3*2);
	var h = 400;
	var data_cont = '<div class="green_box" style="width:600px; height:'+h+'px;" id="modale_cont" >&nbsp;Prašome palaukti ...</div>';
	var data = '<table width="100%" height="'+myHeight+'"><tr><td align="center" valign="middle">'+data_cont+'</td></tr></table>';

	_obj('img_enlarge').innerHTML = data;
	_obj('img_enlarge').style.display = 'block';	
	
	ajax.callPage('fast.php?act=get_content&page='+url+'&h='+h, modale_content);
}

function modale_content(responce)
{
	//alert(responce);
	_obj('modale_cont').innerHTML = responce;
	_obj('modale_cont').scrollTop = 0;
	_obj('modale_cont').scrollLeft = 0;
}








function mfocus(id)
{
	_obj(id).focus();
}


function win_resize()
{
	adjust_main_cont_margin();
	if (_obj('img_enlarge').style.display == 'block')
	{
		getScrollXY();
		scroll(scrOfX+1,scrOfY+1);
		scroll(scrOfX,scrOfY);
		getClientSize();
		getScrollXY();
		_obj('img_enlarge_bg').style.top = (scrOfY + 'px');
		_obj('img_enlarge_bg').style.left = (scrOfX + 'px');
		_obj('img_enlarge_bg').style.width = (myWidth + 'px');
		_obj('img_enlarge_bg').style.height = (myHeight + 'px');
	
		_obj('img_enlarge').style.top = (scrOfY + 'px');
		_obj('img_enlarge').style.left = (scrOfX + 'px');
		_obj('img_enlarge').style.width = (myWidth + 'px');
		_obj('img_enlarge').style.height = (myHeight + 'px');
	}
}

function show_sending_modale()
{
	_obj('bdy').style.overflow = 'hidden';
	getScrollXY();
	scroll(scrOfX+1,scrOfY+1);
	scroll(scrOfX,scrOfY);
	getClientSize();
	getScrollXY();
	
	_obj('img_enlarge_bg').style.top = (scrOfY + 'px');
	_obj('img_enlarge_bg').style.left = (scrOfX + 'px');
	_obj('img_enlarge_bg').style.width = (myWidth + 'px');
	_obj('img_enlarge_bg').style.height = (myHeight + 'px');

	_obj('img_enlarge').style.top = (scrOfY + 'px');
	_obj('img_enlarge').style.left = (scrOfX + 'px');
	_obj('img_enlarge').style.width = (myWidth + 'px');
	_obj('img_enlarge').style.height = (myHeight + 'px');

	_obj('img_enlarge_bg').style.display = 'block';

	//var h = Math.round(myHeight/3*2);
	var data_cont = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="200" height="70" id="siunciami" align="middle"><param name="allowScriptAccess" value="sameDomain" /><param name="wmode" value="transparent" /><param name="allowFullScreen" value="false" /><param name="movie" value="images/siunciami.swf" /><param name="quality" value="high" /><param name="bgcolor" value="#ffffff" /><embed src="images/siunciami.swf" quality="high" wmode="transparent" bgcolor="#ffffff" width="200" height="70" name="siunciami" align="middle" allowScriptAccess="sameDomain" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></object>';
	var data = '<table width="100%" height="'+myHeight+'"><tr><td align="center" valign="middle">'+data_cont+'</td></tr></table>';

	_obj('img_enlarge').innerHTML = data;
	_obj('img_enlarge').style.display = 'block';	
}


