var MAX_HOME_IMAGES = 5;

var CURRENT_MENU = '';

var CYCLE_TIMEOUT = 8000;

var CURRENT_IMAGE = Math.floor(Math.random() * MAX_HOME_IMAGES );

var MAIN_BUFFERED_IMAGES = new Array();

var CURRENT_TESTIMONIAL = 0;

var TESTIMONIALS = new Array(
	'Site of the 2004 US Open Qualifier',
	'Site of the 2003 New York State Amateur',
	'Ranked as one of the Best 25 New Courses to Open by Golf magazine',
	'Ranked as the 3rd Most difficult Public Course in New York State by New York Golf Magazine',
	'Only course in Western New York to offer Pro Link GPS on all carts'
);


function init() {
	buffer_images('homeimage_topimage');
	//fix_layout();
	cycle_testimonials();
	setTimeout(cycle, CYCLE_TIMEOUT);
}

function showhide(textid, bool_show)
{
	var value = (bool_show ? 'block' : 'none');

	if (
		(null == textid) || ('' == textid)
	) {
		textid = CURRENT_MENU;
	}

	if (bool_show) {
		if (CURRENT_MENU == textid) {
			return false;
		}

		if (CURRENT_MENU != '') {
			var elmt = document.getElementById(CURRENT_MENU);
			if (elmt != null) {
				elmt.style.display = 'none';
			}
		}
	}

	if (textid != '') {
		var elmt = document.getElementById(textid);
		if (null == elmt) {
			// alert('Error getting "' + textid + '"');
		} else {
			// alert('setting classname to "' + value + '"');
			elmt.style.display = value;
		}
	}

	if (bool_show) {
		CURRENT_MENU = textid;
	} else {
		CURRENT_MENU = '';
	}

	return false;
}

function show(obj) {
	showhide('topnav-' + obj, true);
}

function hide(obj) {
	var name = ''; // assume
	if (obj != null) {
		name = 'topnav-' + obj;
	} else {
		// alert('obj is null');
	}

	showhide(name, false);
}
function buffer_images( imgId ) {
	var imgObj = document.getElementById(imgId);
	
	if(imgObj) {
	    var imgSrc = imgObj.getAttribute( 'src' );

	    for (var i=0; i < MAX_HOME_IMAGES; i++) {
		    var main_img = new Image();
            urlArr = imgSrc.match( /(.*\/)(.*)[0-9]+(\..+)/ );
		    main_img.src = urlArr[1] + urlArr[2] + (i+1) + urlArr[3];
		    MAIN_BUFFERED_IMAGES[i] = main_img;
	    }
	}
}

function cycle_images( imgId ) {

	var imgObj = document.getElementById( imgId );

	if( imgObj ){
        if ( imgObj.filters ){
		    imgObj.filters[0].apply();
	    }
	    var buffered_img = MAIN_BUFFERED_IMAGES[CURRENT_IMAGE];
	    
	    if (buffered_img) {
	        imgObj.src = buffered_img.src;
		    if (imgObj.filters) {
			    imgObj.filters[0].play();
		    }
		}

	    if (++CURRENT_IMAGE >= MAX_HOME_IMAGES) {
    		CURRENT_IMAGE = 0;
	    }
	}
}

//Randomizes any image on the page with class 'randomize'
//(The image can have other classes, but 'randomize' must be there.)
//For this function to work properly, the images to swap between must be in the same folder, numbered sequentially from 1 with no breaks. Ex: home1.jpg, home2.jpg.
function randomize_images() {
    var images = document.getElementsByTagName( 'img' );
    
    //crude method of detecting IE
    document.attachEvent ? classAttr = 'className' : classAttr = 'class';
    for( var i = 0; i < images.length; i++ ) {
        //Look for the random class
        if ( images[i].getAttribute( classAttr ) && images[i].getAttribute( classAttr ).search(/\brandomize\b/) != -1 ) {
            imgSrc = images[i].getAttribute( 'src' );
            //Separate the path from the string
            urlArr = imgSrc.match( /(.*\/)(.*)[0-9]+(\..+)/ );
            if( urlArr ) {
                images[i].src = urlArr[1] + urlArr[2] + (CURRENT_IMAGE+1) + urlArr[3];
                images[i].style.visibility = 'visible';
            }
        }
    }
}

function cycle_testimonials() {
	testElmt = document.getElementById("testimonials");
	if (null != testElmt) {
		testimonial = TESTIMONIALS[CURRENT_TESTIMONIAL];

		if (!testElmt.firstChild) {
			testElmt.appendChild( document.createTextNode( testimonial ));
		} else {
			testElmt.replaceChild(document.createTextNode( testimonial ), testElmt.firstChild );
		}

		if (++CURRENT_TESTIMONIAL >= TESTIMONIALS.length) {
			CURRENT_TESTIMONIAL = 0;
		}
	}
}

function cycle() {
	cycle_images( 'homeimage_topimage' );
	cycle_testimonials();

	setTimeout(cycle, CYCLE_TIMEOUT);
}

function showForm( divID ){
	var iDiv = document.getElementById(divID);
	if (iDiv.style.display == "none"){
		iDiv.style.display = "block";
	} else if(iDiv.style.display == "block"){
		iDiv.style.display = "none";
	}
}

/*
 * fix_layout - makes the side bar the same height as the main + top navigation by expanding the "blank" part.
 * This feels like a terrible hack, but it's functional as long as Javascript is enabled.
 * Before criticizing it for that, note that navigation does not work without JS.
 * 
 */
function fix_layout() {
	//Get the heights of the elements we need to compare.
	var sideHeight = document.getElementById( 'sidebar' ).offsetHeight;
	var lightHeight = document.getElementById( 'lightCol' ).offsetHeight;

	//Heights of the elements we need to subtract.
	var navHeight = document.getElementById( 'topnavbar' ).offsetHeight;
	var copyHeight = document.getElementById( 'copyright' ).offsetHeight;

	//If the sidebar is shorter than the main
	if( sideHeight < mainHeight ) {
		//Increase its height
		document.getElementById( 'sidebar' ).style.height = mainHeight;
	} else {
		//If the main bar is shorter or equal to the side, increase its height.
		document.getElementById( 'main' ).style.height = sideHeight
	}
}