/* Animated dropdown menu */
$(function(){
	$('#menu > li').hover(
		//$('.submenu', this).stop(false,true).fadeToggle(190);
		//$('.submenu > li:not(.cm-menu-pointer)', this).stop(false,true).slideToggle(250);
		function () {
			$('.submenu', this).stop(true,true).fadeIn(190);
			$('.submenu > li:not(.cm-menu-pointer)', this).stop(true,true).slideDown(250);
			//console.log('in');
		},
		function () {
			$('.submenu > li:not(.cm-menu-pointer)', this).stop(true,true).slideUp(250);
			$('.submenu', this).stop(true,true).fadeOut(190);
			//console.log('out');
		}
	)
});
	
/* Fading headers 
$(document).ready(function(){
	$('.h1-header').animate({opacity: 1.00}, 600, function() {});
});*/

/* Animated Anchor links */
$(document).ready(function(){
	$('a[href*=#]').click(function() {
	  if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')
	  && location.hostname == this.hostname) {
		 var $target = $(this.hash);
		 $target = $target.length && $target
		 || $('[name=' + this.hash.slice(1) +']');
		 if ($target.length) {
			var targetOffset = $target.offset().top;
			$('html,body')
			.animate({scrollTop: targetOffset}, 750);
		  return false;
		 }
	  }
	});
 });

/* Placeholder tekst */
if (!Modernizr.input.placeholder) {
	var active = document.activeElement;
	// Add the text and class on load 
	$('input:not(.invalid,:empty)').val(function() {
	  return $(this).attr('placeholder');
	}).addClass('hasPlaceholder');
			
	$('input:not(.invalid)').focus(function () {
		if ($(this).attr('placeholder') !== '' && $(this).val() === $(this).attr('placeholder')) {
			$(this).val('').removeClass('hasPlaceholder');
		}
	}).blur(function () {
		if ($(this).attr('placeholder') !== '' && ($(this).val() === '' || $(this).val() === $(this).attr('placeholder'))) {
			$(this).val($(this).attr('placeholder')).addClass('hasPlaceholder');
		} else {
			$(this).removeClass('hasPlaceholder');
		}
	});
	$(':text').blur();
	$(active).focus();
	$('form').submit(function () {
		$(this).find('.hasPlaceholder').each(function() { $(this).val(''); });
	});
}

$(".cm-login-tab").focus(); // Focus Login button
// If we have a msg(error)
if ($('.header-top-extend').length !== 0) {
	$("#usern").focus();
}

$('.cm-login-tab').click(function(event) {
	event.preventDefault();
	$('#header-top').removeAttr("style");
  	$('#header-top').toggleClass('header-top-extend', 400);
	$("#usern").focus();
	$('.cm-login-msg').remove();
});

function showLoginForgotten () {
	$('#loginEmail').val('');
	$('#loginForgottenOK').hide();
	$('#loginForgottenErr').hide();
	$('#loginForgottenEnterEmail').show();
	$('#loginForgotten').show();
	$('#loginForm').hide();
	$('#loginFeedback').remove();
	$('#header-top').removeAttr('style');
	$('#loginEmail').focus();
	return false;
}

function hideLoginForgotten () {
	$('#loginForgotten').hide();
	$('#loginSent').hide();
	$('#loginForm').show();
	return false;
}
function sendLoginLink () {
	var emailAddr = $('#loginEmail').val();
	$.ajax({
		type: "GET",
		url: "/conferencemanager.dk/sendloginlink.cfm",
		data: "emailAddr="+emailAddr,
		success: function(status){
		//console.log('Got it!');
		  if (status == "ok") {
				//$('#loginForgottenOK').show();
				$('#loginForgottenEnterEmail').hide();
				$('#loginForgottenErr').hide();
				$('#header-top').css("height","62px");
				if (!$('#loginFeedback').length) {
					$('.login-wrap').prepend('<div id="loginFeedback" class="cm-login-msg"></div>');
				}
				$('#loginFeedback').html('Der er sendt en e-mail med et link til &aelig;ndring af password til '+emailAddr+'.');
				$('#loginForm').show();
				//console.log('OK');
			}
			else if (status == "invalid email") {
				$('#header-top').css("height","62px");
				if (!$('#loginFeedback').length) {
					$('.login-wrap').prepend('<div id="loginFeedback" class="cm-login-msg"></div>');
				}
				$('#loginFeedback').html('Indtast venligst en gyldig e-mail-adresse.');
				//console.log('Email failed')
			}
			else if (status == "not found") {
				$('#header-top').css("height","62px");
				if (!$('#loginFeedback').length) {
					$('.login-wrap').prepend('<div id="loginFeedback" class="cm-login-msg"></div>');
				}
				$('#loginFeedback').html('Den angivne e-mail-adresse findes ikke i systemet.');
			}
			else {
				$('#header-top').css("height","62px");
				if (!$('#loginFeedback').length) {
					$('.login-wrap').prepend('<div id="loginFeedback" class="cm-login-msg"></div>');
				}
				$('#loginFeedback').html('Ukendt fejl, kontakt conferencemanager.');
			}
		}
	 });
}
function getColElements (n) {
switch(n)
	{
		case 4:
			return $('.cm-version-col1, .cm-version-col2, .cm-version-col3');
		break;
		case 3:
			return $('.cm-version-col1, .cm-version-col2, .cm-version-col4');
		break;
		case 2:
			return $('.cm-version-col1, .cm-version-col4, .cm-version-col3');
		break;
		case 1:
			return $('.cm-version-col4, .cm-version-col2, .cm-version-col3');
		break;
	}
};
//<!---:nth-of-type(1),.cm-version dl > dd:nth-of-type(2),.cm-version dl > dd:nth-of-type(3),.cm-version dl > dd:nth-of-type(4)--->
var animationLength = 350;

if($.browser.msie && $.browser.version == "7.0"){
	animationFade = 0.5;
}else{
	animationFade = 1;
}

$('.cm-version dl > dd').hover(
	function (){
		index = $(this).index();
		//console.log(index);
		var colToAnimate = getColElements(index);
		//$('.cm-version-col' + index).stop().fadeTo(animationLength,0);
		$(colToAnimate).stop().fadeTo(animationLength,animationFade);
		$('.cm-version-tip-col' + index).stop().fadeTo(animationLength,1, function() {});
		//console.log('in: %o', colToAnimate);
	},
	function () {
		index = $(this).index();
		var colToAnimate = getColElements(index);
		$(colToAnimate).stop().fadeTo(animationLength,0);
		//console.log('Out:', colToAnimate);
		$('.cm-version-tip-col' + index).stop().fadeTo(animationLength,0);
	}
);
//<!--- IE FIX --->
$('.cm-version-overlay').hover(function (){
	$(this).hide();
});
