/* Element extensions */

Element.extend({
	
	show: function()
	{
		this.setStyles({display: 'block'});
	},
	
	hide: function() {
		this.setStyles({display: 'none'});
	},
	
	hasClass: function(className){
		if (classes = this.getAttribute('class')) {
			if(classes.indexOf(className) != -1) return true;
		}
		
		return false;
	},
	
	setLoader: function(){
		this.set('html', '<div style="text-align:center;">Augnablik<br /><img src="/img/ajaxLoaderWhite.gif" alt="Loading" /></div>');
	},

	AjaxUpdate: function(url, pb){
		
		if (!pb) {
			pb = false;
		}
		
		var update = this;
		var loader = (function() { this.setLoader(); }.bindWithEvent(this)).delay(200);	
		/* VIRKAR Í SAFARI new Request.HTML({ encoding: 'ISO-8859-1', onComplete: function(bla,bla2,text,js){ $clear(loader); update.set('html', text); eval(js); if (pb) { loadPhotoBox(); }  }}).get(url);*/
		new Request.HTML({ update: this, encoding: 'ISO-8859-1', onComplete: function(){ $clear(loader); if (pb) { loadPhotoBox(); }  }}).get(url);
	/*	new Request({
			url: url,
			encoding: 'ISO-8859-1',
			evalScripts: true,
			onComplete: function(text){
				$clear(loader);
				
				if (pb) loadPhotoBox();
				
				update.set('html', text);
			}
		}).get();*/
	},
	
	toggleHidden: function()
	{	
		if (this.getStyle('display') != 'none') {
			var elem = this.setStyles({opacity: 0});
			new Fx.Tween(elem, {duration: 200, onComplete: function() { elem.setStyle('display', 'none'); }} ).start('opacity', 1, 0);
		} else {
			var elem = this.setStyles({display:'', opacity: 0});
			new Fx.Tween(elem, {duration: 500} ).start('opacity', 0, 1);
		}
	},
	
	resize: function(width,height,dur,com)
	{
		if (!width) width = false;
		if (!height) height = false;
		if (!dur) dur = 200;
		if (!com) com = Class.empty;
		
		var myEffects = new Fx.Styles(this, {duration: dur, transition: Fx.Transitions.linear, onComplete: eval(com)});

		if (width && height) {
			myEffects.start({
			    'height': height,
			    'width': width,
			    'margin-left': -(width / 2),
			    'margin-top': -(height / 2)
			});
			alert('nei');
		} else if (width) {
			myEffects.start({
			    'width': width,
			    'margin-left': -(width / 2)
			});
		} else if (height) {
			myEffects.start({
			    'height': height,
			    'margin-top': -(height / 2)
			});
		}

	},
	
	toggleHeight: function(from,to)
	{
		var myEffects = new Fx.Styles(this, {duration: 300, transition: Fx.Transitions.Circ.easeOut});
		
		if (this.getStyle('height') != to + 'px') {
			myEffects.start({'height': to});
			return to;
		} else {
			myEffects.start({'height': from});
			return from;
		}
	}
        
});

/* Global vars */

var pop,smallpop,imagepop,videopop,overlay,popLoader;
var formSubmited = false;
var popFormSubmited = false;

/* Functions */

