/*
	JS by Joshua Gatcke http://www.99lime.com
*/

$(document).ready(function(){

	//alert('jq');

	/*----------------------------------
		Tour First Load/View
	-----------------------------------*/
	// get the url
	// split url to findout what page we are on
	//var page = document.location.toString();
	//var location = page.split('/');
	//var view = 'tour/'+location[location.length-1];
	//load_view(page);
	view_links();
	//do_nav();
	$('.parallax-layer').parallax({mouseport:'.holder'});
	load_thumbs();
	
	/*----------------------------------
		Global Link Delegation
	-----------------------------------*/
	$('body').click(function(e)
	{
		/*----------------------------------
			 Tour Links
		-----------------------------------*/
		if($(e.target).is('a') && $(e.target).attr('rel') == 'view')
		{
			e.preventDefault();
			var link = $(e.target).attr('href');
			load_view(link);
		}
		else if($(e.target).parent('a').attr('rel') == 'view')
		{
			e.preventDefault();
			var link = $(e.target).parent().attr('href');
			load_view(link);
		}
	});

	/*----------------------------------
			Tour Load View
	-----------------------------------*/
	function load_view(link)
	{
		var load_url = link + '?loaded=true';
	
		$('#tour .holder').addClass('remove');
		$('#tour').append('<div class="holder"></div>');
		// load the content via AJAX
		$('#news').fadeOut('slow');
		$('#tour div.cover').fadeIn('slow',function(){
			$('.holder').not('.remove').load(load_url, function(){
					if($(this).find('img').length > 0) {
						// once images load in new view - show it
						$('.holder').not('.remove').onImagesLoad({selectorCallback: showView});
					}else{
						showView();
					}
			});
		});
	}
	
	function showView(){
		$('.holder').not('.remove').show();
		$('#tour .remove').remove();
		$('.parallax-layer').parallax({mouseport:'.holder'});
		$('#tour div.cover').fadeOut('slow');
		view_links();
		load_thumbs();
		// do_nav();
		$('.scroll').jScrollPane();
		$('#news').fadeIn('slow');
	}
	
	function view_links()
	{
		// view-links
		$('a.view-link').hover(function(){
			$(this).stop().animate({'padding':'10px 20px','color':'#fff'}, 500);
		},function(){
			$(this).stop().animate({'padding':'5px 10px','color':'#999'}, 500);
		});
	}
	
	/*----------------------------------
			Navigation menu slide
	-----------------------------------*/
	function do_nav()
	{
		$('#nav ul').hide();
		$('#nav li').hover(function(){
			$(this).find('ul').stop(true,true).slideDown('slow');
		},function(){
			$(this).find('ul').stop(true,true).slideUp('fast');
		});
	}
	
	/*----------------------------------
			Tour Thumbnails
	-----------------------------------*/
	function load_thumbs()
	{
		// find all view images
		// for each one - create a thumbnail
		$('.holder img.view').each(function(i,val){
			// get the thumbnail path
			var path = $(this).attr('src');
			path = path.split('/');
			var image = path[path.length-1];
			path = 'content/res2/thumbs/'+image;
			
			// add class to rel to big image
			$(this).attr('rel',image);
			
			// select first thumb
			var selected = '';
			if(i == 0){ selected = ' class="selected" '; }
			
			// create thumbnails
			$('#thumbs').append('<a href="'+path+'" rel="'+image+'"'+selected+'><img src="'+path+'" /></a>');
			$('#thumbs a').not('.selected').animate({opacity:0.7},1000);

		});
		
		// add event listener to thumbs
		$('#thumbs a').click(function(e){
			e.preventDefault();
			// fade out all big images
			$('.holder img.view').fadeOut('slow');
			$('.holder img[rel='+$(this).attr('rel')+']').fadeIn('slow');
			$('#thumbs a').removeClass('selected').animate({opacity:0.7},500);
			$(this).addClass('selected').animate({opacity:1},500);
		});
		
		$('#thumbs a').hover(function(){
			$(this).stop(true,true).animate({opacity:1},500);	
		},function(){
			$(this).not('.selected').stop(true,true).animate({opacity:0.7},500);	
		});

	}
	
	/*----------------------------------
			Custom Scrollbars
	-----------------------------------*/
	$('.scroll').jScrollPane();

});

	/*----------------------------------
			ShadowBox
	-----------------------------------*/
	Shadowbox.init({
   		// let's skip the automatic setup because we don't have any
   		// properly configured link elements on the page
   	skipSetup: true
	});
	
	// open a welcome message as soon as the window loads 
   /* window.onload = function(){
	    Shadowbox.open({
	        content:    '<div style="text-align:center;"><img src="content/brunch_popup.jpg" align="center" /></div>',
	        player:     "html",
	        title:      "Welcome",
	        height:     600,
	        width:      400
	    });
    
    };  */
