var showcaseRunning = true;
var currentShowCase = 2;
var numberOfShowcaseItems;

// init will be called at the end of the page .. (see page footer)
function init() {
    // do the magic
    extendFormElementsWithDefaultValue();
}

/** MODALBOXES **/

// pops up a modalbox login form
function login() {
    Modalbox.show($ROOT + 'login-popup', {
        title: 'Log in',
        width: 400
    });
}
// posts a modalbox login form
function postLogin() {
    // post it to a next modal box
    Modalbox.show($ROOT + 'login-popup', {
        title: 'Log in',
        params: Form.serialize('frm_login_popup')
    });
}

function showPrivacyStatements() {
     Modalbox.show($ROOT + 'privacy-statements-popup', {
        title: 'Privacy Statements',
        width: 750,
         height: 700
    });
}

// pops up a vacancy reaction form
function showVacancyReactForm($owner_name, $user_name, $employee_id, $publication_id) {
    Modalbox.show($ROOT + 'vacancy-react-form-popup?owner_name=' + $owner_name + '&user_name=' + $user_name
            + '&employee_id=' + $employee_id + '&publication_id=' + $publication_id, {
        title: 'Laat weten dat je ge&iuml;nteresseerd bent',
        width: 400
    });
}
// posts a vacancy reaction form
function postVacancyReaction() {
    Modalbox.show($ROOT + 'vacancy-react-form-popup', {
        title: 'Je bericht is verzonden',
        params: Form.serialize('frm_vacancy_react_popup')
    });
}

// pops up a profiel reaction form
function showProfileReactForm($employeeId) {
    Modalbox.show($ROOT + 'profile-react-form-popup?employee_id=' + $employeeId, {
        title: 'Laat weten dat u ge&iuml;nteresseerd bent',
        width: 400
    });
}
// posts a vacancy reaction form
function postProfileReaction($url) {
    Modalbox.show($ROOT + 'profile-react-form-popup', {
        title: 'Laat weten dat u ge&iuml;nteresseerd bent',
        params: Form.serialize('frm_profile_react_popup'),
        afterHide: function() { closeAndReplacePage() }
    });
}

// closes modalbox
function cancel() {
    Modalbox.hide();
}

// closes modalbox and redirect parent page
function closeAndReplacePage() {
    if ($replace_url.length>0) {
        window.location.replace($replace_url);
    }
}

// closes modalbox and reloads parent page
function closeAndReloadPage() {
    Modalbox.hide();
    window.location.reload();
}

// closes modalbox and reloads parent page
function closeAndGoToMyVacancies() {
    Modalbox.hide();
    window.location = $ROOT + 'mijn-sollicitaties';
}

// closes modalbox and reloads parent page
function closeAndGoToSignup() {
    Modalbox.hide();
    window.location = $ROOT + 'inschrijven';
}

function showMatchModuleLayer() {
    Modalbox.show($ROOT +'matchmodule-layer', {
        title: 'Match Module',
        width: 850
    });
}

function postMMLayerReaction() {
    Modalbox.show($ROOT + 'matchmodule-layer', {
        title: 'Match Module',
        params: Form.serialize('frm_mm_layer')
    });
}

/** MULTI BUTTONS **/

// some required vars
$openTimeout = 500;
$openButton = undefined;
$closeButtonTimer = undefined;

// open one
function openButton($id) {
    // close currently opened button
    if ($openButton != $id) {
        closeButton();
    }
    // open button
    $($id).down('.multi_button_extension').show();
    // set this one as the opened one
    $openButton = $id;
}

// close on delay
function timeCloseButton() {
    $closeButtonTimer = window.setTimeout(closeButton, $openTimeout);
}

// remove close delay
function cancelCloseButton($id) {
    if ($closeButtonTimer) {
        window.clearTimeout($closeButtonTimer);
        $closetimer = undefined;
    }
    // open this button if another is allready opened (open on slide-to)
    if ($openButton != undefined && $openButton != $id) {
        openButton($id);
    }
}

// actually closes a button
function closeButton() {
    if ($openButton != undefined) {
        $($openButton).down('.multi_button_extension').hide();
    }
    $openButton = undefined;
}

/** SAVE VACANCY AJAX **/

