var page={};
window.gdgt=window.gdgt||{};

function loadTrendDescriptions(){
    jQuery("#highlights a").each(
    function(){
        var A=jQuery(this);
        var C=A.parent().find("em");
        if(C.length){
            var B=A.text();
            var D=C.text().replace(new RegExp(B.replace(/([^\w])/gi,"\\$1"),"gi"),"<strong>"+B+"</strong>");
            var E=A.attr("title").length?A.attr("title"):A.attr("name");
            page.trendDescriptions[E]=[B,D]}})
    }
    
    window.gdgt = window.gdgt || {};
    window.gdgt.bounds = window.gdgt.bounds || {};

jQuery.extend(gdgt.bounds, {
  Bounds: function(x, y, width, height) {
    this.x = x;
    this.y = y;
    this.width = width;
    this.height = height;
  }
});

jQuery.extend(gdgt.bounds.Bounds.prototype, {
  encloses: function(x, y) {
    return x > this.x
      && x < this.x + this.width
      && y > this.y
      && y < this.y + this.height;
  },
  toString: function() {
    return "(" + this.x + "," + this.y + ") " + this.width + "x" + this.height;
  }
});

(function(jQuery) {
  jQuery.fn.extend({
    bounds: function() {
      var jq = this.eq(0);
      var offset = jq.offset();
      return new gdgt.bounds.Bounds(offset.left, offset.top, jq.outerWidth(), jq.outerHeight());
    }
  });
})(jQuery);

