

/*  Send Comment Data via Ajax */

function sendComment()

{

    

    formdata = $("#commentform").serialize();

    

    $.ajax({

       dataType: 'json',

       type:     'post',

       url:      'php/process_comments.php',

       data:     formdata,

       

       success: function(msg){

        

            if(msg !== 'error')

            {

              /* 5/16/10 experiment

                $(".signin").before("<div class='commentary'><img src='http://www.gravatar.com/avatar/"+msg['email']+"?s=60&d=wavatar'/><h4>"+msg['name']+" says:</h4><p>"+msg['ecomment']+"</p></div>");

                */

                

                /* 5/16/10 experiment */

                $(".signin").before("<div class='commentary'><h4>"+msg['name']+" says:</h4><p>"+msg['ecomment']+"</p></div>");                

                

                $(".signin").html("Your comment has been processed successfully and should be viewable for all the internet to fawn over.<br/>Congratulations.");

                

            }

            else if(msg == 'error')

            {

                $(".signin").html("Oops. Something flagged your IP and/or you're behind a proxy. We're not allowed to proceed at the moment.");

                

            }

            else

            {

                $(".signin").append("<h4>Whoops! Something screwy happened. Please try again. If you recieve this message a 2nd time, you may wish to come back at a later time</h4><a href='javascript: window.location.reload();'>Try Again</a>");

                

            }

       } 

        

    });

    

    

}

/*
    
    up = 38
    down = 40
    enter = 13
    
*/

var selectCount = -1;
function initSuggestSelect(theKey){
   
    var kcode = theKey.keyCode;
 
    if(kcode == 40 &&  selectCount < $("#suggestList li").size()-1){
        selectCount ++;
        
    } else if(kcode == 38 &&  selectCount > 0){
        selectCount --;
        
    } else if(kcode == 13){
        
        if(selectCount < 0 || selectCount > $('#suggestList li').size()){
            $("#suggestionList").html("");
            $("#suggestions").css('display', 'none');
            $('#subcontent form').submit();
            
  
        }else{
            window.location = $('#suggestList li:eq('+selectCount+') a').attr('href');
        }
    }  
 
    $('#suggestList li').removeClass('over');
    $('#suggestList li:eq('+selectCount+')').addClass('over');
   
    
}





            

/* initialize basic navigation methods on page load */   

$(document).ready(function(){

    

    $(".logo").click(function(){window.location.href = "http://www.prettyandstupid.com/"});

    

    $('.menu li').click(function(){

       

        $('.menu li').removeClass('selected');

        $(this).addClass('selected');

        

    });

               

    $('#idiot_list').change(function(){

        window.location = "http://www.prettyandstupid.com/idiot/"+$(this).val();

    }); 

             



    $('.scrolltoplink').click(function() {

        $('html, body').animate({scrollTop : 0}, 500);



    });

  /*

    $("#commentform").liveValidate();

  */

    $("#commentform").submit(function(){sendComment(); return false;});


 /*

    Star Rating 

  

 */  


         $(".starRating").click(function(){


            var ratingForm = $(this).parent().parent().parent();
 

            var rl         = $(this).parent().parent();
            

            /* reset "clicked" class

            */

            $("li", rl).removeClass("ratingClicked");

            $("li", rl).removeClass("rating");



            /* apply "clicked" class

            */          

            var tempList   = $("input", rl).index(this);

            

            $("li:lt("+(tempList+1)+")", rl).addClass("ratingClicked");
  

            $.ajax({


                url:        "php/process_ratings.php",

                dataType:   "json",

                type:       "post",

                data:       ratingForm.serialize(),


                success: function(msg){


                    /*alert("Was Rating Sent?  "+msg['result']);

                    */

               }                               

            });

        });

        


        $(".ratingList li").mouseover(function(){  

          var rl = $(this).parent();


          var tempList = $("li", rl).index(this);


          $("li:lt("+(tempList+1)+")", rl).addClass("starOn");


        });

            

        $(".ratingList li").mouseout(function(){

          $(".ratingList li").removeClass("starOn");

        });
        
        
        $('input[name$="search"]').attr('autocomplete', 'off');
        $('input[name$="search"]').keyup(function(e){

           
           if(e.keyCode !== 38 && e.keyCode !== 40){ 
               var searchInput  = this.value;
               
               if(this.value == ""){
                  $("#suggestionList").html("");
                  $("#suggestions").css('display', 'none');
                  return false;
                
               } 
               
               $.ajax({
                
                    url: "php/search_suggest.php",
                    dataType: "json",
                    type: "post",
                    data: "search="+searchInput,
                    
                    success: function(msg){
                        selectCount = -1;
                        $("#suggestList").html("");
                        $("#suggestions").css('display', 'block');
                        
                        if(msg.length < 1){
                          $("#suggestions").css("display", "none");
                        } else{
                            for(i=0; i<msg.length; i++){
                                
                                $("#suggestList").append("<li onmousedown='window.location =\"idiot/"+msg[i]['idiot_id']+"\"'><img width=25 height=31 src='img/headshots/"+msg[i]['picture']+"'/><a href='idiot/"+msg[i]['idiot_id']+"'>"+msg[i]['f_name']+" "+msg[i]['m_name']+" "+msg[i]['l_name']+"</a></li>");
                                
                            }
                        }
                        
                        
                    }
                
               }); 
            
           }
           
           $('input[name$="search"]').blur(function(){
                  $("#suggestionList").html("");
                  $("#suggestions").css('display', 'none');
                
           });
                          
           initSuggestSelect(e);
      
        });
        



});
