var gInitialHost;
var gLanguage = 0;
var gHideTime = 50;		// to minimize the initial hiding effect
var gCurrentSkin;

$(document).ready(function()
{
	gInitialHost = $.url.attr("host");
	$('#divDialog').dialog({
		autoOpen: false,
		draggable: true,
		modal: true,
		resizable: false
	});

	AssignLinkFunctionality();
	LoadContent($.url.attr("anchor")); 		// loading the initial content
	$('#btnLanguage').click(ToggleLanguage);
});


// at the moment, this should only be fired once per load
// Current Options:
//	Autumn, Winter
function ChangeSkin(skin)
{
	gCurrentSkin = skin;

	classList = [		// list of all classes affected by the skin
		'headerDiv',
		'headerLinks',
		'bodyDiv',
		'sidebar',
		'content',
		'linksidebar'
	]					// don't forget to change the elements, too (listed below)
	
	for (var index = 0; index < classList.length; index++)
	{
		$('.' + classList[index]).addClass(classList[index] + gCurrentSkin);
	}
	
	ChangeSkinContentLoad();
}

// reapplys the current skin to elements. Should be called on content load
function ChangeSkinContentLoad()
{
	elementList = [
		'body',
		'a'
	]
	
	for (var index2 = 0; index2 < elementList.length; index2++)
	{
		$(elementList[index2]).addClass(elementList[index2] + gCurrentSkin);
	}
}


function AssignLinkFunctionality(jQuerySelector)
{
	if (jQuerySelector == null)
		jQuerySelector = 'a';

	$(jQuerySelector).click(function()
	{
		$.url.setUrl(this.href);

		if ($.url.attr("host") == gInitialHost && !IsNullOrEmpty($.url.attr("anchor")))
			LoadContent($.url.attr("anchor"));
		else if ($.url.attr("host") != gInitialHost)				// if we're going to a different site
		{
			window.open(this.href);
			return false;
		}
	});
}

function PrepGallery()
{
	$("a[rel^='gallery']").colorbox({ opacity:.55 });
}

function ToggleLanguage()
{
	if (gLanguage == 0)
		gLanguage = 1;
	else
		gLanguage = 0;
		
	SetLanguage();
}

function SetLanguage()
{
	if (gLanguage == 0)		// English only
	{
		$('#btnLanguage').val('English & German');
		$('.G').hide();
	}
	else					// both
	{
		$('#btnLanguage').val('English Only');
		$('.G').show();
	}
}

function ShowDialog(contentElementID)
{
	var width = document.getElementById(contentElementID).getAttribute("width")
	
	if (width == null)
	{
		width = 300;
	}
	else if (!IsNumeric(width))
	{
		if (width == 'small') width = 300;
		if (width == 'medium') width = 500;
		if (width == 'large') width = 700;
	}

	$('#divDialog')
		.html($('#' + contentElementID).html())
		.dialog("option", "title", $('#' + contentElementID).attr('title'))
		.dialog("option", "width", width)
		.dialog('open');

	AssignLinkFunctionality("#divDialog a");
}

function LoadContent(filename)
{
	$('#divDialog').dialog('close');						// close dialog first
	
	if (filename == null) filename = 'home.html';			// home page if nothing is defined
	if (filename == 'null') return;							// abort if the destination is named 'null'

	if (filename.indexOf("dialog") > -1) {					// if a dialog box
		ShowDialog(filename.substr(7));							// only return the elementID (gets rid of 'dialog/')
		return;
	}
	
	$('html, body').animate({scrollTop:0}, 'slow');			// slowly scroll to the top of the page
	
	var setLanguage = false;
	if (filename.indexOf("contact") > -1)					// showing the Language button if necessary
		setLanguage = true;

	filename = filename.replace(/.*#/, '');					// fixing links (removing everything before (and including) the # symbol)
	if (filename.indexOf('.') < 0) filename += '.html'; 	// fixing the filename if file type isn't provided
	filename = filename.toLowerCase(); 						// lowercase because of linux server


	// loading and fading the content
	$('#divContentHolder')
		.fadeOut('normal', function()		//400ms is the default
		//.hide('blind', {}, gHideTime, function()
		{
			gHideTime = 500;								// changing the effect time to its normal timing

			$('#divContent').load('content/' + filename, function(data)
			{
				if (IsNullOrEmpty(data))					// 404
					$('#divContent').html("Sorry, but we couldn't find the page you were looking for.")

				if (setLanguage)
				{
					SetLanguage();
					$('#divLanguage').show();
				}
				else
					$('#divLanguage').hide();

				ChangeSkinContentLoad();
				AssignLinkFunctionality('#divContentHolder a');
				PrepGallery();
				
				$('#divContentHolder')
					.fadeIn('normal');
					//.show('blind', {}, gHideTime);
			});
		});

		
	return false;
}

function IsNullOrEmpty(value)
{
	return (!value || Trim(value) == '');
}

function IsNumeric(sentText)
{
	var ValidChars = "0123456789.";
	var Char;

	for (var i = 0; i < sentText.length; i++)
	{
		Char = sentText.charAt(i);
		if (ValidChars.indexOf(Char) == -1)
			return false;
	}

	return true;
}

function Trim(value)
{
	return value.replace(/^\s+|\s+$/, '');
}

function ResizeAndShow(image, maxWidth, maxHeight)
{
	if (maxWidth == null) maxWidth = 200;				// match these to the CSS
	if (maxHeight == null) maxHeight = 140;

	var originalWidth = image.width;
	var originalHeight = image.height;

	var ratio = Math.min(maxWidth / originalWidth, maxHeight / originalHeight);
	image.width = Math.round(ratio * originalWidth);
	image.height = Math.round(ratio * originalHeight);

	image.style.display = 'block';
}

