Element.extend({
	tabsUpdate: function(url, pb){
		if (!pb) { pb = false; }
		var rssWindow = this; 
		var loader = (function() { this.setStyle('opacity', 0.7); }.bindWithEvent(this)).delay(200);	
		new Request.HTML({ update: this, encoding: 'ISO-8859-1', onComplete: function(){ $clear(loader); rssWindow.setStyle('opacity', 1); if (pb) { monitorGallery = new MonitorGallery(); }  }}).get(url);
	},
	searchUpdate: function(url, pb){
		var rssWindow = this; 
		var loader = (function() { this.setStyle('opacity', 0.7); }.bindWithEvent(this)).delay(200);	
		new Request.HTML({ update: this, encoding: 'ISO-8859-1', onComplete: function(){ $clear(loader); rssWindow.setStyle('opacity', 1);  }}).get(url);
	},
	opacityUpdate: function(url, pb){
		var rssWindow = this; 
		var loader = (function() { this.setStyle('opacity', 0.7); }.bindWithEvent(this)).delay(200);	
		new Request.HTML({ update: this, encoding: 'ISO-8859-1', onComplete: function(){ $clear(loader); rssWindow.setStyle('opacity', 1);  }}).get(url);
	}
});

var mySession = function() {
	var sessionRequest = new Request({url: this.sessionUrl}).get();
}

function initSession(url)
{
	mySession.periodical(300000, {sessionUrl: url});
}

// Video check
var thisUrl = document.location.href;
var query = thisUrl.split('#');

if (query.length > 1) {
	var frag = query[1].split('=');
	if (frag.length > 1) {
		switch (frag[0]) {
			case 'v':
				var videoLayer = true;
				break;
			case 'a':
				var albumLayer = true;
				break;
			case 'r':
				document.location = frag[1];
				break;
			case 'p':
		
				var actionFrag = frag[1];
								
				window.addEvent('domready', function(){
					switch (actionFrag) {
						case 'register':
							popupAjax('Skrįset', '/ajax/output.php?section=bloggar&second=register', 300, 380);
							break;
					}
				});
				
				break;
		}
	}
}

var videoPlayerPop = false;
var photoAlbumPop = false;
var monitorPop = false;
var monitorTVWrap = false;
var photoalbumWrap = false;

function displayFrontNews(num) {
	
	//alert(num);
	new Fx.Tween('FrontNewsItem' + newsRotateCurrentNum, {
		duration: 200,
		onComplete: function(){
			
			$$('#FrontNewsContent .item').setStyles({
					'display': 'none',
					'opacity': '0.0'
			});
			
			var fadeIn = $('FrontNewsItem' + num); 
			fadeIn.setStyle('display', '');
			new Fx.Tween(fadeIn, {
				duration: 200,
				onComplete: function(){
					$$('#FrontNews .buttons a').removeClass('selected');
					$$('#FrontNews .buttons a.button' + num).addClass('selected');
				}
			}).start('opacity', '0.0', '1.0');
			
			newsRotateCurrentNum = num;
			
		}
	}).start('opacity', '1.0', '0.0');
}

var newsRotateNum = 1;
var newsRotateCurrentNum = 1;
var rotateNews = function(){
	
	if (newsRotateNum == 10) {
		newsRotateNum = 0;
	}
	
	newsRotateNum++;
	
	if ($('FrontNewsItem' + newsRotateNum)) {
		
		newsRotateCurrentNum = newsRotateNum - 1;
		
		if (newsRotateCurrentNum == 0) {
			newsRotateCurrentNum = 10;
		}
		
		displayFrontNews(newsRotateNum);
		
	}
	
}

function displayFrontVideos(num) {
	
	new Fx.Tween('FrontVideosItem' + videosRotateCurrentNum, {
		duration: 200,
		onComplete: function(){
			
			$$('#FrontVideosContent .item').setStyles({
					'display': 'none',
					'opacity': '0.0'
			});
			
			var fadeIn = $('FrontVideosItem' + num); 
			fadeIn.setStyle('display', '');
			new Fx.Tween(fadeIn, {
				duration: 200,
				onComplete: function(){
					$$('#FrontVideos .buttons a').removeClass('selected');
					$$('#FrontVideos .buttons a.button' + num).addClass('selected');
				}
			}).start('opacity', '0.0', '1.0');
			
			videosRotateCurrentNum = num;
			
		}
	}).start('opacity', '1.0', '0.0');
}

