/**
 * Jquery plugin created specifically for Quad Connect
 * @author Justin McClain
 */
/***************************************advancetextarea plugin (Used in comment and whay) **************************************/
(function($){
 
     $.fn.extend({ 
        //pass the options variable to the function
         advanceTextarea: function(options) {
            //Set the default values, use comma to separate the settings, example:
            var defaults = {
                 width:'300px',
                submitButton: null,
                 helpText: "Enter Text",
                 maxSize: Infinity,
                onSubmit:null,
				buttonClass:null
             }

            var options =  $.extend(defaults, options);
            
			function submit(event,textarea, container){
                  event.preventDefault(); 
                  //ajax call
                   if($.isFunction(options.onSubmit) && textarea.val()!="") {
                             options.onSubmit.call(this, textarea, container);   
                   }
                    textarea.val('');
                    textarea.blur();
             }

             function displayPlaceHolder(placeholder, realinput, hideElement) {
                // show the placeholder with the prompt text and hide the actual password field
                placeholder.show();
               hideElement.hide();
                // when focus is placed on the placeholder hide the placeholder and show the actual password field
                placeholder.focus(function() {
                    placeholder.hide();
                    hideElement.show();
                    realinput.focus();
                });

                // and vice versa: hide the actual password field if no password has yet been entered 
                realinput.blur(function() {
                    if (realinput.val() == '') {
						placeholder.show();
						hideElement.hide();
					}
                });
            } 

            return this.each(function() {
               //div 
                var div=$(document.createElement('div'));
                //div holding post button and text area
                var submitDiv=$(document.createElement('div'));
                submitDiv.attr('class','submit-div');
               //textarea passed in
                var textarea=$(this);
                textarea.attr('class', 'text-area');
               //counter
               var counter=$(document.createElement('span'));
               counter.attr('class', 'counter');
               counter.text(options.maxSize);
               //button that post whay
                var postButton=$(document.createElement('button'));
                postButton.attr('class','button');
                postButton.text(options.submitButton);
                postButton.attr('disabled', 'disabled');
             ////placeholder text
                var tempInput = $(document.createElement('input'));
                tempInput.attr('type','text');
                tempInput.val(options.helpText);    
                                ////get whay from or options if id and text set                
                //attaching elements to dom
                textarea.before(div); 
                submitDiv.append(textarea);
                 if(options.maxSize!=Infinity){
                     submitDiv.append(counter);
					 postButton.css("float","right");
                 }
                    
                if(options.submitButton!=null){
                    submitDiv.append(postButton);
                }
                div.append(tempInput);
                div.append(submitDiv);
                 //css
                textarea.css("width",options.width);
                tempInput.css("border", textarea.css("border"));
                tempInput.css("width", textarea.css("width"));
                tempInput.css("font-size", textarea.css("font-size"));
                tempInput.css("color", "gray");
				if(options.buttonClass!=null)
					postButton.addClass(options.buttonClass);
                //setup placeholder
                displayPlaceHolder(tempInput, textarea, submitDiv);
                //listening to typing
                 textarea.bind("keyup change focus",function(){
                        var count = textarea.val().length;
                        var available = options.maxSize - count;
                        counter.text(available);
                        if(count==0 || count>options.maxSize){
                            postButton.attr('disabled', 'disabled');
                        }else if(count>0){
                            postButton.removeAttr('disabled');
                        }
                        if(available<20){
                            counter.css("color","red");
                        }else{
                             counter.css("color","gray");
                        }
                    });
                 
                //if submit button is set then allow submitting
                if(options.submitButton!=null){
                    postButton.click(function(e){
                                     submit(e,textarea, div);
                                       });

                    textarea.keyup(function(e){
                         if(e.keyCode == 13){
                                submit(e,textarea, div);
                            }
                    });
                }
             });

        }
 
    });

})(jQuery);                   



