var hasFlashInstalled		=	true;


$(document).ready(function() {
	hasFlashInstalled		=	(Object(swfobject.getFlashPlayerVersion()).major != 0);
	//hasFlashInstalled		=	false;
	
	if(!hasFlashInstalled){
		$('#backToFXmedia').remove()
	}

	//Cufon.replace('.menuItem, .SubmenuItem', {  fontFamily:'DIN', hover: {color: '#3769A0'} });
	$newBlok			=	$('#first');
	$currentBlok		=	$('#second');
	
	vendor	=	navigator.vendor;
	if ( vendor!= null){
		if( vendor.indexOf("Google")!=-1 && jQuery.browser.version<400){
			Cufon.replace('.menuItem, .SubmenuItem', {  fontFamily:'DIN', hover: {color: '#3769A0'} });
				
			replaceItems	=	".HTMLcontent p, .HTMLcontent span, .HTMLcontent h1, .HTMLcontent li, .HomeAnnouncement, .HomeTitle, .HomeDescription, .HomeUrlTekst, .infoBlokProject #project span";
			Cufon.replace(replaceItems,{ fontFamily:'DIN'})
		}
		if (vendor.indexOf("Apple")!=-1 ||  vendor.indexOf("Google")!=-1 ){
			animation			=	true;
		} 
	}
});



// Event handlers
$.address.init(function(event) {
	$('a').address(function() {
		var href 	=	$(this).attr('href');
		href = href.replace('http://www.fxmedia.nl', '');
		href = href.replace('http://beta.fxmedia.nl', '');
		
		if(	href.indexOf('mailto:')==0 ){
			window.location	=	href;
		}else if(href.indexOf('http:')==0){
			if(hasFlashInstalled){
				openIframe(href)
			}else{
				window.location	=	href;
			}
		}else{
			href = href.replace(location.pathname, '');
			return href;
		}
	});
	if(event.value!='/'){
		animation	=	false;
	}
}).change(function(event) {
	try{
		var text = (event.value == '') ? 'Home' : 
			event.pathNames[0].substr(0, 1).toUpperCase() + 
			event.pathNames[0].substr(1);
		$('a').each(function() {
			$(this).toggleClass('selected', $(this).text() == text);
		});
	}catch(e){}
}).internalChange(function(event) {
	onAddressChange(event.value,false)
}).bind('externalChange', {msg: 'The value of the event is "{value}".'}, function(event) {
	onAddressChange(event.value,true)
});


function openIframe(url){

	$('#fullScreenMedia').append('<div class="imgMask" style="width:100%"><iframe src ="'+url+'" width="100%" height="100%"><p>Your browser does not support iframes.</p></iframe></div>')
	$('#wrapper').animate2({top:'-100%'}, 1000);
	$('#mediaHolder img').click(goNormalScreen);
}

/*
 *
 *		General functions
 *
 */
function changeAddress(_url){
	$.address.path(_url)
	currentPage	=	_url;
	allowBackSwitching()
}function changeTitle(_title){
	$.address.title(_title)
}


/*
 *
 *		Loading and animate screens
 *
 */
var $newBlok;
var $currentBlok;
var animation			=	false;

var vendor	=	navigator.vendor;




var lastCategory;
function onAddressChange(page, ext){
	if(window.location.pathname == '/en/'){
		language	=	'en';
	}else{
		language	=	'nl';
	}
	
	var pageUrl		=	page.split('/');
	var category	=	pageUrl[1]
	
	if(category==lastCategory){
		if(category=='cases' && ext != true && page!='/cases/index.html' && hasFlashInstalled){
			//Cases intern herladen
		}else if(currentPage!=page&& page!='/'){
			animation			=	false;
			openNewContent(page)
		}else{
			goNormalScreen()
		}
	}
	else if (page!=null && page != '/') {
		openNewContent(page)
		lastCategory	=	category;
	}
}

var menuItems		=	new Array('');
var currentPage;
var currentItemID	=	-1;

function openNewContent(page){
	currentPage	=	page;
	if(page == ' '){
		lastCategory	=	'';
	}
	
	try{
		var newItemID	=	menuItems.indexOf(page);
		goNormalScreen()
	}catch(e){}
	
	//
	if(!animation){
		$currentBlok.find('.contentBlockContent').load('/_php/show_contentItem.php?random='+Math.random(),{openedPage:page, taal:language},onPageLoaded)
	}
	else{
		$newBlok.find('.contentBlockContent').load('/_php/show_contentItem.php?random='+Math.random(),{openedPage:page, taal:language},onPageLoaded)//,function(response, status, xhr) {});
		var oldNewLeft			=	'-50%';
		var newOldLeft			=	'150%';
		
		if(newItemID<currentItemID){
			oldNewLeft			=	'150%';
			newOldLeft			=	'-50%';
		}
		currentItemID	=	newItemID;
		
		$currentBlok.stop(true,false).animate({left: oldNewLeft/*,easing:'easeInOutQuad'*/}, 1000,
			function() {
				$(this).find('.contentBlockContent').html('');
				$(this).css({left:'-50%'});
			}
		);
		$newBlok.css({left: newOldLeft}).stop(true,false).animate({left: '50%'/*,easing:'easeInOutQuad'*/}, 1000, 
			function() {
				//
			}
		);
		
		$currentBlok_temp	=	$newBlok;
		$newBlok			=	$currentBlok;
		$currentBlok		=	$currentBlok_temp;
			
		//onPageLoaded()
		//});
	}
	
	if ( vendor!= null && (vendor.indexOf("Apple")!=-1 ||  vendor.indexOf("Google")!=-1 )){
		animation			=	true;
	} 
}

