
// Donavii.com JavaScript Library

// (c) Donavii.com / Michael Sedlaczek, Koeln

var timeouthandle;

// init fancybox links
$( document ).ready( 
	function() 
	{
		$( "a.zoomthis" ).fancybox
			(
				{
					'overlayOpacity'	: 0.5,
					'overlayColor'		: 'black',
					'padding'			: 20
				}
			);
	}
);

function updateCategory( source )
{
	switch( source.value )
	{
		case "3":
			$( "#type" ).attr( "value", "Buch" );
			break;

		case "250":
			$( "#type" ).attr( "value", "DVD/Blu-ray" );
			break;

		case "15":
			$( "#type" ).attr( "value", "Musik" );
			break;

		case "51":
			$( "#type" ).attr( "value", "Computerspiel" );
			break;
			
		default:
			$( "#type" ).attr( "value", "" );
			break;
	}
}

function hideShowDueDate()
{
	if( document.getElementById( "type1" ).checked )
	{
		$( "#trduedate" ).fadeIn( "slow" );
		$( "#trreminder" ).fadeOut( 0 );
	}
	
	if( document.getElementById( "type2" ).checked )
	{
		$( "#trduedate" ).fadeOut( 0 );
		$( "#trreminder" ).fadeOut( 0 );
	}

	if( document.getElementById( "type3" ).checked )
	{
		$( "#trduedate" ).fadeIn( "slow" );
		$( "#trreminder" ).fadeIn( "slow" );
	}
}

function showLegalPriceHint() 
{
	alert( "Alle Preisangaben in EUR inkl. MwSt. zzgl. Versand- und Verpackungskosten. " +
	"Es gelten die Versand- und Lieferbedingungen der jeweiligen Anbieter." );
	return false;
}

function switchManualGiftMode( mode ) 
{
	if( mode ) 
	{
		document.forms["gift"].save.value = "yes";
		$( "#alwaysmanualspan" ).fadeIn( "slow" );
		$( "#giftcategory" ).fadeIn( "slow" );
		$( "#giftdescription" ).fadeIn( "slow" );
		$( "#giftprice" ).fadeIn( "slow" );
		$( "#giftlink" ).fadeIn( "slow" );
		$( "#giftvendor" ).fadeIn( "slow" );
		$( "#giftimage" ).fadeIn( "slow" );
		$( "#giftpriority" ).fadeIn( "slow" );
		$( "#giftdepends" ).fadeIn( "slow" );
		$( "#listchoices" ).fadeIn( "slow" );
		$( "#buttons" ).fadeIn( "slow" );
		$( "#btnautofill" ).fadeOut( 0 );
		$( "#autofillcats" ).fadeOut( 0 );
		$( "#autofillhint" ).fadeOut( 0 );
	} 
	else 
	{
		document.forms["gift"].save.value = "no";
		$( "#alwaysmanualspan" ).fadeOut( 0 );
		$( "#giftcategory" ).fadeOut( 0 );
		$( "#giftdescription" ).fadeOut( 0 );
		$( "#giftprice" ).fadeOut( 0 );
		$( "#giftlink" ).fadeOut( 0 );
		$( "#giftvendor" ).fadeOut( 0 );
		$( "#giftimage" ).fadeOut( 0 );
		$( "#giftpriority" ).fadeOut( 0 );
		$( "#giftdepends" ).fadeOut( 0 );
		$( "#listchoices" ).fadeOut( 0 );
		$( "#buttons" ).fadeOut( 0 );
		$( "#btnautofill" ).fadeIn( "slow" );
		$( "#autofillcats" ).fadeIn( "slow" );
		$( "#autofillhint" ).fadeIn( "slow" );
	}
	document.forms["gift"].name.focus();
}

function switchProdDetailsMode( mode ) 
{
	if( mode ) 
	{
		$( "#giftlink" ).fadeIn( "slow" );
		$( "#giftvendor" ).fadeIn( "slow" );
		$( "#giftimage" ).fadeIn( "slow" );
		$( "#giftartno" ).fadeIn( "slow" );
		$( "#giftavailability" ).fadeIn( "slow" );
		$( "#giftprice" ).fadeIn( "slow" );
	} 
	else 
	{
		$( "#giftlink" ).fadeOut( 0 );
		$( "#giftvendor" ).fadeOut( 0 );
		$( "#giftimage" ).fadeOut( 0 );
		$( "#giftartno" ).fadeOut( 0 );
		$( "#giftavailability" ).fadeOut( 0 );
		$( "#giftprice" ).fadeOut( 0 );
	}
}

function showListChoices()
{
	$( "#listchoicestable" ).fadeIn( "slow" );
	$( "#listchoicesselected" ).fadeOut( 0 );
}

function showDiv( divname ) 
{
	document.getElementById( divname ).style.display = 'block'; 
}

function hideDiv( divname ) 
{
	document.getElementById( divname ).style.display = 'none'; 
}

