jQuery.extend(  
  jQuery.expr[ ":" ],  
  {  
    /* 
      /:\/\// is simply looking for a protocol definition. 
      technically it would be better to check the domain 
      name of the link, but i always use relative links 
      for internal links. 
    */  
  
    external: function(obj, index, meta, stack)  
    {  
      return /:\/\//.test($(obj).attr("href"));  
    },  
  
    internal: function(obj, index, meta, stack)  
    {  
      return !/:\/\//.test($(obj).attr("href"));  
    }  
  }  
);

jQuery.extend({
  getUrlVars: function(){
    var map = {};
    var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
    map[key] = value;
    });
    return map;
  },
  getUrlVar: function(name){
    return $.getUrlVars()[name];
  }
});

function showNextTab() {
	currentTab++;
	if (currentTab > tabs) { currentTab = 1 };
	//$("#home-header ul.navigation li:nth-child("+currentTab+") a").click();
	tabName = $("#home-header ul.navigation li:nth-child("+currentTab+") a").attr('rel');
	showTab(tabName, true);
}

function showTab(name, fade) {
	//if($(this).hasClass("selected")) {
	//	return;
	//}
	if (!fade) {
		fade = false;
	}
	$("#home-header ul.navigation li").removeClass("selected");
	$("#home-header ul.navigation li a").each(function() {
		if ($(this).attr('rel') == name) {
			$(this).parent().addClass("selected");
		}
	});
	if (fade) { $("#home-header .content").fadeOut(500) } else { $("#home-header .content").hide() }
	if (fade) { $("#"+name).fadeIn(500) } else { $("#"+name).show() };
	$("#home-header .bg").css('z-index', -1);
	if (fade) {
		$("#bg-"+name).css('z-index', 0).fadeIn(500, function() {
			$("#home-header .bg").not("#bg-"+name).hide();
		});
	}
	else {
		$("#bg-"+name).css('z-index', 0).show();
		$("#home-header .bg").not("#bg-"+name).hide();
	}
}

function blink() {
	$('#sign406x186 .eyelid').fadeIn(0).delay(100).fadeOut(0)
	setTimeout("blink()", 6000 + (Math.random()*8000))
}

$(document).ready(function()
{
	
	$('#sign406x186').css('top', '235px');
	$('#sign406x186').show();	
	
	// Convert form links to ajax ones.
	$('.form-link').each(function() {
	  // Get any get vars passed to the page and forward them on to the form.
	  get_vars = $.getUrlVars();
		if (file = $(this).attr('id')) {
			$(this).attr('href', '/forms/' + $(this).attr('rel') + '.php?file_id=' + file + "&" + $.param(get_vars));
		}
		else {
  		$(this).attr('href', '/forms/' + $(this).attr('rel') + '.php?' + $.param(get_vars));
		}
	});
	
	tabs = $('#home-header .content').size();
	currentTab = 1;
	
	setTimeout("blink()", 1000 + (Math.random()*2000))

	// Setup the news ticker
	$("#ticker").vTicker({
		speed: 750,
		pause: 4000,
		showItems: 3,
		animation: 'fade',
		mousePause: true,
		height: 0,
		direction: 'up'
	});
	
	// Fix IE6 issues.
	if ($.browser.msie && $.browser.version == 6) {
		// Fix menu items.
		$("#header .top ul.navigation li, #navigation li").hover(
			function() { $(this).addClass("hover"); },
			function() { $(this).removeClass("hover"); }
		);
	}
	
	$('#sign406x186').hover(function() {
		$(this).stop().animate({ top: '109px' })
	},
	function() {
		$(this).stop().animate({ top: '235px' })
	})
	
	// tack the language on to the end of every internal link that doesn't already have it.
	//$('a:internal[href$=.php]').each(function() {
	//	$(this).attr('href', $(this).attr('href') + '?lang='+lang);
	//});
	//$('a:internal[href$=/]').each(function() {
	//	$(this).attr('href', $(this).attr('href') + '?lang='+lang);
	//});

	$("#home-header ul.navigation li a").click(function(e) {
		e.preventDefault();
		clearInterval(ref);
		showTab($(this).attr('rel'));
	});
	
	var ref = setInterval("showNextTab()", 15000);

	$("#right-column ul.navigation li").click(function() {
		
		$("#right-column ul.navigation li").removeClass("selected");
		$(this).addClass("selected");
		$("#right-column .content").hide();
		$("#right-column ul.navigation li").each(function() {
			$('#' + $(this).attr('id') + '_content').hide();
		})
		$('#' + $(this).attr('id') + '_content').show();
	});
	
	function formatTitle(title, currentArray, currentIndex, currentOpts) {
	    if (title != undefined) {
			return '<h2>' + title + '</h2>';
		}
	}

	$(".fancy").not(".form-link").fancybox({
		"titlePosition"  : "inside",
		"transitionIn"   : "fade",
		"transitionOut"	 : "fade",
		"overlayOpacity" : 0.45,
		"overlayColor"   : "black",
		'titleFormat'	 : formatTitle,
		"scrolling"      : "no",
		"width"			 : 650,
		"height"		 : 406
	});

	$(".form-link").fancybox({
		"onComplete"	 :	function() { // disable mousewheel and arroy key nav for forms because we are using rel.
								$("#fancybox-wrap").unbind('mousewheel.fb');
								$(document).unbind('keydown.fb');
							},
		"titlePosition"  : "inside",
		"transitionIn"   : "fade",
		"transitionOut"	 : "fade",
		"overlayOpacity" : 0.45,
		"overlayColor"   : "black",
		'titleFormat'	 : formatTitle,
		"scrolling"      : "auto",
		"showNavArrows"  : false
	});
	
	$(".map-link").fancybox({
		"onComplete"	 :	function() { // initialize the google maps api
								map_initialize();
							},
		'width'			: 960,
		'height'		: 640
	})
	
	function show_movie(movie) {
		$.fancybox([
			'/flash/player.swf?skin=/movies/nacht.zip&autostart=true&file=/movies/'+movie,
		], {
			'showNavArrows'		: false,
			'type'			: 'swf',
			'width'			: 600,
			'height'		: 400,
			'swf'			: {
			   	 'wmode'		: 'transparent',
				'allowfullscreen'	: 'true'
			}
		});
	}
	
});
