$(window).load(function() {
	
	// Force max-width on images
	var max_width = 620; 	//Sets the max width, in pixels, for every image
	var selector = '.entry img'; 	//Sets the syntax for finding the images.  Defaults to all images.
	
	$(selector).each(function(){
		var width = $(this).width();
		var height = $(this).height();
		if (width > max_width) {
			//Set variables	for manipulation
			var ratio = (height / width );
			var new_width = max_width;
			var new_height = (new_width * ratio);
			
			//Shrink the image and add link to full-sized image
			$(this).height(new_height).width(new_width);
		}
	}
	);
	
	// Set sidebar height to content height for divider border
	var contentHeight = $('#content').height();
	if ($('#sidebar').height() < contentHeight ) {
		$('#sidebar').height(contentHeight);
	}
});

!function($){    
    var toggler = '.dropdown-toggle'
    var timeout;

    /**
     * Dropdown plugin definition
     */ 
    $.fn.dropdown = function(selector, event) {
        var event = event || 'click';

        if(selector) {
            toggler += ", "+selector;
        }

        if(event === 'hover') {
            event = 'mouseover mouseout';
        }

        return this.each(function() {
            $(this).delegate(selector || toggler, event, function (event) {
                var $parent = $(this).parent();

                if($(event.target).attr('href') === '#') {
                    event.preventDefault();
                }

                if (event.type == 'mouseover') {
                    clearTimeout(timeout);
                    clearMenus();
                    
                    $parent.toggleClass('open');
                } else if (event.type == 'mouseout') {
                    var $dropdown = $parent.find('.dropdown-menu');
                    
                    timeout = setTimeout(clearMenus, 300);

                    $dropdown.unbind('mouseenter.dropdown').bind('mouseenter.dropdown', function() {
                        clearTimeout(timeout);
                    });

                    $dropdown.unbind('mouseleave.dropdown').bind('mouseleave.dropdown', function() {
                        timeout = setTimeout(clearMenus, 300);
                    });
                } else {
                    event.stopPropagation();
                    
                    isActive = $parent.hasClass('open');

                    clearMenus();
                    
                    if(!isActive) {
                        $parent.addClass('open');
                        $parent.find('.dropdown-menu').css('top', $parent.find('.dropdown-toggle').height());
                    }
                }
            });
        });
    }

    function clearMenus() {
        $(toggler).parent().removeClass('open');
    }

    $(function () {
        $('html').bind("click", clearMenus)
    });

}( window.jQuery || window.ender );

$(document).ready(function() {

    // Customize Hypebeast blog widget's scroll bar 
    $("#sidebar #hypebeast-blog-widget ul").jScrollPane();

	// Keep blogger list div's height when preloading
	var ch = $('#all_blogs').height();
	$('#all_blogs').height(ch);
	
	// Toggle blogger list on inner pages
	$('.view_all').click(function() {
		$('#all_blogs').slideToggle();
		return false;
	});
	$('#close').click(function() {
		$('#all_blogs').slideToggle();
		return false;
	});
	
	$('ol#comments li:last-child').css('border', 0);
	

    var $nav = $('#main-navigation');

    // site navigation
    $nav.dropdown('.dropdown-toggle-hover', 'hover');

    // login and notification dropdown
    $nav.find('.dropdown').dropdown();
    $nav.find('.dropdown .dropdown-menu').click(function(event){
        event.stopPropagation();
    });
});



/* 
 * jScrollPanel 
 */