function saveVacancy($publicationId, codeOnSuccess, codeOnFailure) {
    if ($publicationId>0) {
        var $uri = $ROOT+'ajax/save-vacancy/'+$publicationId;
        new Ajax.Request($uri, {
            method: 'get',
            parameters: {},
            onSuccess: function (xmlHttpResponse) {
                if (xmlHttpResponse.responseText.isJSON()) {
                    response = xmlHttpResponse.responseText.evalJSON();
                    if (response.msg != undefined) {
                        if ((codeOnSuccess != undefined) && (codeOnSuccess != '')) {
                            callbackOnSuccess = new Function(codeOnSuccess);
                            callbackOnSuccess();
                        }
                        return true;
                    } else {
                        if ((codeOnFailure != undefined) && (codeOnFailure != '')) {
                            callbackOnFailure = new Function(codeOnFailure);
                            callbackOnFailure();
                        }
                        return false;
                    }
                } else {
                    if ((codeOnFailure != undefined) && (codeOnFailure != '')) {
                        callbackOnFailure = new Function(codeOnFailure);
                        callbackOnFailure();
                    }
                    return false;
                }
            },
            onFailure: function () {
                if ((codeOnFailure != undefined) && (codeOnFailure != '')) {
                    callbackOnFailure = new Function(codeOnFailure);
                    callbackOnFailure();
                }
                return false;
            }
        });
    } else {
        if ((codeOnFailure != undefined) && (codeOnFailure != '')) {
            callbackOnFailure = new Function(codeOnFailure);
            callbackOnFailure();
        }
        return false;
    }
}

/** TABS **/
function showTab($tabId) {
    // get parent container
    $tabContainer = $('button_' + $tabId).up('.tab_container');
    // loop thru buttons to set the active one
    $buttons = $tabContainer.down('.tab_button_container').childElements();
    for ($i = 0; $i < $buttons.length; $i++) {
        $button = $buttons[$i];
        if (('button_' + $tabId) == $button.id) {
            $button.addClassName('active');
        } else {
            $button.removeClassName('active');
        }
    }
    // loop thru contents to set the visible one
    $contents = $tabContainer.down('.tab_content_container').childElements();
    for ($i = 0; $i < $contents.length; $i++) {
        $content = $contents[$i];
        if (('content_' + $tabId) == $content.id) {
            $content.removeClassName('hidden');
        } else {
            $content.addClassName('hidden');
        }
    }
}

window.onload = function () {
    if  ($('showcase')) {
        $('showcase').show();
        // Set the height of the surrounding div according to the height of the first image
        var heightOfFirstImage = $('showcase_image_1').getHeight();
        $('showcase_images').style.height = heightOfFirstImage+'px';
        $('showcase_subject_block').style.height = heightOfFirstImage+'px';

        startShowcase();
    }
}


function startShowcase(){
    numberOfShowcaseItems = $$('li.showcase_subject').length;
    new PeriodicalExecuter(function(pe) {
        if (showcaseRunning) {
            showShowcaseItem(currentShowCase);
            if (currentShowCase == numberOfShowcaseItems) {
                currentShowCase = 1;
            } else {
                currentShowCase++;
            }
        }
    }, 3);
}
function showShowcaseItem(itemToShowcase) {
    //Hide all other items and remove the active class of <li>'s in the subject block
    for(var i = 1; i <= numberOfShowcaseItems; i ++) {
        if (i != itemToShowcase) {
            $('showcase_'+i).hide();
            $('showcase_subject_'+(i)).removeClassName('active');
        }
    }
    //Show current item
    $('showcase_'+(itemToShowcase)).show();
    $('showcase_subject_'+(itemToShowcase)).className = 'active';
    currentShowCase = itemToShowcase;
}

function pauseShowcaseAndShowItem(itemToShowcase){
    //Pause the showcase timer
    showcaseRunning = false;
    showShowcaseItem(itemToShowcase);
}
function continueShowcase() {
    showcaseRunning = true;
}


function feedback() {
    Modalbox.show($ROOT + 'feedback-popup', {
        title: 'Jouw mening doet ertoe!',
        width: 555
    });
    return false;
}

// posts a modalbox login form
function postFeedback() {
    //alert(Form.serialize('frm_feedback_popup'));
    // post it to a next modal box
    Modalbox.show($ROOT + 'feedback-popup', {
        title: 'Jouw mening doet ertoe!',
        params: Form.serialize('frm_feedback_popup')
    });
}
