// catch if console doesn't exist (IE)
function log(value) {
	if(typeof(console) != "undefined") {
		console.log(value);
	}
}

function validate_email(email) {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   var address = email;
   if(reg.test(address) == false) {
      return false;
   }
	else {
		return true;
	}
}

jQuery(document).ready(function($) {
	
	$(".singin_layer_open").click(function () {
		$("#signin_layer").fadeIn("medium");
		$("#signin_layer_email").focus();
	});

	 $("#signin_layer_close").click(function () {
		$("#signin_layer").fadeOut("medium");
	 });

	$('.live_sm_bg').click(function() {


	});
	
	// Widget overlay
	
	if($.cookie('overlay') == "hide") {
	// do nothing
	}
	else {
		$.cookie('overlay', "hide", {expires:99});
		$('#widget-overlay').show();
	}
	
	
	$('#close').click(function() {
		$.cookie('overlay', "hide", {expires:99});
		$('#widget-overlay').hide();
		$('#container').hide();
		// return false;
	});
	
	$('#download').click(function() {
		$.cookie('overlay', "hide", {expires:99});
		$('#widget-overlay').hide();
		$('#container').hide();
	});
	// Ends Widget overlay
	
	
	$('#button_signin').click(function () {
		$('#global_signon_form').submit();
		return false;
	});
	
	$('#button_signin2').click(function () {
		$('#signon_form').submit();
		return false;
	});
	
	$('#butt_postcomment').click(function () {
		$('#commentform').submit();
		return false;
	});

	$('#button_mailinglist').click(function () {

		$('#maillinglist_form .txt_error').hide();

		errors = false;

		if(!(validate_email($('#mailinglist_email').val()))) {
			$('#mailinglist_email_error').show();
			errors = true;
		}

		if(!($('#mailinglist_country').val())) {
			$('#mailinglist_country_error').show();
			errors = true;
		}
		
		if($('#mailinglist_privacy:checked').length == 0) {
			$('#mailinglist_privacy_error').show();
			errors = true;
		}		


		if(errors == false) {
			$('#maillinglist_form').submit();
		}

		return false;
	});
	
	$('#form_register #button_register').click(function() {

		$('#form_register .txt_error').hide();

		errors = false;

		if(!(validate_email($('#register_email').val()))) {
			$('#register_email_empty_error').show();
			errors = true;
		}

		if(!($('#register_country').val())) {
			$('#register_country_empty_error').show();
			errors = true;
		}

		if(!$('#register_name').val()) {
			$('#register_name_empty_error').show();
			errors = true;
		}

		if($('#register_password').val().length < 5) {
			$('#register_password_empty_error').show();
			errors = true;
		}

		if($('#register_password2').val() != $('#register_password').val()) {
			$('#register_password2_error').show();
			errors = true;
		}

		if($('#register_privacy:checked').length == 0) {
			$('#register_privacy_empty_error').show();
			errors = true;
		}

		if(errors == false) {
			$('#form_register').submit();
		}
		
		return false;
	});
	
	$('#form_update #button_update').click(function() {

		$('#form_update .txt_error').hide();

		errors = false;

		if(!(validate_email($('#register_email').val()))) {
			$('#register_email_empty_error').show();
			errors = true;
		}

		if(!$('#register_name').val()) {
			$('#register_name_empty_error').show();
			errors = true;
		}

		if($('#register_password').val().length < 5 && $('#register_password').val().length > 0) {
			$('#register_password_empty_error').show();
			errors = true;
		}

		if($('#register_password2').val() != $('#register_password').val()) {
			$('#register_password2_error').show();
			errors = true;
		}

		if(errors == false) {
			$('#form_update').submit();
		}
		
		return false;
	});	
	
	
	$('.widget_tour-widget li.vevent').click(function() {
		
		if($(this).find('.ticketUrl').length > 0) {
			window.open($(this).find('.ticketUrl').attr('href'));
		}
		else {
			window.url = '/tour';
		}
		
		return false;
	});
	
	$('.live_container').click(function() {
		
		if($(this).find('.buyticket').length > 0) {
			window.open($(this).find('.buyticket').attr('href'));
		}
		
		return false;
	});
	
});