(function(b){b.jScrollPane={active:[]};b.fn.jScrollPane=function(c){c=b.extend({},b.fn.jScrollPane.defaults,c);var n=function(){return false};return this.each(function(){var d=b(this),R=this,S=0,l,f,q,K=c.topCapHeight,g;if(b(this).parent().is(".jScrollPaneContainer")){g=b(this).parent();S=c.maintainPosition?d.position().top:0;var i=b(this).parent();l=i.innerWidth();f=i.outerHeight();b(">.jScrollPaneTrack, >.jScrollArrowUp, >.jScrollArrowDown, >.jScrollCap",i).remove();d.css({top:0})}else{d.data("originalStyleTag", d.attr("style"));d.css("overflow","hidden");this.originalPadding=d.css("paddingTop")+" "+d.css("paddingRight")+" "+d.css("paddingBottom")+" "+d.css("paddingLeft");this.originalSidePaddingTotal=(parseInt(d.css("paddingLeft"))||0)+(parseInt(d.css("paddingRight"))||0);l=d.innerWidth();f=d.innerHeight();g=b("<div></div>").attr({className:"jScrollPaneContainer"}).css({height:f+"px",width:l+"px"});c.enableKeyboardNavigation&&g.attr("tabindex",c.tabIndex);d.wrap(g);g=d.parent();b(document).bind("emchange", function(){d.jScrollPane(c)})}q=f;if(c.reinitialiseOnImageLoad){var v=b.data(R,"jScrollPaneImagesToLoad")||b("img",d),T=[];v.length&&v.each(function(a,e){b(this).bind("load readystatechange",function(){if(b.inArray(a,T)==-1){T.push(e);v=b.grep(v,function(k){return k!=e});b.data(R,"jScrollPaneImagesToLoad",v);var h=b.extend(c,{reinitialiseOnImageLoad:false});d.jScrollPane(h)}}).each(function(){if(this.complete||this.complete===undefined)this.src=this.src})})}i={height:"auto",width:l-c.scrollbarWidth- c.scrollbarMargin-this.originalSidePaddingTotal+"px"};if(c.scrollbarOnLeft)i.paddingLeft=c.scrollbarMargin+c.scrollbarWidth+"px";else i.paddingRight=c.scrollbarMargin+"px";d.css(i);var w=d.outerHeight(),z=f/w;i=z<0.99;g[i?"addClass":"removeClass"]("jScrollPaneScrollable");if(i){g.append(b("<div></div>").addClass("jScrollCap jScrollCapTop").css({height:c.topCapHeight}),b("<div></div>").attr({className:"jScrollPaneTrack"}).css({width:c.scrollbarWidth+"px"}).append(b("<div></div>").attr({className:"jScrollPaneDrag"}).css({width:c.scrollbarWidth+ "px"}).append(b("<div></div>").attr({className:"jScrollPaneDragTop"}).css({width:c.scrollbarWidth+"px"}),b("<div></div>").attr({className:"jScrollPaneDragBottom"}).css({width:c.scrollbarWidth+"px"}))),b("<div></div>").addClass("jScrollCap jScrollCapBottom").css({height:c.bottomCapHeight}));l=b(">.jScrollPaneTrack",g);var A=b(">.jScrollPaneTrack .jScrollPaneDrag",g),x,r=[],s,t=function(){if(s>4||s%4==0)o(j+x*L);s++};c.enableKeyboardNavigation&&g.bind("keydown.jscrollpane",function(a){switch(a.keyCode){case 38:x= -1;s=0;t();r[r.length]=setInterval(t,100);return false;case 40:x=1;s=0;t();r[r.length]=setInterval(t,100);return false;case 33:case 34:return false;default:}}).bind("keyup.jscrollpane",function(a){if(a.keyCode==38||a.keyCode==40){for(a=0;a<r.length;a++)clearInterval(r[a]);return false}});if(c.showArrows){var B,U,V=function(){b("html").unbind("mouseup",V);B.removeClass("jScrollActiveArrowButton");clearInterval(U)},W=function(){b("html").bind("mouseup",V);B.addClass("jScrollActiveArrowButton");s=0; t();U=setInterval(t,100)};g.append(b("<a></a>").attr({href:"javascript:;",className:"jScrollArrowUp",tabindex:-1}).css({width:c.scrollbarWidth+"px",top:c.topCapHeight+"px"}).html("Scroll up").bind("mousedown",function(){B=b(this);x=-1;W();this.blur();return false}).bind("click",n),b("<a></a>").attr({href:"javascript:;",className:"jScrollArrowDown",tabindex:-1}).css({width:c.scrollbarWidth+"px",bottom:c.bottomCapHeight+"px"}).html("Scroll down").bind("mousedown",function(){B=b(this);x=1;W();this.blur(); return false}).bind("click",n));var M=b(">.jScrollArrowUp",g),X=b(">.jScrollArrowDown",g)}if(c.arrowSize){q=f-c.arrowSize-c.arrowSize;K+=c.arrowSize}else if(M){i=M.height();c.arrowSize=i;q=f-i-X.height();K+=i}q-=c.topCapHeight+c.bottomCapHeight;l.css({height:q+"px",top:K+"px"});var N=b(this).css({position:"absolute",overflow:"visible"}),y,p,L,j=0,C=z*f/2,D=function(a,e){var h=e=="X"?"Left":"Top";return a["page"+e]||a["client"+e]+(document.documentElement["scroll"+h]||document.body["scroll"+h])||0}, E=function(){return false},G=function(){F();y=A.offset(false);y.top-=j;p=q-A[0].offsetHeight;L=2*c.wheelSpeed*p/w},O=function(){b("html").unbind("mouseup",O).unbind("mousemove",Y);C=z*f/2;b.browser.msie&&b("html").unbind("dragstart",E).unbind("selectstart",E)},o=function(a){g.scrollTop(0);j=a=a<0?0:a>p?p:a;A.css({top:a+"px"});var e=a/p;d.data("jScrollPanePosition",(f-w)*-e);N.css({top:(f-w)*e+"px"});d.trigger("scroll");if(c.showArrows){M[a==0?"addClass":"removeClass"]("disabled");X[a==p?"addClass": "removeClass"]("disabled")}},Y=function(a){o(D(a,"Y")-y.top-C)};A.css({height:Math.max(Math.min(z*(f-c.arrowSize*2),c.dragMaxHeight),c.dragMinHeight)+"px"}).bind("mousedown",function(a){G();C=D(a,"Y")-j-y.top;b("html").bind("mouseup",O).bind("mousemove",Y).bind("mouseleave",O);b.browser.msie&&b("html").bind("dragstart",E).bind("selectstart",E);return false});var Z,H,$,aa=function(){if(H>8||H%4==0)o(j-(j-$)/2);H++},ba=function(){clearInterval(Z);b("html").unbind("mouseup",ba).unbind("mousemove",P)}, P=function(a){$=D(a,"Y")-y.top-C};l.bind("mousedown",function(a){G();P(a);H=0;b("html").bind("mouseup",ba).bind("mousemove",P);Z=setInterval(aa,100);aa();return false});g.bind("mousewheel",function(a,e){e=e||(a.wheelDelta?a.wheelDelta/120:a.detail?-a.detail/3:0);G();F();var h=j;o(j-e*L);return h==j});var I,Q,ca=function(){var a=(I-j)/c.animateStep;if(a>1||a<-1)o(j+a);else{o(I);F()}},F=function(){if(Q){clearInterval(Q);delete I}},u=function(a,e){if(typeof a=="string"){try{$e=b(a,d)}catch(h){return}if(!$e.length)return; a=$e.offset().top-d.offset().top}F();var k=w-f;a=a>k?k:a;d.data("jScrollPaneMaxScroll",k);k=a/k*p;if(e||!c.animateTo)o(k);else{g.scrollTop(0);I=k;Q=setInterval(ca,c.animateInterval)}};d[0].scrollTo=u;d[0].scrollBy=function(a){var e=-parseInt(N.css("top"))||0;u(e+a)};G();u(-S,true);b("*",this).bind("focus",function(){for(var a=b(this),e=0,h=100;a[0]!=d[0];){e+=a.position().top;a=a.offsetParent();if(!h--)return}a=-parseInt(N.css("top"))||0;h=a+f;if(!(e>a&&e<h)){h=e-c.scrollbarMargin;if(e>a)h+=b(this).height()+ 15+c.scrollbarMargin-f;u(h)}});if(c.observeHash){if(location.hash&&location.hash.length>1)setTimeout(function(){u(location.hash)},b.browser.safari?100:0);b(document).bind("click",function(a){$target=b(a.target);if($target.is("a")){var e=$target.attr("href");if(e&&e.substr(0,1)=="#"&&e.length>1)setTimeout(function(){u(e,!c.animateToInternalLinks)},b.browser.safari?100:0)}})}var J,m,da=function(){direction=J<0?-1:1;d[0].scrollBy(J/2)},ea=function(a){var e=d.parent().offset().top,h=e+f;a=D(a,"Y");J= a<e?a-e:a>h?a-h:0;if(J==0){if(m){clearInterval(m);m=undefined}}else m||(m=setInterval(da,100))},fa=function(){b(document).unbind("mousemove.jScrollPaneDragging").unbind("mouseup.jScrollPaneDragging");if(m){clearInterval(m);m=undefined}};g.bind("mousedown.jScrollPane",function(){b(document).bind("mousemove.jScrollPaneDragging",ea);b(document).bind("mouseup.jScrollPaneDragging",fa)});b.jScrollPane.active.push(d[0])}else{d.css({height:f+"px",width:l-this.originalSidePaddingTotal+"px",padding:this.originalPadding}); d[0].scrollTo=d[0].scrollBy=function(){};d.parent().unbind("mousewheel").unbind("mousedown.jScrollPane").unbind("keydown.jscrollpane").unbind("keyup.jscrollpane")}})};b.fn.jScrollPaneRemove=function(){b(this).each(function(){$this=b(this);var c=$this.parent();if(c.is(".jScrollPaneContainer")){$this.css({top:"",height:"",width:"",padding:"",overflow:"",position:""});$this.attr("style",$this.data("originalStyleTag"));c.after($this).remove()}})};b.fn.jScrollPane.defaults={scrollbarWidth:10,scrollbarMargin:5, wheelSpeed:18,showArrows:false,arrowSize:0,animateTo:false,dragMinHeight:1,dragMaxHeight:99999,animateInterval:100,animateStep:3,maintainPosition:true,scrollbarOnLeft:false,reinitialiseOnImageLoad:false,tabIndex:0,enableKeyboardNavigation:true,animateToInternalLinks:false,topCapHeight:0,bottomCapHeight:0,observeHash:true};b(window).bind("unload",function(){for(var c=b.jScrollPane.active,n=0;n<c.length;n++)c[n].scrollTo=c[n].scrollBy=null})})(jQuery);

