/*  
 * Project: QShout - Shoutbox Widget for jQuery
 * Version: v1.1.7 (27/09/‎2010, ‏‎10:11 PM)
 * URL    : http://qshout.borisding.com
 * License: MIT  (http://www.opensource.org/licenses/mit-license.php)
 *          Copyright (c) 2010, Boris Ding P H
 */

(function($){
      //public QShout
      $.QShout = function( uSettings ){                   
      //default settings    
       settings = {
        //misc.
        qsId        : "qsId",
        serverPage  : "/templates/QShout/class_qshout.php",
        loader      : "Loading...",
        //timer       : 60000,
        maxRow      : 30,
        //shoutbox's body
        bwidth      : "745px",
        bheight     : "210px",
        bBorderWidth: "0px",
        bBorderStyle: "solid",
        bBorderColor: "#000000",
        bFontSize   : "11px",
        bFontFamily : "Arial, Helvetica, sans-serif",
        blinkColor  : "#ffccff",
        //shoutbox's content
        evenRowColor: "",
        oddRowColor : "",
        rowPadding  : "0px",
        ipCursor    : "help",
        dUrlColor   : "#888888",
        //shoutbox's footer
        fFieldHight : "auto",
        fFieldSize  : 20,
        fFontSize   : "11px",
        fBorderWidth: "1px",
        fBorderStyle: "solid",
        fBorderColor: "#000000",
        fBgColor    : ""
       };
      //merge/overwrite with user's settings
       settings = ( uSettings )? $.extend( settings, uSettings )
                               : settings;

      if( settings.qsId ){
      //init shoutbox
        $.QShout.init();
      //bind 'Enter' key, code 13 to submit
    
      }     
   };

   $.QShout = $.extend( $.QShout, {
      //load html content
      init: function(){
      //qShout's html      
      var qShoutHtml = "<table cellpadding='0'>" +
                       "<tr><td>"                +
                       //shoutbox's content
                       "<div id='qShout'  style='outline:none;'></div>" +
                       "</td></tr>"              + "<br/>" +
                       //shoutbox's footer
                       "<tr><td>"                +
                       "<div id='qFooter'>"      +
                       "<table>" +
                       "<tr><td>"                +
                       "<label>Name: </label>"   +
                       "</td><td>"               +
                       "<input type='text' id='qName' maxlength='30' style='width:180px;'>" + "&nbsp;" + "&nbsp;" + "&nbsp;" + "&nbsp;" + "&nbsp;" + "&nbsp;" + "&nbsp;" + "&nbsp;" + "&nbsp;" + "&nbsp;" + "&nbsp;" + "&nbsp;" + "&nbsp;" +"&nbsp;" + "&nbsp;" + "&nbsp;" + "&nbsp;" + "&nbsp;" + "&nbsp;" + "&nbsp;" + "&nbsp;" + "&nbsp;" + "&nbsp;" + "&nbsp;" + "&nbsp;" + "&nbsp;" + "&nbsp;" + "&nbsp;" + "&nbsp;" + "&nbsp;" + "&nbsp;" + "&nbsp;" + "&nbsp;" + "&nbsp;" + "&nbsp;" + "&nbsp;" + "&nbsp;" + "&nbsp;" + "&nbsp;" + "&nbsp;" + "&nbsp;" + "&nbsp;" + "&nbsp;" + "&nbsp;" + "&nbsp;" + "&nbsp;" + "&nbsp;" + "&nbsp;" + "&nbsp;" + "&nbsp;" + "&nbsp;" + "&nbsp;" + "&nbsp;" + "&nbsp;" + "&nbsp;" + "&nbsp;" + "&nbsp;" + "&nbsp;" + "&nbsp;" + "&nbsp;" + "&nbsp;" + "&nbsp;" + "&nbsp;" + "&nbsp;" + "&nbsp;" + "&nbsp;" +  "* * Bewerten Sie <b style='color:#fff;'>Keeper del Mar</b> * * " + " <select style='border: 1px solid rgb(0, 0, 0); padding-left:2px;' name='Sterne' id='stars' size='1'>" + "<option value='-'>-</option>" + 
					   "<option value='*star'>1 Stern</option>" + 
      "<option value='*star*star'>2 Sterne</option>" +
      "<option value='*star*star*star'>3 Sterne</option>" +
      "<option value='*star*star*star*star'>4 Sterne</option>" +
      "<option value='*star*star*star*star*star'>5 Sterne</option>" + 
	  
    "</select> " + 

                       "</td></tr>"              +

                       

                       "<tr><td>"                +
                       "<label>Nachricht: </label>"+
                       "</td><td>"               +
                       "<textarea rows='9' style='width:650px; height:40px;' id='qMessage'></textarea>" +
                       "</td></tr>"              +

                       "<tr><td></td>"           +
                       "<td><input type='button' value='Eintragen' onClick='$.QShout.validatePost();' id='qPostBtn'> "+
                       "<a class='refresh' style='color:#fff; font-size:12px; text-decoration:underline; margin-left:500px;' href='javascript:void(0);' onClick='$.QShout.init();'>Aktualisieren</a>"+
                       "</td></tr>" +
                       "</table>"   +
                       "</div>"     +
                       "</div>"     +
                       "</td></tr>" +
                       "</table>";

      $("#" + settings.qsId ).html( qShoutHtml )                       
                             .each(function(){
     
             $(this).find("#qFooter table").css({
               'border-style'    : settings.fBorderStyle,
               'border-width'    : settings.fBorderWidth,
               'border-color'    : settings.fBorderColor,
               'width'           : '745px',
               'background-color': settings.fBgColor,
               'font-size'       : settings.fFontSize
              });

          $(this).find("#qShout").html( settings.loader )
                                 .each(function(){
               $(this).css({
               'border-style': settings.bBorderStyle,
               'border-width': settings.bBorderWidth,
               'border-color': settings.bBorderColor,
               'height'      : settings.bheight,
               'width'       : settings.bwidth,
               'overflow-y'    : 'auto'
               });
			   
			   
          });
       });
	   
			         

       //style footer's form
       this.styleFooterEle();

       //get content of shout box
        setTimeout( function(){
         $.QShout.loadContent();
        }, 300 );
      },
     
      styleFooterEle: function(){
       var inputs = $("#qFooter :input");

      //iterate to check each element type
        inputs.each(function() {
          var currentEleType = this.type;
          switch(currentEleType){
           case "textarea":
            $(this).css({
             'border-width' : settings.fBorderWidth,
             'border-style' : settings.fBorderStyle,
             'border-color' : settings.fBorderColor,
             'font-size'    : settings.fFontSize,
             'height'       : settings.fFieldHeight
            });
			case "text":
            $(this).css({
             'border-width' : settings.fBorderWidth,
             'border-style' : settings.fBorderStyle,
             'border-color' : settings.fBorderColor,
             'font-size'    : settings.fFontSize,
             'height'       : settings.fFieldHeight
            });
            //set the size of textfield
            this.size = settings.fFieldSize;
            break;
            case "submit":
             $(this).css({
              'font-size': settings.fFontSize,
              'height'   : settings.fFieldHeight
             });
              break;
          }//end switch
        });
      },

      loadContent: function( action ){
       
       var act = {load: "load"};
       if( action ) { act = $.extend( act, action ) };

       var maxRow = ( settings.maxRow < 0 ) ? 30 
	                                    : settings.maxRow;
											
       var param = "maxRow="+ maxRow +"&action=get";
       var url   = settings.serverPage + "?" + param;
       $.getJSON(url,
        function( data ){
         var arrayMessage = data.chat.message;   
         var content = "<ul style='margin:0; padding:0px; list-style: none;'>";
          $.each( arrayMessage, function(i){
            if( arrayMessage[i].url != "" ){
             arrayMessage[i].user = arrayMessage[i].user;
            }
           var message = arrayMessage[i].text;
               message = message.replace(/%22/gi,'"');
			
           content += "<li>"+
                      "<div>"+
                      "<b>" + arrayMessage[i].user + "</b>" + " schrieb am " + arrayMessage[i].time +":"+ "<br/>" +
					  
                      "</div>"+
					  "<br/>"+
					  "<div id='nachricht'>"+
                      message+ 
					  "</div>"+
                      
                      "</li>";
          });
           content += "</ul>";
           
           //manipulate content's css
              $("#qShout").html(content)
                          .each(function(){
                $(this).scrollTop( settings.bheight );

                $(this).find("ul").css({                                           
                  'font-size'  : settings.bFontSize,
                  'font-family': settings.bFontFamily
				  
                });
			

                $(this).find("ul>li:even").css({
                  'background-color' : settings.evenRowColor,
                  'padding-top'          : '20px'
				  
                });

                $(this).find("ul>li:odd").css({
                  'background-color' : settings.oddRowColor,
                  'padding-top'          : '20px'
				  
                });

                $(this).find("ul>li a#qIp").css( 'cursor', settings.ipCursor );

                $(this).find("ul>li #dateUrl").css({
                  'text-align': 'right',
                  'color': settings.dUrlColor
                });

                $(this).find("ul>li #nachricht").css({
                  'color': '#333',
                  'width': '700px',
				  'height': 'auto',
				  'text-align': 'left',
				  'padding-bottom':'5px'
                });

                $(this).find("ul>li div a").css({
                  'color': '#333',
                  'font-weight': 'bold'});
				  
			

              //apply animation for newly added
               if( act.load === "inserted" ){
                 var oriColor = $(this).find("ul>li:first").css('background-color');
                 $(this).find("ul>li:first").css('background-color', settings.blinkColor);
                    $(this).find("ul>li:first").animate({
                      opacity: 0.4
                      },'slow',function(){
                       $(this).css({
                         'background-color': oriColor,
                         'opacity': 1
                     });
                    });
                   }
              });

          //reload the content after interval time
          //setTimeout( function(){
			//settings.timer = ( settings.timer < 0 )? 60000
			                                       //: settings.timer;
              //$.QShout.loadContent();
          //}, settings.timer );
        });
      },
      //inputs validation
      validatePost: function(){
       var name    = $.trim($("#qName").val());
       var url     = $.trim($("#qUrl").val());
       var message = $.trim($("#qMessage").val());

       if( name == "" || name == null ){
         $('input#qName').animate({opacity:"0"},200).addClass("error").animate({opacity:"1"},200).animate({opacity:"0"},200).animate({opacity:"1"},200);
         $("#qName").focus();
         return false;
       }
       
       if( name.length > 30 ){
         alert("Name is too long (Max. 30 char)!");
         $("#qName").focus();
         return false;
       }
       
       if( message == "" || message == null ){
         $('textarea#qMessage').animate({opacity:"0"},200).addClass("error").animate({opacity:"1"},200).animate({opacity:"0"},200).animate({opacity:"1"},200);
         $("#qMessage").focus();
         return false;
       }
	   
       if( url != "" ){
         var checkURL = new RegExp();
         checkURL.compile("^[A-Za-z]+://[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+$");
         if( !checkURL.test(url) ){
           alert("Invalid URL!");
           $("#qUrl").focus();
           return false;
         }
       }
       this.postShout();
      },
     //post inputs
     postShout: function(){
       var user    = this.encURI( $("#qName").val() );
       var url     = this.encURI( $("#qUrl").val() );
       var message = this.encURI( $("#qMessage").val() );
	   var stars = this.encURI( $("#stars").val() );
	   
       
       //disable post button
       $("#qPostBtn").attr("disabled", true);

      var inputs = "user=" + user + "&message="  + message +  "\n" + "________________________" +"\n"+ "Bewertung: "+stars +"&action=send";
      $.post(settings.serverPage, inputs, function( result ){
        result = $.trim(result);        
        if( result === "done" ){
            clearInterval( settings.timer );
	    //load inserted
            $.QShout.loadContent({
              load: "inserted"
            });
            //clear all textfield value
            $("#qFooter :input").each(function(){
			
             if( this.type === "textarea" || this.type === "text" ){
               this.value = "";
             }else if( this.type === "button" || this.type === "submit" ){
               $(this).attr("disabled", false);
             }
          });
        }
      });
     },
    //encode
    encURI: function( string ){
      return encodeURIComponent(string);
     }
    });

})(jQuery);