/****************************************placeholder text plugin ********************************/
(function($) {

    $.fn.extend({

        //pass the options variable to the function
        placeholderText: function(options) {


            //Set the default values, use comma to separate the settings, example:
            var defaults = {
                width: '200px',
                fontSize: '100%',
                hideElement: null,
                focus: false,
                replacementText: 'Type Here',
				useTempElem: false
            }

            var options = $.extend(defaults, options);


            function displayPlaceHolder(placeholder, realinput) {
                // show the placeholder with the prompt text and hide the actual password field
                placeholder.show();
                realinput.hide();
                if(options.hideElement)
                        options.hideElement.hide();
                // when focus is placed on the placeholder hide the placeholder and show the actual password field
                placeholder.focus(function() {
                    placeholder.hide();
                    realinput.show();
                    if(options.hideElement)
                        options.hideElement.show();
                    realinput.focus();
                });

                // and vice versa: hide the actual password field if no password has yet been entered
                realinput.blur(function() {
                    if (realinput.val() == '') {
                        placeholder.show();
                        realinput.hide();
                        if(options.hideElement)
                            options.hideElement.hide();
                    }
                });
            }

            function tempTextReplacement(input, tempText) {
                if(!options.focus){
                    input.val(tempText);
                    input.css("color", "gray");
                }
                input.focus(function() {
                    if (input.val() == tempText) {
                        input.val('');
                        input.css("color", "black");
                    }
                });

                input.blur(function() {
                    if (input.val() == '') {
                        input.val(tempText);
                        input.css("color", "gray");
                    }
                });
            }


            return this.each(function() {
                //global
                var input = $(this);
                input.css("width",options.width);
                input.css("font-size",options.fontSize);
                if (input.is("input") && input.attr('type') == 'text' && !options.useTempElem) {
                    tempTextReplacement(input, options.replacementText);
                    //focus
                    if(options.focus){
                        input.focus();
                    }
                } else if (options.useTempElem || input.is("textarea") || (input.is("input") && input.attr('type') == 'password')) {
                    var tempInput = $(document.createElement('input'));
                    tempInput.attr('type','text');
                    tempInput.attr('class',input.attr('class'));
                    tempInput.val(options.replacementText);
                 
                    tempInput.css("border", input.css("border"));
                    tempInput.css("width", input.css("width"));
                    tempInput.css("font-size", input.css("font-size"));
                    tempInput.css("color", "gray");
                    
                    if(options.hideElement){
                        options.hideElement.before(tempInput);
                    }else{   
                        input.before(tempInput);
                    }
                    displayPlaceHolder(tempInput, input);
                    //focus
                    if(options.focus){
                        tempInput.focus();
                    }
                } else{
                    return false;   
                }
                
            });
        }
    });

})(jQuery);