/* 
 * Mousewheel Event
 */
(function(b){b.event.special.mousewheel={setup:function(){var a=b.event.special.mousewheel.handler;b.browser.mozilla&&b(this).bind("mousemove.mousewheel",function(c){b.data(this,"mwcursorposdata",{pageX:c.pageX,pageY:c.pageY,clientX:c.clientX,clientY:c.clientY})});if(this.addEventListener)this.addEventListener(b.browser.mozilla?"DOMMouseScroll":"mousewheel",a,false);else this.onmousewheel=a},teardown:function(){var a=b.event.special.mousewheel.handler;b(this).unbind("mousemove.mousewheel");if(this.removeEventListener)this.removeEventListener(b.browser.mozilla? "DOMMouseScroll":"mousewheel",a,false);else this.onmousewheel=function(){};b.removeData(this,"mwcursorposdata")},handler:function(a){var c=Array.prototype.slice.call(arguments,1);a=b.event.fix(a||window.event);b.extend(a,b.data(this,"mwcursorposdata")||{});var d=0;if(a.wheelDelta)d=a.wheelDelta/120;if(a.detail)d=-a.detail/3;a.data=a.data||{};a.type="mousewheel";c.unshift(d);c.unshift(a);return b.event.handle.apply(this,c)}};b.fn.extend({mousewheel:function(a){return a?this.bind("mousewheel",a):this.trigger("mousewheel")}, unmousewheel:function(a){return this.unbind("mousewheel",a)}})})(jQuery);


