var fragment = document.location.hash;
var image_start_index = '';

for(i=0; i < fragment.length; i++)
{
	if(fragment.charAt(i) != '#')
	{
		image_start_index = image_start_index + fragment.charAt(i);
	}
}

function IsNumeric(sText)
{
	var ValidChars = "0123456789";
	var IsNumber=true;
	var Char;
	
	if(sText.length > 0)
	{
		for (i = 0; i < sText.length && IsNumber == true; i++) 
		{ 
			Char = sText.charAt(i); 
			if (ValidChars.indexOf(Char) == -1) 
			{
				IsNumber = false;
			}
		}
	}
	else
	{
		IsNumber = false;
	}

	return IsNumber;
}

jQuery(document).ready(function(){
	//alert(IsNumeric(image_start_index) ? image_start_index: 'ZERO!!!');
	//prepend span tag
	jQuery(".gallery a").prepend("<span></span>");
	jQuery(".gallerywrapper").prepend("<div class='loading'></div>");
	jQuery('.superslideshow').cycle({ 
	    fx: 'scrollHorz', 
	    speed:  'slow', 
	    timeout: 0, 
	    pager:  '.gallery ul',
	    next: '#next',
		prev: '#prev', 
		startingSlide: (IsNumeric(image_start_index) ? image_start_index: '0'),
	    pagerAnchorBuilder: function(idx, slide) { 
	        // return selector string for existing anchor 
	        return '.gallery ul li:eq(' + idx + ') a'; 
	    } 
	});
		
	jQuery(".superslideshow").touchwipe({
		wipeLeft: function() {
	 		jQuery(".superslideshow").cycle("next");
		},
		wipeRight: function() {
	 		jQuery(".superslideshow").cycle("prev");
		}
	});
	jQuery(document).keydown(function(e){
		
		if (e.which == 39) { // 39 is the right arrow key code.
			jQuery(".superslideshow").cycle("next");
		};
		if (e.which == 37) { // 37 is the left arrow key code.
			jQuery(".superslideshow").cycle("prev");
		};
	});
	jQuery(".superslideshow").fadeTo(0, 0.0);
	jQuery(".loading").Loadingdotdotdot({
	    "speed": 400,
	    "maxDots": 3
	});
	jQuery('.archive #posts').masonry({ columnWidth: 470 });

});

jQuery(window).load(function () {
	jQuery('.loading').remove();
	jQuery(".superslideshow").fadeTo("slow", 1.0 );
});
