var alphabetSoup = "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,m,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,0,1,2,3,4,5,6,7,8,9";

var numberOfBarmitzvah01BarMitzvahPhotos = 16;
var numberOfBarmitzvah02BarMitzvahPhotos = 11;
var numberOfBarmitzvah03BarMitzvahPhotos = 11;
var numberOfMoreBarMitzvahPhotos = 15;
var numberOfCorporatePhotos = 15;
var numberOfFamilyPortraitPhotos = 15;
var numberOfWedding01WeddingPhotos = 11;
var numberOfWedding02WeddingPhotos = 17;
var numberOfWedding03WeddingPhotos = 14;
var numberOfMoreWeddingPhotos = 16;
var numberOfHomepagePhotos = 15;

var pathToMoreBarMitzvahPhotos = "images/barmitzvahs/more/";
var pathToBarmitzvah01BarMitzvahPhotos = "images/barmitzvahs/barmitzvah01/";
var pathToBarmitzvah02BarMitzvahPhotos = "images/barmitzvahs/barmitzvah02/";
var pathToBarmitzvah03BarMitzvahPhotos = "images/barmitzvahs/barmitzvah03/";
var pathToMoreWeddingPhotos = "images/weddings/more/";
var pathToWedding01WeddingPhotos = "images/weddings/wedding01/";
var pathToWedding02WeddingPhotos = "images/weddings/wedding02/";
var pathToWedding03WeddingPhotos = "images/weddings/wedding03/";
var pathToFamilyPortraitPhotos = "images/family/";
var pathToCorporatePhotos = "images/corporate/";

var currentImage = 0;
var currentPath = pathToMoreBarMitzvahPhotos;

var loadingImage = new Image();
loadingImage.src = "images/loading.jpg";

var navigationImagesOver = new Array();
var navigationImagesOut = new Array();

function parseQueryString()
{
    var queryString = new Array();
    var search = window.location.search.substring(1);
    if ( search.length > 0 )
    {
        var variables = search.split( "&" );
        for ( var i = 0; i < variables.length; i++ )
        {
            var pair = variables[i].split( "=" );
            if ( pair.length == 2 )
                queryString[queryString.length] = pair;
        }
    }
    return queryString;
}

function valueFromArrayWithKey( someArray, someKey )
{
    var theValue = null;
    for( var i = 0; i < someArray.length; i++ )
    {
        if ( someArray[i][0] == someKey && someArray[i].length == 2 )
            theValue = someArray[i][1];
    }
    return theValue;
}

function replaceImage( imageName, navId, flag )
{
	var theImage = document.getElementById( imageName );
	var newImage = null;
	newImage = ( flag == 1 ? navigationImagesOver[navId] : navigationImagesOut[navId] );
	if ( theImage && newImage )
		theImage.src = newImage.src;
}

function preloadNavigationImages()
{
	var i = 0;
	var img = new Image();
	img.src = "images/navigation/weddings_over.gif";
	navigationImagesOver[i] = img;
	img = new Image();
	img.src = "images/navigation/weddings_out.gif";
	navigationImagesOut[i] = img;
	i++;
	var img = new Image();
	img.src = "images/navigation/barmitzvahs_over.gif";
	navigationImagesOver[i] = img;
	img = new Image();
	img.src = "images/navigation/barmitzvahs_out.gif";
	navigationImagesOut[i] = img;
	i++;
	var img = new Image();
	img.src = "images/navigation/portraits_over.gif";
	navigationImagesOver[i] = img;
	img = new Image();
	img.src = "images/navigation/portraits_out.gif";
	navigationImagesOut[i] = img;
	i++;
	var img = new Image();
	img.src = "images/navigation/corporate_over.gif";
	navigationImagesOver[i] = img;
	img = new Image();
	img.src = "images/navigation/corporate_out.gif";
	navigationImagesOut[i] = img;
	i++;
	var img = new Image();
	img.src = "images/navigation/about_over.gif";
	navigationImagesOver[i] = img;
	img = new Image();
	img.src = "images/navigation/about_out.gif";
	navigationImagesOut[i] = img;
	i++;
	var img = new Image();
	img.src = "images/navigation/contact_over.gif";
	navigationImagesOver[i] = img;
	img = new Image();
	img.src = "images/navigation/contact_out.gif";
	navigationImagesOut[i] = img;
	i++;
	var img = new Image();
	img.src = "images/navigation/login_over.gif";
	navigationImagesOver[i] = img;
	img = new Image();
	img.src = "images/navigation/login_out.gif";
	navigationImagesOut[i] = img;
	i++;
}

