$(document).ready(function(){ 
	ss();
	updateanchors();
	updatecomments();
	$('.archives-link').click(function(event){
		event.preventDefault();
		$('#smart-archives-block').toggle(400);
		$(this).toggleClass("up");
	})
	$('.feature-menu a').each(function(i,v){
		$(this).click(function(event,keepScroll){
			var os = "-"+(i*818)+"px";
			$('.feature-slide').stop().animate({"left": os},"slow");
			event.preventDefault();
			/* stop auto-advance */
			if(!keepScroll) clearInterval(itvl);
		})
	})
	/* Enable auto-advance */
	var current=1;
	function autoAdvance()
	{
		if(current==-1) return false;

		$('.feature-menu a').eq(current%$('.feature-menu a').length).trigger('click',[true]);
		current++;
	}
	var changeEvery = 4;
	var itvl = setInterval(function(){autoAdvance()},changeEvery*1000);
});
function updatecomments() {
	
}
function updateanchors() {
	$('.anchor').each(function(){
		var idNumber = $(this).attr('id');
		var titleName = $(this).attr('title');
		$('#scrollto').append("<a href='#" + idNumber + "' <span>" + titleName + "</span></a>");
		$("#scrollto a").each(function(){
			$(this).click(function(event){
				event.preventDefault();
				//get the full url - like mysitecom/index.htm#home
				var full_url = this.href;
				//split the url by # and get the anchor target name - home in mysitecom/index.htm#home
				var parts = full_url.split("#");
				var trgt = parts[1];
				//get the top offset of the target anchor
				var target_offset = $("#"+trgt).offset();
				var target_top = target_offset.top + -8;
				//goto that anchor by setting the body scroll top to anchor top
				$('html, body').animate({scrollTop:target_top}, 500);
			});
		    $(this).hover(
		      function () {
		        $(this).children('a>span').fadeIn(200);
		      }, 
		      function () {
		        $(this).children('a>span').fadeOut(200);
		      }
		    );
		});
		$(this).removeClass('anchor');
		$(this).removeAttr('title');
	})
	/* Set the show/hide for each comments form */
	$('.comments_container each').each(function(){
		$(this).children('a.comment-reply').click(function(event){
			event.preventDefault();
			$(this).parent().find('#respond').toggle(200);
			
		})
		$(this).removeClass('each')
	})
	$('.comment-click').each(function(){
		$(this).click(function(event){
			event.preventDefault();
			var comID = "."+$(this).attr('id');
			$(comID).toggle(200);
		})
		$(this).removeClass("comment-click");
	})
}

function ss() {
	$('.ss').each(function(){
		var ssID = "#"+$(this).attr('id');
		$(ssID).html('<div class="ss-main"><div class="ss-gallery"><div class="ss-slides"></div><div class="ss-menu"><ul></ul></div></div></div>');
	
		var totWidth=0;
		var positions = new Array();
		$.getJSON("http://api.flickr.com/services/rest/?method=flickr.photosets.getPhotos&extras=url_o,url_t,description&api_key=f1c4288aab291f2558d7c89e0c0663a2&photoset_id="+$(this).attr('id')+"&format=json&&jsoncallback=?", function(data){
		$.each(data.photoset.photo, function(i,item){
			
			var voffset = ((item.height_o / 2) - ($(ssID+" .desc").height() / 2));
			
			$(ssID+" .ss-slides").append($('<div>').attr({style: 'background-image: url("'+item.url_o+'");height:'+item.height_o+'px;',alt: item.description._content}).html("<span style='display:none;' class='desc'><p style='top:"+voffset+"px;'>"+item.description._content+"</p></span>"));
			
			$("<img />").attr({src: item.url_t,alt: "thumbnail"}).appendTo(ssID+" .ss-menu ul");
		});
		$(ssID+" .ss-slides div").wrap("<div class='ss-slide'/>")
		$(ssID+" .ss-menu ul img").wrap('<li class="ss-menuItem"><a href="#"></a></li>')
		$(ssID+" .ss-slides .ss-slide").each(function(i){

			/* Traverse through all the slides and store their accumulative widths in totWidth */
			positions[i]= totWidth;
			totWidth += $(this).width();
			
			/* The positions array contains each slide's commulutative offset from the left part of the container */
			if($(this).find('p').html() != 0)
			{
				$(this).hover(function(){
					$(this).find('span').fadeIn('slow');
				},function(){
					$(this).find('span').fadeOut('slow');
				});
			}
			
			if(!$(this).width())
			{
				alert("Please, fill in width & height for all your images!");
				return false;
			}

		});

		$(ssID+" .ss-slides").width(totWidth);

		$(ssID+" .ss-menu ul li a").click(function(e,keepScroll){
			
				$(ssID+" li.ss-menuItem").removeClass('act').addClass('inact');
				$(this).parent().addClass('act');

				var pos = $(this).parent().prevAll(ssID+" .ss-menuItem").length;

				$(ssID+" .ss-slides").stop().animate({marginLeft:-positions[pos]+'px'},1000);
				/* Start the sliding animation */
				var h = $(ssID+' .ss-slides div:nth-child('+(pos+1)+') div').height();
				
				$(ssID+" .ss-slides").animate({height: h},500)
				

				e.preventDefault();
				
		});

		$(ssID+' .ss-menu ul li.ss-menuItem:first').addClass('act').siblings().addClass('inact');
		$(ssID+' .ss-slides').animate({height: $(ssID+' .ss-slides div:nth-child(1) div').height()},0)
		
		/* Remove the .ss class so it won't get loaded again */
		$(ssID).removeClass('ss');
		
		
	});
	

	/* End of customizations */
	});
}
