/**
 * jQuery.ScrollTo - Easy element scrolling using jQuery.
 * Copyright (c) 2007-2008 Ariel Flesler - aflesler(at)gmail(dot)com | http://flesler.blogspot.com
 * Dual licensed under MIT and GPL.
 * Date: 9/11/2008
 * @author Ariel Flesler
 * @version 1.4
 *
 * http://flesler.blogspot.com/2007/10/jqueryscrollto.html
 */
;(function(h){var m=h.scrollTo=function(b,c,g){h(window).scrollTo(b,c,g)};m.defaults={axis:'y',duration:1};m.window=function(b){return h(window).scrollable()};h.fn.scrollable=function(){return this.map(function(){var b=this.parentWindow||this.defaultView,c=this.nodeName=='#document'?b.frameElement||b:this,g=c.contentDocument||(c.contentWindow||c).document,i=c.setInterval;return c.nodeName=='IFRAME'||i&&h.browser.safari?g.body:i?g.documentElement:this})};h.fn.scrollTo=function(r,j,a){if(typeof j=='object'){a=j;j=0}if(typeof a=='function')a={onAfter:a};a=h.extend({},m.defaults,a);j=j||a.speed||a.duration;a.queue=a.queue&&a.axis.length>1;if(a.queue)j/=2;a.offset=n(a.offset);a.over=n(a.over);return this.scrollable().each(function(){var k=this,o=h(k),d=r,l,e={},p=o.is('html,body');switch(typeof d){case'number':case'string':if(/^([+-]=)?\d+(px)?$/.test(d)){d=n(d);break}d=h(d,this);case'object':if(d.is||d.style)l=(d=h(d)).offset()}h.each(a.axis.split(''),function(b,c){var g=c=='x'?'Left':'Top',i=g.toLowerCase(),f='scroll'+g,s=k[f],t=c=='x'?'Width':'Height',v=t.toLowerCase();if(l){e[f]=l[i]+(p?0:s-o.offset()[i]);if(a.margin){e[f]-=parseInt(d.css('margin'+g))||0;e[f]-=parseInt(d.css('border'+g+'Width'))||0}e[f]+=a.offset[i]||0;if(a.over[i])e[f]+=d[v]()*a.over[i]}else e[f]=d[i];if(/^\d+$/.test(e[f]))e[f]=e[f]<=0?0:Math.min(e[f],u(t));if(!b&&a.queue){if(s!=e[f])q(a.onAfterFirst);delete e[f]}});q(a.onAfter);function q(b){o.animate(e,j,a.easing,b&&function(){b.call(this,r,a)})};function u(b){var c='scroll'+b,g=k.ownerDocument;return p?Math.max(g.documentElement[c],g.body[c]):k[c]}}).end()};function n(b){return typeof b=='object'?b:{top:b,left:b}}})(jQuery);

(function(){
  anchor = document.location.hash, handlers = [];
var args
  jQuery.extend({
    anchorHandler: {
      add: function(regexp, callback) {
        if (typeof(regexp) == 'object') {
          jQuery.map(regexp, function(arg){
            args = {r: arg[0], cb: arg[1]};});}
        else  args = {r: regexp, cb: callback};
        handlers.push(args);
        return jQuery.anchorHandler;
      }
    }
  })(document).ready(function(){
    jQuery.map(handlers, function(handler){
      match = anchor.match(handler.r) && anchor.match(handler.r)[0] || false;
      if (match) handler.cb.apply(this, [match, (anchor || false)]);});});
})();

// SLIDING FUNCTIONS

