// Global variables
var animation_locked = false;
var default_slide = $('#main_slider .active').attr('id');
var last_loaded_url = '';
var auto_slide_interval = null;

$(document).ready(function(){
	var page_ready = false;

	// Hide any modal window content by default
	$('.modal_content').hide();
	
	function load(url) {
		
		var ajax_url = '[(site_url)]'+url+'?ajax=2';

		$.ajax({
			url: ajax_url,
			success: function(data) {
				$('.modal_content').html(data);
				$('#modal_click').trigger("click");
				
			 },
			 error: function() {
				 //alert("error loading modal data");
			 }
		});

    }

	$.history.init(function(hash){
	    if (page_ready) {

	    	last_loaded_url = hash;

		    // Check first if it's a slide, or any other page
		    if ((hash.length > 5) && (hash.substr(0,5) == 'about')) {
		    	
		    	// Scroll to the top
		    	$('html').scrollTop(0);
		    	
		    	// It's a page, so proceed with ajax call
		    	load(hash);
		    }
		    else {
		    	// Close any fancybox - just to make sure
		    	$.fancybox.close();
		    	
			    // It's a slide
				loadSlide(hash);
		    }
		}
	},
	{ unescape: ",/" });

	page_ready = true;

	$('#left_arrow').click(function(e) {
		e.preventDefault();
		stopSlideShow();
		slide_direction = 'left';
		$('#main_slider li.active .left_arrow').trigger("click");
	});
	
	$('#right_arrow').click(function(e) {
		e.preventDefault();
		stopSlideShow();
		slide_direction = 'right';
		$('#main_slider li.active .right_arrow').trigger("click");
	});

	
	$('a.ajax_link, .ajax_childs a, .left_arrow, .right_arrow, .modal_frame .about_case_studies a, .about_footer a').live('click', function(e) {

		e.preventDefault();
		
		var url = $(this).attr('href');
		url = url.replace(/^.*#/, '');

    	// Avoid that the browser appends the site_url to the link --> happens in IE7 sometimes
    	if (url.length > base_url.length) {
    		if (url.substr(0, base_url.length) == base_url) {
    			url = url.substr(base_url.length);
    		}
    	}
		
		// If the same URL is called, the hash function won't work, so load the request manually here
		if (url == last_loaded_url) {

	    	// Load the URL again
	    	load(url);
		}
		else {
			$.history.load(url);
		}
		
		return false;
	});
	
	// Setup fancybox for the about pages
	$("a#modal_click").click( function() {
		var modal_content = $('.modal_content').html();
		if (modal_content != "") {
			$.fancybox(modal_content, {
				'scrolling': 'no',
				'autoDimensions': false,
				'width': '930',
				'height': '488',
				'margin': '0',
				'padding': '0',
				'overlayColor': '#333',
				'onComplete': function(){ stopSlideShow(); },
				'onClosed' : function() { startSlideShow(); }
			});
		}

	});

});




function preloadGalleryData() {
	var ajax_url = base_url + 'galleries/fluid-10-year-anniversary.html'
	$.ajax({
		url: ajax_url,
		success: function(data) {
			$('#preloaded_data').append('<div id="image_gallery_10_year">'+data+'</div>');
			$("#image_gallery_10_year .zoom").fancybox({
				'scrolling': 'no',
				'cyclic': true,
				'overlayColor': '#333'
			});

			// Add a click event on the ID party_photos_link and trigger actually one of the underlying items
			$('#party_photos_link').live('click', function(e) {
				e.preventDefault();
				$("#image_gallery_10_year .zoom:first-child").trigger('click');
			});
		 },
		 error: function() {
			 //alert("ajax error image gallery");
		 }
	});
}



function loadSlides() {
	var active_slide = $('#main_slider .active').attr('id');

	for (var i=0; i< slides.length; i++) {
		// Load all slides except the current active one
		if (slides[i][1] != active_slide) {
			var ajax_url = base_url + slides[i][0] + '?ajax=1';

			$.ajax({
				url: ajax_url,
				success: function(data) {
					$('#main_slider').append(data)
				 },
				 error: function() {
					 //alert("ajax error");
				 }
			});
		}
	}
}

function loadSlide(hash) {
	var active_slide = $('#main_slider .active').attr('id');
	var new_slide = "";

	if (animation_locked) {
		return; // wait for the animation to be unlocked first
	}

	// Lock the animation here
	animation_locked = true;

	// Hide all other slides first, except the 'active' one as it's overridden by the 'active' setting
	$('#main_slider li').hide();

	// Find the requested slide
	for (var i=0; i< slides.length; i++) {
		if ((slides[i][0] == hash) && ((slides[i][1] != active_slide))) {
			new_slide = slides[i][1];
			break;
		}
	}

	if (new_slide == "") {
		// in case slide not found, set to default slide
		new_slide = default_slide;
		
		// We need to make sure the default slide is not the active slide.
		// In normal operation this should not happen, but in case that happens, unlock and quit
		if (default_slide == active_slide) {
			animation_locked = false;
			return;
		}
	}

	if (slide_direction == 'left') {
		// Move the 'new_slide' LI item to the front
		$('#'+new_slide).prependTo(($('#main_slider')));
	}
	else {
		// Move the 'new_slide' LI item to the end
		$('#'+new_slide).appendTo(($('#main_slider')));
	}
	
	$('#'+new_slide).addClass('sliding');

	if (slide_direction == 'left') {
		$('#'+new_slide).css('left', '-960px');
		$('#'+new_slide).animate({
			left: '+=960'
			}, 750, function() {
				// Animation complete.
				finishAnimation();
		});
	}
	else if (slide_direction == 'right') {
		$('#'+new_slide).css('left', '960px');
		$('#'+new_slide).animate({
			left: '-=960'
			}, 750, function() {
				// Animation complete.
				finishAnimation();
		});
	}
	else {
		// Unknown slide direction, so just reset to zero
		$('#'+new_slide).css('left', '0px');
		finishAnimation();
	}
}

function finishAnimation() {
	var active_slide = $('#main_slider .active').attr('id');
	var new_slide = $('#main_slider .sliding').attr('id');

	$('#'+active_slide).removeClass('active');
	$('#'+new_slide).addClass('active');
	$('#'+new_slide).removeClass('sliding');

	// Unlock the animation
	animation_locked = false;
	
	// Reset the slide direction
	slide_direction = '';
}


function autoNextSlide() {
	slide_direction = 'right';
	$('#main_slider li.active .right_arrow').trigger("click");
}

function startSlideShow() {
	auto_slide_interval = setInterval('autoNextSlide()', 10000);
}

function stopSlideShow() {
	if (auto_slide_interval != null) {
		clearInterval(auto_slide_interval);
	}
}