(function(A){
    A.extend(window,{
        TrendTip:{
            parseIntDefault:function(B,D){
                D=D||0;
                var C=parseInt(B);
                return isNaN(C)?D:C
            },
            clearBounds:function(){
                this.data("bounds",[])
            },
            addToBounds:function(B){
                if(!this.data("bounds")){
                    this.data("bounds",[])
                }
                this.data("bounds").push(B)
            },
            enclosing:function(B,D){
                if(!this.data("bounds")){
                    this.data("bounds",[])
                }
                var C=false;
                A.each(this.data("bounds"),function(F,E){
                    if(E.encloses(B,D)){
                        C=true
                    }
                });
                return C
             },
             clearScrollInterval:function(){
                clearInterval(this.data("interval"))
             },
             setScrollInterval:function(B){
                if(this.data("interval")){
                    this.clearScrollInterval()
                }
                this.data("interval",setInterval(B,30))
             },
             duplicateContent:function(B){
                var C=0;B.children().each(
                    function(){
                        C+=A(this).outerWidth(true);
                        B.append(A(this).clone())
                    });
                B.css({zoom:1,width:(2*C)+"px"});
                return C
             },
             initScroller:function(){
                var C=this;
                var E=this.duplicateContent(C);
                var B=TrendTip.parseIntDefault(C.css("left"),0);
                var D=function(){
                    B=(B%E)-1;
                    C.css({left:B})
                };
                C.bind("trendEnter",function(){
                        C.clearScrollInterval()
                })
                .bind("trendLeave",function(){
                    C.setScrollInterval(D)
                })
                .trigger("trendLeave")}}
        }
    );
    A.extend(
        A.fn,{
            trendTip:function(){
                var B=false;
                var C=A(this);
                A.extend(C,TrendTip);
                C.initScroller();
                C.find("li a").each(
                    function(){
                        var D=A(this).closest("li");
                        var E={
                            mouseenter:function(G){
                                var F=A(this);
                                A("#highlights .inner").trigger("trendLeave");
                                if(C.oldCapturedTrend){
                                    C.oldCapturedTrend.trigger("trendLeave")
                                }
                                C.oldCapturedTrend=F;
                                C.addToBounds(F.bounds());
                                if(C.enclosing(G.pageX,G.pageY)){
                                    A("#highlights .inner").trigger("trendEnter");
                                    F.trigger("trendEnter");
                                    var H=function(I){
                                        if(!C.enclosing(I.pageX,I.pageY)){
                                            A("#highlights .inner").trigger("trendLeave");
                                            F.trigger("trendLeave")
                                        }
                                    };
                                    A(document).bind("mousemove",H);
                                    F.bind("trendLeave",function(I){
                                        C.clearBounds();
                                        A(document).unbind("mousemove",H)}
                                    )
                                 }
                             },
                             trendenter:function(){
                                if(!C.hoveringTrend){

                                    var K=A(this).offset();
                                    var J=Math.round(A(this).outerWidth()/2);
                                    var H=D.find("a").text();
                                    var G=D.find("em.description").html();

                                    B=setTimeout(function(){clearTimeout(B);},400)
                                }
                             },
                             trendleave:function(G){
                                clearTimeout(B);
                                if(C.hoveringTrend){
                                    C.hoveringTrend=false
                                }
                             }
                          };

                          A(this).mouseenter(E.mouseenter)
                                .bind("trendEnter",E.trendenter)
                                .bind("trendLeave",E.trendleave)
                          });
                          return this;
                          }
                          })})(jQuery);

                          function FrontPage(){
                        	  return jQuery.extend(this,{
                        		  $submit:jQuery("#searchform_submit"),$query:jQuery("#searchform_q"),$text:jQuery("#searchform p"),$trends:jQuery("#highlights")})
                          }

                          jQuery.extend(
                          FrontPage.prototype,{
                            init:function(){
                        	  	jQuery('#login_field').isLoginMenu();
                                this.initSearchForm();
                                this.initTrendHover();
                             },
                             initSearch:function(){
                            	 jQuery('#login_field').isLoginMenu();
                                 this.initSearchForm();
                             },
                             initLogin:function(){
                            	 jQuery('#login_field').isLoginMenu();
                             },
                             pickRandomTrend:function(){
                                var B=jQuery("#highlights li a");
                                B=jQuery(jQuery.grep(B,function(E,D){return $.trim(jQuery(E).closest("li").find(".description").text())!=""}));
                                var C=B.length;
                                var A=Math.min(Math.max(0,Math.floor(Math.random()*C)),C-1);
                                return B.eq(A)
                             },
                             searchRandomTrend:function(){
                                var A=this.pickRandomTrend();
                                if(A.length>0){
                                    searchSummize(A.attr("name")?A.attr("name"):A.attr("title"),A,SEARCH_CALLBACKS.trendLink)
                                }
                              },
                              initSearchForm:function(){
                                    var A=this;
                                    var C=true;
                                        this.$query.focus(
                                            function(){
                                                C=false;
                                                A.$text.fadeTo("fast",1)}).blur(function(){
                                                    C=true;
                                                    A.$text.fadeTo("fast",0.6)});
                                                    this.$text.hover(function(){
                                                        if(C){
                                                            jQuery(this).fadeTo("fast",1)
                                                        }
                                                    },
                                                    function(){
                                                        if(C){
                                                            jQuery(this).fadeTo("fast",0.6)
                                                         }
                                                    })
                              },
                              clearBounds:function(){
                                this.bounds=[]
                              },
                              addToBounds:function(A){
                                if(!this.bounds){
                                    this.bounds=[]
                                }
                                this.bounds.push(A)
                              },
                              enclosing:function(A,C){
                                if(!this.bounds){
                                    this.bounds=[]
                                }
                                var B=false;
                                jQuery.each(this.bounds,function(E,D){
                                    if(D.encloses(A,C)){
                                        B=true
                                    }
                                });
                                return B},

                                initTrendHover:function(){
                                    this.hoveringTrend=false;
                                    var A=this;
                                    jQuery("#highlights ul").trendTip()
                                }
                            });



(function(jQuery){
    jQuery.fn.extend({
        isLoginMenu:function(){
            return this.each(function(){
                var B=jQuery(this);
                var C=true;
                jQuery(".login").bind("click focus",function(E){
                    E.preventDefault();
                    if(!C){return }
                        C=false;
                        setTimeout(
                            function(){
                                C=true},500);
                                var D=jQuery(this);
                                D.toggleClass("menu-open");
                                B.toggle();
                      if(D.hasClass("menu-open")){
                            jQuery(document).trigger("loginMenu.show");
                            setTimeout(function(){
                            jQuery("#email").focus()
                       },50)
                     }else{
                        jQuery(document).trigger("loginMenu.hide");
                        setTimeout(function(){
                            jQuery("#home_search_q, #searchform_q").focus()},0)}});
                        B.mouseup(function(){
                            return false
                        });
                        jQuery(document).mouseup(function(D){
                                if(jQuery(D.target).parent("a.login").length==0){
                                    jQuery(document).trigger("loginMenu.hide");
                                    jQuery(".login").removeClass("menu-open");
                                    B.hide()}
                         })})}})})
                         (jQuery);
