/* Author: 

*/

var onOverlayClose;

$(document).ready(function() {

	/* some vars */
	$target			= $('#overlay_body .content');
	$headline		= $('#overlay_pagetitle');
	$subline		= $('#overlay_longtitle');
	$description	= $('#overlay_description');
	$overlay_head	= $('#overlay_head');

	/* prepare overlay */
	$loader = $('<div>').addClass('loader').html('');
	$('#overlay_body').append($loader);
	$('#overlay').hide();
	$('#overlay').removeClass('hidden');
	$('#overlay_blocker').hide();
	$('#overlay_blocker').removeClass('hidden');

	/* catch links for ajax load */
	$('#nav li a, #subnav li a, .articles li a, area, .in_overlay, #overlay_back_handle, #searchresults a').live('click', function(e) {
		e.preventDefault();
	
		if(!$(this).hasClass('no_js')) {
			load_overlay($(this).attr('href'));
		}
	});
	
	$('#overlay_close_handle, #overlay_blocker').click(function(e) {
		e.preventDefault();
		close_overlay();
	});
	
	/* check for hash an load overlay if found */
	var hash = location.hash;
	if(hash && hash != '#!') {
		hash = hash.substr(1, hash.length);
		load_overlay(hash+'.html');
	}
	
	/* hover over area */
	$('area').mouseenter(function() {
		var name = $(this).attr('data-name');
		$('#imagemap').find('div[data-for='+name+']').addClass('active');
	});
	$('area').mouseleave(function() {
		var name = $(this).attr('data-name');
		$('#imagemap').find('div[data-for='+name+']').removeClass('active');
	});
	
	/* hover over unit */
	$('#unit_overview div').live('mouseenter', function() {
		var info = $(this).attr('data-info');
		info = info.replace('FREE', '<span class="blue">FREE</span>');
		$('#unit_info_text').html(info);
	});


	/* QR Code */
	$("#qr").live('click', function(e) {
		e.preventDefault();
			var $close_handle = $('<div>').addClass('close_handle_qr');
			var $imgdiv = $('<div>').attr('id', 'custom_lightbox').addClass('qr').hide().fadeIn(300);
			var $img = $('<img>').attr('src', $(this).attr('src'));
			
			$('body').append($imgdiv);
			$imgdiv.append($img);
			$imgdiv.append($close_handle);
	});

	/* fancybox stuff */
	$("#buildingimages a").live('click', function(e) {
		e.preventDefault();
		$.get($(this).attr('href'), function(data) {
			var $close_handle = $('<div>').addClass('close_handle');
			var $imgdiv = $('<div>').attr('id', 'custom_lightbox').hide().html(data).fadeIn(300);
			$('body').append($imgdiv);
			$imgdiv.append($close_handle);
		});
	});
	
	$('#custom_lightbox').live('click', function() {
		$(this).fadeOut(300, function() {
			$(this).remove();
		});
	});
	
	/* catch contact form and send via ajax */
	$('#contactform').live('submit', function(e) {
	
		e.preventDefault();
		
		if($('#overlay').is(':hidden')) {
			$target.hide();
			$headline.hide();
			$subline.hide();
			$description.hide();
			$loader.show();
			$('#overlay').fadeIn(300);
			$('#overlay_blocker').fadeIn(300);
		} else {
			$target.slideUp(100);
			$headline.fadeOut(100);
			$subline.fadeOut(100);
			$description.fadeOut(100);
			$loader.slideDown(100);
		}
		
		$.ajax({
			url: $('#contactform').attr('action'),
			data: $('#contactform').serialize(),
			type: 'POST',
			success: function(data) {
				var $new_content = $(data).find('#overlay_body .content').html();
				var $new_headline = $(data).find('#overlay_pagetitle').html();
				var $new_subline = $(data).find('#overlay_longtitle').html();
				var $new_description = $(data).find('#overlay_description').html();
				$target.html($new_content);
				$target.slideDown(200);
				$headline.html($new_headline);
				$headline.fadeIn(200);
				$subline.html($new_subline);
				$subline.fadeIn(200);
				$description.html($new_description);
				$description.fadeIn(200);
				$loader.slideUp(200);
				location.hash = file.replace('.html', '');
			}
		});
	
	});
	
	/* change title for ios devices */
	if( navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPod/i) || navigator.userAgent.match(/iPad/i) ) {
		document.title = "Nanocenter";
	}
	
	/* ajax search */
	$('#searchform').live('submit', function(e) {
	
		e.preventDefault();
		search();

	});
	
	var timeout;
	$('#search_sm').live('keydown', function() {
		clearTimeout(timeout);
		timeout = setTimeout('search()', 500);
	    $(this).focus();
	});
	
	

});

function search() {
	var $list = $('#searchresults');
	$loader.slideDown(100);
	
	$list.slideUp(300, function() {
		$.ajax({
			url: 'ajax-searchresults.html',
			data: $('#searchform').serialize(),
			type: 'POST',
			success: function(data) {
				$('#searchresults').html(data);
				$list.slideDown(300);
				$loader.slideUp(100);
			}
		});
	});
}

function load_overlay(file, onclose) {
	
	if(file) {
		if($('#overlay').is(':hidden')) {
			$target.hide();
			$headline.hide();
			$subline.hide();
			$description.hide();
			$loader.show();
			$('#overlay').fadeIn(300);
			$('#overlay_blocker').fadeIn(300);
		} else {
			$target.slideUp(100);
			$headline.fadeOut(100);
			$subline.fadeOut(100);
			$description.fadeOut(100);
			$loader.slideDown(100);
		}
		
		$('#nav a').removeClass('active');
		$('#subnav a').removeClass('active');
		$('a[href=\''+file+'\']').addClass('active');
		
		$.get(file, function(data) {
			if($(data).find('#overlay_head').hasClass('nomargin')) {
				$overlay_head.addClass('nomargin');
			} else {
				if($overlay_head.hasClass('nomargin')) {
					$overlay_head.removeClass('nomargin');
				}
			}
			var $new_content = $(data).find('#overlay_body .content').html();
			var $new_headline = $(data).find('#overlay_pagetitle').html();
			var $new_subline = $(data).find('#overlay_longtitle').html();
			var $new_description = $(data).find('#overlay_description').html();
			$target.html($new_content);
			$target.slideDown(200);
			$headline.html($new_headline);
			$headline.fadeIn(200);
			$subline.html($new_subline);
			$subline.fadeIn(200);
			$description.html($new_description);
			$description.fadeIn(200);
			$loader.slideUp(200);
			location.hash = file.replace('.html', '');
		}, 'html');
	}
	
	if(typeof(onclose) == 'function') {
		onOverlayClose = onclose;
	} else {
		onOverlayClose = false;
	}
	
}

function close_overlay() {
	
	$('#overlay').fadeOut(300, function() {
		if(typeof(onOverlayClose) == 'function') {
			onOverlayClose();
			onOverlayClose = false;
		}
		$('#nav a').removeClass('active');
		$('#subnav a').removeClass('active');
		$('#nav a').first().addClass('active');
		location.hash = '!';
	});
	$('#overlay_blocker').fadeOut(300);
}
