// JavaScript Document

//on dom ready...
window.addEvent('domready', function() {
									 overLogin=0;
	if($('captcha_replace')){
		$('captcha_replace').addEvent('click', function(event) {
			//prevent the page from changing
			event.stop();
			//make the ajax call, replace text
			var dato = new Date();
			$('captcha_box').set('src', '/site/bg/comment/include/captcha.php?'+dato.getTime());
		});
	}
	var togFlash = function(state) {
    var hideobj=new Array("embed", "iframe", "object");
    for (y = 0; y < hideobj.length; y++) {
     var objs = document.getElementsByTagName(hideobj[y]);
     for(i = 0; i < objs.length; i++) {
      objs[i].style.visibility = state;
     }
    }
	}
	if ($('loginAjax')) {
		$('loginKnap').addEvent('click',function(e) {
												
												 e.stop();
												 var sizes2 = window.getSize();
												var scrollto2 = window.getScroll();
												$('loginAjax').addEvents({
																		 'mouseover': function() { overLogin=1; },
																		 'mouseout':function() {  overLogin=0}
																		 });
												//togFlash('hidden');
												 $('loginAjax').setStyles({
																		 'position':'absolute',
																		'left': (scrollto2.x + (sizes2.x - 400) / 2).toInt(),
																		'top': (e.client.y+scrollto2.y).toInt(),
																		'display':'block',
																		'opacity':0
																		  });
												 $('loginAjax').fade('in');
												 });
		$('loginAjax').addEvent('submit',function(e) {
												  e.stop();
												  form = this;
												  form.set('ohtml',this.get('html'));
												form.set('send', {
															onRequest: function(){
																form.set('html','<p style="text-align:center;font-size:13px;font-weight:bold;">Logger ind vent venligst...<br><img src="/site/formatering/ajax-loader.gif"></p>');
															},
															onComplete: function(response){	
																if (response!='') {
																	alert('Forkert brugernavn eller password');	
																	form.set('html',form.get('ohtml'));
																} else {
																	new Fx.Tween(form).start('opacity',0).chain(function() {
																												  form.setStyle('display','none');;
																												  window.location.reload();
																												  });
																}
															}
														 });
													form.send();
												  });
	}
	if ($('stem_nu') || $('loginAjax')) {
		$(document.body).addEvent('click',function(e) {
												 if ($('afgiv_stemme')) {
													new Fx.Tween('afgiv_stemme').start('opacity',0).chain(function() {
																												  $('afgiv_stemme').destroy();
																												  });
												 }
												 if ($('loginAjax') && !overLogin) {
													
																												  new Fx.Tween('loginAjax').start('opacity',0).chain(function() {
																												  	$('loginAjax').setStyle('display','none');;
																												  	togFlash('visible');
																												 
																												  });
												 }												 
												   });
	}
	if ($('stem_nu')) {
		$('stem_nu').addEvent('click',function(e) {
											   	e.stop();
var req = new Request({
																		   method: 'get',
																		   url: this.get('href')+'&stop=true',
																		   onComplete: function(response) {
																				var sizes2 = window.getSize();
																				var scrollto2 = window.getScroll();	
																				if ($('afgiv_stemme')) {
																					$('afgiv_stemme').destroy();	
																				}
																				var scrollto = window.getScroll();
																				var div = new Element('div', {
																					'id':'afgiv_stemme',
																					'html': '<p style="margin:0;color:#FFF;">Afgiv din stemme</p>'+response,
																					'styles': {
																						'padding':'10px',
																						'textAlign':'center',
																						'display': 'block',
																						'border': '1px solid black',
																						'left': e.client.x.toInt(),
																						'top': (e.client.y+scrollto.y).toInt(),
																						'position':'absolute',
																						'border':'1px solid #000',
																						'backgroundColor':'#718fa8',
																						'opacity':0
																					}
																					
																					
																				});
																				$(document.body).adopt(div);
																				$('afgiv_stemme').fade('in');
																				$$('.star_hover').addEvents({
																										   'mouseover':function(e) {
																												var num = this.get('id').replace('h','');
																												var p = this.getParent().getParent();
																												var i=1;
																												$each(p.getChildren(),function(elm) {
																																			 var img = elm.getFirst();
																																			 if (i<=num) {
																																				 img.set('src',img.get('src').replace('star_grey.png','star.png')); 
																																			 } else {
																																				img.set('src',img.get('src').replace('star.png','star_grey.png')); 
																																			 }
																																			 
																																			 i++;
																																			   });
																												
																										   }
																										   });
																				init();
																			  
																		   }
																		   
																		   }).send();												
											   });
	}
	
	
	$$('.popup').each(function(item){
		item.addEvent('click', function(event){
			event.stop();
			var url = item.get('href');
			window.open(url, 'Lęs om' + item.get('rel') + '', 'scrollbars=1,width=350,height=450');
		});
	});
	
	if($('save_comment_form')){
		$('noticeArea').fade('out');
		$('save_comment_form').addEvent('submit',function(e) {
		e.stop();
		this.set('send', {
			onRequest: function(){
				$('save_comment_btn').disabled = 1;
				$('save_comment_btn').set('src', '/site/bg/comment/images/save_wait.gif');
			},
			onComplete: function(response){
				$('save_comment_btn').set('src', '/site/bg/comment/images/save_comment.png');
				$('save_comment_btn').disabled = 0;
				if(response.contains('for din kommentar', ' ')){
					alert(response);
					$('save_comment_form').reset();
					if($('user_name')){
						$('user_name').setStyle('background-color', '#FFFFFF');
					}
					if($('user_id')){
						$('user_id').setStyle('background-color', '#FFFFFF');
					}
					if($('user_comment')){
						$('user_comment').setStyle('background-color', '#FFFFFF');
					}
					if($('captcha_text')){
						$('captcha_text').setStyle('background-color', '#FFFFFF');
					}
				} else {
					(function(){
						if($('user_name')){
							if($('user_name').get('value') == ''){
								$('user_name').setStyle('background-color', '#FFFF95');
							} else {
								$('user_name').setStyle('background-color', '#FFFFFF');
							}
						}
						if($('user_id')){							
							if($('user_id').get('value') == '' || response.contains('er ikke gyldig')){
								$('user_id').setStyle('background-color', '#FFFF95');
							} else {
								$('user_id').setStyle('background-color', '#FFFFFF');
							}
							
						}
						if($('user_comment').get('value') == ''){
							$('user_comment').setStyle('background-color', '#FFFF95');
						} else {
							$('user_comment').setStyle('background-color', '#FFFFFF');
						}
						if($('captcha_text')){
							if($('captcha_text').get('value') == '' || response.contains('forhold til koden')){
								$('captcha_text').setStyle('background-color', '#FFFF95');
							} else {
								$('captcha_text').setStyle('background-color', '#FFFFFF');
							}
							$('captcha_text').set('value', '');
						}
					}).delay(500);
					alert(response);
				}
				var req = new Request({
					method: 'post',
					url: '?action=refreshPosts',
					data: { 'prod_id' : $('formIdentProd').get('value'), 'site_id' : $('formIdentSite').get('value'), 'tab' : $('formIdentTab').get('value') },
					onComplete: function(response){
						$('changeThis').set('html', response);
						if($('captcha_box')){
							var dato = new Date();
							$('captcha_box').set('src', '/site/bg/comment/include/captcha.php?'+dato.getTime());
						}
					}
				}).send();
			}
		});
		this.send();
		});
	}
	
	if($('captcha_box')){
		$('captcha_box').addEvent('click', function(){
			var dato = new Date();
			$('captcha_box').set('src', '/site/bg/comment/include/captcha.php?'+dato.getTime());
		});
	}
	
	var init = function() {
		$$('.save_poll').removeEvents();
		$$('.save_poll').each(function(item){
				item.addEvent('click', function(event) {
					event.stop();
					var req = new Request.HTML({
						method: 'get',
						url: item.get('href'),
						update: $('stars'),
						onComplete: function(response){
							if ($('afgiv_stemme')) {
										new Fx.Tween('afgiv_stemme').start('opacity',0).chain(function() {
																												  $('afgiv_stemme').destroy();
																												  });
							}
							alert('Tak for din stemme!');
						}
					}).send();
				});
			});
			$$('.changePage').removeEvents();
		$$('.changePage').addEvent('click', function(event) {
			//prevent the page from changing
			event.stop();
			//make the ajax call, replace text
			var req = new Request.HTML({
				method: 'get',
				url: this.get('href'),
				data: { 'do' : '1' },
				//onRequest: function() { alert('Request made. Please wait...'); },
				update: $('changeThis'),
				onComplete: function() { init(); }
			}).send();
		});
	}
	init();
});