(function($){
	var formats = {
		'%': function(val) {return '%';},
		'b': function(val) {return  parseInt(val, 10).toString(2);},
		'c': function(val) {return  String.fromCharCode(parseInt(val, 10));},
		'd': function(val) {return  parseInt(val, 10) ? parseInt(val, 10) : 0;},
		'u': function(val) {return  Math.abs(val);},
		'f': function(val, p) {return  (p > -1) ? Math.round(parseFloat(val) * Math.pow(10, p)) / Math.pow(10, p): parseFloat(val);},
		'o': function(val) {return  parseInt(val, 10).toString(8);},
		's': function(val) {return  val;},
		'x': function(val) {return  ('' + parseInt(val, 10).toString(16)).toLowerCase();},
		'X': function(val) {return  ('' + parseInt(val, 10).toString(16)).toUpperCase();}
	};

	var re = /%(?:(\d+)?(?:\.(\d+))?|\(([^)]+)\))([%bcdufosxX])/g;

	var dispatch = function(data){
		if(data.length == 1 && typeof data[0] == 'object') { //python-style printf
			data = data[0];
			return function(match, w, p, lbl, fmt, off, str) {
				return formats[fmt](data[lbl]);
			};
		} else { // regular, somewhat incomplete, printf
			var idx = 0; // oh, the beauty of closures :D
			return function(match, w, p, lbl, fmt, off, str) {
				return formats[fmt](data[idx++], p);
			};
		}
	};

	$.extend({
		sprintf: function(format) {
			var argv = Array.apply(null, arguments).slice(1);
			return format.replace(re, dispatch(argv));
		},
		vsprintf: function(format, data) {
			return format.replace(re, dispatch(data));
		}
	});
})(jQuery);

	(function($){
	    if(!$.sus){
	        $.sus = new Object();
	    }		
	    $.sus.AquaCaption = function(el, options){
	        var base = this;
	        base.$el = $(el);
	        base.el = el; 
	        base.$el.data("sus.AquaCaption", base);
	        
	        base.init = function(){
	            base.options = $.extend({},$.sus.AquaCaption.defaultOptions, options);
	        	base.$el.hover(
	        		function(e){
	        			base.$el.find(base.options.sel).show()
	        		},function(e){
	        			base.$el.find(base.options.sel).hide()
	        		}
	        	);
	        
	        //	$(base.el).live('mouseover',function(e){$(this).find(base.options.sel).show()}).live('mouseout',function(e){$(this).find(base.options.sel).hide()})
	        }
	        base.init();
	    }
	    $.sus.AquaCaption.defaultOptions = {
	        sel: ".caption"
	    }
	    $.fn.sus_AquaCaption = function(options){
	        return this.each(function(){
	            (new $.sus.AquaCaption(this, options));
	        });
	    }
	    $.fn.getsus_AquaCaption = function(){
	        return this.data("sus.AquaCaption");
	    }
		/*********   onload here  ************/
		$(function(){
		
			$.setslide=function(slide){
				$._slide = slide;
				if($(slide).length > 0){
					$('.slide').removeClass('sactive');
					$(slide).addClass('sactive');
					$(slide).parent().animate({"left": -$(slide).position().left}, "fast");
					$('.entry-slideshow-nums a').eq($('.slide').index($(slide))).addClass('hilite').siblings().removeClass('hilite');
					
					$(slide).find('.description').width(Math.max(150,$(slide).find('img:first').width()));
				}
			}
			$('.sd-item').live('mouseover',function(e){$(this).find('.bd-left-item-title').show()}).live('mouseout',function(e){$(this).find('.bd-left-item-title').hide()})
				.click(function(e){location.href=$(this).find('a:first').attr('href')})
			$('.widget-title').toggle(function(e){
				$(this).closest('.widget').siblings().height(25)
				$(this).closest('.widget').height('auto');
			}, function(e){
				$(this).closest('.widget').height(25);
			})
			
			$('.entry-slideshow-nums a').click(function(e){
				e.preventDefault();
				var v = $(this).attr('href').split('-')[1];
				$.setslide( $('.entry-slideshow .slide:eq(' +(v-1)+ ')') )
				e.target.blur();
				
			})
			$('.entry-slideshow div img').click(function(e){
				//$.setslide($(this))
				var s = $._slide.next();
				$.setslide( s.length > 0  ? s : $('.slide:first'));
			})
			
			$('div.wp-caption').hover(
				function(e){
					$(this).find('p').show();
				},function(e){
					$(this).find('p').hide();
				}
			);
			$('.slide img').load(function(){
				//$(this).siblings('.description').width($(this).width());//.show();
			})
			
			$('#sidebarsearch, #cfct-search-input').focus(function(){
				if($(this).val() == 'SEARCH'){
					$(this).val('').css({backgroundColor:'aqua',color:'black'})
				}
			}).blur(function(){
				if($(this).val() == ''){
					$(this).val('SEARCH').css({backgroundColor:'black',color:'#e0e0d7'})
				}
			})
		
			$('.widget_links a, .widget_twitter a').mousedown(function(e){
				//alert($(this).attr('href'))
				$(this).attr('target','_new')
				}
			)
			
			$('#by_author').click(function(e){
				$('#authors').toggle();
				$('.dropdown:not(#authors)').hide();
			})
			$('#by_category').mouseover(function(e){
				$('#categories').show();
				$('.dropdown:not(#categories)').hide();
			})
			
			$('.dropdown').hover(function(e){$(this).show()},function(e){$(this).hide()})
			
			$._sd_query = '';
			$('.dropdown li a, #by_newest ').click(function(e){
					e.preventDefault();
					$(this).closest('.dropdown').hide();
					
					$._sd_query = '';
					
					if($(this).closest('#authors').length > 0) 
						$._sd_query+= '&author_name=' + $(this).attr('href').replace(/.*author\/([a-z-]+)\//, '$1');
					if($(this).closest('#categories').length > 0) 
						$._sd_query+= '&category_name=' + $(this).attr('href').replace(/.*category\/([a-z-]+)\//, '$1');
					
					
					$.getJSON("/?feed=json" + $._sd_query + "&posts_per_page=20&callback=?", function(data){
					
							if(data.items.length > 0) $('#sd-l-menu').empty();
							
							
        					$.each(data.items, function(i,item){
        		 				$('<div class="sd-item"><div id="sdi' +item.id+ '" class="thumbdiv" style="background-image: url('+ item.img +');">&nbsp;</div><div class="bd-left-item-title"><div><a class="item-link" href="'+  item.link +'"  style="font-family: \'Times New Roman\'"><strong>'+ item.sectionrubric +'</strong> '+ item.minibio +'</div><div class="more" style="position:absolute; bottom:0; right:0;">&nbsp;<a href="'+ item.link +'" class="heroic">...MORE</a></div></div><div class="bd-left-item-date bgplus">'+ item.date +'</div></div> <hr style="width:100%; margin:2px 0;"/>').appendTo('#sd-l-menu');
				 				Cufon.refresh();
         					})
         				if(data.items.length <= 0) $._nomoreitems = true;
        			})
        			
			})

			$('#sd-l-more').click(function(e){
				if(!this._initial) this._initial = $('#sd-l-menu .sd-item').length; 
				if(!this._feedbase) this._feedbase = ''; 
				this._count = this._count+1 || 0;
				if(!$._nomoreitems){
					$('#sd-l-menu .sd-item:first, #sd-l-menu hr:first').remove();
					$.getJSON("/?feed=json"+$._sd_query+"&posts_per_page=1&paged="+ (this._initial + this._count +1) +"&callback=?", function(data){
        				$.each(data.items, function(i,item){
				 			$('<div class="sd-item"><div id="sdi' +item.id+ '" class="thumbdiv" style="background-image: url('+ item.img +');">&nbsp;</div><div class="bd-left-item-title"><div><a class="item-link" href="'+  item.link +'"  style="font-family: \'Times New Roman\'"><strong>'+ item.sectionrubric +'</strong> '+ item.minibio +'</div><div class="more" style="position:absolute; bottom:0; right:0;">&nbsp;<a href="'+ item.link +'" class="heroic">...MORE</a></div></div><div class="bd-left-item-date bgplus">'+ item.date +'</div></div> <hr style="width:100%; margin:2px 0;"/>').appendTo('#sd-l-menu');
				 			Cufon.refresh();
         				})
         				if(data.items.length <= 0) $._nomoreitems = true;
        			})	
        		}
			})


			
			$.sdBar={pos:0,length:0}
			$.sdBar=function(){
							
			}
			
			$(document).keyup(function(event){
				Keys={SPACE:32,LEFT:37,RIGHT:39,UP:38,DOWN:40,ESC:27,RETURN:13};
				if($._slide) {
					switch(event.keyCode){
						case Keys.SPACE:
						case Keys.RIGHT:
							try{
								var s = $._slide.next();
								$.setslide( s.length > 0 ? s : $('.slide:first')); 
							} catch(e){;};break;
						case Keys.LEFT:
							var s = $._slide.prev(); 
							$.setslide( s.length > 0 ? s : $('.slide:last')); break;
						case Keys.ESC:
							history.back();break;
					}
				}
			});
			
			try{
				$.setslide($('.slide:first'))
				$('.slide:first img').load(function(e){$(this).siblings('.description').width($(this).width()) })
			} catch(e){;}
			
			
	try {
		$('<div class="header-image" id="banner-image" style="padding:5px 10px 0px 10px; margin:5px 0px; border:0px solid black; position: absolute; display:none"><img src="http://www.sightunseen.com/img/PJ_013RT_V2.jpg" style="margin:0px; padding:0px; width: 457px; height:104px;"/></div>').appendTo('#hd-msg')
		$('<div class="header-message" id="banner-text" style="padding:5px 10px 0px 10px; margin:10px; border: 1px dotted black; height:97px; cursor: pointer; overflow:hidden;"><h2 style="font-size:150%; padding-bottom:2px;" class="caslon">Today\'s Sponsor: <a href="http://bernhardtdesign.com/discover/altered_landscapes.htmlutm_source=sightunseen&utm_campaign=sightunseen0" target="_external">Bernhardt Design</a></h2><p style="margin-bottom:0px;">Patrick Jouin&apos;s new Item sofa lets you invent your own seating landscape: flat, sloped, curved, or twisted. Fourteen simple elements, infinite possibilities. Find out more at bernhardtdesign.com</p></div>')
			.appendTo('#hd-msg');
		
		$('#hd-msg').click(function(e){
				e.preventDefault();
				_gaq.push(['_trackEvent','adverts','clicked','Advert','1']);
				window.open('http://bernhardtdesign.com/discover/altered_landscapes.html?utm_source=sightunseen&utm_campaign=sightunseen01');
			}).hover(function(e){
				$('#banner-text').hide(); 
				$('#banner-image').show();
			},function(e){
				$('#banner-text').show(); $('#banner-image').hide();
			}).css('cursor','hand');
		Cufon.refresh();
	} catch (err){}

			
		})
	})(jQuery)
