/*--------------------------------------------------------------------------
 *  Smooth Scroller Script, version 1.0.1
 *  (c) 2007 Dezinerfolio Inc. <midart@gmail.com>
 *
 *  For details, please check the website : http://dezinerfolio.com/
 *
/*--------------------------------------------------------------------------*/

Scroller = {
	// control the speed of the scroller.
	// dont change it here directly, please use Scroller.speed=50;
	speed:10,

	// returns the Y position of the div
	gy: function (d) {
		gy = d.offsetTop
		if (d.offsetParent) while (d = d.offsetParent) gy += d.offsetTop
		return gy
	},

	// returns the current scroll position
	scrollTop: function (){
		body=document.body
	    d=document.documentElement
	    if (body && body.scrollTop) return body.scrollTop
	    if (d && d.scrollTop) return d.scrollTop
	    if (window.pageYOffset) return window.pageYOffset
	    return 0
	},

	// attach an event for an element
	// (element, type, function)
	add: function(event, body, d) {
	    if (event.addEventListener) return event.addEventListener(body, d,false)
	    if (event.attachEvent) return event.attachEvent('on'+body, d)
	},

	// kill an event of an element
	end: function(e){
		if (window.event) {
			window.event.cancelBubble = true
			window.event.returnValue = false
      		return;
    	}
	    if (e.preventDefault && e.stopPropagation) {
	      e.preventDefault()
	      e.stopPropagation()
	    }
	},
	
	// move the scroll bar to the particular div.
	scroll: function(d){
		i = window.innerHeight || document.documentElement.clientHeight;
		h=document.body.scrollHeight;
		a = Scroller.scrollTop()
		if(d>a)
			if(h-d>i)
				a+=Math.ceil((d-a)/Scroller.speed)
			else
				a+=Math.ceil((d-a-(h-d))/Scroller.speed)
		else
			a = a+(d-a)/Scroller.speed;
		window.scrollTo(0,a)
	  	if(a==d || Scroller.offsetTop==a)clearInterval(Scroller.interval)
	  	Scroller.offsetTop=a
	},
	// initializer that adds the renderer to the onload function of the window
	init: function(){
		Scroller.add(window,'load', Scroller.render)
	},

	// this method extracts all the anchors and validates then as # and attaches the events.
	render: function(){
		a = document.getElementsByTagName('a');
		Scroller.end(this);
		window.onscroll
	    for (i=0;i<a.length;i++) {
	      l = a[i];
	      if(l.href && l.href.indexOf('#') != -1 && ((l.pathname==location.pathname) || ('/'+l.pathname==location.pathname)) ){
	      	Scroller.add(l,'click',Scroller.end)
	      		l.onclick = function(){
	      			Scroller.end(this);
		        	l=this.hash.substr(1);
		        	 a = document.getElementsByTagName('a');
				     for (i=0;i<a.length;i++) {
				     	if(a[i].name == l){
				     		clearInterval(Scroller.interval);
				     		Scroller.interval=setInterval('Scroller.scroll('+Scroller.gy(a[i])+')',10);
						}
					}
				}
	      	}
		}
	}
}
// invoke the initializer of the scroller
Scroller.init();


/*------------------------------------------------------------
 *						END OF SCROLLER CODE
/*-----------------------------------------------------------*/

$(document).ready(function() {
		
		if($('h1.title').text()=='Work'){
			$('#pagination').hide();
		}

		$('#linklove').append($('div > .linklove'));
   		$('.linklove').show();
		
		$('a').each(function(intIndex) {
		if ($( this ).attr('target')=="_blank" && $("img", this).length < 1){
					$( this ).wrap("<span class=\"external_site\"></span>");
		}
		if ($("img", this).length>0){
		if ($(this).attr('class') != 'work_img'){
		$(this).addClass('imgOutLink');
		$(this).attr('target','_blank');
		$(this).mouseenter(function(){
          	$("img", this).fadeTo("fast", 0.7);
         	 })
         	 $(this).mouseleave(function(){
          	$("img", this).fadeTo("fast", 1);
         	 })
         	 }
		}
		});
		
		$('#sidebar a').each(function(intIndex) {
			if ($( this ).parent().hasClass('cat-item') || $( this ).parent().hasClass('soc_item')){
			$( this ).wrap("<span class=\"sidebarli\"></span>");
			}
		});
		
	$('#to_top').click(function(){
		$.scrollTo( 0 , 200 );
	});
	
	$('#to_footer').click(function(){
		$.scrollTo( '#footer', 200);
	});
	
	$('#menu li:last').css('margin-right','0px');
	$('.post:last').css('border-bottom','none');
	$('#footer .grid_2:last').css('border-right','none');
	$('.post:first').css('margin-top','0');
	$('.pagepost:first').css('margin-top','0');
	$('#mycommentlist .ind_comment:last').css('border-bottom','none');
	
	if ($('#mycommentlist .ind_comment').length<1){
		//$('#loggedinuser').css('border-top','none').css('margin', '0 0 10px 0;').css('padding','0 0 20px 0');
	}
	
	/*Flickr*/
	$.getJSON('http://api.flickr.com/services/feeds/photos_public.gne?id=60615284@N00&lang=en-us&format=json&jsoncallback=?', function(data){
			$.each(data.items, function(i,item){
		 	var title = item.title;
		 	if (!title){
		 	title = "untitled"}
			var picture = item.media.m;
			var properpicture = item.media.m.split('_');
			var finalimg = properpicture[0]+'_'+properpicture[1]+'_s.jpg';
			var image = '<a href="'+item.link+'" target="_blank" title="'+title+'"><img src="'+finalimg+'"/></a>';
            $("#flickrfeed").append(image);
            if ( i == 11 ) return false;
          });
          $('#flickrfeed img').mouseenter(function(){
          	$(this).fadeTo("fast", 0.7);
          })
          $('#flickrfeed img').mouseleave(function(){
          	$(this).fadeTo("fast", 1);
          })
	})

		/*IE Check*/
	if(typeof document.body.style.maxHeight === "undefined") {
		$('#content').prepend('<h1>IE6 is so 2001. <a href="http://firefox.com">Get a safer, more secure browser.</a></h1>');
		}
		
		

 $("ul.subnav").parent().append("<span></span>"); //Only shows drop down trigger when js is enabled (Adds empty span tag after ul.subnav*)

	$("ul#header_dd_menu li span").click(function() { //When trigger is clicked...
		//Following events are applied to the subnav itself (moving subnav up and down)
		$(this).parent().find("ul.subnav").slideDown('fast').show(); //Drop down the subnav on click



		//Following events are applied to the trigger (Hover events for the trigger)
		}).hover(function() {
			$(this).addClass("subhover"); //On hover over, add class "subhover"
		}, function(){	//On Hover Out
			$(this).removeClass("subhover"); //On hover out, remove class "subhover"
		});
	
		$("ul#header_dd_menu li").hover(function() {
		}, function(){
			$(this).parent().find("ul.subnav").slideUp('slow'); //When the mouse hovers out of the subnav, move it back up
		});
		
		/*Work etc
		if($('div#sidebar ul.lcp_catlist').length > 0 && !$('div#content').hasClass('workpage')){
			var current = $('h1.title').text();
			$('div#sidebar ul.lcp_catlist a').each(function(){
				if($(this).text() == current){
					$(this).hide();
				}
			});
		}*/

});