/**********************************************Searchbar Plugin **************************************/	
/*(function($){

     $.fn.extend({ 
         
        //pass the options variable to the function
         searchbar: function(options) {


            //Set the default values, use comma to separate the settings, example:
            var defaults = {
                width: '100%',
                fontSize: '125%',
                border: '#A1B9ED 1px solid',
                outline:'yellow solid thin',
                onChange:null,
                clearTooltip:'Clear Search',
                clearColor:'#A1B9ED',
                clearHoverColor:'#2200C1',
                clearFont:'normal normal normal 20px/normal Arial, sans-serif',
                clearFontSize:'20px',
                searchbarTooltip:'Search',
                replacementText:'',
                focus:false
            }
                
            var options =  $.extend(defaults, options);
            
            function displayPlaceHolder(placeholder,realinput,hiddenElement){
                // show the placeholder with the prompt text and hide the actual password field
                placeholder.show();
                hiddenElement.hide();
                // when focus is placed on the placeholder hide the placeholder and show the actual password field

                placeholder.focus(function(){
                    placeholder.hide();
                    hiddenElement.show();
                    realinput.focus();
                });

                // and vice versa: hide the actual password field if no password has yet been entered

                realinput.blur(function(){
                    if (realinput.val() == '') {
                        placeholder.show();
                        hiddenElement.hide();
                    }
                });
            }


            
            var previousVal='';
             return this.each(function() {
                //global
                var input= $(this);
                var outlineDiv=$(document.createElement('div'));
                var table=$(document.createElement('table'));
                table.attr('class','searchbar-table');
                var clearX=$(document.createElement('a'));
                clearX.attr('href','#');
                clearX.attr('class','searchbar-clear');
                clearX.text('x');
                var row=$(document.createElement('tr'));
                clearX.hide();
                
                //appending
                outlineDiv.insertBefore(input);             
                outlineDiv.append(table);
                table.append(row.append($(document.createElement('td'))).append(input));
                
                table.append(row.append($('<td />')).append(clearX));
                
                    
                //css
                outlineDiv.css("width",options.width);
				outlineDiv.css("height","100%");
                table.css("border",options.border);
                table.css("width",options.width);
                input.css("border","none");
                input.css("width","100%");
                input.css("font-size",options.fontSize);
                input.attr('title',options.searchbarTooltip);
                clearX.css("cursor","pointer");
                clearX.css("text-decoration","none");
                clearX.attr('title',options.clearTooltip);
                clearX.css("color",options.clearColor);
                clearX.css("font",options.clearFont);
                clearX.css("font-size", options.clearFontSize);
                //clearX.css("padding-left","3px");
                //clearX.css("padding-right","3px");
       			if(options.replacementText!=''){
                    //make placeholder to match table
                    var tempInput = $(document.createElement('input'));
                    tempInput.attr('type','text');
                    tempInput.attr('class',input.attr('class'));
                    tempInput.val(options.replacementText);
                    
                    
                    tempInput.css("border",options.border);
                    tempInput.css("width",options.width);
					tempInput.css("height",outlineDiv.css('height'));
                    tempInput.css("font-size",options.fontSize);
                    tempInput.css("color","gray");
                    table.before(tempInput);
                    displayPlaceHolder(tempInput,input,table);
                    if(options.focus)
                        tempInput.focus();
                }
  
            //add x clearing function
            clearX.click(function(e){
                e.preventDefault();
                clearX.hide();
                input.val('');
                input.focus();
                
            });
            
            //add hiding and showing x and handling callback
           input.bind('cut copy paste keyup change focus hover mouseenter mouseout', function(e) {
                if(input.val() == ''){
                    clearX.hide();
                }else{
                    clearX.show();    
                }
               if(previousVal!=input.val()){
                   if(options.onChange){
                       if(typeof options.onChange== 'function'){
                            options.onChange(previousVal, input.val());
                      }
                   }
                   previousVal=input.val();
               }
            });
            //clear hover
            clearX.hover(function () {
                clearX.css("color",options.clearHoverColor); 
              }, 
              function () {
                clearX.css('color',options.clearColor);
              }
            );
                
             
            //input focus
            input.focus(function () {
                input.css("outline","none"); 
                outlineDiv.css('outline',options.outline);
            });
            input.blur(function () {
                outlineDiv.css('outline','none');
            });
            
            //starting focus code
            if(options.replacementText!='' && options.focus){
                tempInput.focus();
            }
        });
      }
    });
    
})(jQuery);*/
(function($){
     $.fn.extend({ 
        //pass the options variable to the function
         searchbar: function(options) {
            //Set the default values, use comma to separate the settings, example:
            var defaults = {
                width: '100%',
                fontSize: '125%',
                border: '#A1B9ED 1px solid',
                outline:'yellow solid thin',
                onChange:null,
                clearTooltip:'Clear Search',
                clearColor:'#A1B9ED',
                clearHoverColor:'#2200C1',
                clearFont:'normal normal normal 20px/normal Arial, sans-serif',
                clearFontSize:'20px',
                searchbarTooltip:'Search',
                replacementText:'',
                focus:false
            }
             
            var options =  $.extend(defaults, options);
            
            function tempTextReplacement(input, tempText) {
                if(!options.focus){
                    input.val(tempText);
                    input.css("color", "gray");
                }
                input.focus(function() {
                    if (input.val() == tempText) {
                        input.val('');
                        input.css("color", "black");
                    }
                });

                input.blur(function() {
                    if (input.val() == '') {
                        input.val(tempText);
                        input.css("color", "gray");
                    }
                });
            }
          
            var previousVal='';
             return this.each(function() {

                //global
                var input= $(this);
                var outlineDiv=$(document.createElement('div'));
                var clearX=$(document.createElement('a'));
                clearX.attr('href','#');
                clearX.attr('class','searchbar-clear');
                clearX.text('x');
                clearX.hide();
              
                //appending
                outlineDiv.insertBefore(input);             
                outlineDiv.append(input);
                outlineDiv.append(clearX);
                   
                //css
                outlineDiv.css("width",options.width);
                outlineDiv.css("border",options.border);                
				input.css("border","none");
                input.css("width","90%");
                input.css("font-size",options.fontSize);
                input.attr('title',options.searchbarTooltip);
                clearX.css("cursor","pointer");
				clearX.css("float","right");
				clearX.css("vertical-align","middle");
				clearX.css("margin-right","10px");
                clearX.css("text-decoration","none");
                clearX.attr('title',options.clearTooltip);
                clearX.css("color",options.clearColor);
                clearX.css("font",options.clearFont);
                clearX.css("font-size", options.clearFontSize);

             
       if(options.replacementText!=''){
           tempTextReplacement(input, options.replacementText);
           if(options.focus)
               input.focus();
        }

            //add x clearing function
            clearX.click(function(e){
                e.preventDefault();
                clearX.hide();
                input.val('');
                input.focus();             
            });

            

            //add hiding and showing x and handling callback
           input.bind('cut copy paste keyup change focus hover mouseenter mouseout', function(e) {
                if(input.val() == '' || input.val()==options.replacementText){
                    clearX.hide();
                }else{
                    clearX.show();    
                }
               if(previousVal!=input.val()){
                   if(options.onChange){
                       if(typeof options.onChange== 'function'){
                           if(input.val()!=options.replacementText){
                            	options.onChange(previousVal, input.val());
                       		}	
                      	}
                   }
                   previousVal=input.val();
               }
            });

            //clear hover
            clearX.hover(function(){
                clearX.css("color",options.clearHoverColor); 
             }, 
             	function(){
                	clearX.css('color',options.clearColor);
              }
            );
                             
            //input focus
            input.focus(function () {
                input.css("outline","none"); 
                outlineDiv.css('outline',options.outline);
            });

            input.blur(function () {
                outlineDiv.css('outline','none');
            });
           
            //starting focus code
            if(options.focus){
                input.focus();
            }
        });

      }

    });

})(jQuery);

