<!--
/**
 * JavaScrits
 *
 * @author Aurimas Tubis a.k.a Okanakis <Aurimas.Tubis@alumni.vdu.lt>
 * @package SkemtaTVS
 * @version v1.0/2009
 */

var working = 0;
var indexDeleted = 0;

setContentOuterWidth = function(topW) {
	if (topW)
		$('div.content_outer').css("width", topW);
}

/*
setContentWidth = function(topW) {
	if (topW)
		$('div.content').css("width", topW);
}
*/

showLoading = function()
{
	var yy = document.all? truebody().scrollTop : pageYOffset;
	if ((!yy) || (yy < 0))
		yy = 0;
	$('#loading').css({	top: yy+'px' });
	$('#loading').show();
}

moveLeft = function(topW, speed)
{
	var treeObj = $('#treeBox');
	var tw = $('#top').width();
	var diff = 0;
	var a = -439;
	var b = 710;
	if (tw < 970) {
		diff = Math.floor((970 - tw) / 2);
		a = a + diff;
	}
	var goto = a - b + 34; //-1115
	
	var margin = parseInt($("#top_inner").css("marginLeft"));
	margin -= topW;
	if (!speed)
		speed = 500;
	showLoading();

	$('#treeBox .navRightArrow').show();
	treeObj.animate({"marginLeft": goto+"px"}, speed, function(){
		$('#treeBox .navLeftArrow').hide();
	});

	$("#top_inner").animate({"marginLeft": margin+"px"}, speed, function(){
		$('#loading').hide();
		working = 0;
		deleteIndexPage(topW);
	});
}


moveRight = function(topW, speed)
{
	var treeObj = $('#treeBox');
	var tw = $('#top').width();
	var diff = 0;
	var a = -439;
	var b = 651;
	if (tw < 970) {
		diff = Math.floor((970 - tw) / 2);
		a = a + diff;
	}
	var goto = a + b - 44; //168

	var margin = parseInt($("#top_inner").css("marginLeft"));
	margin += topW;
	if (!speed)
		speed = 500;
	showLoading();
	
	$('#treeBox .navLeftArrow').show();
	treeObj.animate({"marginLeft": goto+"px"}, speed, function(){
		$('#treeBox .navRightArrow').hide();
	});
	
	$("#top_inner").animate({"marginLeft": margin+"px"}, speed, function(){
		$('#loading').hide();
		working = 0;
		deleteIndexPage(topW);
	});
}

deleteIndexPage = function(topW)
{
	if (indexDeleted == 1) {
		return false;
	}
	
	var row = document.getElementById('container_row');
	var margin = parseInt($("#top_inner").css("marginLeft"));

	if (row && (parseInt(row.cells.length) > 2))
	{
		row.deleteCell(1);
		if (margin < 0) {
			margin += topW;
			$("#top_inner").css("marginLeft", margin+"px");
		}
		indexDeleted = 1;
	}
}

