(function($) {

    $.fn.pdSearch = function(settings) {
        var options = {
            contentId:-1,
            persistUrlContentId:false,
            showButton:true,
            showButtonClass:'button',
            showButtonText:'Go',
            clearHistory: true,
            taskName:'Search',
            useAutoComplete:true
            };
            
        if (settings) $.extend(options, settings);
        
        this.each(function() {
          var $this = $(this);
                    
          $this.bind('submit', function() {
            var q = $this.val();
             if ((q != null) && (q.length > 0)) {
                q = q.replace(/~/g, '');
                
                var ctQ = '';
                var ct = '';
                if (options.persistUrlContentId) {
                    ct = getParameterByName('ct');
                }
                if (ct.length == 0 && options.contentId != -1) {
                    ct = options.contentId.toString();
                }
                if (ct.length > 0 && ct != '-1') {
                    ctQ = '&ct=' + ct;
                }
                if (window.pdGlobal && pdGlobal.userLoggedIn == true) {
                    __pdL(52830, options.taskName, 1, 'q=' + q + ctQ.replace('&','~'), '', options.clearHistory, 0, '', '');
                } else {
                    window.location.href = '/podium/default.aspx?t=52830&q=' + escape(q) + ctQ;
                }
            }
          });
          
          $this.keypress(function(e){
                var c = e.which ? e.which : e.keyCode;
                if (c == 13) {
                   $this.submit();
                   e.preventDefault();
                   return false;       
                }
            });
          
           if (options.useAutoComplete) {
                var acUrl = '/podium/tools/searchsuggest.aspx';
                var acOptions = {
                    matchContains:true,
                    selectFirst:false,
                    width: $this.outerWidth()
                };

                if (!jQuery.fn.autocomplete || !jQuery.fn.autocomplete.result) {
                    if ($('link[href*="autocomplete"]').length == 0) {
                        $('<link>').appendTo('head').attr({rel:'stylesheet',type:'text/css',href:'/ftpimages/999/podium/libs/jquery-autocomplete/jquery.autocomplete.css'});
                    }
                    $.getScript('/ftpimages/999/podium/libs/jquery-autocomplete/jquery.autocomplete.min.js', function() {
                        $this.autocomplete(acUrl,acOptions);
                        $this.result(function(event, data, formatted){$this.submit();});
                    });
                } else {
                    $this.autocomplete(acUrl,acOptions);
                    $this.result(function(event, data, formatted){$this.submit();});
                }
           }
                              
          if (options.showButton) {
            var $button = $('<input type="button" />');
            $button.addClass(options.showButtonClass);
            $button.val(options.showButtonText);
            $button.css({'cursor':'pointer','margin-left':'3px'});
            $this.after($button);
            $button.click(function(){
                $this.submit();
            });
          }
          
        });
                
        return this;
    };
          
})(jQuery);

function getParameterByName(name) {
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp(regexS);
  var results = regex.exec(window.location.href);
  if(results == null)
    return "";
  else
    return results[1];
}

var searchAdvObj;
function pdSearchAdvObj() {
    this.All;
    this.Exact;
    this.Or;
    this.None;
    
    this.fullText = function() {
        var str = '';
        
        if (this.All && this.All.length > 0) {
            str += this.All;
        }
        
        if (this.Exact && this.Exact.length > 0) {
            if (str.length > 0) str += ' ';
            str += '"' + this.Exact + '"';
        }
        
        if (this.Or && this.Or.length > 0) {
            var orArray = this.Or.split(' ');
            var orStr = '';
            for (var c=0, lenC=orArray.length; c<lenC; ++c) {
                var s = orArray[c];
                if (s.length > 0 && s != 'OR' && s != 'or') {
                    if (orStr.length > 0) orStr += ' OR ';
                    orStr += s;
                }
            }
            if (str.length > 0) str += ' ';
            str += orStr;
        }
               
        if (this.None && this.None.length > 0) {
            if (str.length > 0) str += ' ';
            str += '-' + this.None.replace(/ /gi, ' -');
        }
        
        return str;
    }
    
    this.setValue = function(input) {
        $input = $(input);
        if ($input.hasClass('qAll')) {
            this.All = $input.val();
        } else if ($input.hasClass('qExact')) {
            this.Exact = $input.val();
        } else if ($input.hasClass('qOr')) {
            this.Or = $input.val();
         } else if ($input.hasClass('qNone')) {
            this.None = $input.val();
        }
    }
}

function pdSearchAdvInit(searchBoxId) {
    searchAdvObj = new pdSearchAdvObj();
    searchAdvObj.All = $('.qAll').val();
    searchAdvObj.Exact = $('.qExact').val();
    searchAdvObj.Or = $('.qOr').val();
    searchAdvObj.None = $('.qNone').val();
    
    $("INPUT.qAdv").keyup(function(e){
        searchAdvObj.setValue(this);
        $('#' + searchBoxId).val(searchAdvObj.fullText());
    });  
}

