﻿/********************************************************************************************************************************************
MAIN.JS
Copyright 2010-2011 by Matthias Altmann, Systemberater
---------------------------------------------------------------------------------------------------------------------------------------------
HISTORY
---------------------------------------------------------------------------------------------------------------------------------------------
2010-09-14	Created. Added FocusFirst(), CheckForm(), CheckSubmitForm()
2011-05-10	Added Highlightmenu(), ImageTitles(), Lightbox(), LightboxByGroups(), ShowMailLink()
2011-07-10	Added AnimatedDetails(), 
********************************************************************************************************************************************/
var xlate_caution					= 'Achtung!';
var xlate_fill_field_1				= 'Bitte füllen Sie das Feld "%1$s" aus.';
var xlate_select_field_1			= 'Bitte treffen Sie eine Auswahl im Feld "%1$s".';
var xlate_passwords_do_not_match	= 'Die Passwörter stimmen nicht überein.';

var detailsStatus 					= true; // details hidden by default

var base_url = (location.host == 'dmz.altmann.de') ? '/www.altmann.de/' : '/';
var lightbox_url = base_url + 'lib/jquery-lightbox-0.5/';


var DEBUG = (location.host == 'dmz.altmann.de');
function DebugLog(msg)
{
	//if (DEBUG && console) {console.log(msg);}
}