function truebody() {
	return (!window.opera && document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}


var containerHeight = 0;
var scrollCotent = '';
var sc = '';
var sch = 0;
var crollerItemHeight = 19;
var loadOnce = 0;
var timer = 0;

function getContainerHeight(o) {
	containerHeight = $(o).parent().height();
	//containerHeight -= $(o).height();
}

function getScrollContent(o) {
	//sc = $(o).next();
	var a = $(o).parent().parent().parent();
	var b = a.find('.conten_text_inner .text_container');
	sc = b;
	sch = sc.height();
}

function clickScroll(ob, val) {
	if (typeof(ob) != 'object') {
		ob = $(ob + ' .bottom_item');
		if (ob.length == 0)
			return false;
	}
	
	var box = ob.parent();
	var txtc = box.parent().find('.text_container');
	
	sch = txtc.height();
	if (containerHeight == 0)
		containerHeight = box.find('.middle_item').height();
	
	var top = parseFloat(txtc.css('top'));
	if (isNaN(top))
		top = 0;
	top += val;
	if (top > 0)
		top = 0;
	
	//alert('sch: ' + sch + ' containerHeight: ' + containerHeight);
	
	var move = sch - containerHeight;
	if (move > 0)
	{
		if (Math.abs(top) > move)
			top = move * (-1);
		var t = 0;
		t = ((containerHeight - crollerItemHeight) * top) / move;
		if (t < 0)
			t = t * (-1);
		var d = box.find('.scroller');
		txtc.css({"top": top+"px"});
		d.css({"top": t+"px"});
		
		if (timer) {
			var id = box.attr('id');
			if (id) {
				timer = setTimeout("clickScroll('#"+id+"', "+val+")", 60);
			}
		}
	} else if (timer) {
		clearTimeout(timer);
		timer = 0;
	}
}

function initDraggable ()
{
	$('.list_contacts .c_title a').click(function(){
		if (working == 1)
			return false;
		
		working = 1;
		var href = $(this).attr('href');
		if (href) {
			var blockP = $(this).parent().parent().parent(); //.list_contacts
			blockP.find('.c_title a').attr({'class':''});
			var local_container = blockP.parent();//.find('.contact_preview'); //.text_container
			$(this).attr({'class':'active'});
			$(this).blur();
			getContactItemInfo(href, local_container);
		}
		
		return false;
	});

	loadOnce = loadOnce + 1;
	if (loadOnce > 1)
		return 0;
	
	$('a[@rel*=lightbox_a]').lightBox({
		txtImage: textImage,
		txtOf: textOf
	});
	$('a[@rel*=lightbox_b]').lightBox({
		txtImage: textImage,
		txtOf: textOf
	});

	$('.scroller_box').each(function(i){
		var p = $(this).parent();
		var indx = i + 1;
		$(this).attr('id','scb_'+indx);
		var contH = p.find('.conten_text_inner').height() - 20;
		var txtcH = p.find('.text_container').height();
		
		//alert('txtcH: ' + txtcH + ' contH: ' + contH);
		
		if ((txtcH - contH) > 0)
			$(this).show();
		else if ($(this).is(':visible'))
			$(this).hide();
			
	});

	$(".scroller_box .top_item").mousedown(function(){
		timer = 1;
		clickScroll($(this), 20);
    }).mouseup(function(){
		clearTimeout(timer);
		timer = 0;
    });

	$(".scroller_box .bottom_item").mousedown(function(){
		timer = 1;
		clickScroll($(this), -20);
    }).mouseup(function(){
		clearTimeout(timer);
		timer = 0;
    });

	/*
	$('.scroller_box .top_item').click(function(){
		clickScroll($(this), 20);
	});
	$('.scroller_box .bottom_item').click(function(){
		clickScroll($(this), -20);
	});
	*/
	
	/*
	$(".text_container").mousewheel(function(objEvent, intDelta){
		var a = 20;
		
		//if (sch == 0)
		sch = $(this).height();
		if (containerHeight == 0)
			containerHeight = $(this).parent().height() - 20; // - 20;
		
		if (intDelta > 0){ //up
			a = a * 1;
		}
		else if (intDelta < 0){ //down
			a = a * (-1);
		}
		
		var top = parseFloat($(this).css('top'));// * (-1);
		if (isNaN(top))
			top = 0;
		top += a;
		if (top > 0)
			top = 0;
		
		var move = sch - containerHeight;
		if (move > 0)
		{
			var c = $(this).parent().parent();
			var d = c.find('.scroller_box .scroller');
			//var e = c.find('.scroller_box');
			
			//if (e.is(':visible'))
			//e.show();
			
			if (Math.abs(top) > move)
				top = move * (-1);
			var t = 0;
			t = ((containerHeight - crollerItemHeight) * top) / move;
			if (t < 0)
				t = t * (-1);
			$(this).css({"top": top+"px"});
			scr = d;
			scr.css({"top": t+"px"});
		}
	});
	*/
	
	$('.scroller').Draggable(
	{
		zIndex: 	1000,
		containment:     'parent',
		axis:    'vertically',
		opacity: 	0.8,
		onStart: function() {
			getContainerHeight(this);
			getScrollContent(this);
		},
		onDrag : function(x,y)
		{
			if (sch > containerHeight) {
				var tmp_ch = containerHeight - crollerItemHeight;
				var move = sch - tmp_ch;
				
				if (y>0 && y<(tmp_ch)) {
					t = y/containerHeight*move;
					if (t > (sch - containerHeight))
						t = sch - containerHeight;
					if (t > 0)
						t = t * (-1);
					sc.css({"top": t+"px"});
					//sc.top(- y/containerHeight*move + "px");
				}
			}

			return {x: x,y: y}
		}
	});
}

function preScroll(local_container)
{
	//var textc = $(local_container).parent();
	var textc = local_container; //.text_container
	var contH = textc.parent().height() - 20;
	var txtcH = textc.height();
	
	//alert('txtcH: ' + txtcH + ' contH: ' + contH);
	var scb = textc.parent().parent().find('.scroller_box');
	if (scb.length) {
		if ((txtcH - contH) > 0)
			scb.show();
		else if (scb.is(':visible'))
			scb.hide();
	}
}

$(document).ready( function() {
	$('.navLeftArrow a').click(function(){
		return false;	/*Donato keista, kad nevigacija neveiktu - laikinai isjungtas psl.*/
		if (working == 1)
			return false;
			
		working = 1;
		moveLeft(topW, speed);
		$(this).blur();
		return false;
	});

	$('.navRightArrow a').click(function(){
		return false; /*Donato keista, kad nevigacija neveiktu - laikinai isjungtas psl.*/
		if (working == 1)
			return false;
		
		working = 1;
		moveRight(topW, speed);
		$(this).blur();
		return false;
	});
	
	$('#leftSide .navmenu li a').click(function(){
		if (working == 1)
			return false;
			
		working = 1;
		var href = $(this).attr('href');
		if (href) {
			//if ($(this).attr('class') != 'active')
			//{
				var p = $(this).parent().parent().parent();
				var pClass = p.attr('class');
				//$('#leftSide .navmenu li a').attr({'class':''});
				if (pClass == 'container')
					p.find('ul li a').attr({'class':''});
				else {
					$('#leftSide .navmenu li a').attr({'class':''});
					$('#leftSide .navmenu .container ul').slideUp('fast');
					var pp = $(this).parent();
					if (pp.attr('class') == 'container') {
						//var ppUL = pp.find('ul');
						//if (!(ppUL.is(':visible')))
						//	ppUL.slideDown('fast');
						pp.find('ul').slideDown('fast');
					}
				}
			//}
			var local_container = '#leftSide';
			$(this).attr({'class':'active'});
			$(this).blur();
			//if ($(this).parent().attr('class') != 'container')
				getPageContent(href, local_container);
			//else
			//	working = 0;
		}
		
		return false;
	});

	$('#rightSide .navmenu li a').click(function(){
		if (working == 1)
			return false;
			
		working = 1;
		var href = $(this).attr('href');
		if (href) {
			//if ($(this).attr('class') != 'active')
			//{
				var p = $(this).parent().parent().parent();
				var pClass = p.attr('class');
				//$('#rightSide .navmenu li a').attr({'class':''});
				if (pClass == 'container')
					p.find('ul li a').attr({'class':''});
				else {
					$('#rightSide .navmenu li a').attr({'class':''});
					$('#rightSide .navmenu .container ul').slideUp('fast');
					var pp = $(this).parent();
					if (pp.attr('class') == 'container') {
						//var ppUL = pp.find('ul');
						//if (!(ppUL.is(':visible')))
						//	ppUL.slideDown('fast');
						pp.find('ul').slideDown('fast');
					}
				}
			//}
			var local_container = '#rightSide';
			$(this).attr({'class':'active'});
			$(this).blur();
			//if ($(this).parent().attr('class') != 'container')
				getPageContent(href, local_container);
			//else
			//	working = 0;
		}
		
		return false;
	});
	
	//#leftSide, #rightSide
	$('.minigallery').cycle({ 
		fx:      'fade', 
		speed:   2000, 
		timeout: 5
	});
	/*
	$('a[@rel*=lightbox_mini_left]').lightBox({
		txtImage: textImage,
		txtOf: textOf
	});
	$('a[@rel*=lightbox_mini_right]').lightBox({
		txtImage: textImage,
		txtOf: textOf
	});
	*/

	initDraggable();

	getPageContent = function(href, container)
	{
		loadOnce = 0;
		var n = 0;
		var local_container = container + ' .content_text';
		$(local_container).ajaxStart(function() {
			if (n++ > 0) return;
			$(local_container).fadeOut('slow');
			$(container + ' .loading_image').fadeIn('fast');
		});
		
		$.ajax({
			url: href + ',ajax.1',
			cache: false,
			success: function(html) {
				if (html) {
					$(local_container).html(html);
				/*//dataType: "json",
				if (data.html) {
					$(local_container).html(data.html);*/
					$(container + ' .scroller_box').hide();
					$(local_container).fadeIn('slow');
					$(container + ' .loading_image').fadeOut('slow');
				}
				initDraggable();
				working = 0;
			}
		});
		
		$.ajax({
			url: href + ',ajaxgetimg.1',
			cache: false,
			success: function(html) {
				if (html) {
					$(container).find('.minigallery').html(html);
				/*//dataType: "json",
				if (data.html) {
					$(local_container).html(data.html);*/
					//$(container + ' .scroller_box').hide();
					//$(local_container).fadeIn('slow');
					//$(container + ' .loading_image').fadeOut('slow');
				}
				//initDraggable();
				//working = 0;
			}
		});

		return true;
	}

	getContactItemInfo = function(url, local_container) {
		var n = 0;
		var loadingImg = local_container.find('.contacts_loading_image');
		var cPlace = local_container.find('.contact_preview');
		
		loadingImg.ajaxStart(function() {
			if (n++ > 0) return;
			$(this).fadeIn('fast');
			cPlace.fadeOut('slow');
		});
		$.ajax({
			url: url + ',ajaxmore.1',
			cache: false,
			success: function(html) {
				if (html) {
					cPlace.html(html);
					local_container.css({"top": "0px"});
					cPlace.fadeIn('slow');
					loadingImg.fadeOut('slow');
				}
			},
			complete: function(){
				//var t = setTimeout("preScroll('"+local_container+"')", 500); //local_container == (str) .contact_preview_+{id}
				preScroll(local_container); //local_container == (obj) .text_container
				$('a[@rel*=lightbox_a]').lightBox({
                    txtImage: textImage,
                    txtOf: textOf
				});
				$('a[@rel*=lightbox_b]').lightBox({
                    txtImage: textImage,
                    txtOf: textOf
				});
				working = 0;
			}
		});
	}
});


//-->

