function scrollbars()
{
	var content = $('#content > .container').html();

	if (/^\s*$/.test(content))
	{
		if ($(window).height() < 617)
		{
			$('html, body').css('overflowY', 'auto');
		}
		else
		{
			$('html, body').css('overflowY', 'hidden');
		}
	}
}

$(window).resize(scrollbars);

$(function(){
	scrollbars();
});

/**
 * Reterns wether a logged in admin is browsing the website.
 * 
 * @return boolean
 */
function is_admin()
{
	return ( ! window['admin']) ? false : true; 
}

/**
 * Removes the first frame image that is on top of a video.
 * @param {Object} obj Video object
 */
function video_ready(obj)
{
	setTimeout(function() {
		$('#' + obj.id).closest('.video_wrapper').find('img').hide(0);		
	}, 1000);
}

var current_page = 'index';
var uri = window.location.href.replace(base_url, '').replace(/\/$/, '');
var antro_lygio = false;

if (uri.indexOf('#') == -1 && uri.length > 2)
{
	antro_lygio = true;
	$('html, body').css('overflowY', 'auto');
}

// On DOM ready:
$(function() {
	

	$("a[href^="+base_url+"galerija/]").click(function(e) {
		var gallery = $(this).attr('href').replace(base_url+"galerija/", '');
		
		$.get(base_url + 'index/gallery', {
			gallery: gallery
		}, function(response) {
		
			response = eval(response);
		
			var files = [];
			
			$.each(response, function() {
				files.push(base_url + 'assets/galleries/galerijos/' + gallery + '/' + this);
			});
			
			$.fancybox(files, {
				'padding'			: 0,
				'transitionIn'		: 'none',
				'transitionOut'		: 'none',
				'type'              : 'image',
				'changeFade'        : 0
			});
		});
		
		e.preventDefault();
	});	

	if (is_admin())
	{
		$('.cms-editable[data-type=char]').click(function()
		{
			if (this.contentEditable == true)
			{
				return false;
			}
			
			this.contentEditable = true;
			
			var original = $(this).html();
			
			$(this).focus();
			
			$(this).unbind('keydown').keydown(function(event)
			{
				if (event.keyCode == '13')
				{
					if ($.trim($(this).html()) == '')
					{
						return false;
					}
					
					this.contentEditable = false;
					$(this).blur();
					
					if ($.trim(original) != $.trim($(this).html()))
					{
						$.post('admin/update_text', {
							key: $(this).attr('data-key'),
							value: $.trim($(this).html()),
							url: window.location.href.replace(base_url, '')
						});
					}
					
					return false;
				}
				
				if (event.keyCode == '27')
				{
					this.contentEditable = false;
					$(this).blur();
					$(this).html(original);
				}
			});

			
			$(this).unbind('blur').blur(function()
			{
				if ($(this).attr('contenteditable') == 'true')
				{
					this.contentEditable = false;
					$(this).html(original);
				}
			});
			
			return false;
			
		});
		
		
		$('.cms-editable[data-type=text]').each(function()
		{
			var url = window.location.href.replace(base_url, '');
			var key = $(this).attr('data-key');
			
			var width = $(this).attr('data-width')
				? $(this).attr('data-width')
				: '600px';
				
			var height = $(this).attr('data-height')
				? $(this).attr('data-height')
				: '300px';
			   
		   $(this).editable(base_url + 'admin/update_text', {
		   	  submitdata: {
			  	url: url,
				key: key
			  },
		      type : 'mce',
		      submit : 'Išsaugoti',
			  cancel : 'Atšaukti',
		      indicator : "Saugoma...",
		      tooltip : 'Spustelkite kairiu pelės mygtuku...',
		      width : width,
		      height : height
		   });

		});
	}
	
	// TODO: change this...
	$('<div>')
		.attr('id', 'page_url')
		.css('display', 'none')
		.html(window.location.href.replace(base_url, '').replace(/\/?$/, ''))
		.prependTo('body');
		
	// Searchbox behavior
	$('#search_button').toggle(function() {
		$(this).addClass('active');
		$('#search_box').fadeIn(500);
	},
	function() {
		$('#search_button').removeClass('active');
		$('#search_box').hide(0);
	});

	// Udjust spaces between top navigation links
	 
	var top_nav_width = 0;
	$('#top-nav a').each(function() {
		top_nav_width += $(this).width();
	});
	
	var w = 990 - 140 - top_nav_width;
	var margin = w/$('#top-nav a').size();
	margin = Math.floor(margin);
	$('#top-nav li').not(':last').css('marginRight', (margin-15) + 'px');
	$('#top-nav li:last').css('marginRight', 0);
	
	// Some fixes for IE6

	if ($('body').hasClass('IE6'))
	{
		$('body, html').css('overflowX', 'hidden');
		$('.inner #content').css('height', 'auto');
		$('.index #content').css('height', '200px');
	}

	// Select a top navigation link
	if (uri.indexOf('#') != -1 && uri.length > 3)
	{
		$("#top-nav li a[href=" + uri + "]").closest('li')
			.addClass('active')
			.addClass('selectedLava');
	}
	
	if ( ! is_admin())
	{
		// Cufon some text
		if (current_lang != 'ru')
		{
			Cufon.replace('#bar1 h1', {
				fontFamily: 'HelveticaXBlkCn',
				textShadow: '1px 1px rgba(0, 0, 0, 0.2)'
			});
	
			Cufon.replace('.bar2 .large_title', { fontFamily: 'HelveticaBlk', textShadow: '-1px -1px rgba(0, 0, 0, 0.2)' });
			Cufon.replace('.bar2 h1',           { fontFamily: 'HelveticaTh' });
			Cufon.replace('#footer',            { fontFamily: 'HelveticaXBlkCn' });
	
			Cufon.replace('#content h1', { fontFamily: 'HelveticaTh' });
			Cufon.replace('#content h2', { fontFamily: 'HelveticaTh' });
		}
		else
		{
			Cufon.replace('#bar1 h1', {
				fontFamily: 'arialblackbold',
				textShadow: '1px 1px rgba(0, 0, 0, 0.2)'
			});
	
			Cufon.replace('.bar2 .large_title', { fontFamily: 'arialblackbold', textShadow: '-1px -1px rgba(0, 0, 0, 0.2)' });
			Cufon.replace('.bar2 h1',           { fontFamily: 'arialcufoned' });
			Cufon.replace('#footer',            { fontFamily: 'arialblackbold' });
	
			Cufon.replace('#content h1', { fontFamily: 'arialcufoned' });
			Cufon.replace('#content h2', { fontFamily: 'arialcufoned' });
		}
	}

	// CSS corrections
	if (current_lang == 'lt' || current_lang == 'ru' || current_lang == 'en')
	{
		$('#nav a:first span').css('lineHeight', '28px');		
	}

	if (current_lang == 'ru')
	{
		$('#nav a:eq(2) span').css('lineHeight', '10px');		
	}

	var nav_animating = 0;

	// Top nav lavaLamp effects
	$("#top-nav")
	.lavaLamp({
        fx: "easeOutBack",
        speed: 800,
		autoReturn: true,
		returnDelay: 600,
		homeTop: 0, 
		homeLeft: -100, 
		homeHeight: 1, 
		homeWidth: 1,
		click: function(event, menuItem) {
			$('#top-nav li').removeClass('active');
			$(menuItem).addClass('active');
		}
    });

	$("#top-nav")
	.find('a').click(function(e)
	{
		if (is_admin() == true && $(this).attr('href').indexOf('#') != -1)
		{
			window.location.href = $(this).attr('href');
			window.location.reload();
			return true;
		}

		if (antro_lygio || $(this).closest('li').hasClass('active'))
		{
			return true;
		}

		nav_animating++;

		var tmp = nav_animating;
		var self = this;
		var id = $(self).attr('href').replace(/.{3}/, '');

		// Change background image

		var original = $('.header-bg')[0];
		var $new = $(original).clone();

		$new
			.css('zIndex', 4)
			.css('backgroundImage', 'url(' + base_url + 'assets/img/backgrounds/' + $(self).attr('data-bg') + '.jpg)')
			.prependTo('body');

		$('.header-bg').not(original).not($new).remove();

		$(original).stop(true, true).fadeOut(1000, function() {
			$new.css('zIndex', 5);
		});
		
		var template = $(self).attr('data-template');

		if (template != 'trumpa_info')
		{
			setTimeout(function() {
				$('html, body').css('overflowY', 'auto');
				var html = $('#hidden_content_' + $(self).attr('href').replace(/.{2}#/, '')).html();
				$('#content > .main').hide(0).html(html).fadeIn(1000);
				$('#content').addClass('like_inner');
				$('#footer').css('top', '-20px');
		

		////////////////////////////////////////	

		// Cufon some text
		if (current_lang != 'ru')
		{
			Cufon.replace('#bar1 h1', {
				fontFamily: 'HelveticaXBlkCn',
				textShadow: '1px 1px rgba(0, 0, 0, 0.2)'
			});
	
			Cufon.replace('.bar2 .large_title', { fontFamily: 'HelveticaBlk', textShadow: '-1px -1px rgba(0, 0, 0, 0.2)' });
			Cufon.replace('.bar2 h1',           { fontFamily: 'HelveticaTh' });
			Cufon.replace('#footer',            { fontFamily: 'HelveticaXBlkCn' });
	
			Cufon.replace('#content h1', { fontFamily: 'HelveticaTh' });
			Cufon.replace('#content h2', { fontFamily: 'HelveticaTh' });
		}
		else
		{
			Cufon.replace('#bar1 h1', {
				fontFamily: 'arialblackbold',
				textShadow: '1px 1px rgba(0, 0, 0, 0.2)'
			});
	
			Cufon.replace('.bar2 .large_title', { fontFamily: 'arialblackbold', textShadow: '-1px -1px rgba(0, 0, 0, 0.2)' });
			Cufon.replace('.bar2 h1',           { fontFamily: 'arialcufoned' });
			Cufon.replace('#footer',            { fontFamily: 'arialblackbold' });
	
			Cufon.replace('#content h1', { fontFamily: 'arialcufoned' });
			Cufon.replace('#content h2', { fontFamily: 'arialcufoned' });
		}


		///////////////////////////////////////////

			}, 2500);
		}
		else
		{
			//$('#footer').css('opacity', '0');
			if (current_page != 'index')
			{
			setTimeout(function() {
				$('#content').removeClass('like_inner');
				$('#content > .main').fadeOut(1000, function() {
					$('#footer').animate({
						top: '5px'//,
						//opacity: 1
					}, 500)
					$(this).html('');
					$('html, body').css('overflowY', 'hidden');
				});
			}, 3000);
			}
		}


		if (current_page == 'index')
		{
			setTimeout(function() {	nav_hide         (600)     },  500);
			setTimeout(function() { bar1_hide        (800)     }, 800);
			setTimeout(function() { bar2_show        (800, id) }, 1900);
			setTimeout(function() { footer_position2 (2700)    }, 2750);
			setTimeout(function() { $('.banner').fadeOut(1000) }, 1000);

		}
		else
		{
			setTimeout(function() { bar2_hide        (800)     }, 800);
			setTimeout(function() { bar2_show        (800, id) }, 1900);
		}

		current_page = 'inner';

		setTimeout(function()
		{
				if (tmp == nav_animating)
				{
					var so = new SWFObject(base_url + 'assets/js/player.swf', 'mpl', '254', '143', '9');
					so.addParam('allowfullscreen', 'true');
					so.addParam('allowscriptaccess', 'always');
					so.addParam('wmode', 'opaque');
					so.addVariable('file', base_url + 'assets/flash/' + $(self).attr('data-video') + '.flv');
					so.addVariable('smoothing', 'false');
					so.addVariable('playerready', 'video_ready');
					//so.addVariable('bufferlength','2');
					so.addVariable('backcolor', 'FFFFFF');
					so.addVariable('frontcolor', 'FFFFFF');
					so.addVariable('lightcolor', 'FFFFFF');
					so.addVariable('screencolor', 'FFFFFF');
					so.addVariable('controlbar', 'none');
					so.addVariable('autostart', 'true');
					so.addVariable('repeat', 'always');
					so.addVariable('stretching', 'fill');

					if ( ! is_admin())
					{
						so.write('video_' + id);						
					}
				}
		}, 3000);
		
		e.preventDefault();
	});

	// Preload images
	$('<img />').attr('src', base_url + 'assets/img/backgrounds/imone.jpg').css('display', 'none').appendTo('body');
	$('<img />').attr('src', base_url + 'assets/img/backgrounds/gaminiai.jpg').css('display', 'none').appendTo('body');
	$('<img />').attr('src', base_url + 'assets/img/backgrounds/paslaugos.jpg').css('display', 'none').appendTo('body');
	$('<img />').attr('src', base_url + 'assets/img/backgrounds/klientai.jpg').css('display', 'none').appendTo('body');
	$('<img />').attr('src', base_url + 'assets/img/backgrounds/susisiekite.jpg').css('display', 'none').appendTo('body');
	$('<img />').attr('src', base_url + 'assets/img/info.png').css('display', 'none').appendTo('body');
	$('<img />').attr('src', base_url + 'assets/img/white_opacity20.png').css('display', 'none').appendTo('body');
	$('<img />').attr('src', base_url + 'assets/img/bar1.png').css('display', 'none').appendTo('body');
	$('<img />').attr('src', base_url + 'assets/img/bar2.png').css('display', 'none').appendTo('body');
	$('<img />').attr('src', base_url + 'assets/img/bg_bottom_border.png').css('display', 'none').appendTo('body');
	$('<img />').attr('src', base_url + 'assets/img/bottom1.gif').css('display', 'none').appendTo('body');
	$('<img />').attr('src', base_url + 'assets/img/top.png').css('display', 'none').appendTo('body');
	$('<img />').attr('src', base_url + 'assets/img/logo.png').css('display', 'none').appendTo('body');
});



// When page loads (including images)

$(window).load(function() {

	if ($('body').hasClass('index'))
	{
		var uri = window.location.href.replace(base_url, '');
		
		var template = $('#top-nav a[href*=' + uri + ']').attr('data-template');	
		var bg =       $('#top-nav a[href*=' + uri + ']').attr('data-bg');	
		
		$('.header-bg').css('backgroundImage', 
			'url(' + base_url + 'assets/img/backgrounds/' + bg + '.jpg)');

		// Fade in background images
		$('.header-bg, #content, #logo').fadeIn(400);

		if (uri.indexOf('#') != -1 && uri.length > 3)
		{
			uri = uri.replace(/.*#/, '');
			
			if (template != 'trumpa_info')
			{
				setTimeout(function() {
					$('html, body').css('overflowY', 'auto');
					var html = $('#hidden_content_' + uri.replace(/.{2}#/, '')).html();
					$('#content > .main').hide(0).html(html).fadeIn(1000);
					$('#content').addClass('like_inner');
				}, 1500);
				setTimeout(function() { footer_position2  (1000) }, 1500);
				setTimeout(function() { top_bar_show (1000) }, 1000);
			}
			else
			{
				setTimeout(function() { footer_position2  (800) }, 1000);
				setTimeout(function() { top_bar_show (1000) }, 1900);
			}
			
			setTimeout(function() { bar2_show    (600, uri) }, 400);
			setTimeout(function() {

				var so = new SWFObject(base_url + 'assets/js/player.swf', 'mpl', '254', '143', '9');
				so.addParam('allowfullscreen', 'true');
				so.addParam('allowscriptaccess', 'always');
				so.addParam('wmode', 'opaque');
				so.addVariable('file', base_url + 'assets/flash/' + $('#top-nav a[href*='+uri+']').attr('data-video') + '.flv');
				so.addVariable('smoothing', 'false');
				so.addVariable('playerready', 'video_ready');
				//so.addVariable('bufferlength','2');
				so.addVariable('backcolor', 'FFFFFF');
				so.addVariable('frontcolor', 'FFFFFF');
				so.addVariable('lightcolor', 'FFFFFF');
				so.addVariable('screencolor', 'FFFFFF');
				so.addVariable('controlbar', 'none');
				so.addVariable('autostart', 'true');
				so.addVariable('repeat', 'always');
				so.addVariable('stretching', 'fill');

				if ( ! is_admin())
				{
					so.write('video_' + uri);					
				}
			}, 4000);
		}
		else
		{
			setTimeout(function() { bar1_show    (600) }, 400);
			setTimeout(function() { nav_show     (800) }, 1000);
			setTimeout(function() { footer_show  (800) }, 1800);
			setTimeout(function() { slogan_show  (800) }, 2600);
			setTimeout(function() { top_bar_show (1600) }, 3400);
			setTimeout(function() { $('.banner').fadeIn(1000) }, 5500);
		}
	}
	else
	{

		// Fade in background images
		$('.header-bg, #content, #logo').fadeIn(0);

		// Animations

		top_bar_show (0);

		setTimeout(function() {

			var id = $('.video').attr('id').replace(/video_/, '');
			var so = new SWFObject(base_url + 'assets/js/player.swf', 'mpl', '254', '143', '9');
			so.addParam('allowfullscreen', 'true');
			so.addParam('allowscriptaccess', 'always');
			so.addParam('wmode', 'opaque');
			so.addVariable('file', base_url + 'assets/flash/' + id + '.flv');
			so.addVariable('smoothing', 'false');
			so.addVariable('playerready', 'video_ready');
			//so.addVariable('bufferlength','2');
			so.addVariable('backcolor', 'FFFFFF');
			so.addVariable('frontcolor', 'FFFFFF');
			so.addVariable('lightcolor', 'FFFFFF');
			so.addVariable('screencolor', 'FFFFFF');
			so.addVariable('controlbar', 'none');
			so.addVariable('autostart', 'true');
			so.addVariable('repeat', 'always');
			so.addVariable('stretching', 'fill');
				
			if ( ! is_admin())
			{
				so.write('video_' + id);				
			}
		}, 1000);
	}	

}); // On window load



/**
 * Show the main navigation
 */
function nav_show(speed) {

	$('#nav a img').css({
		width: '1px',
		height: '1px'
	});

	$('#nav span').css('fontSize', '1px');

	$('#nav').show();

	$('#nav a img:not(:eq(2))').animate({
		width: '176px',
		height: '111px'
	}, speed, 'easeOutQuart');

	$('#nav a img:eq(2)').animate({
		width: '246px',
		height: '155px'
	}, speed, 'easeOutQuart');

	$('#nav span:not(:eq(2))').animate({
		fontSize: '.8em'
	}, speed, 'easeOutQuart');

	$('#nav span:eq(2)').animate({
		fontSize: '1.1em',
		bottom: '14px'
	}, speed, 'easeOutQuart');

	// Attach event handlers
	nav_events();
}

/**
 * Hide the main navigation
 */

function nav_hide(speed) {

	// Remove events
	nav_events(true);

	// Special for IE
	if ($.browser.msie) {
		$('#nav a img').animate({
			width: '123px',
			height: '76px'
		}, speed);

		$('#nav span').animate({
			fontSize: '5px'
		}, speed);

		setTimeout(function() {
			$('#nav').hide(0);
		}, speed - 200);

		return;
	}

	$('#nav a img').animate({
		width: '1px',
		height: '1px'
	}, speed);

	$('#nav span').animate({
		fontSize: '1px'
	}, speed);

	setTimeout(function() {
		$('#nav').hide();
	}, speed);	
}



/**

 * Adds event handlers to the main navigation

 * 

 * @param remove Removes the event handlers

 */

function nav_events(remove) {

	

	if (remove) {

		$('#nav a').unbind();

		return;

	}

	

	$('#nav a').mouseenter(function() {

		

		if ($(this).hasClass('zoomed')) {

			return;

		}

		

		// Zoom out the currently zoomed in navigation "box"

		nav_zoom_out($('#nav a.zoomed'), 600);

		

		nav_zoom_in(this, 600);

	})

	

	.mouseleave(function() {



		if ($(this).hasClass('zoomed')) {

			return;

		}

		

		nav_zoom_out(this, 600);

	});

}



/**

 * Zooms in a navigation anchor

 * 

 * @param el The anchor tag of the navigation "box" that we want to zoom in

 * @param speed Animation speed

 */

function nav_zoom_in(el, speed) {

	

	$('img', el).stop().animate({

		width: '246px',

		height: '155px'

	}, speed, 'easeOutCubic');

	

	$('span', el).stop().animate({

		fontSize: '1.1em',

		bottom: '14px'

	}, speed, 'easeOutCubic');

	

	$(el).addClass('zoomed');

}



/**

 * Zooms out a navigation anchor

 * 

 * @param el The anchor tag of the navigation "box" that we want to zoom out

 * @param speed Animation speed

 */

function nav_zoom_out(el, speed) {

	

	$('img', el).stop().animate({

		width: '176px',

		height: '111px'

	}, speed, 'easeOutCubic');

	

	$('span', el).stop().animate({

		fontSize: '.8em',

		bottom: '9px'

	}, speed, 'easeOutCubic');

	

	$(el).removeClass('zoomed');

}



/**

 * Show the top bar

 * @param speed Animation speed

 */

function top_bar_show(speed) {

	$('#top-bar').animate({top: 0}, speed, 'easeOutQuart', function() {

		$('#top-bar li.active').mouseenter();

	});

	

	if ($('body').hasClass('IE6')) {

		$('#top-bar #logo').animate({top: '28px'}, speed, 'easeOutQuart');

	}

	else {

		$('#top-bar #logo').animate({top: '18px'}, speed, 'easeOutQuart');	

	}

}



function bar1_show(speed) {

	$('#bar1 > .container').show(0).stop(true, true).animate({top: '353px'}, speed, 'easeOutQuart');

	$('#bottom-border').stop(true, true).animate({top: '491px'}, speed);

}



function bar1_hide(speed) {

	$('#bar1 > .container').stop(true, true).animate({top: '553px'}, speed, 'easeInQuart').hide(0);

	$('#bottom-border').stop(true, true).animate({top: '691px'}, speed, 'easeInQuart');

}



function bar2_show(speed, id) {

	

	$('#' + id + '.bar2 > .container').show(0)

		.stop().animate({top: '200px'}, speed, 'swing', function() {

			$('.bar2').not($('#' + id))

				.removeClass('opened_bar2')

				.find('.video').html('').end()

				.find('img').show(0).end()

				.find('.container').hide(0);

		});

		

	$('.bar2').not('#' + id).find('> .container')

		.stop().animate({top: '553px'}, speed);

		

	$('#' + id).addClass('opened_bar2');

	$('#bottom-border').stop(true, true).animate({top: '491px'}, 0);

}



function bar2_hide(speed) {



	$('.bar2.opened_bar2')

		.removeClass('opened_bar2')

		.find('> .container')

			.stop(true, true).animate({top: '553px'}, speed, 'easeInQuart');

	$('#bottom-border').stop(true, true).animate({top: '691px'}, speed, 'easeInQuart');

}



function slogan_show(speed) {

	$('#bar1 .heading1').fadeIn(speed);

}



function footer_show(speed) {

	var top = ($.browser.msie && ($('body').hasClass('IE6') || $('body').hasClass('IE7'))) 

		? '38px' : '50px'; 

	$('#footer').animate({top: top}, speed, 'easeOutQuart');

}



function footer_position2(speed) {

	var top = ($.browser.msie && ($('body').hasClass('IE6') || $('body').hasClass('IE7'))) 

		? '-32px' : '-20px';

 

	$('#footer').animate({top: top}, speed, 'easeOutQuart');

}



function footer_show_inner(speed) {

	$('#footer').animate({bottom: 0}, speed, 'easeOutQuart');

}



function footer_position2_inner(speed) {

	$('#footer').animate({bottom: 0}, speed, 'easeOutQuart');

}




