/*Cufon.replace('#menu-horizontal ul li a', {
	hover: true
});
Cufon.replace('#company_quote');
Cufon.replace('#news_header');
*/
$(document).ready(function(){
	// Enables deep linking for ajax
	$.address.change(function(event) {
		if ( event.value != '/' ) {
			slideStop();
			$('#content').load(event.value, function(){
				//Cufon.replace('#content .title');
				
				//Lightbox
				execLightbox();
			});
			
			$('#menu-horizontal li[id^="page_"]').removeClass('active');
			$('#menu-horizontal #page_'+event.pathNames[2]).addClass('active');
			//Cufon.refresh('#menu-horizontal ul li a');
		} else {
			slideStart();
		}
	});
	$('a.ajax').click(function() {
		$.address.value($(this).attr('href'));
		
		return false;
	});
	/*
	// Highlight first word using a regex
	$('#menu-vertical li.item > a').each(function(){
		var item = $(this);
		
		item.html(item.text().replace(/(^\w+)/, '<span class="first">$1</span>'));
	});
	*/
	// Menu vertical hover
	$('#menu-vertical ul#menu li').hover(function(){
		$(this).addClass('active');
	}, function(){
		$(this).removeClass('active');
	});
	
	// Vertical menu fixes
	$('#menu ul').css({display: 'none'}); // Opera Fix
	$('#menu li').hover(function(){
		$(this).find('ul:first').css({visibility: 'visible',display: 'none'}).show();
	},function(){
		$(this).find('ul:first').css({visibility: 'hidden'});
	});
	
	$('#languages a').click(function(){
		var href = $(this).attr('href');
		var hash = ( $.address.path() == '/' ) ? '' : '#'+$.address.path()
		
		$(this).attr('href', href+hash);
	});
	
});

function slideStart(){
	$('#content').hide();
	
	initImageIndicator();
	
	$('#slideshow').show().cycle({
		fx: 'fade',
		before: function(curr){
			$('#image_indicator .indicator').removeClass('active');
			$('#image_indicator .indicator:eq('+$(this).index()+')').addClass('active');
		}
	});
};

function initImageIndicator(){
	var count = $('#slideshow img').length;
	
	$('#image_indicator').empty();
	
	for ( i=1; i<=count; i++ ){
		$('#image_indicator').append('<div class="indicator"></div>');
	}
	
	$('#image_indicator .indicator:first').addClass('active');
};

function slideStop(){
	$('#slideshow').cycle('destroy').hide();
	$('#image_indicator').empty();
	$('#content').show();
};

function execLightbox(){
	$('img.lightbox').lightBox({
		imageLoading:	'images/lightbox/lightbox-ico-loading.gif',
		imageBtnClose:	'images/lightbox/lightbox-btn-close.gif',
		imageBtnNext:	'images/lightbox/lightbox-btn-next.gif',
		imageBtnPrev:	'images/lightbox/lightbox-btn-prev.gif',
		imageBlank:		'images/lightbox/lightbox-blank.gif'
	});
};