
var i = 0;
function addEmail(){
	email = document.getElementById("addemail");
	if (checkEmail(email.value)){
		wrap = document.getElementById("emailwrap");
		wrap.innerHTML = wrap.innerHTML + ('<div id="email' + i + '"><input id="emailinput' + i + '" class="removeemail" type="hidden" name="emails[]"  value="' + email.value + '"><p><span>' + email.value + '</span><a class="removeemail" href="javascript:clearOneEmail(\'' + i + '\')"><span>x</span></a></p></div>');
		i++;
	}
}

function clearAllEmails(){
	wrap = document.getElementById("emailwrap");
	wrap.innerHTML = '';
}

function clearOneEmail(id){
	wrap = document.getElementById("emailwrap");
	email = document.getElementById('email' + id);
	wrap.removeChild(email);
}

function checkEmail(email) {
	error = document.getElementById("emailerror");
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(email)){

		emails = new Array();
		for (j=i;j>=0;j--){
			if (tmp = document.getElementById('emailinput' + j )){
				error.innerHTML = email + tmp.value;
				if (email == tmp.value){
					error.innerHTML = 'Emaildressen er allerede tilf\u00F8jet.';
					return (false)
				}
			}
		}
		error.innerHTML = '';
		return (true)
	}

	error.innerHTML = 'Fejl i email';
	return (false)
}

function showedit(id){
	elem = document.getElementById(id);
 	if (elem.style.display=='block')
 		elem.style.display = 'none';
 	else
 		elem.style.display = 'block';
}

function submitdelete(id, formid){
	divwrap = document.getElementById('mail'+id);
        divwrap.style.display = 'none';
	elem = document.getElementById('control'+id);
 	elem.name = 'DeleteSubUser';
 	elem.value = 'Delete';
	myform = document.getElementById(formid);
 	myform.submit();
}

function submitedit(id){
	divwrap = document.getElementById('mail'+id);
	divwrap.style.display = 'none';
	elem = document.getElementById('control'+id);
 	elem.name = 'EditPassword';
 	elem.value = 'Gem';
}
function in_array( what, where ){
	for(var i=0;i<where.length;i++){
	  if(what == where[i]){
	    return true;
        break;
	  }
	}
	return false;
}
	
$.fn.clearOnFocus = function(){
    return this.focus(function(){
        var v = $(this).val();
        $(this).val( v === this.defaultValue ? '' : v );
    }).blur(function(){
        var v = $(this).val();
        $(this).val( v.match(/^\s+$|^$/) ? this.defaultValue : v );
    }); 
};

jQuery.fn.ajaxSubmit = function(current_div) {
		// Change a form's submission type to ajax 
		this.submit(function(){
			var params = {};
	    jQuery(this)
	    .find("input, :selected, textarea")
	    .each(function() {
	      	params[ this.name || this.id || this.parentNode.name || this.parentNode.id ] = this.value;
	    });
	    jQuery(this)
	    .find("input:checkbox")
	    .each(function() {
	      	params[ this.name || this.id || this.parentNode.name || this.parentNode.id ] = this.checked ? this.value : 0 ;
	    });
	    jQuery(current_div).html("");
	    jQuery("#fancy_loading").show();
	 	
			

	   	jQuery.post(this.getAttribute("action"), params, function(responseData){
			strError = "Unable to submit form. Please try again later.";
			jQuery(current_div).html(responseData).show('fast');
			jQuery("#fancy_loading").hide();
		}, "html");
		return false;
	});
	return this;
}

jQuery.fn.delay = function( time, name ) {

    return this.queue( ( name || "fx" ), function() {
        var self = this;
        setTimeout(function() { jQuery.dequeue(self); } , time );
    } );

};


