$(document).ready(function() {

    var $el, leftPos, newWidth, $mainNav = $("#tour-nav ul");
    $mainNav.append("<li id='magic-line'></li>");
    var $magicLine = $("#magic-line");

	// Set magicline properties
	function magicProp () { 
	    $magicLine
	        .width($("#tour-nav a.selected").width())
	        .css("left", $("#tour-nav a.selected").position().left)
	        .data("origLeft", $("#tour-nav a.selected").position().left)
	        .data("origWidth", $("#tour-nav a.selected").width());
	}
	
	// Initialize magicline properties
	magicProp();
	
	// Magicline Hover
    $("#tour-nav ul li a").hover(function() {
        $target = $(this);
        leftPos = $target.position().left;
        newWidth = $target.parent().width();
        $magicLine.stop().animate({
            left: leftPos,
            width: newWidth
        });
    }, function() {
        $magicLine.stop().animate({
            left: $magicLine.data("origLeft"),
            width: $magicLine.data("origWidth")
        });
    });
    
	$.ajaxSetup ({  
		cache: false  
	});

    // Content Loader
    function contentLoader(target) {
    	// Execute Magicline & arrow link defenition functions
    	magicProp();
    	arrowLink();
    	DD_roundies.addRule('.command-detail', '10px');
    	DD_roundies.addRule('a.button.large', '10px');
    	// Execute Loading Screen
    	$('#sub-wrapper').remove();
    	$('#static-wrapper').addClass('loading');
    	$('#slide-wrapper').removeClass('newSlide').addClass('oldSlide');
    	$('#slideshow').append('<div id="slide-wrapper" class="newSlide"></div>'); 
    	 	
   		// Find and load targets
   		var targetContent = target + ' #slideshow .slide';
   		var targetStory = target + ' #sub-wrapper'; 
   					
		$('.newSlide')
			.hide()	
			.load(targetContent, null, function(){				
				// find new image 
				var _img = $(this).find('img');
				// test if new image is fully loaded 
				_img.load(function(){
					$('#static-wrapper').removeClass('loading');
					// Fade In New Slide
					$('.newSlide').fadeIn(500, function () { 
						$('.oldSlide').remove();
					});
				});
				
				// Load Story    	 
		   		$('#main-wrapper').load(targetStory, function(){$(this).hide().fadeIn(300);}); 
		});
	
    }
    
    // Assign next & prev arrow href values
    function arrowLink () {
    	var prevStory;
    	var nextStory;	
    	if ($('#tour-nav ul li a.selected').parent().hasClass('first')) {
    		prevStory = $('#tour-nav ul li.last').find('a').attr('href');
    		nextStory = $('#tour-nav ul li a.selected').parent().next().find('a').attr('href');
    	} else if($('#tour-nav ul li a.selected').parent().hasClass('last')) { 
    		nextStory = $('#tour-nav ul li.first').find('a').attr('href');
    		prevStory = $('#tour-nav ul li a.selected').parent().prev().find('a').attr('href');
    	} else {
    		prevStory = $('#tour-nav ul li a.selected').parent().prev().find('a').attr('href');
    		nextStory = $('#tour-nav ul li a.selected').parent().next().find('a').attr('href');
    	};
    	$('a.home-prev').attr("href", prevStory);
    	$('a.home-next').attr("href", nextStory);
    }
    
    // Initial Loader
    var target = $('#tour-nav ul li a.selected').attr("href");
   	$('#tour-nav li:first').addClass('first');
    $('#tour-nav li:last').prev().addClass('last');
        
    // Tour nav click function
    $('#tour-nav ul li a').click(function(){ 
    	$('#tour-nav ul li a.selected').removeClass('selected');
    	$(this).addClass('selected');
    	target = $(this).attr("href");
    	contentLoader(target);	
    	return false;  	
    });
    
    // Home-next click function
    $('a.home-next').click(function() {
    	if($('#tour-nav ul li a.selected').parent().hasClass('last')) {
    		var currentStory = $('#tour-nav li:first').find('a');
    	} else {
    		var currentStory = $('#tour-nav ul li a.selected').parent().next().find('a');
    	};	
    	$('#tour-nav ul li a.selected').removeClass('selected');
    	currentStory.addClass('selected');
    	target = $(this).attr("href"); 	
    	contentLoader(target);
    	return false; 
    });

	// Home-prev click function
	$('a.home-prev').click(function() {
		if ($('#tour-nav ul li a.selected').parent().hasClass('first')) {
			var currentStory = $('#tour-nav li:last').prev().find('a');
		} else {
    		var currentStory = $('#tour-nav ul li a.selected').parent().prev().find('a');
    	};	
    	$('#tour-nav ul li a.selected').removeClass('selected');
    	currentStory.addClass('selected');
    	target = $(this).attr("href");  	
		contentLoader(target);
    	return false; 
    }); 
});