function showWaitingScreen() 
{
	document.getElementById('wrapper').style.filter = 'alpha(opacity = 70)'; 
	document.getElementById('wrapper').style.opacity = '0.70';
	document.getElementById('wrapper').style.zoom = '1';   // to work around IE bug not showing opacity correctly
	document.getElementById( 'PleaseWait' ).style.display = 'block';

	window.setTimeout( "hideWaitingScreen();", 10000 );
}

function hideWaitingScreen() 
{
	document.getElementById('wrapper').style.filter = 'alpha(opacity = 100)'; 
	document.getElementById('wrapper').style.opacity = '1.00'; 
	document.getElementById('wrapper').style.zoom = '1';   // to work around IE bug not showing opacity correctly
	document.getElementById('PleaseWait').style.display = 'none';
}

function delayedSubmit( formname ) 
{
	window.clearTimeout( timeouthandle );
	timeouthandle = window.setTimeout( "showWaitingScreen(); document.forms['" + formname + "'].submit()", 1000 );
}

function reminderSelectAll() 
{
	f = document.forms['list'];
	
	f.reminder6.checked = true;
	f.reminder4.checked = true;
	f.reminder2.checked = true;
	f.reminder0.checked = true;
}

function reminderDeselectAll() 
{
	f = document.forms['list'];
	
	f.reminder6.checked = false;
	f.reminder4.checked = false;
	f.reminder2.checked = false;
	f.reminder0.checked = false;
}

function deleteRecord( name, type, id ) 
{
		from = location.pathname + location.search;
		
		switch( type ) {
			case "list":
				if( confirm( "Möchtest du '" + name + "' wirklich löschen?" ) )
					location.href = "/delete.php?what=list&id=" + id; break;
			case "gift":
				if( confirm( "ACHTUNG: Das Geschenk wird von allen Listen gelöscht!\n\nMöchtest du '" + name + "' wirklich löschen?" ) )
					location.href = "/delete.php?what=gift&id=" + id + "&from=" + from; break;
			case "invitation":
				if( confirm( "Möchtest du die Einladung an '" + name + "' wirklich löschen?\n\nDie Person kann anschließend nicht mehr auf diese Liste zugreifen." ) )
					location.href = "/delete.php?what=invitation&code=" + id; break;
			case "invitee":
				if( confirm( "Möchtest du deine Einladung für diese Liste wirklich löschen?\n\nDu kannst anschließend nicht mehr auf die Wunschliste zugreifen und deine Reservierungen werden gelöscht." ) )
					location.href = "/delete.php?what=invitee&code=" + id; break;
			case "reservation":
				if( confirm( "Möchtest du die Reservierung für '" + name + "' wirklich löschen?\n\nDas Geschenk kann anschließend von anderen neu reserviert werden." ) )
					location.href = "/delete.php?what=reservation&id=" + id; break;
			case "allreservations":
				if( confirm( "Möchtest du wirklich alle Reservierungen für die Geschenke auf deiner Wunschliste löschen?" ) )
					location.href = "/delete.php?what=allreservations&id=" + id; break;
			default:
				alert( "Unbekannter Typ!" );
		}	
}

function resetPassword() 
{

	f = document.forms['login'];

	if( f.username.value == "" ) {
		alert( "Bitte gib zuerst deinen Benutzernamen ein." );
		f.username.focus();
		return;
	}

	if( confirm( "Dein jetziges Passwort wird gelöscht.\n\nMöchtest du dein Passwort wirklich zurücksetzen?" ) ) {
		
		f.resetpassword.value = "1";
		f.submit();
	}
}

function isImageOk( img ) 
{
	// thanks to http://talideon.com/weblog/2005/02/detecting-broken-images-js.cfm
	
	// During the onload event, IE correctly identifies any images that
    // weren't downloaded as not complete. Others should too. Gecko-based
    // browsers act like NS4 in that they report this incorrectly.
    if (!img.complete) {
        return false;
    }

    // However, they do have two very useful properties: naturalWidth and
    // naturalHeight. These give the true size of the image. If it failed
    // to load, either of these should be zero.
    if (typeof img.naturalWidth != "undefined" && img.naturalWidth == 0) {
        return false;
    }

    // No other way of checking: assume it's ok.
    return true;
}


function checkImages() 
{
	// thanks to http://talideon.com/weblog/2005/02/detecting-broken-images-js.cfm

	for (var i = 0; i < document.images.length; i++) {
        if (!isImageOk(document.images[i])) {
            document.images[i].src = "/images/gift.jpg";
        }
    }
}

function updateListChoices( sourceField )
{
	if( sourceField.value != "0" )
	{
		$( "#listchoices" ).fadeOut( 0 );
		$( "#helptext" ).fadeIn( "slow" );
	}
	else
	{
		$( "#listchoices" ).fadeIn( "slow" );
		$( "#helptext" ).fadeOut( 0 );
	}
}