(function($)
{

	//-------------------------------------------------------------------------------------------------------------------------------------------
	// <frm>.FocusFirst() 
	// Sets focus on first visible, enabled form element
	$.fn.FocusFirst = function()
	{
		$(":input:visible:enabled:first",this).focus();
		return this;
	}

	//-------------------------------------------------------------------------------------------------------------------------------------------
	// <frm>.CheckForm()
	// Checks all required fields and password/confirm fields in a form and returns true/false
	$.fn.CheckForm = function()
	{
		var ok = true;
		$(".required",this).each(
			function(i) 
			{
				if ($.trim($(this).val()) == '')
				{
					//var label = $('#label_'+$(this).attr('id')).html().replace(/\<[^\>]+\>/g,'').replace(/:/g,'');
					var label = $('label[for='+$(this).attr('id')+']').html().replace(/\<[^\>]+\>/g,'').replace(/:/g,'');
					var msg = '';
					switch ($(this).attr('type'))
					{
						case 'text': 		msg = xlate_fill_field_1; break;
						case 'textarea': 	msg = xlate_fill_field_1; break;
						case 'password':	msg = xlate_fill_field_1; break;
						case 'select-one':	msg = xlate_select_field_1; break; 
						case 'file': 		msg = xlate_select_field_1; break;
						default:			msg = xlate_fill_field_1; break;
					}
					FieldError(this,sprintf(msg,label)); ok = false; return false;
				}
			}
		);
		if (!ok) return false;
		$("input[type='password']",this).each(
			function(i) 
			{
				var frm = $(this); 
				if ( $(this).attr('id').match(/_confirm$/))
				{
					var pwfieldid = $(this).attr('id').replace(/_confirm$/,'');
					var pwfield = $('#'+pwfieldid);
					if (pwfield)
					{
						if ($.trim(pwfield.val()) != $.trim($(this).val()))
						{
							var label = pwfield.html().replace(/\<[^\>]+\>/g,'').replace(/:/g,'');
							FieldError(pwfield,xlate_passwords_do_not_match); ok = false; return false;
						}
					}
				}
			}
		);
		return ok;
	}
	//-------------------------------------------------------------------------------------------------------------------------------------------
	// <frm>.CheckSubmitForm()
	// Checks a form and submits it if ok. Before submit, set <field> to <value>
	$.fn.CheckSubmitForm = function(field,value)
	{
		if ($(this).CheckForm())
		{
			if (field && value)
			{
				$("input[name='"+field+"']").val(value);
				$(this).submit();
			}
		}
		return false;
	}
	//-------------------------------------------------------------------------------------------------------------------------------------------
	// Shows a dialog box with an error message for an invalid field content/selection. Focusses the related field when dialog box is closed
	function FieldError(field,msg)
	{
		DialogBox( { color:'red', title:xlate_caution, text:msg, onclose:function(){ field.focus(); } } );
	}	

	
	//-------------------------------------------------------------------------------------------------------------------------------------------
	$.fn.HighlightMenu = function()
	{
		// evaluate the current path
		var current_path = window.location.pathname;
		// add "index.html" for directory URL
		if (current_path.lastIndexOf('/') == current_path.length-1) {current_path += 'index.html';}
		DebugLog('current_path: '+current_path);

		var ref = $('a#menuref');
		DebugLog('ref: '+ref);
		if (ref && ref[0]) 
		{
			DebugLog('found a#menu_ref');
			current_path = ref[0].pathname;
			DebugLog('virtual current_path: '+current_path);
		}
		if (current_path.substr(0,1) != '/') {current_path = '/'+current_path;} 

		// mark the current item in menubar
		$('#menu > a').each(
			function(i) 
			{
				var link = $(this);
				var link_path = link[0].pathname; 
				if (link_path)
				{
					// on IE link.pathname does not contain leading slash...
					if (link_path.substr(0,1) != '/') {link_path = '/'+link_path;} 
					DebugLog('link_path: '+link_path);
					//if (link_path == current_path)
					if (current_path.indexOf(link_path) == 0)
					{
						$(this).addClass('active');
						DebugLog('current menu item: '+link_path);
					}
				}
			}
		);
	}
	
	//-------------------------------------------------------------------------------------------------------------------------------------------
	// <document>.ImageTitles
	// Sets TITLE of all images to ALT text except title already defined
	$.fn.ImageTitles = function()
	{
		DebugLog('ImageTitles()');
		$("img",this).each(
			function(i) 
			{
				DebugLog('src='+$(this).attr('src')+', alt='+$(this).attr('alt')+', title='+$(this).attr('title'));
				if (($(this).attr('title') == null) || ($.trim($(this).attr('title')) == ""))
				{
					DebugLog('setting title to '+$(this).attr('alt'));
					$(this).attr('title',$(this).attr('alt'));
				}
				
			}
		);	
	}


	//-------------------------------------------------------------------------------------------------------------------------------------------
	// <element>.Lightbox( [selector] )
	// Enables LightBox for all links of class imageviewer (or any custom selector)
	$.fn.Lightbox = function(selector)
	{
		if (selector == null) {selector = '.imageviewer';}
			$(selector,this).lightBox({	
			fixedNavigation:		false,
			imageLoading:			lightbox_url+'images/lightbox-ico-loading.gif',
			imageBtnPrev:			lightbox_url+'images/lightbox-btn-prev.gif',
			imageBtnNext:			lightbox_url+'images/lightbox-btn-next.gif',
			imageBtnClose:			lightbox_url+'images/lightbox-btn-close.gif',
			imageBlank:				lightbox_url+'images/lightbox-blank.gif',
			txtImage:				'Bild',
			txtOf:					'von'
		});	
	}
	//-------------------------------------------------------------------------------------------------------------------------------------------
	// <document>.LightboxByGroups()
	// Enables LightBox for all links of class lightbox and separates by groups
	$.fn.LightboxByGroups = function()
	{
		var groups = new Array();
		// find all groups in document
		$('a').each(function()
		{
			var group = $(this).attr('group');
			if (group)	{groups[group] = 1;}
		});	
		for (group in groups)
		{
			$(document).Lightbox('a.lightbox[group='+group+']');
		}
	}

	
	


	//-------------------------------------------------------------------------------------------------------------------------------------------
	// <element>.ShowMailLink()
	// Replaces the HTML contents off the specified <element> with a mail link
	$.fn.ShowMailLink = function()
	{
		var ma = new Array('&#115;&#121;&#115;&#116;&#101;&#109;&#98;&#101;&#114;&#97;&#116;&#101;&#114;','&#64;','&#97;&#108;&#116;&#109;&#97;&#110;&#110;&#46;&#100;&#101;');
		$(this).html(	'<a href="'+'mail'+'to:'+ma[0]+ma[1]+ma[2]+'">'+ma[0]+ma[1]+ma[2]+'</a>');
	}

	//-------------------------------------------------------------------------------------------------------------------------------------------
	// <document>.AnimatedDetails
	// Hides all blocks with class .details and insert a [...] link before which will show the details on mouseover.
	$.fn.AnimatedDetails= function()
	{
		var easing = 200;
		
		// in print mode, display all hidden details
		if ($('.details',this).first().css('volume')) 
		{
			alert('print mode');
			$('.details',this).each(
			function(i) 
			{
				$(this).toggle(easing);
			});
			return;
		}
		
		$('.details',this).each(
			function(i) 
			{
				
				$('<span class="noprint" style="color:grey;">[Details]</span>').insertBefore(this);
				$(this).prev().css('cursor','pointer');
				
				//$(this).animate({witdh:'toggle', height:'toggle', opacity: 'toggle'}, easing);
				$(this).toggle(easing);
				$(this).prev().mouseenter(
					function() 
					{
						//$(this).next('.details').animate({width:'toggle', height:'toggle', opacity: 'toggle'}, easing);
						$(this).next('.details').toggle(easing);
					}
				);
				$(this).prev().mouseleave(
					function() 
					{
						//$(this).next('.details').animate({width:'toggle', height:'toggle', opacity: 'toggle'}, easing);
						$(this).next('.details').toggle(easing);
					}
				);

			}
		);
	}
	


})(jQuery); 



$(document).ready(function() {
	$(document).HighlightMenu();
	$(document).ImageTitles();
	$(document).AnimatedDetails();
		
});






