/* GENERATE TABS */
$(function () {
    var tabContainers = $('div.tabs > div');

    $('div.tabs ul.tabNavigation a').click(function () {
        // Need to activate only first tab for 'add company' page
        if ("" == this.hash) {
            return false;
        }

        tabContainers.hide().filter(this.hash).show();

        $('div.tabs ul.tabNavigation a').removeClass('selected');
        $(this).addClass('selected');

        return false;
    }).filter(':first').click();
});


/* ACCORDIAN */

jQuery().ready(function(){
        // simple accordion
        jQuery('#list1a').accordion();
        jQuery('#list1b').accordion({
            autoheight: false
        });

        // second simple accordion with special markup
        jQuery('#navigation').accordion({
            active: false,
            header: '.head',
            navigation: true,
            event: 'mouseover',
            fillSpace: true,
            animated: 'easeslide'
        });

        // highly customized accordion
        jQuery('#list2').accordion({
            event: 'mouseover',
            active: '.selected',
            selectedClass: 'active',
            animated: "bounceslide",
            header: "dt"
        }).bind("change.ui-accordion", function(event, ui) {
            jQuery('<div>' + ui.oldHeader.text() + ' hidden, ' + ui.newHeader.text() + ' shown</div>').appendTo('#log');
        });

        // first simple accordion with special markup
        jQuery('#list3').accordion({
            header: 'div.title',
            active: false,
            alwaysOpen: false,
            animated: false,
            autoheight: false
        });

        var wizard = $("#wizard").accordion({
            header: '.title',
            event: false
        });

        var wizardButtons = $([]);
        $("div.title", wizard).each(function(index) {
            wizardButtons = wizardButtons.add($(this)
            .next()
            .children(":button")
            .filter(".next, .previous")
            .click(function() {
                wizard.accordion("activate", index + ($(this).is(".next") ? 1 : -1))
            }));
        });

        // bind to change event of select to control first and seconds accordion
        // similar to tab's plugin triggerTab(), without an extra method
        var accordions = jQuery('#list1a, #list1b, #list2, #list3, #navigation, #wizard');

        jQuery('#switch select').change(function() {
            accordions.accordion("activate", this.selectedIndex-1 );
        });
        jQuery('#close').click(function() {
            accordions.accordion("activate", -1);
        });
        jQuery('#switch2').change(function() {
            accordions.accordion("activate", this.value);
        });
        jQuery('#enable').click(function() {
            accordions.accordion("enable");
        });
        jQuery('#disable').click(function() {
            accordions.accordion("disable");
        });
        jQuery('#remove').click(function() {
            accordions.accordion("destroy");
            wizardButtons.unbind("click");
        });
    });

// Create new image
var loadingImage = new Image();

// Set loading image's source to pre-load image
loadingImage.src = baseUrl + "img/indicator.gif";

// Loading HTML
var loadingHtml = '<div style="background-color: white; text-align: center;"><h1><img alt="" src="' + loadingImage.src + '" valign="middle" /> &nbsp; <span style="vertical-align: top;">' + loadingText + '</span></h1></div>';

// When document is ready
$(document).ready(function()
{
    // Attach auto-complete plugin for other location's city textbox
    $("#JobCity, #JobSeekCity").autocomplete(baseUrl + "countries/autocomplete_cities");

    // Convert job description textarea to HTML editor using custom toolbar set
    if (null != document.getElementById("JobDescription"))
    {
        var editor = new FCKeditor("JobDescription", 373, 200, "JobDescription");
        editor.BasePath = baseUrl + "js/fckeditor/";
        editor.ReplaceTextarea();
    }

    // Convert frontend job description textarea to HTML editor using custom toolbar set
    if (null != document.getElementById("frontendJobDescription"))
    {
        var editor = new FCKeditor("frontendJobDescription", 373, 200, "Frontend");
        editor.BasePath = baseUrl + "js/fckeditor/";
        editor.ReplaceTextarea();
    }

    // When process button is clicked
    $("#processButton").click(function()
    {
        // Hide cake's SQL log table in body
        $(".cake-sql-log").hide();

        // Hide main wrapper in body
        $("#body_wrapper").hide();

        // Put loading text in body
        $("body").append(loadingHtml);

        // by default return true
        return true;
    });

    // Make note textareas (sometimes, of task) as WYSIWYG editors
    $(".noteTextarea").each(function()
    {
        var editor = new FCKeditor(this.id, 373, 200, "JobDescription");
        editor.BasePath = baseUrl + "js/fckeditor/";
        editor.ReplaceTextarea();
    });
});

// OPENS POPUP FOR OLD PORTAL NAVIGATION

// Eine Liste aller geöffneten Popups
var PopupList = new Array();


function js_popup(str_url, str_name, str_window, int_width, int_height, int_x, int_y, str_features)
{
        str_features = str_features + ',width=' + int_width + ',height=' + int_height;

        if (int_x=='false')
             str_features = str_features + ',left=' + ((screen.width - int_width) / 2) + ',top=' + ((screen.height - int_height) / 2);
        else
             str_features = str_features + ',left=' + int_x + ',top=' + int_y;

        if (PopupList[str_window] && PopupList[str_window].closed == false)
            PopupList[str_window].close();

        obj_window = window.open(str_url, str_window, str_features);
        PopupList[str_window] = obj_window;
        obj_window.focus();
}


//function that can be used to disable the screen till we want.
// Can be used when ajax requests are working, remove this by using tb_remove()
function tb_loading() {

    try {
        if (typeof document.body.style.maxHeight === "undefined") {//if IE 6
            $("body","html").css({height: "100%", width: "100%"});
            $("html").css("overflow","hidden");
            if (document.getElementById("TB_HideSelect") === null) {//iframe to hide select elements in ie6
                $("body").append("<iframe id='TB_HideSelect'></iframe><div id='TB_overlay'></div><div id='TB_window'></div>");
            }
        } else {//all others
            if(document.getElementById("TB_overlay") === null){
                $("body").append("<div id='TB_overlay'></div><div id='TB_window'></div>");
            }
        }

        if (tb_detectMacXFF()){
            $("#TB_overlay").addClass("TB_overlayMacFFBGHack");//use png overlay so hide flash
        } else {
            $("#TB_overlay").addClass("TB_overlayBG");//use background and opacity
        }

        var caption="";
        $("body").append("<div id='TB_load'><img src='"+imgLoader.src+"' /></div>");//add loader to the page
        $('#TB_load').show();//show loader
    } catch(e) {
        //nothing here
    }
}//end tb_loading()

// Function to decrypted the email id
function js_mailto(str_crypted) {
    str_email='';
    for (int_pos=0; int_pos < str_crypted.length; int_pos++)
    str_email += String.fromCharCode(str_crypted.charCodeAt(int_pos)+1);
    location.href='mailto:' + str_email;
}