/* decryp addresses*/
function dcmadr(nnnn){
    var a = "";
    for(i=0,m=nnnn.length;i < m;i++){
        if(i%3==0){
            a += String.fromCharCode(nnnn.substr(i, 3));
        }
    }
    location.href=(a);
}
/* sound embeding / meta navigation */
var _isFirst = null;
var _offtxt = null;
var _ontxt = null;
var _soundfile = null;
var _soundplayer = null;
var _isLoop = null;

function handleSound(isFirst, offtxt, ontxt, soundfile, soundplayer, isLoop){
    if(_isFirst==null)_isFirst = isFirst;
    if(_offtxt==null)_offtxt = offtxt;
    if(_ontxt==null)_ontxt = ontxt;
    if(_soundfile==null)_soundfile = soundfile;
    if(_soundplayer==null)_soundplayer = soundplayer;
    if(_isLoop==null)_isLoop = isLoop;
    displaySoundSwitch();
    playSoundIfEnabled();
}
function playSoundIfEnabled(){
    var disableSound = $.cookie('teufelhof_sound');
    if(disableSound != 'true'){
        var html='<object width="1" height="1"><param name="flashVars" value="soundPath='+_soundfile+'&soundLoop='+_isLoop+'" /><param name="movie" value="'+_soundplayer+'"><embed src="'+_soundplayer+'" flashVars="soundPath='+_soundfile+'&soundLoop='+_isLoop+'" width="1" height="1"></embed></object>';
        $("#soundBox").html(html);
    }else{
        $("#soundBox").html("");
    }
}
function displaySoundSwitch(){
    var disableSound = $.cookie('teufelhof_sound');
    var metaNavElement = " | ";
    if(_isFirst == 'true'){metaNavElement="";}
    if(disableSound == 'true'){
        metaNavElement += '<a href="javascript:toggleSound(\''+false+'\')">'+_ontxt+'</a>';
    }else{
        metaNavElement += '<a href="javascript:toggleSound(\''+true+'\')">'+_offtxt+'</a>';
    }
    $("#soundSwitch").html(metaNavElement);
}
function toggleSound(val){
    $.cookie('teufelhof_sound', val, { expires: 365 });
    displaySoundSwitch();
    playSoundIfEnabled();
}



/* select form field element radio/checkbox */
function selectFormFieldElement(group, index) {
    var c = document.contactForm[group];
    if(c.length>0){
        c = c[index];
    }
    if(c.type == "radio"){
        c.checked = true;
    }else{
        c.checked = !c.checked;
    }
}


/* booking form elements */
var bookingValues = new Array();
function toggleBookingElement(formElementId){
    $("#"+formElementId).slideToggle("fast", function(){updateBookingInfo(formElementId);});
}
function updateBookingInfo(formElementId){
    var d = $("#field"+formElementId);
    var cb = $('input[name=cb'+formElementId+']');
    if(!bookingValues[formElementId]){bookingValues[formElementId] = d.val();}
    if($("#"+formElementId).is(":visible")){
        if(!cb.is(':checked')){cb.attr('checked', true);}
        var sd = $("#startdate"+formElementId).val();
        var ed = $("#enddate"+formElementId).val();
        if(sd != "" && ed != ""){
            var t = sd+" - "+ed+"\n"+$("#adt"+formElementId).val()+": "+$("#ad"+formElementId).val()+"\n"+$("#cht"+formElementId).val()+": "+$("#ch"+formElementId).val()+"\n"+$("#dgt"+formElementId).val()+": "+$("#dg"+formElementId).val()+"\n";
            d.val(t);
        }
    }else{
        if(cb.is(':checked')){cb.attr('checked', false);}
        d.val(""+bookingValues[formElementId]);
    }
}
function updateTableBookingInfo(formElementId){
    var d = $("#field"+formElementId);
    var cb = $('input[name=cb'+formElementId+']');
    if(!bookingValues[formElementId]){bookingValues[formElementId] = d.val();}
    if($("#"+formElementId).is(":visible")){
        $("#field_"+formElementId).attr("name", "field_"+formElementId);

        if(!cb.is(':checked')){cb.attr('checked', true);}
        var sd = $("#startdate"+formElementId).val();
        if(sd != ""){
            var t = sd+", "+$("#th"+formElementId).val()+":"+$("#tm"+formElementId).val()+" Uhr\n"+$("#p"+formElementId).val()+": "+$("#ad"+formElementId).val()+"\n";
            d.val(t);
        }
    }else{
        if(cb.is(':checked')){cb.attr('checked', false);}
        d.val(""+bookingValues[formElementId]);
    }
}

var missing_booking_date = "Bitte Datum angeben.";
function setMissing_booking_date(text){
    missing_booking_date = text;
}
function checkBookingField(){
    var room_bookings = $(".room_booking").get();
    var c = room_bookings.length;
    for(var i=0; i<c; i++){
        var x = room_bookings[i];
        var sd = $(".startdate", x);
        var ed = $(".enddate", x);
        var goOn = true;
        if(sd.is(":visible")){
            if(ed.val()==""){ed.addClass("emptymandatory");goOn=false;ed.focus();}
            if(sd.val()==""){sd.addClass("emptymandatory");goOn=false;sd.focus();}
            if(!goOn){
                alert(missing_booking_date);
                return false;
            }
        }else{
            //get the id of the booking container
            var formElementId = sd.attr('id').substring(10);
            //rename the element to avoid inclusion in generated email
            $("#field_"+formElementId).attr("name", "disabled_"+formElementId);
        }
    }
    return true;
}

/* wine order */
function checkWineOrderParagraphs(){
    $(".wine").each(function(){
        var $i = $("input", $(this));
        var c = $i.val();
        if(c<1){$i.remove();}
    });    
}


/* search box */
function removeDefault(formField, value){
    if(formField.value == value){
        formField.value = "";
    }
    formField.style.color = "#000";
}
function resetDefault(formField, value){
    if(formField.value == ""){
        formField.value = value;
        formField.style.color = "#888";
    }
}
function checkSearchBox(ff,st,eft){
    var t = ff.search.value;
    if(t == ''||t==st){
        ff.search.focus();
        ff.search.select();
        return false;
    }
}



$(document).ready(function(){
     $(".hideLink").mousedown(function(){
        $("#contentparagraphs").fadeOut("fast");
        setTimeout('revealContent()', contentFadeInTime)
    });
    $(".history").mousedown(function(){
        var act = !$(".historyListDetail",this).is(':visible');
        if(act){
            $(".historyListDetail.openEntry").slideUp();
            $(".historyListDetail.openEntry").removeClass("openEntry");
            $(".historyListDetail",this).slideDown();
            $(".historyListDetail",this).addClass("openEntry")
        }
    });
    $("input[type=text]").mouseenter(function(){
        $(this).focus();
        $(this).select();
    });
});



/* helper methods */

Array.prototype.shuffle = function( b ) {
 var i = this.length, j, t;
 while( i ) {
  j = Math.floor( ( i-- ) * Math.random() );
  t = b && typeof this[i].shuffle!=='undefined' ? this[i].shuffle() : this[i];
  this[i] = this[j];
  this[j] = t;
 }
 return this;
};
function get_radio_value(formhandle){
    var r = "";
    for (var i=0; i < formhandle.length; i++){
        if (formhandle[i].checked){
            r = formhandle[i].value;
        }
    }
    return r;
}

