var base = "http://www.louisiana-young.dk/";
//var base = "http://localhost/louisiana-young/";
Event.observe(window, 'load', function() {
	// observe add category
	$$('#dragAndDrop .drag').invoke('observe','click', function(event){
		Event.stop(event);
	});
	
	$$('#dragAndDrop .drag').each( function(item){
		new Draggable(item,{starteffect : dragger(item), endeffect : dragger(item)});
	});
	function dragger(item){
		   new Effect.Opacity(item, {from:1, to:1.0, duration:0});
	}
	$$('#dragAndDrop a').invoke('observe','dblclick',function(event){
		var element = Event.element(event);
		if(element.hasClassName('btn')) var link = element.up().href;
		else var link 	= element.href;
		window.location.href = link;
	});
	
	$$('#dragAndDrop .intro').each( function(item){
		new Draggable(item,{starteffect : dragger(item), endeffect : dragger(item)});
	});
	function dragger(item){
		   new Effect.Opacity(item, {from:1, to:1.0, duration:0});
	}
	
	$$('#dragAndDrop .themas').each( function(item){
		new Draggable(item,{starteffect : dragger(item), endeffect : dragger(item)});
	});
	
	$$('.btn').invoke('observe','click',function(event){
		var element = Event.element(event);
		var swapper = new swapBackground(element)
	});

	if($('navigation') != null){
		$$('#navigation a').invoke('observe','click', function(event){
			Event.stop(event);
		});
		$$('#navigation a').each( function(item){
			new Draggable(item,{starteffect : dragger(item), endeffect : dragger(item)});
		});
		$$('#navigation a').invoke('observe','dblclick',function(event){
			var element = Event.element(event);
			if(element.hasClassName('btn')) var link = element.up().href;
			else var link 	= element.href;
			window.location.href = link;
		});
	}

	if($('search') != null) $('search').observe('keyup', updateTags);
	if($('searchsite') != null) $('searchsite').observe('click', createSearch);		

	var i = 1;
	$$(".youtube").each(function(flv){
		var url = flv.innerHTML;
		flv.update("<div id='youtubeFlv"+ i +"'></div>");
		var swf = swfobject.embedSWF(url, "youtubeFlv"+ i +"", "590", "400", "8.0.0", false);
		i++;
	});

	$$("#dragAndDrop .close").invoke('observe','click',function(event){
		var element = Event.element(event);
		var wrapper = element.up();
		new Effect.Fade(wrapper,{duration:0.5});
		wrapper.addClassName("hidden");
		
	});
	
	$$("#dragAndDrop .language").invoke('observe','click',function(event){
		var element = Event.element(event);
		if(element.hasClassName("dk")){
			if(!$('dk_intro').hasClassName("hidden")){
				new Effect.Appear('dk_intro',{duration:0.5});
				$('dk_intro').addClassName("hidden");
			}else{
				new Effect.Fade('dk_intro',{duration:0.5});
				$('dk_intro').removeClassName("hidden");
			}
		}else{
			if(!$('en_intro').hasClassName("hidden")){
				new Effect.Appear('en_intro',{duration:0.5});
				$('en_intro').addClassName("hidden");
			}else{
				new Effect.Fade('en_intro',{duration:0.5});
				$('en_intro').removeClassName("hidden");
			}
		}
	});
	
	if($('userUpload') != null){
		initUserUpload();
	}
});

function initUserUpload(){
	
		// get variables
		

		var swfu = new SWFUpload({

			upload_url: base+"swfupload/upload_v2.php",
			flash_url : base+"swfupload/swfupload.swf",
			
			// File Upload Settings
			file_size_limit : "10MB",
			file_types : "*.jpg;*.gif;*.png",
			file_upload_limit : 2,

			file_queue_error_handler : fileQueueError,
			file_dialog_complete_handler : fileDialogComplete,
			upload_progress_handler : uploadProgress,
			upload_error_handler : uploadError,
			upload_success_handler : uploadSuccess,
			upload_complete_handler : uploadComplete,

			// Button Settings
			button_placeholder_id : 'userUpload',
			button_width: 150,
			button_height: 20,
			button_text : '<span class="button">Select images</span>',
			button_text_style : '.button { font-family: Helvetica, Arial, sans-serif; font-size: 12px;} .buttonSmall { font-size: 10pt; }',
			button_text_top_padding: 5,
			button_text_left_padding: 5,
			button_window_mode: SWFUpload.WINDOW_MODE.TRANSPARENT,
			button_cursor: SWFUpload.CURSOR.HAND,
			
			custom_settings : {
				upload_target : 'uploadProgress',
				thumb_container: 'uploadedImg'
			},
						
			// Debug Settings
			debug: false
		});
	
}

