/**
 *	\file  
 *	\brief JS File, Matching Functions JS File of this service. 
 *	No more Details. 
*/ 

/**
 * @note very heavy under construction
 * @author (last change) hs
 * @version 0.2
 * @date Feb 2009
 */
 
/**
 *	\package js.app.matching_js_log
 *	\brief The Javascript File of matching functions
*/ 
//* package js.app.global; 
var matching_me_fit_test_numbers = new Array(1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15,16,17,18,19,20);
var matching_me_fit_test_questions = new Array();
matching_me_fit_test_questions[0] = new Array('0','Was beschreibt Dich besser?',
'Es ist ganz normal, dass Beschreibungen von beiden Seiten zutreffen. Entscheide intuitiv und ohne lange nachzudenken. Wähle einfach spontan den Punkt aus, der am ehesten zutrifft.','Ich plane meinen Alltag gerne im voraus','Ich lasse die Dinge einfach auf mich zukommen');
matching_me_fit_test_questions[1] = new Array('1','Noch eine weitere Frage?','Frage 2','Antwort 1','Antwort 2');
var matchingTestResultHandelnCookieName = COOKIE_NAME_MATCHING_ME_HANDELN;
var matchingTestResultDenkenCookieName = COOKIE_NAME_MATCHING_ME_DENKEN;
var matchingTestResultEnscheidenCookieName = COOKIE_NAME_MATCHING_ME_ENTSCHEIDEN;
var matchingTestResultLebenCookieName = COOKIE_NAME_MATCHING_ME_LEBEN;
var matchingTestResultHandelnOneLetterCookieName = 'fas_handeln_one';
var matchingTestResultDenkenOneLetterCookieName = 'fas_denken_one';
var matchingTestResultEnscheidenOneLetterCookieName = 'fas_entscheiden_one';
var matchingTestResultLebenCookieOneLetterName = 'fas_leben_one';
var matchingTestResultCodeCookie = 'fas_result_code';

/*-----------------------------------------

    Matching me - Settings

----------------------------------------*/
$(document).ready(function(){
	Array.prototype.shuffle =arrayShuffle;
});

function setMatchingMeBtn_go_fit_test_obj(){
    $('#btn_go_fit_test_obj').click(function(){
        matching_me_fit_test_numbers.shuffle();
        for(var i=0;i<matching_me_fit_test_numbers.length;i++){
            $('#qnum_'+(i+1)).html(matching_me_fit_test_numbers[i]);
        }
        
        $('.matching_me_whoiam_question_ec').removeClass('matching_me_whoiam_question_invisible_dc')
        .addClass('matching_me_whoiam_question_invisible_dc');
        
        $('.matching_me_whoiam_question_ec').find('.q_'+trimstring($('#qnum_1').html())).parent()
        .removeClass('matching_me_whoiam_question_invisible_dc').find('.num_question_ec').html('1');
        
        $('.matching_me_whoiam_question_ec').find('.q_'+trimstring($('#qnum_1').html())).parent()
        .find('.numsum_question_ec').html(matching_me_fit_test_numbers.length);
        
        $('.matching_me_whoiam').hide();
        
        var nextNum;
        
        $('.qresult_ec').click(function(event){
           //check box 1
           //$(this).removeClass('matching_me_check_regular_dc').addClass('matching_me_check_active_dc');
           if($(this).hasClass('qresult_1')){
               var q_num = $(this).parent().parent().find("div[class^='q_']").attr('class').split('_')[1];
               $("div[id^='resultnum_']").each(function(){
                   if(trimstring($(this).html())=='0'){
                       $(this).html('1');
                       set_counter_from_questions(q_num,'1');
                       set_sum_question_counter();
                       $("div[id^='resultnum_']").each(function(){
                          if(trimstring($(this).html())=='0'){
                            nextNum = $(this).attr('id').split('_')[1];
                            timeout_effect_nextquestion(1,event.target,nextNum);
                            return false;
                          }
                       });
                       return false;
                   }
               });
               return false;
           }
           else if($(this).hasClass('qresult_2')){
               //check box 2
               var q_num = $(this).parent().parent().find("div[class^='q_']").attr('class').split('_')[1];
               $("div[id^='resultnum_']").each(function(){
                   if(trimstring($(this).html())=='0'){
                       $(this).html('2');
                       set_counter_from_questions(q_num,'2');
                       set_sum_question_counter();
                       $("div[id^='resultnum_']").each(function(){
                          if(trimstring($(this).html())=='0'){
                            nextNum = $(this).attr('id').split('_')[1];
                            timeout_effect_nextquestion(2,event.target,nextNum);
                            return false;
                          }
                       });
                       return false;
                   }
               });
               return false;
           }
        });
        return false;
    });
}

