var navigation = {
    navstore : {
        dict : {},
        register : function (el, state) {
            var id = $(el).attr("href");
            if (id in this.dict) {} else {
                this.dict[id] = state;
            }
        },
        out : function(el) {
            var id = $(el).attr("href");
            if (id in this.dict) {} else {
                $(el).removeClass("active");
            }
        }
    },
    init: function(pathinfo) {  
        var self = this;
        $("#navigationbox li").hover(function() { 
            $(this).find("p").show();
            $(this).find("a").first().addClass("active");
        } , function() { 
            // on hover out... (check what to do
            $(this).find("p").hide();
            self.navstore.out($(this).find("a").first());
        });
                                                        
        // register
        $("#navigationbox a").each(function(i,n){
            var href = $(n).attr("href");
            if (href == pathinfo) {
                // (url rewriting must work for this)
                // same pathinfo = active item 
                $(n).addClass("active");
                self.navstore.register(n, true);
                
                // also register the parent as active
                var root = $(n).parent().siblings("a");
                $(root).addClass("active");
                self.navstore.register(root, true);
            }
        });
    } 
};

var jobsearching = {
    autocomplete : "/autocomplete.php?render=json",
    isInit : function( field ) {
        var title = $(field).attr("title");
        var current = $(field).val();
        return current.length > 0 && current != title;
    },
    checkInit : function( field ) {
        var id = $(field).attr("id");
        var title = $(field).attr("title");
        var current = $(field).val();
        if (current == '') {
            $(field).val(title);
        } else {
            if (current == title) {
                $(field).val('');
            }
        }
    },
    restoreInit : function( field ) {
        this.checkInit(field);
    },
    submit : function() {
        $("#searchfields input[type='text']").each(function(i,n) {
            jobsearching.checkInit(n);
        });
        $("#searchform").submit();
    },
    countChecked : function(container) {
        var count = 0;
        $(container).each(function(i,n) {
            if ($(n).prop("checked")) {
                count = count + 1;
            }
        })
        return count;
    },
    checkItems : function(id, items) {
        $(id + " input[type='checkbox']").each(function(i,n) {
            var val = $(n).val();
            if ($.inArray(val, items) != -1) {
                if (! $(n).prop("checked")) {
                    $(n).click();
                }
            }
        });
    },
    currentTextUpdate : function() {
        $(".multiple").each(function(i,n) {
            var id = $(n).attr("id");
            var title = $(n).attr("title");
            
            // change event count total
            var count = jobsearching.countChecked("#" + id + " ul.checklist li input");
            var text = '';
            if (count == 0) {
                text = 'Alle ' + title;
            } else if (count == 1) {
                text = $("#" + id + " ul.checklist input:checked").parent().text();
            } else {
                text = count + ' ' + title + ' gew&auml;hlt.';
            }
            $("#" + id + " a.current span.text").html(text);
        });
    },
    testme : function() {
        console.log("test success");
    },
    init : function() {
        // bind it
        $("#search").autocomplete({source: this.autocomplete});
        
        $("#searchfields input[type='text']").each(function(i,n){
            jobsearching.checkInit(n);
        });
        $("#searchfields input[type='text']").click(function() {
            jobsearching.checkInit(this);
        });
        $("#searchfields input[type='text']").blur(function() {
            jobsearching.restoreInit(this);
        });
        $("#searchfields input[type='text']").keypress(function(e) {
            if(e.keyCode == 13) {
                if ($(this).val() == "") {
                    // need to set this attribute to have checkinit
                    // strip it away
                    $(this).val($(this).attr("title"));
                }
                jobsearching.submit();
            }
        });
        $("#findjob").click(jobsearching.submit);
        
        // jobselector
        $("#jobselector .tab_content").hide(); // hide all content
        $("#jobselector .tab_content:first").show(); // show first tab content
        $("#jobnav ul.tabs li:first").addClass("active").show(); // activate first tab
        $("#jobnav ul.tabs li").click(function() {
            $("ul.tabs li").removeClass("active"); //Remove any "active" class
            $(this).addClass("active"); //Add "active" class to selected tab
            $(".tab_content").hide(); //Hide all tab content

            var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content
            $(activeTab).fadeIn(); //Fade in the active ID content
            return false;
        });
        
        // update form on change
        $(".multiple ul.checklist li input").change(function() {
            jobsearching.currentTextUpdate();
            if ($(this).prop("checked")) {
                $(this).parent().addClass("active");
            } else {
                $(this).parent().removeClass("active");
            }
        });
        // update the current text
        this.currentTextUpdate();
        
        // multiple opener function
        $(".multiple a.current").click(function() {
            $(this).siblings("ul.checklist").toggle();
        });
        
        // click events in multiple select
        $(".multiple").click(function(event) {
            event.stopPropagation();
        });
        
        // hide all
        $('html').click(function() {
            /*if ($("ul.checklist").prop("style").display == "block") {}*/
            $(".opener ul.checklist").hide();
        })
        
        // region select ie fix
        $(".multiple ul.checklist label").hover(function() {
            $(this).addClass("hover");
        }, function() {
            $(this).removeClass("hover");
        });
        
        // search results hover effect
        $(".searchresult tbody tr[class!='delim']").hover(function() {
            $(this).addClass("hover");
        }, function() {
            $(this).removeClass("hover");
        });
    }    
};

