$(function(){
	//table
	$('.table').find('tr').click(function(){
		var url = $(this).find('td').find('a').first().attr('href');
		//alert(url);
		if (url){
			window.open(url);
		}
		
		//location.href = url;
	});
	//nav
	$('.nav_compare').mouseover(function(){
		$(this).next().show();
	});
	$('.nav_compare').mouseout(function(){
		$(this).next().hide();
	});
	$('.nav_ul').mouseover(function(){
		$(this).show();
	});
	$('.nav_ul').mouseout(function(){
		$(this).hide();
	});
	/********/
	$('#cover_alert_close').click(function(){
		$('#cover_alert').hide();
		$('.cover').hide();
		window.location.reload();
	})
	//$('#email').blur(function(){
	//	$('#email_alert').hide();
	//});
	$('#email_submit').click(function(){
		var email = $('#email');
		if(vaild()){
			$('.cover').show();
			$('#cover-waiting').show();
			$.get('/ajax/email/add',{'email':email.val()},function(data){
				var message = $('#cover_alert_content');
				if (data){
					message.html(data); 
				} else {
					message.html("Try again.");
				}
				$('#cover-waiting').hide();
				$('#cover_alert').show();
			});
		}
		return false;
		function vaild(){
			var alert = $('#email_alert');
			alert.hide();
			if (email.val() == '') {
				email.focus();
				alert.html('Dieses Feld ausfüllen');
				alert.show();
				return false;
			}else if (!isEmail(email.val())){
				email.focus();
				alert.html("Bitte gültige Emailadresse eingeben.");
				alert.show();
				return false;
			}
			else{
				return true;
			}
		}
	});
	/************comment***********/
$('#comment_box').jqTransform({imgPath:'jqtransformplugin/img/'});
	$("#contrl_comment_box").fancybox({
			'overlayShow'	: true,
			'transitionIn'	: 'elastic',
			'transitionOut'	: 'elastic'
	});
	$('#contrl_comments').click(function(){
		$('#comments_div').toggle();
		return false;
	});
	$('#comment_submit').click(function(){
		var name = $('#comment_name');
		var email = $('#comment_email');
		var content = $('#comment_content');
		var nid = $('#comment_nid');
		if(vaild()){
			$('.cover').show();
			$('#cover-waiting').show();
			$.get('/ajax/comment/add',{'email':email.val(),'name':name.val(),'content':content.val(),'nid':nid.val()},function(data){
				var message = $('#cover_alert_content');
				if (data === 'OK'){
					message.html('Dein Kommentar wurde erfolgreich abgeschickt.'); 
				} else {
					message.html("Désolé: votre commentaire n'a pas été posté.");
				}
				$('#cover-waiting').hide();
				$('#cover_alert').show();
			});
		}
		return false;
		function vaild(){
			var alert = $('#comment_alert');
			alert.hide();
			if (name.val() == ''){
				name.focus();
				alert.html('Dieses Feld ausfüllen');
				alert.show();
				return false;
			}else if (content.val() == ''){
				content.focus();
				alert.html('Dieses Feld ausfüllen');
				alert.show();
				return false;
			}else if (email.val() == '') {
				email.focus();
				alert.html('Dieses Feld ausfüllen');
				alert.show();
				return false;
			}else if (!isEmail(email.val())){
				email.focus();
				alert.html("Bitte gültige Emailadresse eingeben.");
				alert.show();
				return false;
			}
			else{
				return true;
			}
		}
	});
	/*****************/

});

function isEmail(str){
		   var reg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,3}){1,2})$/;
		   return reg.test(str);
	}