jQuery(function( $ ){	
			//by default, the scroll is only done vertically ('y'), change it to both.
			$.scrollTo.defaults.axis = 'x'; 			
			//this one is important, many browsers don't reset scroll on refreshes
			$('div#Slider').scrollTo(0);//reset all scrollable panes to (0,0)
			$('div#overlay').scrollTo(0);//reset all scrollable panes to (0,0)
			$.scrollTo( 0 );//reset the screen to (0,0)
			

			
			//Target examples bindings
			var $overlayTarget = $('#Overlay');
			var $imageTarget = $('#Slider');//the scrolled div
			
			$('#navStory1').click(function(){ 
					$imageTarget.stop().scrollTo( '0', 1000, {axis:'x', onAfter:function(){  } });
					$overlayTarget.stop().scrollTo( '0', 1000, {axis:'x', onAfter:function(){  } });
					changeSelectedStyle($(this).parent());
					return false;
			});
			$('#navStory2').click(function(){
					$imageTarget.stop().scrollTo( '500', 1000, { axis:'x',onAfter:function(){  } });
					$overlayTarget.stop().scrollTo( '1000', 1000, { axis:'x',onAfter:function(){  } });
					changeSelectedStyle($(this).parent());
					return false;
			});
			$('#navStory3').click(function(){
					$imageTarget.stop().scrollTo( '1000', 1000, { axis:'x',onAfter:function(){  } });
					$overlayTarget.stop().scrollTo( '2000', 1000, { axis:'x',onAfter:function(){  } });
					changeSelectedStyle($(this).parent());
					return false;
			});
			
		/**
		 * NAVIGATION STUFF
		 * 
		 */
		var $navigationUL = "#MonoRailNav"
		$($navigationUL).show();	
		
		$($navigationUL + " li a").css({ opacity: 0.4 });
		if ($($navigationUL + "li.selected").length < 1) {			
			$($navigationUL + " li.selected").children().css({ opacity: 1});
			$($navigationUL + " li.selected a").click();
		} else {
			$($navigationUL + " li:first").addClass("selected").children().css({ opacity: 1});
		};
		
		$($navigationUL + " li a").hover(
			function(){
				if($(this).parent().hasClass("selected")){
					
				}else{
					$(this).animate({ opacity: 1}, 100 );
				}
			},
			function(){
				if($(this).parent().hasClass("selected")){
				}else{
					$(this).animate({ opacity: 0.4}, 100 );
				}
			}
	
		);
});


$(document).ready(function(){
		$("body").removeClass("noJS");		
		
		//$('#MonoRailNav a').each(function(i, n){ $(this).attr({ name: $(this).attr("href"),href: $(this).attr("name")});});
		
		var $list = $('#MonoRailNav');		
		var thisURL = document.location.toString();
		
		if (thisURL.match('#')) { // the URL contains an anchor
			// click the navigation item corresponding to the anchor
			var thisAnchor = '#' + thisURL.split('#')[1];
			$('#MonoRailNav li a[name="' + thisAnchor + '"]').click();
		} else if ($('#MonoRailNav li.selected').length < 1) { 
			if ( $.browser.msie & parseInt(jQuery.browser.version) == 6 ) {
				$('li:eq(0)',$list).addClass("selected").children("a").animate({ opacity: 1}, 100 ).click();
			} else {
				var randomnumber=Math.floor(Math.random()*3);
				$('li:eq(' + randomnumber + ')',$list).addClass("selected").children("a").animate({ opacity: 1}, 100 ).click();
			}
		}
		
		$("#MonoRailNav").everyTime(14000,function(i){
			if ($($list).hasClass("timer") == true) {
				var $selectedItem = $("#MonoRailNav li.selected");
				$selectedItem.removeClass("selected").children("a").animate({ opacity: 0.4}, 100 )
				$selectedItem.next("li").addClass("selected").children("a").animate({ opacity: 1}, 100 );
				// if there wasn't a next li,
				// show the last li in the list
				if ($('li.selected',$list).length < 1) { $('li:first',$list).addClass("selected").children("a").animate({ opacity: 1}, 100 ); }
				$("li.selected a").click();
			};			
		});
		$("#MonoRailNav li").mouseup(function(){ $(this).parent().removeClass("timer"); })

		
		$("#MonoRail").click(function(){ 
			var currentLink = $('#MonoRailNav li.selected a').attr("href");
			window.location= currentLink;			
		})		
	});


function changeSelectedStyle (e) {
	$(e).addClass("selected");
	$(e).siblings().removeClass("selected");
	$(e).siblings().children().animate({ opacity: 0.4}, 100 );
}


