(function($,i,b){var j,k=$.event.special,c="location",d="hashchange",l="href",f=$.browser,g=document.documentMode,h=f.msie&&(g===b||g<8),e="on"+d in i&&!h;function a(m){m=m||i[c][l];return m.replace(/^[^#]*#?(.*)$/,"$1")}$[d+"Delay"]=100;k[d]=$.extend(k[d],{setup:function(){if(e){return false}$(j.start)},teardown:function(){if(e){return false}$(j.stop)}});j=(function(){var m={},r,n,o,q;function p(){o=q=function(s){return s};if(h){n=$('<iframe src="javascript:0"/>').hide().insertAfter("body")[0].contentWindow;q=function(){return a(n.document[c][l])};o=function(u,s){if(u!==s){var t=n.document;t.open().close();t[c].hash="#"+u}};o(a())}}m.start=function(){if(r){return}var t=a();o||p();(function s(){var v=a(),u=q(t);if(v!==t){o(t=v,u);$(i).trigger(d)}else{if(u!==t){i[c][l]=i[c][l].replace(/#.*/,"")+"#"+u}}r=setTimeout(s,$[d+"Delay"])})()};m.stop=function(){if(!n){r&&clearTimeout(r);r=0}};return m})()})(jQuery,this);

jQuery(function($) {

  var group = $('.content-menu');
  group.hide().filter(':first').show();
  
  $(window).bind('hashchange', function () {
  
  var defaultContent = $('.toc li a:nth(0)').attr('href');
    var hash = window.location.hash || defaultContent;
    
    group.hide();
    group.filter(hash).show();
    $('.toc li').removeAttr('class');
    $('a[hash=' + hash + ']').parent().attr('class', 'current');
  });
 
   $(window).trigger( 'hashchange' ); 
   
   $('.toc li.current a').live('click', function()
   	{
   		return false;
   	});
   
	var i = $('#railroad article').length;
	
	if ( i > 1 )
	{
		$('ul.cliche-nav').fadeIn(500);
	}
	
	$('#railroad article:nth(0)').addClass('active');
	// sets target article

	$('ul.cliche-nav li.disabled a').live('click', function()
		{
			// alert('this operation is prohibited');
			return false;
		});

	$('ul.cliche-nav li:not(.disabled) a').live('click',function(e){

			if ( $('#railroad').is(':animated') )
				{
					return false;
				}

			else
				{

					h = $(this).parent().attr('class')
					var pos = $('#railroad').position();
					var left = pos.left;
					$currentSlide = $('#railroad article.active');

					if ( h == 'push on')
					{
						w = $currentSlide.width();
						var anim = left-w;
					}

					else
					{
						w = $currentSlide.prev().width();
						var anim = left + w;
					}
						$('#railroad').animate({left: anim},800, 'easeInOutExpo', function()
							{

								if ( h == 'push on')
									{
										$currentSlide.removeClass('active').next().addClass('active');
									}

								else
									{
										$currentSlide.removeClass('active').prev().addClass('active');
									}

								if ( $('#railroad article.active').next().width() == null )
									{
										$('.cliche-nav li').removeClass('disabled');
										$('.cliche-nav li:nth(1)').addClass('disabled');

									}

								else if ( $('#railroad article.active').prev().width() == null )
									{
										$('.cliche-nav li').removeClass('disabled');
										$('.cliche-nav li:nth(0)').addClass('disabled');
									}

								else
									{
										$('.cliche-nav li').removeClass('disabled');
									}

							});
				}


		e.preventDefault();

	});

$('article nav a').live('click', function(e)
	{
		e.preventDefault();
		var a = this.hash,
		$article = $(this).closest('article'),
		w = $article.width(),
		$content = $article.find('.main-content'),
		$social = $article.find('.social');
		$article.find('nav a').removeClass('current');
		$(this).addClass('current');
		$content.width(w-60);
		
		if ( a == '#read')
			{
				if ( $content.is(':visible') )	
					{
						$content.slideUp(300);
						// alert(w);
						$(this).removeClass('current');
					}
				
				else
					{
						$content.slideDown(500);
						if ( $social.is(':visible') )
							{
								$social.hide();
							}
					}
					
			}
		
		else
			{
				if ( $social.is(':visible') )
					{
						$social.fadeOut(500);
						$(this).removeClass('current');
						
					}
				
				else
					{
						if ( $content.is(':visible') )	
							{
								$content.slideUp(300);
							}
						$social.fadeIn(500);
					}
			}
	});

$('.media').each(function()
	{
		var id = $(this).attr('id');
		var vAttr = id.split('-');
		
		if ( vAttr[0] == "vimeo" )
		
			{
				$(this).css({width:  vAttr[2], height: vAttr[3]}).append('<iframe src="http://player.vimeo.com/video/' + vAttr[1] + '?wmode=transparent&amp;title=0&amp;byline=0&amp;portrait=0&amp;color=aaaaaa" width="' + vAttr[2] + '" height="' + vAttr[3] + '" frameborder="0" webkitAllowFullScreen allowFullScreen></iframe>');
			}
		
		if ( vAttr[0] == "youtube" )
				
			{
				 $(this).css({width:  vAttr[2], height: vAttr[3]}).append('<iframe width="' + vAttr[2] + '" height="' + vAttr[3] + '" src="http://www.youtube.com/embed/' + vAttr[1] + '?wmode=transparent" frameborder="0" wmode="Opaque"></iframe>');
			}
	});

// comment validation

var c = $('#commentform:visible').length;
if ( c != 0 )

{

var errorContainer = $('<div class="error">Please fill out the required fields</div>').prependTo('#commentform .form-submit').hide();
var errorLabelContainer = $('<div class="error labels"></div>').appendTo('#commentform').hide();
$('#commentform').validate({
		rules: {
			author: 'required',
			email: {
				required: true,
				email: true
			},
			url: 'url',
			comment: 'required'
		},
		errorContainer: errorContainer,
		errorLabelContainer: errorLabelContainer,
		ignore: ':hidden'
	});
$.validator.messages.required = '';
$.validator.messages.email = $.validator.messages.email;
$.validator.messages.url = $.validator.messages.url;

}
	
});
