/* 
 *  © Chris How, Primesolid 2011
 *  All rights reserved.
 */

var surveyTabShowing = false;
var surveyTabAnimationSpeed = 'fast';
var $survey = $('#survey');

$(window).bind('load', function() {
    //    var $survey = $('#survey');
    if($survey.length > 0) {
        initSurvey();
    } else {
        initSurveyInvite();
    }
});


function initSurvey() {
    // initial settings
    if($survey.find('form input[name=data[venue_location]]:checked').val() != 'other') {
        $('#venue_location_other').css('visibility', 'hidden'); 
    }
    if($survey.find('form input[name=data\[club_frequency\]]::checked').val() != 'other') {
        $('#club_frequency_other').css('visibility', 'hidden');        
    }
    if($survey.find('form input[name=data\[volunteers_active_on_craft_club_website\]]:checked').val() != 'no') {
        $('#why_volunteers_not_active_on_website_div').hide();
    }
    if($survey.find('form input[name=data\[pupils_active_on_craft_club_website\]]:checked').val() != 'no') {
        $('#why_pupils_not_active_on_website_div').hide();
    }
    if($survey.find('form input[name="data[crafts_explored][other]"]:checked').length == 0) {
        $('#crafts_explored_other').css('visibility', 'hidden');
    }
    if($survey.find('form input[name=data\[volunteers_signed_agreement]]:checked').val() == 'yes') {
        $('#volunteers_signed_agreement_prompt').hide();
    }
    if($survey.find('form input[name=data\[added_to_map]]:checked').val() == 'yes') {
        $('#added_to_map_prompt').hide();
    }
    
    $survey.find('form input').bind('change', function(){
        var $this = $(this);
        var id = $this.attr('id');
        var name = $this.attr('name');
        var value = $this.val();
//        log('id: ' + id);
//        log('name: ' + name);
//        log('value: ' + value);
        
        switch(name) {
            case 'data[venue_location]':
                if(value == 'other') {
                    $('#venue_location_other').css('visibility', 'visible');
                } else {
                    $('#venue_location_other').css('visibility', 'hidden');
                }
                break;
            case 'data[club_frequency]':
                if(value == 'other') {
                    $('#club_frequency_other').css('visibility', 'visible');
                } else {
                    $('#club_frequency_other').css('visibility', 'hidden');
                }
                break;
            case 'data[volunteers_active_on_craft_club_website]':
                if(value == 'no') {
                    $('#why_volunteers_not_active_on_website_div').show();
                } else {
                    $('#why_volunteers_not_active_on_website_div').hide();
                }
                break;
            case 'data[pupils_active_on_craft_club_website]':
                if(value == 'no') {
                    $('#why_pupils_not_active_on_website_div').show();
                } else {
                    $('#why_pupils_not_active_on_website_div').hide();
                }
                break;
            case 'data[crafts_explored][other]':
                if($this.attr('checked')) {
                    $('#crafts_explored_other').css('visibility', 'visible');
                } else {
                    $('#crafts_explored_other').css('visibility', 'hidden');
                }
                break;
            case 'data[volunteers_signed_agreement]':
                if(value == 'no') {
                    $('#volunteers_signed_agreement_prompt').show();
                } else {
                    $('#volunteers_signed_agreement_prompt').hide();
                }
                break;
            case 'data[added_to_map]':
                if(value == 'no') {
                    $('#added_to_map_prompt').show();
                } else {
                    $('#added_to_map_prompt').hide();
                }
                break;
        }
    });
    
}



function initSurveyInvite() {
    var tabState = readCookie('showSurveyInvite');
    log('tabState: ' , tabState);
    
    if(typeof SURVEY_USER_HAS_SAID_NO !== 'undefined' && SURVEY_USER_HAS_SAID_NO === 1) {
        return;
    }
    
    
    if(tabState === null || tabState === '1') {
        setTimeout(function() {
            //            IEPNGFix.update();
            showSurveyInvite();
        }, 500);            
    }
}


function showSurveyInvite() {
    var $inviteTab = $('#survey_invite');
    $inviteTab.animate({
        left: 0
    }, surveyTabAnimationSpeed, 'swing', function() {
        surveyTabShowing = true;
    });    
}

function hideSurveyInvite() {
    var $inviteTab = $('#survey_invite');
    $inviteTab.animate({
        left: -568
    }, surveyTabAnimationSpeed, 'swing', function() {
        surveyTabShowing = false;
    });    
}

function surveyActionNo() {
    hideSurveyInvite();
    createCookie('showSurveyInvite',0);
    $.ajax({
        url: '/survey_support/user_does_not_want_survey.php',
        cache: false,
        data: {user_id: SURVEY_USER_ID},
        dataType: "text",
        success: function(data) {
            log('surveyActionNo success');
            log(data);
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            log('surveyActionNo AJAX error');
        }
    });		
}


function surveyActionYes() {
//    alert("User now goes to survey page...\n\n[now resetting cookie...]");
    createCookie('showSurveyInvite',1);
    window.location = '/survey';
}

function surveyActionMaybe() {
    hideSurveyInvite();
    createCookie('showSurveyInvite',0);
}

function surveyTabToggle() {
    if(surveyTabShowing) {
        hideSurveyInvite();
    } else {
        showSurveyInvite();
    }
}