/// old stuff

function doubleFieldData(f_whichform, f_whichCheckbox, f_whichStringCode)
{
    //alert(f_whichform + " " + f_whichCheckbox + " " + f_whichStringCode)
    var tlen, i, j;
    tlen = f_whichStringCode.length;
    if (f_whichCheckbox.checked == true)
    {
        i = 0;
        while (document.forms[f_whichform].getElementsByTagName("input")[i])
        {
            if (document.forms[f_whichform].getElementsByTagName("input")[i].name.substr(0, tlen) != f_whichStringCode)
            {
                tstr = f_whichStringCode+document.forms[f_whichform].getElementsByTagName("input")[i].name;
                //alert(document.forms[f_whichform].getElementsByName(tstr)[0]);
      
                if (document.forms[f_whichform].elements[f_whichStringCode+document.forms[f_whichform].getElementsByTagName("input")[i].name])
                {
                    document.forms[f_whichform].elements[f_whichStringCode+document.forms[f_whichform].getElementsByTagName("input")[i].name].value = document.forms[f_whichform].getElementsByTagName("input")[i].value;
                }
            }  
            i++; 
        }
        i = 0;
        while (document.forms[f_whichform].getElementsByTagName("textarea")[i])
        {
            if (document.forms[f_whichform].getElementsByTagName("textarea")[i].name.substr(0, tlen) != f_whichStringCode)
            {
                if (document.forms[f_whichform].elements[f_whichStringCode+document.forms[f_whichform].getElementsByTagName("textarea")[i].name])
                {
                    document.forms[f_whichform].elements[f_whichStringCode+document.forms[f_whichform].getElementsByTagName("textarea")[i].name].innerHTML = document.forms[f_whichform].getElementsByTagName("textarea")[i].innerHTML;
                }
            }  
            i++; 
        }
        i = 0;
        while (document.forms[f_whichform].getElementsByTagName("select")[i])
        {
            if (document.forms[f_whichform].getElementsByTagName("select")[i].name.substr(0, tlen) != f_whichStringCode)
            {
                if (document.forms[f_whichform].elements[f_whichStringCode+document.forms[f_whichform].getElementsByTagName("select")[i].name])
                {
                    if (document.forms[f_whichform].elements[f_whichStringCode+document.forms[f_whichform].getElementsByTagName("select")[i].name].multiple)
                    {  
                        for (j = 0; j < document.forms[f_whichform].getElementsByTagName("select")[i].options.length; j++)
                        {
                            if (document.forms[f_whichform].getElementsByTagName("select")[i].options[j].selected == true)
                            {document.forms[f_whichform].elements[f_whichStringCode+document.forms[f_whichform].getElementsByTagName("select")[i].name].options[j].selected = true;}
                            else
                            {document.forms[f_whichform].elements[f_whichStringCode+document.forms[f_whichform].getElementsByTagName("select")[i].name].options[j].selected = false;}
                        }
                    }
                    else
                    {
                        document.forms[f_whichform].elements[f_whichStringCode+document.forms[f_whichform].getElementsByTagName("select")[i].name].selectedIndex = document.forms[f_whichform].getElementsByTagName("select")[i].selectedIndex;
                    }
                }
            }  
            i++; 
        }
    }
    else
    {
        // empty all boxes
        i = 0;
        while (document.forms[f_whichform].getElementsByTagName("input")[i])
        {
            if (document.forms[f_whichform].getElementsByTagName("input")[i].name.substr(0, tlen) != f_whichStringCode)
            {
                if (document.forms[f_whichform].elements[f_whichStringCode+document.forms[f_whichform].getElementsByTagName("input")[i].name])
                {
                    document.forms[f_whichform].elements[f_whichStringCode+document.forms[f_whichform].getElementsByTagName("input")[i].name].value = "";
                }
            }  
            i++; 
        }
        i = 0;
        while (document.forms[f_whichform].getElementsByTagName("textarea")[i])
        {
            if (document.forms[f_whichform].getElementsByTagName("textarea")[i].name.substr(0, tlen) != f_whichStringCode)
            {
                if (document.forms[f_whichform].elements[f_whichStringCode+document.forms[f_whichform].getElementsByTagName("textarea")[i].name])
                {
                    document.forms[f_whichform].elements[f_whichStringCode+document.forms[f_whichform].getElementsByTagName("textarea")[i].name].innerHTML = "";
                }
            }  
            i++; 
        }
        i = 0;
        while (document.forms[f_whichform].getElementsByTagName("select")[i])
        {
            if (document.forms[f_whichform].getElementsByTagName("select")[i].name.substr(0, tlen) != f_whichStringCode)
            {
                if (document.forms[f_whichform].elements[f_whichStringCode+document.forms[f_whichform].getElementsByTagName("select")[i].name])
                {
                    for (j = 0; j < document.forms[f_whichform].getElementsByTagName("select")[i].options.length; j++)
                    {
                        document.forms[f_whichform].elements[f_whichStringCode+document.forms[f_whichform].getElementsByTagName("select")[i].name].options[j].selected = false;
                    }
                }
            }  
            i++; 
        }
  
    }
}


