/*
 *      PublicPollMain.js
 *      Main js file for public form
 *      Date : 17.03.2010
 *      Author : Djordje Zeljic
 */
var current;
PGoat.msg = {};
PGoat.msg.show = 'Show results';
PGoat.msg.hide = 'Hide results';
PGoat.state = {};
PGoat.state.rq = 0;
$(document).ready(function(){

    // load png fix plugin
    $(function(){
        $(document).pngFix();
    });

    /*
     *      Show info about poll author
     */
    $('#showauthor a').bind('click',function(){
        var authordiv = $('#author');
        if(authordiv.is(':visible')){
            authordiv.fadeOut('slow');
            $(this).css({
                'background':''
            });
        }else{
            authordiv.fadeIn('slow');
            $(this).css({
                'background':'#212121'
            });
        }
        return false;
    });

    /*
     *          Mouse hover on button
     */
    $('#submitbtn').mouseover(function(){
        $(this).css({
            'cursor':'pointer'
        });
    });
    
    $('#submitbtn').mouseout(function(){
        $(this).css({
            'cursor':''
        });
    });

    // load default value of button
    current = $('#submitbtn').val();


    $('.onequestion ul li').mouseover(function(){
        if(!$(this).find('input').is(':checked')){
            $(this).css({
                'background-color':PGoat.hexColorNoChecked
            });
            $(this).find('*').css({
                'cursor':'pointer'
            });
        }
    });

    $('.onequestion ul li').mouseout(function(){
        if(!$(this).find('input').is(':checked')){
            $(this).css({
                'background-color':''
            });
        }
        $(this).find('*').css({
            'cursor':'pointer'
        });
    });

    $('.onequestion ul li label').bind('click',function(){
        $(this).parent().parent().find('li').css({
            'background-color':''
        });
        $(this).parent().css({
            'background-color':PGoat.hexColorLabel
        });
    });

    /*
     *      hover on .onequestion
     */
    $('.onequestion').mouseover(function(){
        $(this).css({
            'background-color':PGoat.hexMouseOver
        });
    });

    $('.onequestion').mouseout(function(){
        $(this).css({
            'background-color':''
        });
    });
    
    //      SUBMIT FORM
    $('#mainform').live('submit',function(){
        PGoat.indicator('show','fast');
        PGoat.state.rq = 0;
        $('#results').hide();
        $.ajax({
            type : 'POST',
            url : '/public/poll/submit',
            dataType : 'json',
            data : $(this).serialize(),
            
            complete: function(){
                PGoat.indicator('hide','fast');  
            },
            
            success : function(data){
                if(data.success == 'success'){
                    PGoat.info('green','<h1>'+PGoat.afterMessage+'</h1>','show');
                    $('#mainform').fadeOut('fast',function(){
                        $('#mainform').remove();
                    });
                                
                    if(PGoat.showResults)
                        PGoat.loadResults();
                    
                }else{
                    if(data.errortype == 'voted'){
                        PGoat.info('red','<h1>All right</h1>'+data.message,'show');
                        $('#mainform').fadeOut('fast',function(){
                            $('#mainform').remove();
                        });
                            
                        if(PGoat.showResults)
                            PGoat.loadResults();
    
                    }
                    else if(data.errortype == 'no_answer'){
                        PGoat.info('red','<h1>Please complete the form below.</h1>'+data.message,'show');
                    }
                    else if(data.errortype == 'preview'){
                        PGoat.info('red','<h1>Hi member :)</h1>'+data.message,'show');
                        $('#mainform').fadeOut('fast',function(){
                            $('#mainform').remove();
                        });
                        if(PGoat.showResults)
                            PGoat.loadResults();
                    }
                }
            },
            error: function(){
                PGoat.info('red','<h1>System error. Please <a href="/public/contactus">contact us.</a></h1>','show');
            }
        });
    });
    
    // show results
    $('.show_results_link').live('click',function(){
        
        var state = ($(this).text() == PGoat.msg.show) ? 'show' : 'hide';
        if(state == 'show'){
            PGoat.loadResults();
            $(this).text(PGoat.msg.hide);
        }else{
            $('#results').fadeOut('slow');
            $(this).text(PGoat.msg.show);
        }
    });
});

PGoat.indicator = function (to, speed){
    if(to == null)
        to = 'show';
    
    if(speed == null)
        speed = 'normal';
    
    if(to == 'show')
        $('#submitbtn').val('Processing, please wait...');
    
    if(to == 'hide')
        $('#submitbtn').val(current);
    
}

PGoat.loadResults = function loadResults(){
    if(PGoat.state.rq == 1){
        $('#results').fadeIn('slow');
        return;
    }
    
    $('#lindicator').show();
    
    $.ajax({
        type: 'POST',
        url: '/public/poll/loadresults',
        dataType: 'json',
        data: {
            project_id:PGoat.projectID
        },
        complete: function(){
            $('#lindicator').hide();
        },
        success: function(data){
            if(data.success == 'success'){
                $('#rtables').html(data.html);
                $('#results').fadeIn('slow');
                $(".tablesorter").tablesorter({
                    widthFixed: true,
                    widgets: ['zebra'],
                    sortList: [[2,1]],
                    headers: {
                        1: {
                            sorter: false
                        }
                    }
                });
                $.scrollTo( '#results', 500, {
                    offset:{
                        top:-10
                    }
                });
            }else{
                PGoat.info('red','<h1>System error. Please <a href="/public/contactus">contact us.</a></h1>','show');
            }
            PGoat.state.rq = 1;
        },
        error: function(){
            PGoat.info('red','<h1>System error. Please <a href="/public/contactus">contact us.</a></h1>','show');
        }
    });
}

PGoat.info = function (type,message,to){
    if(to === null) to = 'show';
    
    var infomess = $('#infomessage');
    
    var colors = {};
    
    colors.green = PGoat.greed;
    colors.red = PGoat.red;
    
    infomess.css({
        'border-left-color':colors[type]
    });
        
    if(message !== null)
        infomess.html(message);
    else{
        infomess.html(' ');
        infomess.hide();
    }
    
    if(to == 'show')
        infomess.fadeIn('normal');
    
    if(to == 'hide')
        infomess.fadeOut('normal');
    
    // scroll to info message
    $.scrollTo( '#infomessage', 500, {
        offset:{
            top:-10
        }
    });
}
