// DL10Base.js
//
// Copyright © 2007-2008 DL10 Design Limited. All Rights Reserved.
//
// 1 March 2007

//
// Global Variables
//

var currentRow = null;
var originalClass = null;
var currentID = null;

//
// Base Functions
//

google.load( "maps", "2.x" );

// Browser Detect Functions

var BrowserDetect = {
    init : function()
    {

	    this.browser = this.searchString( this.dataBrowser )
	            || "An unknown browser";
	    this.version = this.searchVersion( navigator.userAgent )
	            || this.searchVersion( navigator.appVersion )
	            || "an unknown version";
	    this.OS = this.searchString( this.dataOS ) || "an unknown OS";
    },
    searchString : function( data )
    {

	    for ( var i = 0; i < data.length; i++ )
	    {
		    var dataString = data[ i ].string;
		    var dataProp = data[ i ].prop;
		    this.versionSearchString = data[ i ].versionSearch
		            || data[ i ].identity;
		    if ( dataString )
		    {
			    if ( dataString.indexOf( data[ i ].subString ) != -1 )
				    return data[ i ].identity;
		    }
		    else if ( dataProp )
			    return data[ i ].identity;
	    }
    },
    searchVersion : function( dataString )
    {

	    var index = dataString.indexOf( this.versionSearchString );
	    if ( index == -1 )
		    return;
	    return parseFloat( dataString.substring( index
	            + this.versionSearchString.length + 1 ) );
    },
    dataBrowser : [ {
        string :navigator.userAgent,
        subString :"OmniWeb",
        versionSearch :"OmniWeb/",
        identity :"OmniWeb"
    }, {
        string :navigator.vendor,
        subString :"Apple",
        identity :"Safari"
    }, {
        prop :window.opera,
        identity :"Opera"
    }, {
        string :navigator.vendor,
        subString :"iCab",
        identity :"iCab"
    }, {
        string :navigator.vendor,
        subString :"KDE",
        identity :"Konqueror"
    }, {
        string :navigator.userAgent,
        subString :"Firefox",
        identity :"Firefox"
    }, {
        string :navigator.vendor,
        subString :"Camino",
        identity :"Camino"
    }, { // for newer Netscapes (6+)
                string :navigator.userAgent,
                subString :"Netscape",
                identity :"Netscape"
            }, {
                string :navigator.userAgent,
                subString :"MSIE",
                identity :"Explorer",
                versionSearch :"MSIE"
            }, {
                string :navigator.userAgent,
                subString :"Gecko",
                identity :"Mozilla",
                versionSearch :"rv"
            }, { // for older Netscapes (4-)
                string :navigator.userAgent,
                subString :"Mozilla",
                identity :"Netscape",
                versionSearch :"Mozilla"
            } ],
    dataOS : [ {
        string :navigator.platform,
        subString :"Win",
        identity :"Windows"
    }, {
        string :navigator.platform,
        subString :"Mac",
        identity :"Mac"
    }, {
        string :navigator.platform,
        subString :"Linux",
        identity :"Linux"
    } ]

};
BrowserDetect.init();

// Printing Functions

function printPage( printFormatedPage )
{

	var display_settings = "toolbar=yes,location=no,directories=yes,menubar=yes,scrollbars=yes,width=650"

	var docPrint = window.open( "", "_blank", display_settings );
	docPrint.location = printFormatedPage;
	docPrint.focus();

}

// Validation Functions

function TD_findObj( n, d )
{

	var x;

	if ( !d )
	{

		d = document;

	}

	x = d.getElementById( n );

	return x;

}

function TD_validateForm()
{

	var i, p, q, nm, test, num, min, max, errors = '', args = TD_validateForm.arguments;

	for ( i = 0; i < ( args.length - 2 ); i += 3 )
	{

		test = args[ i + 2 ];
		val = TD_findObj( args[ i ] );

		if ( val )
		{

			nm = val.name;

			if ( ( val = val.value ) != "" )
			{

				if ( test.indexOf( 'isEmail' ) != -1 )
				{

					p = val.indexOf( '@' );
					if ( p < 1 || p == ( val.length - 1 ) )
					{

						errors += '- ' + nm + ' must contain an e-mail address.\n';

					}

				}
				else if ( test != 'R' )
				{

					num = parseFloat( val );

					if ( isNaN( val ) )
					{

						errors += '- ' + nm + ' must contain a number.\n';

					}
					if ( test.indexOf( 'inRange' ) != -1 )
					{

						p = test.indexOf( ':' );
						min = test.substring( 8, p );
						max = test.substring( p + 1 );

						if ( num < min || max < num )
						{

							errors += '- ' + nm
							        + ' must contain a number between ' + min
							        + ' and ' + max + '.\n';

						}

					}

				}

			}
			else if ( test.charAt( 0 ) == 'R' )
			{

				errors += '- ' + nm + ' is required.\n';

			}

		}

	}

	if ( errors )
	{

		alert( 'The following error(s) occurred:\n' + errors );
		errors == '';
		return false;
	}

	errors == ''
	return true;

}