function timeout_effect_nextquestion(num_clicked,obj,nextNum){
    $('.matching_me_whoiam_question_describe_dc').fadeOut("slow");
    $(obj).removeClass('matching_me_check_regular_dc').addClass('matching_me_check_active_dc');
    $(obj).parent().fadeOut().animate({left: '250'}, "slow");
    if(num_clicked==1){
        $(obj).parent().next().fadeOut().animate({left: '250'}, "slow");
    }else{
        $(obj).parent().prev().fadeOut().animate({left: '250'}, "slow");
    }
   
    
    window.setTimeout("get_next_question("+nextNum+")",700);
    
    return false;
}

function get_next_question(nextNum){
	//$('.matching_me_whoiam_question_ec').hide();
    //$('.q_result_ec').hide('slow');
    $('.matching_me_whoiam_question_describe_dc').fadeIn('fast');
    $('.matching_me_whoiam_question_ec').removeClass('matching_me_whoiam_question_invisible_dc')
    .addClass('matching_me_whoiam_question_invisible_dc');
    
    $('.matching_me_whoiam_question_ec').find('.q_'+trimstring($('#qnum_'+nextNum).html())).parent()
    .removeClass('matching_me_whoiam_question_invisible_dc').find('.num_question_ec').html(nextNum);
    $('.matching_me_whoiam_question_ec').find('.q_'+trimstring($('#qnum_'+nextNum).html())).parent()
    .find('.numsum_question_ec').html(matching_me_fit_test_numbers.length);
 	
 	return false;
}

function set_counter_from_questions(qNum,answerVal){

    if(parseInt(qNum)>=1 && parseInt(qNum)<=5){
        //E or I
        if(answerVal=='1'){
            var n = parseInt($('#qnum_e_counter').html())+1;
            $('#qnum_e_counter').html(n);
            return false;
        }
        if(answerVal=='2'){
            var n = parseInt($('#qnum_i_counter').html())+1;
            $('#qnum_i_counter').html(n);
            return false;
        }
    }
    
    if(parseInt(qNum)>=6 && parseInt(qNum)<=10){
        //P or T
        if(answerVal=='1'){
            var n = parseInt($('#qnum_p_counter').html())+1;
            $('#qnum_p_counter').html(n);
            return false;
        }
        if(answerVal=='2'){
            var n = parseInt($('#qnum_t_counter').html())+1;
            $('#qnum_t_counter').html(n);
            return false;
        }
    }
    
    if(parseInt(qNum)>=11 && parseInt(qNum)<=15){
        //L or F
        if(answerVal=='1'){
            var n = parseInt($('#qnum_l_counter').html())+1;
            $('#qnum_l_counter').html(n);
            return false;
        }
        if(answerVal=='2'){
            var n = parseInt($('#qnum_f_counter').html())+1;
            $('#qnum_f_counter').html(n);
            return false;
        }
    }
    
    if(parseInt(qNum)>=16 && parseInt(qNum)<=20){
        //G or S
        if(answerVal=='1'){
            var n = parseInt($('#qnum_g_counter').html())+1;
            $('#qnum_g_counter').html(n);
            return false;
        }
        if(answerVal=='2'){
            var n = parseInt($('#qnum_s_counter').html())+1;
            $('#qnum_s_counter').html(n);
            return false;
        }
    }
    
    
}

function set_sum_question_counter(){
    var n = parseInt($('#qnum_sum_counter').html())+1;
    $('#qnum_sum_counter').html(n);
    if(n==20){
        finish_Matching_Me_Fit_Test();
    }
    
}


