function fontReplace() {
	Cufon.replace('#topmenu a', { hover: true });
}
fontReplace();

var ratio = 1697/1129;
var slideWidth = 0;
var slideHeight = 0;
var w;
var h;
var r;

var page = 1;


function setSize() {
	w = $(window).width();
	h = $(window).height();
	
	$('#bgImage').css("width",10*w+'px'); // made the width of bgImage bigger than it should be.

	//console.log(w+','+h);
	
	r = w/h;
	if (r > ratio) {
		slideWidth = w;
		slideHeight = w/ratio;
	} else {
		slideHeight = h;
		slideWidth = h*ratio;
	}
		
	$("#container img").attr("width",slideWidth);
	$("#container img").attr("height",slideHeight);
		
	$('#container').css("margin-left",-slideWidth/2);
	$('#container').css("margin-top",-slideHeight/2);
		
	$('#bgImage').css("margin-left",-(page-1)*slideWidth);
	
}


function slideTo(from,to, page_content) {
	closePopup();
	if (typeof(from) == 'undefined') from = 0;

	if (typeof(page_content) == 'undefined') {
		page = to;

		switch(page) {
			case 2: page_content = 'our_approach'; break;
			case 3: page_content = 'what_we_do'; break;
			case 4: page_content = 'reference_list'; break;
			case 5: page_content = 'spotlight_on_partners'; break;
			case 6: page_content = 'who_we_work_for'; break;
			case 7: page_content = 'talk_to_us'; break;
			case 8: page_content = 'work_for_us'; break;
			case 1:
			default: page_content = 'home';
		}

	} else {
		switch(page_content) {
			case 'our_approach': page = 2; break;
			case 'what_we_do': page = 3; break;
			case 'reference_list': page = 4; break;
			case 'spotlight_on_partners': page = 5; break;
			case 'who_we_work_for': page = 6; break;
			case 'talk_to_us': page = 7; break;
			case 'work_for_us': page = 8; break;
			case 'home':
			default: page = 1;
		}
	}
	
	if (page > 0 && page < 9 && page != from) {
		if (to == '') {
			$('#bgImage').css('margin-left',-(page-1)*slideWidth);
			activeMenu('#tab-0'+page);
			
		} else {
			$('#bgImage').animate({'marginLeft':-(page-1)*slideWidth},1500, function() {
				activeMenu('#tab-0'+page);
	
			});
		}
		
		$.get('content.php', {content:page_content}, function(data) {
			$('#content').html(data);
			$('#topmenu li').each(function() {
				$(this).removeClass('active');
			});
			$('#tab'+page).addClass('active');
			fontReplace();
			imgRollover();
		});
		
		$.history.add(page_content);
				
	}
}


// menu section
function activeMenu(id) {
	$('#main-nav .rollover').show();
	$('#main-nav .active').hide();
	$(id+" .active").show();
	$(id+" .rollover").hide();
}

function imgRollover() {
	$("img.rollover").hover(
		function() { $(this).attr("src", $(this).attr("src").split("_s.").join("_o.")); },
		function() { $(this).attr("src", $(this).attr("src").split("_o.").join("_s.")); }
	);
}

// on ready, on change...
$(document).ready(function() {
	$('.fancybox').fancybox();
	
	$("#topmenu li a").hover(
		function () {
			if($(this).parent().hasClass('active') == false) {
				$(this).parent().addClass('hover');
			}
		},
		function () {
			$(this).parent().removeClass('hover');
		}
	);

	setSize();
	imgRollover();
	activeMenu('#tab-01');
});
$(window).resize(function(){
	setSize();
});


/* The following script is added to the document onready queue */
$(function() {
	$('#content').history(function(e, currentHash, previousHash) {
		if(currentHash == '') {
			currentHash = 'home';
		}
		slideTo('','',currentHash);
	});
	
	/* Use getCurrent() function to get the hash for the initial load or if the user refreshes the page */
	var initialHash = $.history.getCurrent();
	if(initialHash == '') {
		initialHash = 'home';
	}
	slideTo('','',initialHash);
});


/* legal info */
function showPopup() {
	$("#main").fadeOut(300, function() {
		$("#popup").fadeIn(300);
	});
	
}

function closePopup() {
	$("#popup").fadeOut(300, function() {
		$("#main").fadeIn(300);
	});
}
