function isEmpty( s )
{
	var re = new RegExp("[^\\s]|[ ]")
	return !re.test( s )
}

function isEmail( s )
{
	var reg1 = /(@.*@)|(\.\.)|(,)|(@\.)|(\.@)|(^\.)|( )|([æøåÆØÅüöäÜÖÄ])/;	// definitions for nonvalid email
	var reg2 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2}|com|net|org|gov|mil|int|biz|pro|info|arpa|aero|coop|name|museum|mobi)(\]?)$/; 	// definitions for valid email
	if (!reg1.test( s ) && reg2.test( s ) ) {
		return true
	} else {
		return false
	}
}

function isPhonenumber( s ) {
	var re = new RegExp("^\\s*\\d{8}\\s*$")
	return re.test( s )
}


function newsletterSubscribe( f )
{
	var error = []
	if ( isEmpty(f.subscriber_firstname.value) || f.subscriber_firstname.value == f.subscriber_firstname.defaultValue ) error.push('Du mangler at angive fornavn.')
	if ( isEmpty(f.subscriber_surname.value) || f.subscriber_surname.value == f.subscriber_surname.defaultValue  ) error.push('Du mangler at angive efternavn.')
	if ( isEmpty(f.subscriber_email.value) || f.subscriber_email.value == f.subscriber_email.defaultValue  ) {
		error.push('Du mangler at angive email.')
	} else if ( !isEmail(f.subscriber_email.value) ) {
		error.push('Du har ikke angivet en korrekt email.')
	}
	if ( error.length > 0 ){
		alert( error.join("\n"))
		return false
	} else {
		return true
	}
}

function scrollToAndHighlight( scrollToWhat, highlightWhat )
{
	if ( typeof(highlightWhat) == 'undefined' ) highlightWhat = scrollToWhat;
	$.scrollTo(
		scrollToWhat, 
		{
			duration:	500, 
			axis:		"y", 
			onAfter:	function(){	highlight( highlightWhat ); }
		}
	);
}

function highlight( selector )
{
	$(selector).effect("highlight", {color: "#ffff00"}, 1000);
}

var modFavourite = function()
{
	return {
		request: function ( cmd, type, dataId, callback )
		{
			if ( typeof(callback) != 'function' ) callback = function(response){};
			App.showOverlay();
			App.request(
				'/_ajax/mod/favourite/?cmd='+cmd+'&type='+type+'&data_id='+dataId,
				{
					success: function(response){
						App.hideOverlay();
						callback(response);
					},
					error: function(response){

						App.hideOverlay();
						callback(response);
					}
				}
			);
		},

		add: function (type, dataId, callback)
		{
			modFavourite.request('add', type, dataId, callback);
		},

		remove: function (type, dataId, callback)
		{
			modFavourite.request('remove', type, dataId, callback);
		}
	};
}();

function makeDefaultValue(o)
{
	if ( typeof(o) == 'undefined' ) o = this;
	$(o).css('color','#aaaaaa');
	$(o).focus(
		function(){
			if ( $(this).val() == $(this).prop('defaultValue') ) {
				$(this).css('color','#333333');
				$(this).val('');
			}
		}
	);
	$(o).blur(
		function(){
			if ( $.trim($(this).val()) == '' ) {
				$(this).css('color','#aaaaaa');
				$(this).val($(this).prop('defaultValue'));
			}
		}
	);
}

/**
 * Homemade dropdown :o)
 */
var modDropDown = function(){
	return {
		currentOpened: false,
		
		init: function()
		{
			$(document).mouseup(
				function(event) {
					if ( modDropDown.currentOpened !== false ) {
						if ( modDropDown.currentOpened.attr('id') != $(event.target).closest('.cms-dropdown').attr('id') ) {
							modDropDown.close();
						} 
					}
				}
			);
		},
		
	    create: function( id, config )
	    {
	    	
	    	var top = 26;
	    	var left = -1;
	    	var width = 185;
	    	
	    	if ( typeof(config) != 'undefined') {
	    		if ( typeof(config.top) != 'undefined' )	top = config.top;
	    		if ( typeof(config.left) != 'undefined' )	left = config.left;
	    		if ( typeof(config.width) != 'undefined' )	width = config.width;
	    	}
	    	
	    	dropdown = $('#'+id); 
		    // label (opener)
		    dropdown.find('.cms-dropdown-opener').bind( 'click', function(){modDropDown.open(id);} );
		    // dropdown container
		    dropdown.find('.cms-dropdown-content').css('top', top + 'px').css('left', left + 'px').css('width', width + 'px').css('z-index',1000);
		    // dropdown list
		    dropdown.find('ul li').each( 
				function(index){
					if ( $(this).hasClass('selected') ) modDropDown.select( id, index );
					$(this).bind( 'click' , function(){ modDropDown.select(id, index);return false; });
		    	}
		    );

	    },

	    close: function()
	    {
		    if ( modDropDown.currentOpened === false ) return 
	    	modDropDown.currentOpened.find('.cms-dropdown-content').hide();
	    	modDropDown.currentOpened = false;
		},
	    
		open: function( id )
		{
			current = $('#' + id);
			if ( modDropDown.currentOpened !== false && modDropDown.currentOpened.attr('id') == current.attr('id') ){
				modDropDown.close();
			} else {
				current.find('.cms-dropdown-content').show();
				modDropDown.currentOpened = current;
			}
		},

		select: function( dropDownId, index ) 
		{
			dropdown = $('#'+dropDownId); 
			dropdown.find('li.selected').removeClass('selected');
			list = dropdown.find('ul li'); 
			if ( list[index] ) {
				$(list[index]).addClass('selected'); 
				text = $(list[index]).find('a').text();
				dropdown.find('label').text( text );
			}
			modDropDown.close();
		},

		getSelectedIndex: function(dropDownId)
		{
			return $('#'+dropDownId).find('li.selected').index(); 
		},
		
		getSelectedId: function(dropDownId)
		{
			return $('#'+dropDownId).find('li.selected').attr('id'); 
		}
		
	};
}();