function createSearch(event){
	var element = Event.element(event);
}
function updateTags(event){
	var element = Event.element(event);

	var keycode = event.keyCode;
	if (keycode == 8 || keycode >= 48 && keycode <= 90) {
		var params = 'string='+element.value;
		new Ajax.Updater('usedTags',base+'pages/get_tags.php',{
			parameters: params,
			onComplete: function () {}
		});
	}
}


var active = $('bg1');
var swapBackground = Class.create({
	initialize: function(element) {

		if($('bg1') == null){
			$$("body").first().insert(
				new Element('div',{ id: 'bg1' ,style: 'display:none'})
		    );
		}
		if($('bg2') == null){
			$$("body").first().insert(
				new Element('div',{ id: 'bg2',style: 'display:none' })
		    );
		}
		this.element 		= element;
		this.swap_id		= this.element.id;
		this.bg1			= $('bg1');
		this.bg2			= $('bg2');
		this.setBgSize(); 
	},
	setBgSize: function(){
		var arrayPageSize = this.getPageSize();
		var img = 'images/bg'+this.swap_id+'.gif';
		
		if(!this.bg1.hasClassName('active')){
			this.bg1.setStyle({ height: arrayPageSize[1] + 'px', background:' url('+base+img+')'});
			new Effect.Appear(this.bg1,{duration:1.5});
			new Effect.Fade(this.bg2,{duration:1.5});
			this.bg1.addClassName('active')
		}else{
			this.bg2.setStyle({ height: arrayPageSize[1] + 'px', background:' url('+base+img+')'});
			new Effect.Appear(this.bg2,{duration:1.5});
			new Effect.Fade(this.bg1,{duration:1.5});
			this.bg1.removeClassName('active')
		}
	},
	getPageSize: function(){
		var xScroll, yScroll;
		
		if (window.innerHeight && window.scrollMaxY) {	
			xScroll = window.innerWidth + window.scrollMaxX;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}
		
		var windowWidth, windowHeight;
		
		if (self.innerHeight) {	// all except Explorer
			if(document.documentElement.clientWidth){
				windowWidth = document.documentElement.clientWidth; 
			} else {
				windowWidth = self.innerWidth;
			}
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}	
		
		// for small pages with total height less then height of the viewport
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		} else { 
			pageHeight = yScroll;
		}
	
		// for small pages with total width less then width of the viewport
		if(xScroll < windowWidth){	
			pageWidth = xScroll;		
		} else {
			pageWidth = windowWidth;
		}

		return [pageWidth,pageHeight];
		
	}
});


var overlay = Class.create({
	initialize: function(element) {
		$$("body").first().insert(
	      new Element('div',{ id: 'overlay' })
	    );
		$$("body").first().insert(
	      new Element('div',{ id: 'overlayBox' })
	    );
		this.element  	= element;
		this.delay		= 2000;
		this.overlay 	= $('overlay'); 
		this.overlayBox	= $('overlayBox'); 
		this.overlayBox.hide();
		this.overlay.hide();
		this.showOverlay();
	},
	showOverlay: function(){
		var arrayPageSize = this.getPageSize();
		this.overlay.setStyle({ height: arrayPageSize[1] + 'px' });
		this.overlayBox.setStyle({ top: '50px' });
		
		new Effect.Appear(this.overlay,{duration:0.5, from:0.0, to:0.8});
		this.overlay.observe('click',this.closeOverlay.bind(this))
	},
	showInner: function(){
		new Effect.Appear(this.overlayBox,{delay:0.6,duration:0.5});
	},
	updateOverlay: function(update){
		new Ajax.Updater(this.overlayBox,update,{
			onComplete: function () {
				$$('.radio').invoke('observe','click', checkRadio);
			}
		});
	},
	closeOverlay: function(){
		new Effect.Fade(this.overlayBox,{duration:0.2, from:1, to:0.0});
		new Effect.Fade(this.overlay,{delay:0.2, duration:0.5, from:0.8, to:0.0});
		
		timer = setTimeout( function(){
			$('overlayBox').remove();
			$('overlay').remove();
		}, 600);
	},
	getPageSize: function(){
		var xScroll, yScroll;
		
		if (window.innerHeight && window.scrollMaxY) {	
			xScroll = window.innerWidth + window.scrollMaxX;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}
		
		var windowWidth, windowHeight;
		
		if (self.innerHeight) {	// all except Explorer
			if(document.documentElement.clientWidth){
				windowWidth = document.documentElement.clientWidth; 
			} else {
				windowWidth = self.innerWidth;
			}
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}	
		
		// for small pages with total height less then height of the viewport
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		} else { 
			pageHeight = yScroll;
		}
	
		// for small pages with total width less then width of the viewport
		if(xScroll < windowWidth){	
			pageWidth = xScroll;		
		} else {
			pageWidth = windowWidth;
		}

		return [pageWidth,pageHeight];
		
	}
});