/**********************************************Vticker Plugin **************************************/	
//TODO add add/remove method that stops timer and adds to end
(function($){

     $.fn.extend({ 
         
        //pass the options variable to the function
         vTicker: function(options) {


            //Set the default values, use comma to separate the settings, example:
            var defaults = {
                animationSpeed: 1000,
                pause: 2000,
                showItems: 3,
                hoverPause: false,
				updateCallback:null,
				updateInterval: 0,
                nxtBtn: null,
                backBtn:null
            }
                
            var options =  $.extend(defaults, options);
           
            //Moves the first item to the last position
            function rotate(ul){
                var first = ul.children(":first");
                first.slideUp(options.animationSpeed, function() {
                                 ul.append($(this));
                                $("ul li:nth-child("+options.showItems+")").fadeIn(options.animationSpeed);
                              });
                
            }
             //rotates backwards
            function rotateBackwards(ul){
                var last = ul.children(":last");
                last.hide();
                $("ul li:nth-child("+(options.showItems)+")").fadeOut(options.animationSpeed, function(){
                    ul.prepend(last);
                    last.slideDown(options.animationSpeed);
                }); 
                
            }
            //hides li
            function hideLi(index, elem){
                if((index+1)>options.showItems){
                    $(elem).hide();
                }
            }
            //holds the timer interval id
            var intervalId = null;
             
             return this.each(function() {
                 //global
                var mainDiv= $(this);
                var ul=mainDiv.children('ul:first');
                //hiding no visibile       
                ul.children('li').each(hideLi);
                 //show previous
                 if(options.backBtn){
                     options.backBtn.click(function(e){
                         e.preventDefault();
                         rotateBackwards(ul);
                     });
                 }
                 
                 //show next item
                 if(options.nxtBtn){
                     options.nxtBtn.click(function(e){
                         e.preventDefault();
                         rotate(ul);
                     });
                 }
                 
                 //adds animation
				 if(options.pause && ul.children().size()>options.showItems){
                     intervalId = window.setInterval(function(){
                         rotate(ul);
                     }, options.pause);
                 }
				 
				 //add update function
				 if($.isFunction(options.updateCallback) && options.updateInterval>0) {
                             window.setInterval(function(){
		                         //call callback
								 options.updateCallback.call(this, ul);
								  //hide all new children
								  ul.children('li').each(hideLi);
							  	   //stop and start interval
								  if(!intervalId)
							  		window.clearInterval(intervalId);
								  if(options.pause && ul.children().size()>options.showItems){
	                     				intervalId = window.setInterval(function(){
	                         			rotate(ul);
	                     			}, options.pause);
	                 			  }
								 
		                     }, options.updateInterval);	 
                   }
          
                //adds hover pause
             if(options.hoverPause){
                mainDiv.hover(
                    function () {
                        window.clearInterval(intervalId);
                    },
                    function () {
                        if(ul.children().size()>options.showItems){
							intervalId = window.setInterval(function(){
	                         rotate(ul);
	                     	}, options.pause);
						}
                    }
                );
            }
            
            
        });//end of each
      }
    });
    
})(jQuery);

