var menuVisible = false;
var propagate = true;
function copyShow(id)
{
    $('#subbartxt').html($('#smenu'+id).html());
    $('#it'+id).css('text-decoration','underline');
    $('#subbar').slideDown('medium',function(){
        menuVisible = id;
    });
}
function shMenu(id)
{
    propagate = false;
    if ( menuVisible != false )
    {
        $('#subbar').slideUp('medium',function(){
            $('#it'+menuVisible).css('text-decoration','none');
            if ( menuVisible == id )
            {
		        menuVisible = false;
            }
            else
            {
                copyShow(id);
            }
        });
    }
    else
    {
        copyShow(id);
    }
    return false;
}

$(document).ready(function(){
    $(document).click(function(){
        if ( propagate && menuVisible != false )
        {
            resetMenu();
            /*
            $('#it'+menuVisible).css('text-decoration','none');
            $('#subbar').slideToggle('fast');
            menuVisible = false;
            */
        }
        propagate = true;
    });
    $("#srch").focus(function(){
        if ( $(this).val() == 'Søg ...' )
        {
            $(this).css("color","#4e4e4e").val('');
        }
    });
    $("#srch").blur(function(){
        if ( $(this).val() == '' )
        {
            $(this).css("color","#adadad").val('Søg ...');
        }
    });
});

/*
    Based on this article:
    http://www.html-advisor.com/javascript/hide-email-with-javascript-jquery/

    Example markup:
    ---------------

    <span class="mailme" title="Send me a letter!">me at mydomain dot com</span>

    Example code:
	-------------

	// Replaces all the matching elements with a <a href="mailto:..> tag.

	$('span.mailme').mailme();

*/

jQuery.fn.mailme = function() {
    var at = / snabel-a /;
    var dot = / punktum /g;
    this.each( function() {
        var addr = jQuery(this).text().replace(at,"@").replace(dot,".");
        var title = jQuery(this).attr('title')
        $(this)
            .after('<a href="mailto:'+addr+'" title="'+title+'">'+ addr +'</a>')
            .remove();
    });
};

$(document).ready(function(){
  $('span.mailme').mailme();
});

function chkDelTil()
{
    err = '';
    if ( $("#virksomhed").val() == '' )
    {
        err += "Virksomhedsnavn skal udfyldes\n";
    }
    if ( $("#kontaktperson").val() == '' )
    {
        err += "Kontaktperson skal udfyldes\n";
    }
    if ( $("#adresse").val() == '' )
    {
        err += "Adresse skal udfyldes\n";
    }
    if ( $("#postnr").val() == '' )
    {
        err += "Postnummer skal udfyldes\n";
    }
    if ( $("#bynavn").val() == '' )
    {
        err += "Bynavn skal udfyldes\n";
    }
    if ( $("#tlf").val() == '' )
    {
        err += "Telefonnummer skal udfyldes\n";
    }
    if ( $("#ean").val() == '' )
    {
        err += "EAN-nummer skal udfyldes\n";
    }
    else
    {
        if ( !$("#ean").val().match( /^\d+$/ ) )
        {
          err += "EAN-nummer lader ikke til at være gyldig\n";
        }
    }
    email = $("#email").val();
    if ( email == '' )
    {
        err += "Email skal udfyldes\n";
    }
    var filter = /^([a-zA-Z0-9_\.+-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,6})+$/;
    if ( !filter.test( email ) )
    {
        err += "Email lader ikke til at være gyldig\n";
    }

    if ( err != '' )
    {
        alert(err);
        return false;
    }
    return true;
}

jQuery.fn.extend({

	highlight: function(search, insensitive, klass){
		var regex = new RegExp('(<[^>]*>)|(\\b'+ search.replace(/([-.*+?^${}()|[\]\/\\])/g,"\\$1") +')', insensitive ? 'ig' : 'g');
		return this.html(this.html().replace(regex, function(a, b, c){
			return (a.charAt(0) == '<') ? a : '<strong class="'+ klass +'">' + c + '</strong>';
		}));
	}

});

function focSrch(blur)
{
    if ( blur == 1 )
    {
        if ( $("#q").val() == '' )
        {
            $("#q").val('Søg ...');
        }
    }
    else
    {
        if ( $("#q").val() == 'Søg ...' )
        {
            $("#q").val('');
        }
    }
}

var tgls = {};

function tglPrice(id)
{
    n = tgls[id].length;
    if ( n > 0 )
    {
        $("#"+id+"_order").show('fast');
        if ( typeof prices[n] != 'undefined' )
        {
            standplads = $('#'+id+'_opt_1').is(':checked');
            pri = prices[n][0] + ( standplads ? prices[n][1] : 0 );
            $("#"+id+"_price").html( pri );
            $("#"+id+"_bookprice").val( pri );
        }
        else
        {
            $("#"+id+"_price").html( "Ugyldigt antal kvadratmeter valgt" );
            $("#"+id+"_bookprice").val( "-1" );
        }
    }
    else
    {
        $("#"+id+"_order").hide('fast');
        $("#"+id+"_bookprice").val( '' );
    }
}

function toggleIt(element)
{
    if ( notloggedin )
    {
        return;
    }
    parentid = $(element).parent().parent().parent(".bookContainer").attr("id");
    $(element).toggleClass("bc bf");
    name = $(element).attr("id").substr( $(element).attr("id").lastIndexOf("_")+1 );
    if ( $(element).hasClass('bc') )
    {
        if ( typeof tgls[parentid] == 'undefined' )
        {
            tgls[parentid] = [];
        }
        tgls[parentid].push(name);
        tgls[parentid].sort(function(a,b){return a - b});
    }
    else
    {
        tgls[parentid].splice(tgls[parentid].indexOf(name),1);
    }
    tglPrice( parentid );
    $("#"+parentid+"_pladser").html( tgls[parentid].length == 0 ? ingen : tgls[parentid].toString().replace( /,/g, ", " ) );
}

if (!Array.prototype.indexOf)
{
  Array.prototype.indexOf = function(elt /*, from*/)
  {
    var len = this.length;

    var from = Number(arguments[1]) || 0;
    from = (from < 0)
         ? Math.ceil(from)
         : Math.floor(from);
    if (from < 0)
      from += len;

    for (; from < len; from++)
    {
      if (from in this &&
          this[from] === elt)
        return from;
    }
    return -1;
  };
}


