var Main = {
	base_url : "/site",
	ui_url : "/site/ui", 
	site_name : "OneLegacy",
	// site drop down menu stuff //
	ddtimeout : 500,
	ddclosetimer : 0,
	ddmenuitem : 0
};


Main.init_page = function () {
	//new Ajax.Updater('header', Main.ui_url + '/header.html');
	//new Ajax.Updater('sidebar', Main.ui_url + '/sidebar.html');
	//new Ajax.Updater('footer', Main.ui_url + '/footer.html');
	document.title = Main.site_name + ' ' + document.title;
	$$('div.slideshow').each(function(i) { Main.build_slideshow(i)	});
	document.onclick = Main.mclose;
};


Main.build_slideshow = function (elem) {
	var imgs = elem.getElementsBySelector('img');
	var frame = 0;

	elem.style.display = 'block';  // slideshows should initially be hidden till we reach this point
	elem.style.width = imgs[0].width + 'px';  // set width of slideshow so css margin:auato centers it
	for(var i=0; i < imgs.length; i++) imgs[i].style.display = 'none';  // Effect.appear only works on images that have style set to none
	Effect.Appear(imgs[frame]);		// Immediately display an image

	new PeriodicalExecuter(function() {
		Effect.Fade(imgs[frame]);
		if (frame == imgs.length-1) { frame = 0; } else { frame++; }
		Effect.Appear(imgs[frame]);		
	}, 4);
}

/*
Main.fadeInOut = function (frame, start_frame, end_frame, delay, list) {
//	var imgs = elem.readAttribute('images').split(/\s+/);
//	setTimeout( Main.fadeInOut(imgs.length-1, 0, imgs.length-1, 4000, imgs), 0);
	return (function() {
		Effect.Fade(list[frame]);
		if (frame == end_frame) { frame = start_frame; } else { frame++; }
		Effect.Appear(list[frame]);
		setTimeout(Main.fadeInOut(frame, start_frame, end_frame, delay, list), delay);
	});
}
*/

Main.load_select = function (select_field, data_src, option_label, second_label, option_value, default_value, append, optgrp_fld) {
	/* 
		This function will load a select field with a data set
		The label/value params can be name of fields in the data set or function refs
		Can handle mutliple default values for selects with the multiple option set
		Can pass append parameter which lets you start appending to the end of the select list
		The optgrp_fld parameter is the name of the field in the datasate to key off of for groupings,
		however if there is no optgrp_fld in the dataset then we assume the user just wants to create
		a single optgroup and place the data_src records under it
	*/

	// "Hash" for setting multi default values //
	var ops = [];  
	if (default_value != undefined && typeof default_value == 'object') for (var i=0; i< default_value.length; i++) ops[default_value[i]] = 1;  

	// Wipe out existing select options and optgroups unless append is true //
	if (append != true) {
		select_field.length = 0;
		var children = select_field.childNodes;
		for(var i=children.length-1; i>=0; i--) select_field.removeChild(children[i]);
	}

	// Load the select field with options and optgroups (if applicable) //
	if (optgrp_fld != undefined) {		
		// DEAL WITH OPT GROUPS //
		for (var i=0; i < data_src.length; i++) {
			if (i==0 || data_src[i][optgrp_fld] != data_src[i-1][optgrp_fld]) {  
				var optgrp = document.createElement("optgroup");  optgrp.label = data_src[i][optgrp_fld];
				select_field.appendChild(optgrp);
			}
			select_field.add(new Option(data_src[i][option_label], data_src[i][option_value]));
			if (ops[data_src[i][option_value]] != undefined) select_field.options[i].selected = true;
		}
	} else {
		// NO OPT GROUPS //
		for (var i=0; i < data_src.length; i++) {
			select_field.add( 
				new Option( 
					(typeof option_label == 'string') ? 
						((second_label) ? data_src[i][second_label] + ' - ' + data_src[i][option_label] : data_src[i][option_label]) : 
						option_label(data_src[i], i, data_src), 
					(typeof option_value == 'string') ? data_src[i][option_value] : option_value(data_src[i], i, data_src)
				),
				null
			);
			if (ops[data_src[i][option_value]] != undefined) select_field.options[i].selected = true;
		}
	}
	
	// Set default value if applicable //
	if (default_value != undefined && typeof default_value != 'object') select_field.value = default_value;

	return select_field;
}


// Site drop down Menu methods //
Main.mopen = function (id) {	
	Main.mcancelclosetime();	
	if (Main.ddmenuitem) {
		Main.ddmenuitem.style.visibility = 'hidden';
		document.getElementById(Main.ddmenuitem.id+'li').style.borderTop = '2px solid #FFFFFF';
	}

	Main.ddmenuitem = document.getElementById(id);
	if (Main.ddmenuitem) {
		Main.ddmenuitem.style.visibility = 'visible';	
		document.getElementById(id+'li').style.borderTop = '2px solid #A90D22';
	}
};

Main.mclose = function () {
	if (Main.ddmenuitem) {
		Main.ddmenuitem.style.visibility = 'hidden';
		document.getElementById(Main.ddmenuitem.id+'li').style.borderTop = '2px solid #FFFFFF';
	}
}

Main.mclosetime = function () {
	Main.ddclosetimer = window.setTimeout(Main.mclose, Main.ddtimeout);
}

Main.mcancelclosetime = function () {
	if(Main.ddclosetimer) {
		window.clearTimeout(Main.ddclosetimer);
		Main.ddclosetimer = null;
	}
};