function getPageSize(){
	
	var xScroll = $(window).getScrollWidth();
	var yScroll = $(window).getScrollHeight();
	
	var windowHeight = $(window).getHeight();
	var windowWidth = $(window).getWidth();
	
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

function insertOverlay(dur, op)
{
	if (!op) op = 0.5; 
	if (!dur) dur = 200;
	if (overlay) return false;
	
	pageSize = getPageSize();
	
	overlay = new Element('div', { 'id': 'overlay' });
	overlay.setStyle('height',  pageSize[1]);
	overlay.setStyle('width',  pageSize[2]);
	
	var fadeOverlay = overlay.setStyles({display:'', opacity: 0});
	new Fx.Tween(fadeOverlay, {duration: Math.round(dur / 2) } ).start('opacity', 0, op);
	
	$(document.body).adopt(overlay);
	
	window.addEvent('resize', function(){ if(overlay){ pageSize = getPageSize(); overlay.setStyle('width', pageSize[2]); }});
}

function popNotice(message)
{
	if (smallpop) {
		smallpop.destroy();
		smallpop = false;
	}
	
	smallpop = new Element('div', { 'id': 'PopUpSmall' });
	var wrap = new Element('div', { 'class': 'wrap' });
	
	wrap.set('html', '<p>' + message + '</p>');
	
	smallpop.adopt(wrap);
	
	var popHeight = Math.round(smallpop.getStyle('height'));
	
	$(document.body).adopt(smallpop);
	
	var fadePop = smallpop.setStyles({
			marginLeft: -150,
			marginTop: -(popHeight / 2),
			display: '',
			opacity: 0,
			width: 300
	});
	
	new Fx.Tween(fadePop, {duration: 300 } ).start('opacity', 0, 1);
	smallpopKill.delay(1500);
	return false;
	
}

function popError(message)
{
	if (smallpop) {
		smallpop.destroy();
		smallpop = false;
	}
	
	smallpop = new Element('div', { 'id': 'PopUpSmall' });
	var wrap = new Element('div', { 'class': 'wrap' });
	
	wrap.addClass('error');
	wrap.set('html', '<p>' + message + '</p>');
	
	smallpop.adopt(wrap);
	
	var popHeight = Math.round(smallpop.getStyle('height'));
	
	$(document.body).adopt(smallpop);
	
	var fadePop = smallpop.setStyles({
			marginLeft: -150,
			marginTop: -(popHeight / 2),
			display: '',
			opacity: 0,
			width: 500,
			overflow: 'auto'
	});
	
	new Fx.Tween(fadePop, {duration: 300 } ).start('opacity', 0, 1);
	//smallpopKill.delay(1500);
	smallpop.addEvent('click', function(){ smallpopKill(true); });
	return false;
	
}

function popConfirm(elem,message)
{
	if (smallpop) {
		smallpop.destroy();
		smallpop = false;
	}
	
	if (pop) pop.tween('opacity', 0.5);
	
	var parent = $(elem);
	
	smallpop = new Element('div', { 'id': 'PopUpSmall' });
	var wrap = new Element('div', { 'class': 'wrap' });
	var buttonWrap = new Element('div', { 'class': 'buttonWrap' });
	var confirmLink = new Element('input', {
		'type': 'submit',
		'value': 'Já, áfram',
		'styles': { 
			'margin-right': '20px'
		} 
	});
	var closeLink = new Element('input', {
		'type': 'submit',
		'value': 'Hætta við',
		'events': {
			'click': function (e) {
				e.preventDefault();
				if (pop) pop.tween('opacity', 1);
				smallpopKill();
			}
		}
	});
	
	elemRel = parent.getProperty('rel');
	elemType = parent.get('tag');
	
	if (elemType == 'form') {
		
		var formID = parent.getProperty('id');
		
		if (elemRel) {
			confirmLink.set({'events':{'click': function (e) { 
				e.preventDefault();
				eval(elemRel); 
				if (pop) pop.tween('opacity', 1);
				smallpopKill(); 
			}}});
		} else if (formID) {
			confirmLink.addEvent('click', function (e) { e.preventDefault(); $(formID).submit(); });
		} else {
			alert('No id or rel set on form');
		}
	
	} else if (elemType == 'input') {
		
		var inputType = parent.get('type');
		
		/* Not working!!
		if (inputType == 'checkbox') {
			
			if (parent.get('checked')) {
				confirmLink.set({'events':{'click': function (e) { e.preventDefault(); parent.erase('checked'); } }});
			} else {
				confirmLink.set({'events':{'click': function (e) { e.preventDefault(); parent.set('checked', 'checked'); } }});
			}
		}*/
		
		if (elemRel) {
			confirmLink.addEvent('click', function (e) { 
				e.preventDefault();
				eval(elemRel); 
				if (pop) pop.tween('opacity', 1);
				smallpopKill(); 
				return true;
			});
		} else {
			confirmLink.addEvent('click', function(e){
				e.preventDefault();
				parent.set('checked', 'checked');
				if (pop) pop.tween('opacity', 1);
				smallpopKill(); 
			});
		}
		
	} else if (elemType == 'a') {
		
		
		if (elemRel) {
			confirmLink.addEvent('click', function (e) {
				e.preventDefault();
				eval(elemRel);
				if (pop) pop.tween('opacity', 1);
				smallpopKill();
			});
			
		} else {
			//confirmLink.set({'href': parent.getProperty('href')});
			confirmLink.set({'events':{'click': function (e) { document.location = parent.getProperty('href'); }}});
		}
	}
	
	wrap.set('html', '<p>' + message + '</p>');
	
	buttonWrap.adopt(confirmLink, closeLink);
	wrap.adopt(buttonWrap);
	smallpop.adopt(wrap);
	
	popHeight = Math.round(smallpop.getStyle('height'));
	
	$(document.body).adopt(smallpop);
	
	var fadePop = smallpop.setStyles({
			marginLeft: -150,
			marginTop: -(popHeight / 2),
			display: '',
			opacity: 0,
			width: 300
	});
	
	insertOverlay();
	
	new Fx.Tween(fadePop, {duration: 300 } ).start('opacity', 0, 1);
	
	return false;
}

function popAjax(title,url,width,height,dur)
{
	if (!dur) dur = 200;
	
	if (pop) {
		popResize(width,height);
		popWrap.AjaxUpdate(url);
		popTitle.set('html', title);
		return false;
	}
	
	winWidth = $(window).getWidth();
	winHeight = $(window).getHeight();
	
	if (winWidth < width) width = winWidth - 50;
	if (winHeight < height) height = winHeight - 50;
	
	pop = new Element('div', {
		'id': 'PopUp',
		'class': 'PopUp',
		'styles': {
			'width': width,
			'margin-left': -(width / 2),
			'margin-top': -( (height / 2) + 50)
		} 
	});
	
	popWrap = new Element('div', {
		'id': 'PopUpWrap',
		'class': 'wrap',
		'styles': {
			'overflow': 'auto',
			'height': height
		}
	});
	
	popTitle = new Element('a', { 'class': 'title' }).set('html', title);
	
	var closeLink = new Element('a', {
		'href': '#',
		'class': 'close',
		'events': {
			'click': function(e) {
				e.preventDefault();
				popKill();
			}
		}
	});
	
	
	closeLink.set('html', 'Lat aftur');
	
	
	var fadePop = pop.setStyles({display:'', opacity: 0});
	
	insertOverlay(dur);
	
	pop.adopt(closeLink,popTitle);
	pop.adopt(popWrap);
	$(document.body).adopt(pop);
	
	pop.setStyles('display', '');
	
	new Fx.Tween(fadePop, {duration: dur } ).start('opacity', 0, 1);
		
	popWrap.AjaxUpdate(url);
}

function popImage(image,title)
{
	if (!title) title = '';
	
	if ($$('.OpenX')) $$('.OpenX').setStyle('display', 'none');

	insertOverlay();
	
	smallpop = new Element('div', {
			'id': 'PopUpSmall',
			'styles': {
				'width' : 300,
				'margin-left': -150
			}
	});
		
	var loader = (function() {
		popWrap = new Element('div', {'class':'wrap'});
		popWrap.set('html', '<h1>Augnablik</h1><p><img src="/img/ajaxLoaderWhite.gif" alt="Loading" /></p>');
		smallpop.adopt(popWrap);
		popHeight = Math.round(smallpop.getStyle('height'));
		smallpop.setStyle('margin-top', -(popHeight / 2));
		$(document.body).adopt(smallpop);
	}).delay(300);
	
	var img = new Asset.image(image,{
		onload: function(){
		
			$clear(loader);
			smallpop.destroy();
			
			if (pop) pop.tween('opacity', 0.5);
			
			winWidth = $(window).getWidth();
			winHeight = $(window).getHeight();
			
			ratio = img.width / img.height;
			
			if (winHeight < img.height) {
			 	height = winHeight - 30;
			 	width = height * ratio;	
			} else {
				width = img.width;
				height = img.height;
			}
			
			imagepop = new Element('div', {
				'id': 'PopUpImage',
				'styles': {
					'width': width,
					'margin-left': -(width / 2),
					'margin-top': -( (height / 2))
				} 
			});
			
			showImg = new Element('img', {
				'src': image,
				'styles': {
					'width': width,
					'height': height
				}
			});
			
			var fadePop = imagepop.setStyles({display:'', opacity: 0});
			
			imagepop.adopt(showImg);
			
			if (title != '') {
				caption = new Element('p').set('html', title);
				imagepop.adopt(caption);
			}
			
			$(document.body).adopt(imagepop);
			
			new Fx.Tween(fadePop, {duration: 200 } ).start('opacity', 0, 1);
			
			$$(overlay,showImg).addEvent('click', function(){
				if (pop) pop.tween('opacity', 1);
				imagepopKill();
				if ($$('.OpenX')) $$('.OpenX').setStyle('display', '');
			});
		},		
		onerror: function(){
			$clear(loader);
			smallpop.destroy();
			if (pop) pop.tween('opacity', 1);
			imagepopKill();
			if ($$('.OpenX')) $$('.OpenX').setStyle('display', '');
		}
	});
	
}

function popResize(width,height)
{
	if (pop) {
		popWrap.setStyle('overflow', 'hidden');
		new Fx.Styles(popWrap, {duration: 200, transition: Fx.Transitions.linear}).start({'height': height});
		new Fx.Styles(pop, {duration: 200, transition: Fx.Transitions.linear, onComplete: function(){popWrap.setStyle('overflow', 'auto');}}).start({
			'width': width,
			'margin-left': -(width / 2),
			'margin-top': -( (height / 2) + 50)
		});
	}
}

function overlayKill(dur,force)
{
	if (!force) force = false; 
	if (overlay) {
		
		if (!(pop && smallpop) || force == true) {
			ol = new Fx.Tween(overlay, {
					duration: Math.round(dur * 1.3),
					onComplete: function(){
						if (overlay) {
							overlay.destroy();
							overlay = false;
						}
					}
			}).start('opacity', 0.5, 0);
		
		}
	}
}

function smallpopKill(skipOverlay)
{
	if (!skipOverlay) {
		overlayKill(200);
	}
	
	if (smallpop) {
		new Fx.Tween(smallpop, {
				duration: 200,
				onComplete: function(){
					smallpop.destroy();
					smallpop = false;
				}
		}).start('opacity', 1, 0);
	}
}

function imagepopKill()
{
	overlayKill(200);
	if (imagepop) {
		new Fx.Tween(imagepop, {
				duration: 200,
				onComplete: function(){
					if ($(imagepop)) {
						$(imagepop).destroy();
					}
					
					
					if ($$('#PopUpImage')) {
						$$('#PopUpImage').destroy();
					}
					imagepop = false;
				}
		}).start('opacity', 1, 0);
	}
}

function popKill(dur)
{
	if (!dur) dur = 200;
	
	overlayKill(dur,true);
	smallpopKill();
	
	if (pop) {
		new Fx.Tween(pop, {
				duration: dur,
				onComplete: function(){
					pop.destroy();
					pop = false;
				}
		}).start('opacity', 1, 0);
	}
	
	return false;
}

function bloggarDelete(id,row)
{
	$$('tr.' + row + id).each(function(element) {
		new Fx.Tween(element, {
			duration: 400,
			onComplete: function(){
				element.destroy();
			}
		}).start('opacity', 1, 0);
	});	
}

function bloggarFormSubmit()
{
	if (formSubmited) {
		return false;
	}
	
	formSubmited = true;
	
	if (smallpop) {
		smallpop.destroy();
		smallpop = false;
	}
	
	insertOverlay();
	
	smallpop = new Element('div', {
		'id': 'PopUpSmall',
		'styles': {
			'width' : 300,
			'margin-left': -150
		}
	});
	
	popWrap = new Element('div', {'class':'wrap'});
	popWrap.set('html', '<h1>Augnablik</h1><p><img src="/img/ajaxLoaderWhite.gif" alt="Loading" /></p>');
	smallpop.adopt(popWrap);
	
	popHeight = Math.round(smallpop.getStyle('height'));
	smallpop.setStyle('margin-top', -(popHeight / 2));
	
	$(document.body).adopt(smallpop);
	
	return true;
}

function popFormSubmit()
{
	if (!pop) return false;
	
	if (popFormSubmited) {
		return false;
	}
	
	popFormSubmited = true;
	
	popLoader = $('PopUpWrap').clone();
	popLoader.set({'id': 'PopUpLoader'});
	popLoader.setStyle('text-align', 'center');
	popLoader.set('html', '<h1>Augnablik</h1><p><img src="/img/ajaxLoaderWhite.gif" alt="Loading" /></p>');
	$('PopUpWrap').setStyle('display', 'none');
	pop.adopt(popLoader);
	return true;
}

function popFormRevert()
{
	if (popLoader) {
		popLoader.destroy();
		$('PopUpWrap').setStyle('display', 'block')
	}
}

function showProgress(key, id)
{
	$(id).AjaxUpdate('/stats/ajax/progress.php?progressKey=' + key);
}

function loadMediaPlayer(video){
	//var colorWrap = $$('div.PopUp .wrap');
	var mediaPlayer = new Swiff('/swf/mediaplayer.swf', {
		width: 500,
		height: 400,
		params: {
			allowfullscreen: 'true',
			wmode: 'transparent',
			bgcolor: '#000000'
		},
		vars: {
			autostart: 'true',
			overstretch: 'false',
			file: video,
			backcolor: '0x1E1E1E',
			frontcolor: '0xCCCCCC',
			lightcolor: '0x2E4E5D'
		}
	});
	
	$('PopUpWrap').adopt(mediaPlayer);
	
}

function popVideo(title,video,width,height,dur)
{
	if (!dur) dur = 200;
	
	if (pop) {
		popResize(width,height);
		popWrap.AjaxUpdate(url);
		popTitle.set('html', title);
		return false;
	}
	
	winWidth = $(window).getWidth();
	winHeight = $(window).getHeight();
	
	if (winWidth < width) width = winWidth - 50;
	if (winHeight < height) height = winHeight - 50;
	
	pop = new Element('div', {
		'id': 'PopUp',
		'class': 'PopUp',
		'styles': {
			'width': width,
			'margin-left': -(width / 2),
			'margin-top': -( (height / 2) + 50)
		} 
	});
	
	popWrap = new Element('div', {
		'id': 'PopUpWrap',
		'class': 'wrap',
		'styles': {
			'background': '#000',
			'height': height,
			'padding': '30px'
		}
	});
	
	popTitle = new Element('a', { 'class': 'title' }).set('html', title);
	
	var closeLink = new Element('a', {
		'href': '#',
		'class': 'close',
		'events': {
			'click': function(e) {
				e.preventDefault();
				popKill();
			}
		}
	});
	
	
	closeLink.set('html', 'Loka myndbandi');
	
	
	var fadePop = pop.setStyles({display:'', opacity: 0});
	
	insertOverlay(dur);
	
	pop.adopt(closeLink,popTitle);
	pop.adopt(popWrap);
	$(document.body).adopt(pop);
	
	pop.setStyles('display', '');
	
	new Fx.Tween(fadePop, {duration: dur } ).start('opacity', 0, 1);
	
	popWrap.set('html', '<object width="500" height="400"><param name="movie" value="/videoplayer/'+video+'/play"></param><param name="allowFullScreen" value="true"></param><embed src="/videoplayer/'+video+'/play" type="application/x-shockwave-flash" allowfullscreen="true" width="500" height="400"></embed></object>');
	/*
	new Ajax(url, {
		onSuccess: function(text) {
			var mediaPlayer = new Swiff('/swf/mediaplayer.swf', {
				width: 500,
				height: 400,
				params: {
					allowfullscreen: 'true',
					wmode: 'transparent',
					bgcolor: '#000000'
				},
				vars: {
					autostart: 'true',
					overstretch: 'false',
					file: text,
					//image: text.replace('video', 'image').replace('.flv', '.jpg'),
					backcolor: '0x1E1E1E',
					frontcolor: '0xCCCCCC',
					lightcolor: '0x2E4E5D'
				}
			});
			
			popWrap.adopt(mediaPlayer);
		}
	}).request();
	*/
}

var audioPlayer = new Class({
	
	initialize: function(){
		this.player = null;
		this.file = null;
		this.width = 300;
		this.preset = '';
		this.artist = false;
		this.title = false;
		this.animation = 'yes';
	},
	
	setFile: function(file){
		this.file = file;
	},
	
	filePreset: function(preset){
		this.preset = preset;
	},
	
	setTitle: function(artist,title)
	{
		this.artist = artist;
		this.title = title;
	},
	
	setWidth: function(width)
	{
		this.width = width;
	},
	
	setAnimation: function(animation)
	{
		this.animation = animation;
	},
	
	load: function(container)
	{
		if (!container) {
			container = false;
		}
		
		this.player = new Swiff('http://monitor.is/swf/mp3player.swf', {
			height: 24,
		    width: this.width,
		    container: container,
		    params: {
		        wmode: 'transparent'
		    },
		    vars: {
		        soundFile: this.preset + this.file.substring(0, 1) + '/' + this.file.substring(1, 2) + '/' + this.file.substring(2,3) + '/' + this.file,
		        titles: this.title,
		        artists: this.artist,
		        animation: this.animation
		    }
		});
	}
});