function finish_Matching_Me_Fit_Test(){
    $('.matching_me_whoiam_question_ec').removeClass('matching_me_whoiam_question_invisible_dc')
    .addClass('matching_me_whoiam_question_invisible_dc');
    var e = parseInt($('#qnum_e_counter').html());
    var i = parseInt($('#qnum_i_counter').html());
    var p = parseInt($('#qnum_p_counter').html());
    var t = parseInt($('#qnum_t_counter').html());
    var l = parseInt($('#qnum_l_counter').html());  
    var f = parseInt($('#qnum_f_counter').html());  
    var g = parseInt($('#qnum_g_counter').html());  
    var s = parseInt($('#qnum_s_counter').html());  
    var resultCode = '';
    
    if(e>i){
        $('#qnum_ei_factor').html('E');
        resultCode = resultCode + 'E';
    }else{
        $('#qnum_ei_factor').html('I');      
        resultCode = resultCode + 'I';  
    }

    if(p>t){
        $('#qnum_pt_factor').html('P');
        resultCode = resultCode + 'P';
    }else{
        $('#qnum_pt_factor').html('T'); 
        resultCode = resultCode + 'T';       
    }
    
    if(l>f){
        $('#qnum_lf_factor').html('L');
        resultCode = resultCode + 'L';
    }else{
        $('#qnum_lf_factor').html('F');    
        resultCode = resultCode + 'F';    
    }

    if(g>s){
        $('#qnum_gs_factor').html('G');
        resultCode = resultCode + 'G';
    }else{
        $('#qnum_gs_factor').html('S');  
        resultCode = resultCode + 'S';      
    }
    
    $('#qnum_result_code').html(resultCode);
    
    $('#result_matrix_obj').hide();
    $('#result_matrix_counter_obj').hide(); 
    $('#result_factor_matrix_obj').hide(); 
    $('#result_factor_resultcode_obj').hide(); 
    
    //save values
    Set_Cookie(matchingTestResultHandelnCookieName,'E'+e+'I'+i);
    Set_Cookie(matchingTestResultDenkenCookieName,'P'+p+'T'+t);
    Set_Cookie(matchingTestResultEnscheidenCookieName,'L'+l+'F'+f);
    Set_Cookie(matchingTestResultLebenCookieName,'G'+g+'S'+s);
    
    Set_Cookie(matchingTestResultHandelnOneLetterCookieName,resultCode[0]);
    Set_Cookie(matchingTestResultDenkenOneLetterCookieName,resultCode[1]);
    Set_Cookie(matchingTestResultEnscheidenOneLetterCookieName,resultCode[2]);
    Set_Cookie(matchingTestResultLebenCookieOneLetterName,resultCode[3]);
    
    Set_Cookie(matchingTestResultCodeCookie,resultCode);
    
        
    setSubHandler_matching_me_result_site();
    return false;
}

function setSubHandler_matching_me_result_site(){
	var resultCodeVal =  Get_Cookie(matchingTestResultCodeCookie);
	window.location.href = LOAD_MATCHING_ME_RESULT+'/'+resultCodeVal;
	return false;
}

function setSubHandler_matching_me_get_result(){

    var handelnVal = Get_Cookie(matchingTestResultHandelnCookieName);
    var denkenVal = Get_Cookie(matchingTestResultDenkenCookieName);
    var entscheidenVal = Get_Cookie(matchingTestResultEnscheidenCookieName);
    var lebenVal = Get_Cookie(matchingTestResultLebenCookieName);
    
    var handelnOneLetterVal = Get_Cookie(matchingTestResultHandelnOneLetterCookieName);
    var denkenOneLetterVal = Get_Cookie(matchingTestResultDenkenOneLetterCookieName);
    var entscheidenOneLetterVal = Get_Cookie(matchingTestResultEnscheidenOneLetterCookieName);
    var lebenOneLetterVal = Get_Cookie(matchingTestResultLebenCookieOneLetterName);
    
    var resultCodeVal =  Get_Cookie(matchingTestResultCodeCookie);
   
    $('.handeln_ec').addClass('matching_me_result_test_'+handelnVal+'_dc');
    $('.denken_ec').addClass('matching_me_result_test_'+denkenVal+'_dc');
    $('.entscheiden_ec').addClass('matching_me_result_test_'+entscheidenVal+'_dc');    
    $('.leben_ec').addClass('matching_me_result_test_'+lebenVal+'_dc');      
    $('.oneletter_handeln_ec').addClass('matching_me_sign_'+handelnOneLetterVal+'_dc');
    $('.oneletter_denken_ec').addClass('matching_me_sign_'+denkenOneLetterVal+'_dc');
    $('.oneletter_entscheiden_ec').addClass('matching_me_sign_'+entscheidenOneLetterVal+'_dc');
    $('.oneletter_leben_ec').addClass('matching_me_sign_'+lebenOneLetterVal+'_dc');
    $('#result_code_obj').html(resultCodeVal);
    
    Delete_Cookie(matchingTestResultHandelnCookieName);
    Delete_Cookie(matchingTestResultDenkenCookieName);
    Delete_Cookie(matchingTestResultEnscheidenCookieName);
    Delete_Cookie(matchingTestResultLebenCookieName);

    Delete_Cookie(matchingTestResultHandelnOneLetterCookieName);
    Delete_Cookie(matchingTestResultDenkenOneLetterCookieName);
    Delete_Cookie(matchingTestResultEnscheidenOneLetterCookieName);
    Delete_Cookie(matchingTestResultLebenCookieOneLetterName);
    
    Delete_Cookie(matchingTestResultCodeCookie);
 
    return false;
}


function arrayShuffle(){
  var tmp, rand;
  for(var i =0; i < this.length; i++){
    rand = Math.floor(Math.random() * this.length);
    tmp = this[i]; 
    this[i] = this[rand]; 
    this[rand] =tmp;
  }
}