/**********************************************Agenda Plugin *********************************************/
(function($){

     $.fn.extend({ 
         
        //pass the options variable to the function
         agenda: function(options) {


            //Set the default values, use comma to separate the settings, example:
            var defaults = {
                height: "100px",
                callback: null
            }
                
            var options =  $.extend(defaults, options);
             
             //checks if near end of div  
             $.fn.isNearTheEnd = function() { 
                return this[0].scrollTop + this.height() >= this[0].scrollHeight; 
            }
              
             
             
             return this.each(function() {
                 //global
                var mainDiv= $(this);
                 var loadMoreLink= $(document.createElement('a'));
                 loadMoreLink.attr('href','#');
                 loadMoreLink.text("Load More");
                 var loadingIcon=$(document.createElement('label'));
                 loadingIcon.text("Loading");
                   loadingIcon.hide();
                //adding to dom
                mainDiv.append(loadMoreLink);
                mainDiv.append(loadingIcon); 
                //css
                mainDiv.css("overflow","auto");
                mainDiv.css("height",options.height);
                //scroll listener 
                mainDiv.scroll(function(){
                     if(mainDiv.isNearTheEnd()){
                          if($.isFunction(options.callback)) {
                             options.callback.call(this, mainDiv, loadMoreLink);   
                   			}
                         
                     }
                 }); 
                 //click load more 
                loadMoreLink.click(function (e){
                    e.preventDefault();
                     if($.isFunction(options.callback)) {
                             options.callback.call(this, mainDiv, loadMoreLink);   
                   	}
                });
            
        });//end of each
      }
    });
    
})(jQuery);



/********** Click2Edit plugin *********************************************/
(function($){

     $.fn.extend({ 
         
        //pass the options variable to the function
         click2Edit: function(options) {


            //Set the default values, use comma to separate the settings, example:
            var defaults = {
                width:"300px"
               
            }
                
            var options =  $.extend(defaults, options);     
             
            return this.each(function() {
                //div passed in
                var input=$(this);
                input.css("border","none");
                input.attr("readonly","readonly");
                //boolean for is clicked
				var clicked=false;
                //click handler & focus???
                input.bind("click focus",function(){
                     input.css("border","inset");
                     input.attr("readonly","");
                	clicked=true;
                });
                input.blur(function(){
                    input.css("border","none");
                    input.attr("readonly","readonly");
					clicked=false;
                });
              	input.keyup(function(e) {
					if(e.keyCode == 13) { // on enter
						input.blur();
					}
				});      
	              //clear hover
	            input.hover(function () {
                     input.css("border","inset"); 
	              }, 
	              function () {
						if(!clicked)
							input.css("border","none");
	              }
	            );
                
                
            });
        }
    });
    
})(jQuery);

/********************************Get url plugin**********************************/
$.extend({
  getUrlVars: function(){
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
      hash = hashes[i].split('=');
      vars.push(hash[0]);
      vars[hash[0]] = hash[1];
    }
    return vars;
  },
  getUrlVar: function(name){
    return $.getUrlVars()[name];
  }
});

