jQuery.fn.initFirstPage = function(url) {

//    jQuery.fancybox('/registration/index', {
    jQuery.fancybox(url, {
        type: 'ajax',
        onComplete: function() {
            jQuery(this).initFormTrigger('initSecondPage');
        }
    });
}

jQuery.fn.initSecondPage = function() {
//    jQuery.fancybox('/registration/step2', {
    jQuery.fancybox('/registrierung/step2', {
        type: 'ajax' ,
        onComplete: function() {
            jQuery.each(jQuery('select'), function(key, sbox) {
                var other_field = jQuery('#other_' + jQuery(sbox).attr('id')).hide();

                jQuery(sbox).change(function() {
                    if(jQuery(sbox).val() == 'other') {
                        other_field.show();
                    } else {
                        other_field.hide();
                    }
                });
            });

            jQuery(this).initFormTrigger('initThirdPage');
        }
    });
}

jQuery.fn.initThirdPage = function() {
//    jQuery.fancybox('/registration/step3', {
    jQuery.fancybox('/registrierung/step3', {
        type: 'ajax',
        onComplete: function() {
            jQuery(this).initFormTrigger('initFourthPage');
        }
    });
}

jQuery.fn.initFourthPage = function() {
//    jQuery.fancybox('/registration/step4', {
    jQuery.fancybox('/registrierung/step4', {
        type: 'ajax',
        onComplete: function() {
            jQuery.each(jQuery('div.accordeon'), function(key, a_box) {
                jQuery(a_box).accordion({
                    header: 'h3', autoHeight: false
                });
            });
            jQuery(this).initFormTrigger('initFourthPage');
        }
    });
}

jQuery.fn.initFormTrigger = function(onFinish) {

    jQuery(".tab_menu a.daten").click(function(){
        return initAnyPage("initFirstPage('/registrierung/index')");
    });
    jQuery(".tab_menu a.steckbrief").click(function(){
        return initAnyPage("initSecondPage()");
    });
    jQuery(".tab_menu a.jobauswahl").click(function(){
        return initAnyPage("initThirdPage()");
    });
    jQuery(".tab_menu a.jobportal").click(function(){
        return initAnyPage("initFourthPage()");
    });
    
    jQuery('form').submit(function() {

        jQuery.ajax({
            type: jQuery(this).attr('method'),
            cache: false,
            url: jQuery(this).attr('action'),
            data: jQuery(this).serializeArray(),
            success: function(response) {
                if(response.error_fields) {
                    jQuery.each(jQuery('label.error'), function(key, label) {
                        jQuery(label).removeClass('error');
                    });
                    jQuery.each(response.error_fields, function(key, field) {
                        jQuery('label[for=user_' + field + ']').addClass('error');
                    });
                } else if(onFinish) {
                    eval('jQuery(this).' + onFinish + '()');
                }
            }
        });
        return false;
    });
}

function initAnyPage(onFinish){
        jQuery.ajax({
            type: jQuery('#fancybox-content form').attr('method'),
            cache: false,
            url: jQuery('#fancybox-content form').attr('action'),
            data: jQuery('#fancybox-content form').serializeArray(),
            success: function(response) {
                if(response.error_fields) {
                    jQuery.each(jQuery('label.error'), function(key, label) {
                        jQuery(label).removeClass('error');
                    });
                    jQuery.each(response.error_fields, function(key, field) {
                        jQuery('label[for=user_' + field + ']').addClass('error');
                    });
                } else if(onFinish) {
                    eval('jQuery(this).' + onFinish + '');
                }
            }
        });
        return false;
}

jQuery(document).ready(function() {

    jQuery('a#regist, a.fancybox').click(function() {
        jQuery(this).initFirstPage(jQuery(this).attr('href'));
//        jQuery(this).initFirstPage();
        return false;
    });

    jQuery(".fancy").fancybox(null, {
            type: 'ajax'
        }
    );
      
   initToggleStudentJobDetails('#joblist_table tr');

});


function initToggleStudentJobDetails(id){
    jQuery.each(jQuery('tr.joblist-row'), function(key, job_tr) {
        jQuery(this).initClickTrigger(jQuery(job_tr));
    });
}


jQuery.fn.initClickTrigger = function(tr_el) {
    tr_el.click(function(e) {
       var description = tr_el.after().next();
       description.toggle();
       description.find('a:first').focus();
    });
}


function popWindowApply4Job(i,a,url)
{
  var applyUrl = url + '/joblisting_popup.php?apply=';
  popurl = applyUrl + a +'&id='+i;
  popname = 'pop_'+i+'_'+a;
  window.open(popurl,popname,'left=0,top=0,toolbar=no,menubar=no,titlebar=no,location=no,status=0,scrollbars=1,width=900,height=870');
}