/*
CSS Browser Selector v0.3.3 (Sep 09, 2009)
Rafael Lima (http://rafael.adm.br)
http://rafael.adm.br/css_browser_selector
License: http://creativecommons.org/licenses/by/2.5/
Contributors: http://rafael.adm.br/css_browser_selector#contributors
*/
function css_browser_selector(u){var ua = u.toLowerCase(),is=function(t){return ua.indexOf(t)>-1;},g='gecko',w='webkit',s='safari',h=document.getElementsByTagName('html')[0],b=[(!(/opera|webtv/i.test(ua))&&/msie\s(\d)/.test(ua))?('ie ie'+RegExp.$1):is('firefox/2')?g+' ff2':is('firefox/3.5')?g+' ff3 ff3_5':is('firefox/3')?g+' ff3':is('gecko/')?g:/opera(\s|\/)(\d+)/.test(ua)?'opera opera'+RegExp.$2:is('konqueror')?'konqueror':is('chrome')?w+' chrome':is('iron')?w+' iron':is('applewebkit/')?w+' '+s+(/version\/(\d+)/.test(ua)?' '+s+RegExp.$1:''):is('mozilla/')?g:'',is('j2me')?'mobile':is('iphone')?'iphone':is('ipod')?'ipod':is('mac')?'mac':is('darwin')?'mac':is('webtv')?'webtv':is('win')?'win':is('freebsd')?'freebsd':(is('x11')||is('linux'))?'linux':'','js']; c = b.join(' '); h.className += ' '+c; return c;}; css_browser_selector(navigator.userAgent);