function formCheckboxes(whichbox)
{
    if (document.getElementById(whichbox))
    {
        if (document.getElementById(whichbox).checked == true)
        {document.getElementById(whichbox).checked = false;}
        else
        {document.getElementById(whichbox).checked = true;}
    }
}


function swapText(whichLayer, whichContent) {
    //swap content of div
    if (document.layers) 
    {
        document.layers[whichLayer].document.write(whichContent);
        document.layers[whichLayer].document.close();
    }
    else
    {
        if (document.getElementById) 
        {
            document.getElementById(whichLayer).innerHTML = whichContent;
        } 
        else
        {
            document.all[whichLayer].innerHTML=whichContent;
        }
    }
}

function searchAndReplace(sourceTxt, searchTxt, replaceTxt)
{   
    if ((sourceTxt == null) || (searchTxt == null))           {return null;}
    if ((sourceTxt.length == 0) || (searchTxt.length == 0))   {return sourceTxt;}
    if ((replaceTxt == null) || (replaceTxt.length == 0))    {replaceTxt = "";}
    var searchTxtLen = searchTxt.length;
    var replaceTxtLen = replaceTxt.length;
    var Pos = sourceTxt.indexOf(searchTxt, 0);
    while (Pos >= 0)
    {
        sourceTxt = sourceTxt.substring(0, Pos) + replaceTxt + sourceTxt.substring(Pos + searchTxtLen);
        Pos = sourceTxt.indexOf(searchTxt, Pos + replaceTxtLen);
    }
    return sourceTxt;
}

function setValueFromSelect(f_whichSelect, f_whichTargetField, f_whichData) {
    if (f_whichTargetField.value == "") {
        if (f_whichData == "") // could be value (std) or title
        {f_whichData = "value";}
        for (i = 0; i < f_whichSelect.length; ++i) {
            if (f_whichSelect.options[i].selected == true) {
                if (f_whichData == "value")
                {f_whichTargetField.value = f_whichSelect.options[i].value;break;}
                if (f_whichData == "title")
                {f_whichTargetField.value = f_whichSelect.options[i].title;break;}
            }
        }
    }
}