function commentBlog(){
	form_values = $('commentForm').serialize(true);
	var missed;

	var name 	= form_values['name'];
	var email 	= form_values['email'];
	var comment = form_values['comment'];
	
	var ul1 	= form_values['user_upload'];
	var ul2 	= form_values['user_upload2'];
	
	if($('user_upload') != null) var img1 = "<div class=\"userImg\"><img src=\""+ ul1 +"\"><div class=\"bottom\"><span></span></div></div>";
	else var img1 = "";
	if($('user_upload2') != null) var img2 = "<div class=\"userImg\"><img src=\""+ ul2 +"\"><div class=\"bottom\"><span></span></div></div>";
	else var img2 = "";
		
	var newComment = "<p>"+comment+"</p>"+img1+img2;
	form_values['comment'] = newComment;
	

	var ans 	= form_values['answer'];
	var x 		= parseFloat(form_values['x']);
	var y 		= parseFloat(form_values['y']);	
	var t 		= x + y;

	if(ans == ''){
		$('answer').addClassName('missed');
		missed = true;
	}else{
		$('answer').removeClassName('missed');
	}

	if(name == ''){
		$('name').addClassName('missed');
		missed = true;
	}else{
		$('name').removeClassName('missed');
	}
	
	if(email == ''){
		$('email').addClassName('missed');
		missed = true;
	}else{
		$('email').removeClassName('missed');
	}

	if(comment == ''){
		$('comment').addClassName('missed');
		missed = true; 
	}else{
		$('comment').removeClassName('missed');
	}



	if(ans != t){
		$('answer').addClassName('missed');
		$('msg').innerHTML= "&nbsp;! The answer of the sum was not correct.";
		$('answer').value ='';
		return false;	
	}else{
		$('answer').removeClassName('missed');
	}
	
	if(missed == true){
		$('msg').innerHTML= "! You didn't fill in all required fields";
		return false;
	}

	new Ajax.Updater('commentForm',base+'pages/send_comment.php',{
		parameters: form_values,
		onComplete: function () {
			Effect.Appear('data',{ duration: 1.0 });
		}
	});
	
}

function sendMessage(){
	form_values = $('messageForm').serialize(true);
	var missed;

	var name 	= $('name');
	var address	= $('address');
	var pc 		= $('pc');
	var city 	= $('city');
	var tel 	= $('tel');
	var email 	= $('email');
	var mes 	= $('mes');
	var ans 	= $('answer');
	
	var x 		= parseFloat(form_values['x']);
	var y 		= parseFloat(form_values['y']);	
	var t 		= x + y;

	var arr		= [name,email,mes,ans];
	var i 		= 0;	
	
	arr.each( function(f){
		if($F(f) == ''){ 
			$(f).addClassName('missed');
			missed = true; 
		}
		else $(f).removeClassName("missed");
	});
	
	if(missed == true){
		$('msg').innerHTML= "! You didn't fill in all required fields";
		return false;
	}
	
	if(ans.value != t){
		$('answer').addClassName('missed');
		$('msg').innerHTML= "&nbsp;! The answer of the sum was not correct.";
		$('answer').value ='';
		return false;	
	}else{
		$('answer').removeClassName('missed');
		$('msg').update("");
	}

	new Ajax.Updater('messageForm',base+'pages/send_message.php',{
		parameters: form_values,
		onComplete: function () {
			$('maincontainer').scrollTo();
			Effect.Appear('data',{ duration: 1.0 });
		}
	});
	
}

