/**
 * @author PresTeamShop.com
 * @copyright PresTeamShop.com - 2012
 */
 
var limitCharacters = 140;

$(function(){    
    $('#btnDisplayComment').click(function(){
        displayCommentIfeedback();
    });

    if (ifeedback_by_email){
        if (ifb_allow_guest || is_logged){
            displayCommentIfeedback(); 
        }else{
            if (!is_logged)
                alert(msg_no_logged);
        }
    }
});
 
function newCaptcha()
{
    if (ifb_enable_captcha){
        var c_currentTime = new Date();
        var c_miliseconds = c_currentTime.getTime();
        
        document.getElementById('imgCaptcha').src = ifeedback_dir + 'image.php?x='+ c_miliseconds;
    }
}

function cleanForm(){
    $('#dialog_ifeedback_comment #textComment').attr('disabled','disabled');
    $('#dialog_ifeedback_comment #txtValStar').val('');
    $('#dialog_ifeedback_comment #textComment').val('');
    $('#dialog_ifeedback_comment #span_valstar').empty();
    $('#dialog_ifeedback_comment #li_current_rating').removeAttr('style');
    $('#dialog_ifeedback_comment #textCaptcha').val('');
    $('#dialog_ifeedback_comment #divErrorCaptcha').empty();
    $('#dialog_ifeedback_comment #ifb_characters').html(limitCharacters);        
}

function saveRating(){
    var rate    = $('#dialog_ifeedback_comment #txtValStar').val();
    var comment = $('#dialog_ifeedback_comment #textComment').val();

    if (ifb_enable_captcha){   
        var captcha = $('#dialog_ifeedback_comment #textCaptcha').val();

        if (comment != undefined && rate != undefined && captcha != undefined){
            $.ajax({
               type: 'POST',
               url: ifeedback_dir + 'actions.php',
               async: true,
               cache: false,
               dataType : 'html',
               data: 'action=validateCaptcha&captcha=' + captcha,
               success: function(Data)
               {                 
                    if (Data == "1"){
                        cleanForm();
                        
                        $.ajax({
                           type: 'POST',
                           url: ifeedback_dir + 'actions.php',
                           async: true,
                           cache: false,
                           dataType : 'html',
                           data: 'action=saveRating&rate=' + rate + '&comment=' + comment,
                           beforeSend: function()
                           {
                                $("#div_loading").empty().html('<center><img src="img/loader.gif" /></center>');
                           },
                           success: function(Data)
                           {
                                $("#div_loading").empty().html('<p style="text-align:center;">'+Data+'</p>');   
                                                                
                           },
                           error: function(XMLHttpRequest, textStatus, errorThrown) {alert("TECHNICAL ERROR: unable to save rating \n\nDetails:\nError thrown: " + XMLHttpRequest + "\n" + 'Text status: ' + textStatus);}
                       });
                    }else{            
                        $("#div_loading").empty().html(Data);
                        $('#dialog_ifeedback_comment #textCaptcha').val('');                      
                        newCaptcha();
                    }                                 
               },
               error: function(XMLHttpRequest, textStatus, errorThrown) {alert("TECHNICAL ERROR: unable validate captcha \n\nDetails:\nError thrown: " + XMLHttpRequest + "\n" + 'Text status: ' + textStatus);}
           });
       }   
   }else{
        if (comment != undefined && rate != undefined){
            cleanForm();
            
            $.ajax({
               type: 'POST',
               url: ifeedback_dir + 'actions.php',
               async: true,
               cache: false,
               dataType : 'html',
               data: 'action=saveRating&rate=' + rate + '&comment=' + comment,
               beforeSend: function()
               {
                    $("#div_loading").empty().html('<center><img src="img/loader.gif" /></center>');
               },
               success: function(Data)
               {
                    $("#div_loading").empty().html('<p style="text-align:center;">'+Data+'</p>');                                  
               },
               error: function(XMLHttpRequest, textStatus, errorThrown) {alert("TECHNICAL ERROR: unable to save rating \n\nDetails:\nError thrown: " + XMLHttpRequest + "\n" + 'Text status: ' + textStatus);}
           });
       }
    }
}

function viewRating(page){
    var filter = $('#lstOrderBy').val();
    var star = $('#txtStar').val();
    if (page != undefined && star != undefined){        
        $.ajax({
           type: 'POST',
           url: ifeedback_dir + 'actions.php',
           async: false,
           cache: false,
           dataType : 'html',
           data: 'action=viewRating&page=' + page + '&star=' + star + '&filter=' + filter,
           beforeSend: function()
           {
                $("#ifeedback_rating").empty().html('<center><img src="img/loader_big.gif" /></center>');
           },
           success: function(Data)
           {
                $("#ifeedback_rating").empty().html(Data);                                   
           },
           error: function(XMLHttpRequest, textStatus, errorThrown) {alert("TECHNICAL ERROR: unable to view rating \n\nDetails:\nError thrown: " + XMLHttpRequest + "\n" + 'Text status: ' + textStatus);}
       }); 
       $('#txtStar').val(star); 
       $('#lstOrderBy').val(filter);      
   }
}

function displayComment(valStar){
    $('#tableContenComment').show('slow');
    $('#dialog_ifeedback_comment #textComment').removeAttr('disabled');
    if (!ifb_enable_captcha)
        $('#dialog_ifeedback_comment #btnSave').removeAttr('disabled').removeClass('button_disabled').addClass('button');
    
    $('#li_current_rating').attr('style','width:'+(valStar*star_width)+'px'); 
    
    $('#dialog_ifeedback_comment #img_arrow').attr('style','display:none;');
    $('#dialog_ifeedback_comment #txtValStar').val(valStar);
    $('#dialog_ifeedback_comment #span_valstar').html(valStar);
    $('#dialog_ifeedback_comment #textComment').focus();
    $("#div_loading").empty();
}

function displayCommentIfeedback(){      
    $.ajax({
       type: 'POST',
       url: ifeedback_dir + 'actions.php',
       async: false,
       cache: false,
       dataType : 'html',
       data: 'action=viewComment',
       success: function(html)
       {                 
            $('body').append('<div class="ui-widget-overlay" style="width: '+$(document).width()+'px; height: '+$(document).height()+'px; z-index: 1001;"></div>');
            $('body').append(html); 
            
            $('#dialog_ifeedback_comment').css({
               position:'absolute',
               left: ($(window).width() - $('#dialog_ifeedback_comment').outerWidth())/2,
               top: ($(window).height() - $('#dialog_ifeedback_comment').outerHeight())/2
            });   
            
            cleanForm();    
            $('#dialog_ifeedback_comment #textComment').attr('disabled','disabled');
            $('#dialog_ifeedback_comment #btnSave').attr('disabled','disabled').addClass('button_disabled').attr('style','display : inline; float : none');        
            $('#dialog_ifeedback_comment #img_arrow').attr('style','display:block;float:left;');
            $('#div_loading').empty();        
                   
            window.scrollTo(0, 0);
            $(window).resize();
            
            newCaptcha();                  
       },
       error: function(XMLHttpRequest, textStatus, errorThrown) {alert("TECHNICAL ERROR: unable view comment \n\nDetails:\nError thrown: " + XMLHttpRequest + "\n" + 'Text status: ' + textStatus);}
   });                  
}

function hideCommentIfeedback(){      
    cleanForm();     
    $('#tableContenComment').hide();      
    $('#dialog_ifeedback_comment').remove();
    $('body .ui-widget-overlay').remove();
}