jQuery(document).ready(function(){
	FLIR.init( { path: '/facelift/' } );
	
	$(".flir").each( function() { FLIR.replace(this, new FLIRStyle({ cFont:'sarireg', mode:'wrap', realFontHeight: 'true'})); this.style.display = 'inline-block'; } );
	$(".flir-toplink").each( function() { FLIR.replace(this, new FLIRStyle({ cFont:'sarimed', mode:'nowrap', realFontHeight: 'true'})); this.style.display = 'block'; } );
	$(".flir-medium").each( function() { FLIR.replace(this, new FLIRStyle({ cFont:'sarimed', mode:'wrap', realFontHeight: 'true'})); this.style.display = 'block'; } );
	$(".flir-orange").each( function() { FLIR.replace(this, new FLIRStyle({ cColor: 'f57a28',cFont:'sarimed', mode:'wrap', realFontHeight: 'true'})); this.style.color = '#f57a28';}); 
	$(".flir-white-right-big").each( function() { FLIR.replace(this, new FLIRStyle({ cLine: '2.0',cColor: 'ffffff',cFont:'sarireg', mode:'wrap', realFontHeight: 'true'})); this.style.color = '#ffffff';}); 
	$(".flir-white-right").each( function() { FLIR.replace(this, new FLIRStyle({ cLine: '1.3', cColor: 'ffffff',cFont:'sarireg', realFontHeight: 'true'})); this.style.color = '#ffffff';}); 
	$(".flir-white").each( function() { FLIR.replace(this, new FLIRStyle({ cColor: 'ffffff',cFont:'sarimed', mode:'wrap', realFontHeight: 'true'})); this.style.color = '#ffffff';}); 
	$("h1.flir-onwhite").each( function() { FLIR.replace(this, new FLIRStyle({ cColor: '#303337', cFont:'default', mode:'wrap', realFontHeight: 'true'})); this.style.color = '#303337';} );
	$("h2.flir-onwhite").each( function() { FLIR.replace(this, new FLIRStyle({ cColor: '#303337', cFont:'default', mode:'wrap', realFontHeight: 'true'})); this.style.color = '#4e608a';} )
	$("h3.flir-onwhite").each( function() { FLIR.replace(this, new FLIRStyle({ cColor: '#303337', cFont:'default', mode:'wrap', realFontHeight: 'true'})); this.style.color = '#303337';} );
	
	jQuery('#login-activate').click(function(){
		window.scroll(0,0);

		if( jQuery('#login-inner').hasClass("unfold") ){
			jQuery('#login-inner').animate( { height:"0px" });
			jQuery('#login-inner').removeClass("unfold");
		} else {
			jQuery('#login-inner').animate( { height:"140px" });
			jQuery('#login-inner').addClass("unfold");
		}
	});
	
	jQuery('#tablogin').clearOnFocus();
	/*
	jQuery('#tabpassword').focus(function(){

	 	var newInput = $('<input />')
		.attr( 'name', 'Password' )
		.attr( 'type', 'password' )
		.attr( 'id', 'tabpassword' )
		$(this).replaceWith( newInput );
		jQuery('#tabpassword').select();	
	 });
	*/
	if($('.tip')[0]){
		 $('.tip').each(function()
		 {	
		 	$(this).addClass('relative');
		 	$(this).prepend('<div class="tip-inner" ><span>?</span></div>');
	
	   		$(this).children('.tip-inner').qtip({
				content: {
				      url: '/layout/set/ajax/qtipfused/show/',
				      data: { id: $(this).attr('id') },
				      method: 'post'
				 },
		
		   		show: 'mouseover',
		   		hide: 'mouseout',
		   		style: { 
			      width: 250,
			      padding: 10,
			      
			      color: 'black',
			      textAlign: 'left',
			      border: {
			         width: 1,
			         radius: 2
		
			      },
			      tip: 'leftTop',
			      name: 'green'
		   		}
			})
		});
	}
	
	$('.class-faq .question').click(function(){
		$(this).parent().children('.attribute-message').toggle();
	});
	
	var timeout    = 1000;
	var closetimer = 0;
	var ddmenuitem = 0;

	function ddmenu_open(){
		ddmenu_canceltimer();
		   ddmenu_close();
		   ddmenuitem = $(this).find('ul').css('visibility', 'visible');
	}

	function ddmenu_close(){
		if(ddmenuitem) ddmenuitem.css('visibility', 'hidden');
	}

	function ddmenu_timer(){
		closetimer = window.setTimeout(ddmenu_close, timeout);
	}

	function ddmenu_canceltimer(){
		if(closetimer){
			window.clearTimeout(closetimer);
		        closetimer = null;
	}}

	$(document).ready(function(){
		$('#ddmenu > li').bind('mouseover', ddmenu_open)
		$('#ddmenu > li').bind('mouseout',  ddmenu_timer)
	});

	document.onclick = ddmenu_close;
	
	$('.expanding-box .title').each(function()
 	{
 		$(this).click(function(){
 			if($(this).hasClass('selected')){
 				$(this).removeClass('selected');
 				$(this).parent().children('.content-body').toggle('fast');
 			} else {
 				$('.expanding-box .selected').each(function(){
 					$(this).removeClass('selected');
	 				$(this).parent().children('.content-body').hide('fast');
 				});
 				$(this).addClass('selected');
 				$(this).parent().children('.content-body').toggle('fast');
 			}
 		});
 	});

});
