/*
 * Cookie tools
 */

var Cookie = {};

Cookie.createCookie = function(name, value) {
    document.cookie = name + '=' + value + '; path=/';
}

Cookie.readCookie = function(name) {
    name += '=';
    var snippets = document.cookie.split(';');
    for(var i = 0; i < snippets.length; i++) {
        var c = snippets[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(name) == 0) return c.substring(name.length, c.length);
    }
    return '';
}

Cookie.eraseCookie = function(name) {
    Cookie.createCookie(name, '');
}

/*
 * FTSearch tools
 */

var FTSearch = {};

FTSearch.cacheFulltextSearchForm = function() {
    var encodedParams = escape($('#searchTerm').val()) + '|';
    encodedParams += escape($('#yearFrom').val()) + '|';
    encodedParams += escape($('#yearTo').val());
    Cookie.createCookie('fulltextSearch', encodedParams);
}

FTSearch.populateFulltextSearchForm = function() {
    var parts = Cookie.readCookie('fulltextSearch').split('|');
    if (parts.length > 1) {
        $('#searchTerm').val(unescape(parts[0]));
        $('#yearFrom').val(unescape(parts[1]));
        $('#yearTo').val(unescape(parts[2]));
    }
}

FTSearch.clearFulltextSearchForm = function() {
    Cookie.eraseCookie('fulltextSearch');
}


/* 
 * Clear form function
 */
$.fn.clearForm = function() {
    return this.each(function() {
      var type = this.type, tag = this.tagName.toLowerCase();
      if (tag == 'form')
        return $(':input',this).clearForm();
      if (type == 'text' || type == 'password' || tag == 'textarea')
        this.value = '';
      else if (type == 'checkbox' || type == 'radio')
        this.checked = false;
      else if (tag == 'select')
        this.selectedIndex = -1;
    });
};

$(document).ready(function() {
    /*
     * tab navigation
     */
    // make both tabs visible, default is "display: none" (via CSS)
    $('#tab-issueTree').show();
    $('#tab-searchForms').show();
    
    $('#navSecondary li.search a').attr('href', '#tab-searchForms');
    $('#navSecondary li.navi a').attr('href', '#tab-issueTree');
    
    // if anchor is set in URL, activate navigation panel
    var selectedTab = document.location.hash ? 1 : 0;
    
    // load tab navigation panel
    var tabs = $('#side_navi').tabs({selected: selectedTab});
    
    /*
     * authorContact form
     */
    // hide all upload fields, except first
    $('#ct_files label:not(:first)').hide();
    $('#ct_files input:not(:first)').hide();
    
    // show next upload field on change
    $('#ct_files input[type=file]').change(function(){
        $(this).next('label').show();
        $(this).next().next('input').show();
    });
    
    $('input[type=reset]').click(function(){
        //console.log($(this).parents('form'));
        $(this).parents('form').clearForm();
        return false;
    });

    // setup form behaviour
    $('#form_fulltext input[type=submit]').click(function() {
        FTSearch.cacheFulltextSearchForm();
        return true;
    });
    $('#form_fulltext input[type=reset]').click(function() {
        FTSearch.clearFulltextSearchForm();
        return true;
    });
    FTSearch.populateFulltextSearchForm();

    $('#issuesearch_jsActivated').val(1);
});