function validateForm( formElement )
{

	var valid = TD_validateForm( 'contact_name', 'Name', 'R',
	        'contact_telephone', 'Telephone', 'R', 'contact_email', 'e-Mail',
	        'RisEmail', 'contact_query', 'Query', 'R' );

	if ( valid )
	{
		formElement.action = "processing/processquery.php";
	}
	else
	{
		$( 'contact_name' ).focus();
	}

	return valid;

}

function loadAddress()
{

	var map = null;
	var latitude = 54.181275;
	var longitude = -1.172361;

	if ( GBrowserIsCompatible() )
	{
		map = new GMap2( $( 'map' ) );

		var address = new GLatLng( latitude, longitude );

		map.setCenter( address, 14, G_NORMAL_MAP );
		map.enableDoubleClickZoom();
		map.enableContinuousZoom();
		map.addControl( new GSmallMapControl(), new GControlPosition(
		        G_ANCHOR_TOP_LEFT, new GSize( 0, 0 ) ) );

		var marker = new GMarker( address );
		map.addOverlay( marker );
		var markerData = "<h2>Address:</h2><p>Newburgh Priory<br />Coxwold, York, North Yorkshire, YO61 4AS<br />Tel: 01347 868372</p>";
		marker.openInfoWindowHtml( markerData );
	}
}

// Page Loading

function loadPage( newURL )
{

	document.location = newURL;

}

function loadExternalPage( newURL, windowName )
{

	if ( window != "" )
	{

		var windowHandle = window.open( 'http://' + newURL, windowName, '' );

	}
	else
	{

		document.location = "http://" + newURL;

	}

}

// Rollovers

function showOverState( element )
{

	var currentImage = element.src;
	var filePath = currentImage.split( "/" );
	var fileName = filePath[ filePath.length - 1 ];

	var currentID = element.id;
	var currentClass = element.className;

	if ( currentClass == 'clickable'
	        || ( currentID != 'galNavPrev' && currentID != 'galNavNext' ) )
	{

		var re = new RegExp( '_', '' );

		fileName = fileName.replace( re, "_over_" );

		var newPath = "";
		var numFilePathParts = filePath.length - 1;

		for ( var i = 0; i < numFilePathParts; i++ )
		{
			newPath = newPath + filePath[ i ] + "/";
		}

		element.src = newPath + fileName;

	}

}

function showExitState( element )
{

	var currentImage = element.src;
	var filePath = currentImage.split( "/" );
	var fileName = filePath[ filePath.length - 1 ];

	var re = new RegExp( '_over_', '' );

	fileName = fileName.replace( re, "_" );

	var newPath = "";
	var numFilePathParts = filePath.length - 1;

	for ( var i = 0; i < numFilePathParts; i++ )
	{
		newPath = newPath + filePath[ i ] + "/";
	}

	element.src = newPath + fileName;

}

function doCrumbHover( element )
{

	var id = $( element ).id;
	var idParts = id.split( "_" );
	var index = idParts[ 1 ];
	var nextCrumb = null;

	var part1 = null;
	var part2 = null;
	var part3 = null;

	if ( index == 1 )
	{
		nextCrumb = $( 'crumb_' + ( Number( index ) + 1 ) );
	}

	if ( null == nextCrumb && index == 1 )
	{
		part1 = $( 'crumb_' + index );
		part2 = $( 'crumbt_' + index );
	}
	else
	{
		part1 = $( 'crumb_' + index );
		part2 = $( 'crumbt_' + index );
		part3 = $( 'crumbt_' + ( index - 1 ) );
	}

	if ( index == 1 && null != nextCrumb )
	{
		doHover( part1, 'standby' );
		doHover( part2, 'standbyn' );
	}
	else if ( index == 1 && null == nextCrumb )
	{
		doHover( part1, 'standby' );
		doHover( part2, 'standby' );
	}
	else
	{
		doHover( part1, 'standby' );
		doHover( part2, 'standby' );
		doHover( part3, 'standby' );
	}

}

function doCrumbUnHover( element )
{

	var id = $( element ).id;
	var idParts = id.split( "_" );
	var index = idParts[ 1 ];
	var nextCrumb = null;

	var part1 = null;
	var part2 = null;
	var part3 = null;

	if ( index == 1 )
	{
		nextCrumb = $( 'crumb_' + ( Number( index ) + 1 ) );
	}

	if ( null == nextCrumb && index == 1 )
	{
		part1 = $( 'crumb_' + index );
		part2 = $( 'crumbt_' + index );
	}
	else
	{
		part1 = $( 'crumb_' + index );
		part2 = $( 'crumbt_' + index );
		part3 = $( 'crumbt_' + ( index - 1 ) );
	}

	if ( index == 1 && null != nextCrumb )
	{
		doUnHover( part1, 'standby' );
		doUnHover( part2, 'standbyn' );
	}
	else if ( index == 1 && null == nextCrumb )
	{
		doUnHover( part1, 'standby' );
		doUnHover( part2, 'standby' );
	}
	else
	{
		doUnHover( part1, 'standby' );
		doUnHover( part2, 'standby' );
		doUnHover( part3, 'standby' );
	}
}