
var desktop_frame = 'f2';
var mobile_frame  = 'f3';
var splash_frame  = '';

var content_sections = ['download', 'features', 'screenshots', 'support', 'opensource'];

function show_desktop(e) {
	if (e) {
		e.preventDefault();
	}

	$('#submenu').show();
				
	$('#splash>img').addClass('darkened');

	if ( $('#content').is(':visible') ) {
		$('#content').hide('blind', function() {
			$('#splash').show('blind');
		});
	};
	
	if ( $('#submenu-mobile').is(':visible') ) {
		$('#submenu-mobile').hide( 'blind', function() {
			$('#submenu-desktop').show( 'blind' );
		});
	} else {
		$('#submenu-desktop').show( 'blind' );
	}
	$('#button-desktop').removeClass('darkened');
	$('#button-mobile').addClass('darkened');

	scrollToTop();
};

function show_mobile(e) {
	if (e) {
		e.preventDefault();
	}

	$('#submenu').show();
	
	$('#splash>img').addClass('darkened');
	
	if ( $('#content').is(':visible') ) {
		$('#content').hide('blind', function() {
			$('#splash').show('blind');
		});
	};

	
	if ( $('#submenu-desktop').is(':visible') ) {
		$('#submenu-desktop').hide( 'blind', function() {
			$('#submenu-mobile').show( 'blind' );
		});
	} else {
		$('#submenu-mobile').show( 'blind' );
	}
	$('#button-desktop').addClass('darkened');
	$('#button-mobile').removeClass('darkened');
	
	scrollToTop();
};

function show_splash(e) {
	if (e) {
		e.preventDefault();
	}
	
	$('#splash>img').removeClass('darkened');
	$('#button-desktop').removeClass('darkened');
	$('#button-mobile').removeClass('darkened');
	
	if ( $('#content').is(':visible') ) {
		$('#content').hide('blind', function() {
			$('#splash').show('blind');
		});
	};
	
	if ( $('#submenu').is(':visible') ) {
		$('#submenu').hide('blind');
	}
	$('#submenu-desktop').hide();
	$('#submenu-mobile').hide();
	
	scrollToTop();
	
};


function show_content(e,type) {
	if (e) {
		e.preventDefault();
	}

	$('.content-panel').hide();
	
	function reveal_content_element() {
		$('.content-panel').each( function() {
			if ( this.id !== type ) {
				$(this).hide();
			}
		});
		$('#'+type).show('slide');
	}
	
	
	if ( $('#splash').is(':visible') ) {
		$('#splash').hide('blind', function() {
			$('#content').show('blind', function() {
				reveal_content_element();
			});
		});
	} else {
		reveal_content_element();
	}
	// if (e) {
	// 	e.stopPropagation();
	// }
	
	scrollToTop();
	
	// return false;
};


function process_hash(hash) {
	
	if (hash.indexOf('?') === 0) {
		hash = hash.replace('?', '#');
	}
		
	switch (hash) {
		case '#submenu-mobile':
			show_mobile();
			break;
		case '#submenu-desktop':
			show_desktop();
			break;
		default:
			if (hash.indexOf('desktop') !== -1) {
				show_desktop();
				show_content(null, hash.slice(1))
			}
			if (hash.indexOf('mobile') !== -1) {
				show_mobile();
				show_content(null, hash.slice(1))
			}
			break;
			
	}
	
}


function scrollToTop() {

	function _scroll() {
		$.scrollTo('#titlebar');
	}
	
	setTimeout(_scroll,1);
}



$().ready( function() {
	/*
		hide at startup
	*/
	$('#content').hide();
	$('#submenu').hide();
	show_splash();
	
	$('#titlebar').click(show_splash);
	$('#button-mobile').click(show_mobile);
	$('#button-desktop').click(show_desktop);

	$.each( content_sections, function(i) {
		var type = this;
		$('#button-mobile-'+type).click(function(e) {
			show_content(e,'mobile-'+type);
		});
	});

	$.each( content_sections, function(i) {
		var type = this;
		$('#button-desktop-'+type).click(function(e) {
			show_content(e,'desktop-'+type);
		});
	});
	
	
	/*
		Flickr screenshots
	*/
	$('#spaz-desktop-flickr').flickr({     
		api_key: "e0462e3921ae1f3a87f0409b737d1fca",     
		per_page: 30,
		type:     'search', 
		user_id:  '39241541@N00',
		tags:     'spaz',
		sort:     'date-posted-desc',
		params:   '&content_type=7',
		callback: function(list) {
			$('#spaz-desktop-flickr ul li a').lightBox({
				imageLoading: 'http://funkatron.com/assets/jquery.lightbox/images/lightbox-ico-loading.gif',
				imageBtnClose:'http://funkatron.com/assets/jquery.lightbox/images/lightbox-btn-close.gif',
				imageBtnPrev: 'http://funkatron.com/assets/jquery.lightbox/images/lightbox-btn-prev.gif',
				imageBtnNext: 'http://funkatron.com/assets/jquery.lightbox/images/lightbox-btn-next.gif'
			});
		}
	});

	$('#spaz-mobile-screens a').lightBox({
		imageLoading: 'http://funkatron.com/assets/jquery.lightbox/images/lightbox-ico-loading.gif',
		imageBtnClose:'http://funkatron.com/assets/jquery.lightbox/images/lightbox-btn-close.gif',
		imageBtnPrev: 'http://funkatron.com/assets/jquery.lightbox/images/lightbox-btn-prev.gif',
		imageBtnNext: 'http://funkatron.com/assets/jquery.lightbox/images/lightbox-btn-next.gif'
	});
	
	
	
	$('a[name]').css('position', 'absolute')
				.css('top',      '0px')
				.css('left',     '0px')
				.css('border',   '1px solid blue');
	
	/*
		replace hashes with question marks in local hrefs urls
	*/
	// $("a[href^='#']").each(function(i) {
	// 	$(this).removeAttr('href');
	// });
	
	if (location.hash) {
		process_hash(location.hash);
	}
	
	
	
});
