(function($) {
	$.fn.horizontalAccordion = function(option) {
		var config = {
			min				: 0,			// Minimum size of each element. All other elements other than the currently hovered element get this size
			max				: 0,			// Middle size of each element. All the elements get this size when no element is hovered upon
			duration			: "normal",	// Speed with which the animation should be applied while moving the focus for the element
			easing			: null,		// The easing effect to be applied to the animation that moves the element's focus  
			callback			: null
		}
		config = $.extend(config, option);
		return this.each(function() {
			var el = $(this);
			$(this).addClass("ha")
			$(this).find("a").hoverIntent(
				function(e){
					$(el).find("a").not(this).stop().animate({width: config.min+"px"}, { queue:false, duration:config.duration, easing:config.easing, callback:config.callback });
					$(this).stop().animate({width: config.max+"px"}, { queue:false, duration:config.duration, easing:config.easing, callback:config.callback});
				},
				function() {}
			);
		});
	};
})(jQuery);

jQuery.extend( jQuery.easing,
{
	easeOutExpo: function (x, t, b, c, d) {
		return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
	}
});

function initAccordion(sID) {
	$(sID+' .info').hide();
	$(sID+' .info:first').show();
	$(sID+' .information:first h3 a').addClass("active");
	$(sID+' .information h3 a').hoverIntent(
		function() {
			var checkElement = $($(this).attr("href"));
			if(checkElement.is(':visible')) {
				return false;
			} else {
				$(sID+' .active').removeClass("active");
				$(this).addClass("active");
				$(sID+' .info:visible').slideUp('normal');
				checkElement.slideDown('normal');
				return false;
			}

		}, 
		function() {}
	);
	$(sID+' .information h3 a').click( function() {
			var checkElement = $($(this).attr("href"));
			if(checkElement.is(':visible')) {
				return false;
			} else {
				$(sID+' .active').removeClass("active");
				$(this).addClass("active");
				$(sID+' .info:visible').slideUp('normal');
				checkElement.slideDown('normal');
				return false;
			}

		});
}

$(document).ready(function() {

/* 
	Accordion Horizontal 
*/
	$(".accordion").horizontalAccordion({min:200, max:500, easing:"easeOutExpo"});

/* 
	Accordion Vertical 
*/
	var minHeight = 150;
	$(".information").each( function() {
		if ($(this).height() > minHeight) { minHeight = $(this).height(); }
	});
	$(".informations").css("min-height", minHeight+75+"px");

/* 
	Menu
*/
	$("#topMenu ul:first > li").hoverIntent(function() {
		$("ul:first", this).show();
	},
	function() {
		$("ul:first", this).hide();
	});
	
	$("#topMenu ul:first li li").hoverIntent(function() {
		$("ul:first", this).each(function() {
			$(this).css("top", $(this).parent().position().top );
			$(this).css("left", $(this).parent().position().left + $(this).parent().width() );
			$(this).show();
		});
	},
	function() {
		$("ul:first", this).hide();
	});
});

//Checks is an email is a valid email
function IsEmail(obj)
{
    var str= obj.value;
    if(str != '')
    {
		var filter= /^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
		var testResult = filter.test(str);
		if(!testResult)
		{
		  obj.focus();
		  alert('Email er ikke en gyldig email');
		  return false;
		}
		else
		{
			return true;
		}  
	}
	return false;
}

//Checks if all obligatory information has been filled in
function validateInput(id) 
{
    var oForm = document.getElementById(id);
    var error = '';
    var koeb = '';
    for( i = 0; i < document.shoppingCartEdit.Koeb.length; i++ )
	{
	if( document.shoppingCartEdit.Koeb[i].checked == true )
	koeb = document.shoppingCartEdit.Koeb[i].value;
	}

    var firma = document.getElementById('Firma');
    var addresse = document.getElementById('Addresse');
    var postnrBy = document.getElementById('PostnrBy');
	var telefon = document.getElementById('Telefon');
	var email = document.getElementById('Email');
	
	if(koeb == ''){error+='\n- Vælg venligst leveringstype (enkelt/løbende/prøveperiode)';}
	if(firma.value == ''){error+='\n- Firma skal udfyldes';}
	if(addresse.value == '') {error+='\n- Adresse skal udfyldes';}
	if(postnrBy.value == '') {error+='\n- Post nr. / By skal udfyldes';}
	if(telefon.value == '') {error+='\n- Telefon skal udfyldes';}
	if(!IsEmail(email)) {error+='\n- Email skal udfyldes og være gyldig';}

    if(error=='')
	{ 
	    oForm.submit();
	    return true;
    }
    else
    {
        alert("Ret venligst følgende fejl:" + error);
        return false;
    }
}

//Checks if the value is integer and more that 0
function IsAmount(obj)
{
    var str= obj.value;
    if(str != '' && str != '0')
    {
		var filter= /^[0-9]{1,100}$/i;
		var testResult = filter.test(str);
		if(!testResult)
		{
		  obj.focus();
		  return false;
		}
		else
		{
			return true;
		}  
	}
	return false;
}

function AddProduct(id) 
{
    var oForm = document.getElementById(id);

    var error = '';
    var product = document.getElementById('Product');
	var amount = document.getElementById('Amount');
	
	if(product.value == ''){error+='\n- Variant skal være valgt';}
	if(!IsAmount(amount)) {error+='\n- Antal skal være et tal over 0';}

    if(error=='')
	{ 
	    oForm.submit();
	    window.location = '/bestilling';
	    return true;
    }
    else
    {
        alert("Ret venligst følgende fejl:" + error);
        return false;
    }
}