var videosRotateNum = 1;
var videosRotateCurrentNum = 1;
var rotateVideos = function(){
	
	if (videosRotateNum == 6) {
		videosRotateNum = 0;
	}
	
	videosRotateNum++;
	
	if ($('FrontVideosItem' + videosRotateNum)) {
		
		videosRotateCurrentNum = videosRotateNum - 1;
		
		if (videosRotateCurrentNum == 0) {
			videosRotateCurrentNum = 6;
		}
		
		displayFrontVideos(videosRotateNum);
	}
	
}

window.addEvent('domready', function(){
		
	if ($('monitor')) {
		$$('#Colors a').each(function(el){
			el.addEvent('click', function(e){
				e.preventDefault();
				var color = el.getProperty('rel');
				
				if (color) {
					Cookie.write('theme', color, {duration: 365, path: '/'});
					$('monitor').setProperty('class', color);
				}
			});
		});
		
	}
	
	if (videoLayer) {
		document.location = '/tv/redirect/' + frag[1]
	}
	
	var frontVideos = $('FrontVideos');
	
	if ($('FrontNews')) {
		
		var rotNews = rotateNews.periodical(5000);
		var pause = $('NewsPause');
		
		$('FrontNews').addEvent('mouseover', function(){
			rotNews = $clear(rotNews);
			pause.addClass('on');
		});
		
		$('FrontNews').addEvent('mouseout', function(){
			rotNews = rotateNews.periodical(5000);
			pause.removeClass('on');
		});
		
		$$('#FrontNews .buttons a').each(function(e,i){
			e.addEvent('click', function(el){
				el.preventDefault();
				num = e.get('text');
				displayFrontNews(num);
				newsRotateNum = num;
			});
		});
	
	} else if (frontVideos) {
		
		var rotVideos = rotateVideos.periodical(8000);
		var vPause = $('VideosPause');
		
		frontVideos.addEvent('mouseover', function(){
			rotVideos = $clear(rotVideos);
			vPause.addClass('on');
		});
		
		frontVideos.addEvent('mouseout', function(){
			rotVideos = rotateVideos.periodical(8000);
			vPause.removeClass('on');
		});
		
	}
	
	/*
	if (frontVideos) {
	
		$$('#FrontVideos .buttons a').each(function(e,i){
			e.addEvent('click', function(el){
				el.preventDefault();
				num = e.get('text');
				displayFrontVideos(num);
				videosRotateNum = num;
			});
		});
	}
	*/
	
	if ($('AdsTowerRight')) {
		
		var AdsTowerPos = $('AdsTowerRight').getTop();
		
		function checkTowerAd()
		{
			if ((Window.getScrollTop() + 5) >= AdsTowerPos) {
				$('AdsTowerRight').setStyles({
					'position': 'fixed',
					'top': '5px',
					'left': $('AdsTowerRight').getLeft()
				});
			} else {
				$('AdsTowerRight').setStyle('position', 'static');
			}
		}
		
		checkTowerAd();
		
		window.addEvent('scroll', function(){
			//$('AdsTowerRight').set('text', Window.getScrollTop() + ' - ' + AdsTowerPos);
			checkTowerAd();
		});
	}
	
	// Right Menu
	
	var eventBodyBlur = function(){
		$$('body').removeEvent('click', eventBodyBlur);
		resetRightMenu();
		
		$$('.share').each(function(e,i){
			var link = e.getFirst();
			var box = link.getNext();
			link.removeClass('on');
			box.setStyle('display', 'none');		
		});
	}
	
	function setBodyBlur() { $$('body').addEvent('click', eventBodyBlur); }
	function removeBodyBlur() { $$('body').removeEvent('click', eventBodyBlur); }
	
	var Login = $('Login');
	var Search = $('Search');
	
	Login.addEvent('click', function(e){ e.stopPropagation(); });
	Search.addEvent('click', function(e){ e.stopPropagation(); });

	
	function resetRightMenu()
	{
		if ($('LoginLink')) {
			$('LoginLink').removeClass('on');
		}
		
		$('SearchLink').removeClass('on');
		$$('#Login', '#Search').setStyle('display', 'none');
	}
	
	var monitorLogin = function(e){
		
		e.preventDefault();
		
		new Request({
			url: '/ajax/monitorlogin.php',
			method: 'post',
			data: {'username': $('LoginFormUser').value, 'password': $('LoginFormPass').value},
			onComplete:function(t){
				if (t == '1') {
					location.reload(true);
				} else {
					popError('Skeivt brśkaranavn og/ella loyniorš.');
				}
			}
		}).send();
	}
	
	$('LoginSubmit').addEvent('click', monitorLogin);
	$('LoginForm').addEvent('submit', monitorLogin);
	
	if ($('LoginLink')) {
		$('LoginLink').addEvent('click', function(e){
	
			e.preventDefault();
			e.stopPropagation();
			
			if (this.hasClass('on')) {
				this.removeClass('on');
				Login.setStyles({
					'display': 'none'
				});
				removeBodyBlur();
			} else {
				resetRightMenu();
				this.addClass('on');
				Login.setStyle('display', '');
				setBodyBlur();
			}
			
		});
	}

	$('SearchLink').addEvent('click', function(e){

		e.preventDefault();
		e.stopPropagation();
		
		if (this.hasClass('on')) {
			this.removeClass('on');
			Search.setStyles({
				'display': 'none'
			});
			removeBodyBlur();
		} else {
			resetRightMenu();
			this.addClass('on');
			Search.setStyle('display', '');
			$('SearchInput').focus();
			setBodyBlur();
		}
		
	});

	var searchSubmit = function(e){
		e.preventDefault();
		$('SearchResults').searchUpdate('/ajax/output.php?section=forsida&second=searchResults&search=' + $$('#SearchForm input').getProperty('value'));
	}

	$('SearchForm').addEvent('submit', searchSubmit);
	$$('#SearchForm a').addEvent('click', searchSubmit);
	
	$('FooterLogin').addEvent('click', function(e){
		
		e.preventDefault();
		
		resetRightMenu();
		
		new Fx.Scroll(window, {
			onComplete: function(){
				$('LoginLink').addClass('on');
				$('Login').setStyle('display', '');
				setBodyBlur();
			}
		}).toTop();
		
	});
	
	$('LoginRegister').addEvent('click', function(e){
		//alert('wtf');
		e.preventDefault();
		//new Fx.Scroll(window).toTop();
		popupAjax('Skrįset', '/ajax/output.php?section=bloggar&second=register', 300, 380);
	});

	
	$$('.share').each(function(e,i){
		
		e.addEvent('click', function(e){ e.stopPropagation(); });
		
		var link = e.getFirst();
		var box = link.getNext();
		
		link.addEvent('click', function(el){
			el.preventDefault();
			el.stopPropagation();
			
			if (this.hasClass('on')) {
				this.removeClass('on');
				box.setStyle('display', 'none');
				removeBodyBlur();
			} else {
				this.addClass('on');
				box.setStyle('display', '');
				setBodyBlur();
			}
		})
	});
	
});

window.addEvent('resize', function(){
	
});

function fbs_click(u,t) {
	if (!u) u=location.href;
	if (!t) t=document.title;
	window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');
	return false;
}

function FitToContent(id, maxHeight)
{
   var text = id && id.style ? id : $(id);
   if ( !text )
      return;

   var adjustedHeight = text.clientHeight;
   if ( !maxHeight || maxHeight > adjustedHeight )
   {
      adjustedHeight = Math.max(text.scrollHeight, adjustedHeight);
      if ( maxHeight )
         adjustedHeight = Math.min(maxHeight, adjustedHeight);
      if ( adjustedHeight > text.clientHeight )
         text.style.height = adjustedHeight + "px";
   }
}

function logoutUser()
{
	new Request({
		method: 'get',
		url: '/ajax/logout.php',
		onComplete:function(t){
			location.reload(true);
		}
	}).send();
}

/*
window.addEvent('domready', function(){
	$('testarea').addEvent('keyup', function(){ FitToContent(this, document.documentElement.clientHeight); });
  
});
*/