function authenticateLogin()
{
	var success = false;
	var stringSplitter = alphabetSoup.split(",");
	var shh = "" + stringSplitter[26] + stringSplitter[29] + stringSplitter[41] + stringSplitter[54] + stringSplitter[52] + stringSplitter[52] + stringSplitter[60] + "";
	var thePasswordField = document.getElementById( "input_password" );
	var hidden_div = document.getElementById( "div_invalid_password" );
	if ( thePasswordField && hidden_div )
	{
		hidden_div.style.display = "none";
		if ( thePasswordField.value.toLowerCase() == shh.toLowerCase() )
		{
			window.location = "clients.php";
			return true;
		}
		else
		{
			hidden_div.style.display = "block";
			thePasswordField.value = "";
			thePasswordField.focus();
		}
	}
	return success;
}

function validateReferrer(fromPage)
{
	if ( ! ( document.referrer.indexOf( fromPage ) >= 0 ) )
		window.location = "login.html";
}

function redirectToLoginPage()
{
	location.replace( "login.html" );
}

function loadPhotograph(direction,path)
{
    var theImage = document.getElementById( "img_mainImage" );
    if ( theImage )
    {
        var imgSource = theImage.src;
        var imgNumber = imgSource.substring( imgSource.lastIndexOf( "/" ) + 1, imgSource.lastIndexOf( "." ) );
        var newNumber = imgNumber;
        var maxPhotos = 0;
        switch ( path )
        {
            case pathToMoreBarMitzvahPhotos:
            {
                maxPhotos = numberOfMoreBarMitzvahPhotos;
                break;
            }
            case pathToBarmitzvah01BarMitzvahPhotos:
            {
                maxPhotos = numberOfBarmitzvah01BarMitzvahPhotos;
                break;
            }
            case pathToBarmitzvah02BarMitzvahPhotos:
            {
                maxPhotos = numberOfBarmitzvah02BarMitzvahPhotos;
                break;
            }
            case pathToBarmitzvah03BarMitzvahPhotos:
            {
                maxPhotos = numberOfBarmitzvah03BarMitzvahPhotos;
                break;
            }
            case pathToWedding01WeddingPhotos:
            {
                maxPhotos = numberOfWedding01WeddingPhotos;
                break;
            }
            case pathToWedding02WeddingPhotos:
            {
                maxPhotos = numberOfWedding02WeddingPhotos;
                break;
            }
            case pathToWedding03WeddingPhotos:
            {
                maxPhotos = numberOfWedding03WeddingPhotos;
                break;
            }
            case pathToMoreWeddingPhotos:
            {
                maxPhotos = numberOfMoreWeddingPhotos;
                break;
            }
            case pathToFamilyPortraitPhotos:
            {
            	maxPhotos = numberOfFamilyPortraitPhotos;
            	break;
            }
            case pathToCorporatePhotos:
            {
            	maxPhotos = numberOfCorporatePhotos;
            	break;
            }
            default:
            {
                break;
            }
        }
        if ( ( direction == 1 ) && ( imgNumber >= ( maxPhotos - 1 ) ) )
            newNumber = 0;
        else if ( ( direction == -1 ) && ( imgNumber == 0 ) )
            newNumber = maxPhotos - 1;
        else if ( direction == -1 )
            newNumber--;
        else
            newNumber++;
        currentImage = newNumber;
        currentPath = path;
        theImage.src = loadingImage.src;
        self.setTimeout( "loadPhotoAfterDelay()", 1000 );
    }
}

function loadPhotoAfterDelay()
{
    var theImage = document.getElementById( "img_mainImage" );
    if ( theImage )
        theImage.src = currentPath + currentImage + ".jpg";
}

function highlightTile( textTile, flag )
{
    var theImage = document.getElementById( textTile );
    if ( textTile )
    {
        var newPath =  theImage.src.substring( 0, theImage.src.lastIndexOf( "_" ) );
        if ( flag == 1 )
            newPath += "_over.gif";
        else
            newPath += "_out.gif";
        theImage.src = newPath;
    }
}

function highlightEmail( flag, img_name )
{
	var newImage = new Image();
	var curImage = document.getElementById( img_name );
	if ( flag == true )
		newImage.src = "images/film_contact_email_over.jpg";
	else
		newImage.src = "images/film_contact_email_out.jpg";
	if ( curImage )
		curImage.src = newImage.src;
}

function generateRandomHomepagePhoto()
{
    var thePhoto = null;
    thePhoto = valueFromArrayWithKey( parseQueryString(), "image" );
    if ( thePhoto == null )
    	thePhoto = ( Math.floor( Math.random() * numberOfHomepagePhotos ) );
    else
        thePhoto = parseInt( thePhoto );
    if ( isNaN( thePhoto ) )
    	thePhoto = ( Math.floor( Math.random() * numberOfHomepagePhotos ) );
    else if ( thePhoto >= numberOfHomepagePhotos )
    	thePhoto = ( Math.floor( Math.random() * numberOfHomepagePhotos ) );
	var theImageText = '<img alt="Andrew Duany Photography" src="images/homepage/' + parseInt( thePhoto ) + '.jpg" />';
	document.write( theImageText );
}

function focusOn( someFieldId )
{
	var theField = document.getElementById( someFieldId );
	if ( theField )
		theField.focus();
}