// Custom JavaScript for content display 
// (uses jquery.js libraries)

// Define variables 
var bodycontentdiv='#interiorContent'; // div to load contents into 
var getcontentscript='index.cfm'; // name of script to call for content 
var loadingdisplay="<img src='view/images/loading.gif' width='32' height='32' alt='Loading ...' />";
var scrollPane;
var activeLink;
var currentpage = 0;

// Disables $ shortcut so MooTools can work here too 
jQuery.noConflict();

/* 
	This is the function we can call from the flash
	ActionScript example: ExternalInterface.call("loadpage", { fuseaction:"section.home", id:"12" });
*/
function loadpage(querypairs) {
	// querypairs need to be passed like this: loadpage({ fuseaction:"section.home", id:"12" });
	jQuery("body").removeClass("landing");
	jQuery(bodycontentdiv).show();
	jQuery(bodycontentdiv).html(loadingdisplay);
	jQuery.get(getcontentscript, querypairs, function(content) { 
		jQuery(bodycontentdiv).html(content); 
		if (jQuery('#contentpane')) { 
			myscrollbar("#contentpane"); 
		}
		currentpage = querypairs.id;
		// add style for special pages such as map and gallery (map: 19, gallery: 34, media login: 46. 
		// Adding pagebg:1 to the query will override the background (use this to change BGs in forms, etc)
		if ((querypairs.id==19 || querypairs.id==34 || querypairs.id==46 || querypairs.id == 7 || 
			 	querypairs.id == 28) && querypairs.pagebg != 1) { 
			jQuery("body").addClass("profiles");
			jQuery("#pagetools").hide();
		} else {
			jQuery("body").removeClass("profiles");
			detectMacXFF2();
			jQuery("#pagetools").show();
		}
	});
}
function myscrollbar(scrolldiv) {
/* Removed jScrollPane because it's not compatible with SWFAddress strict mode:
	jQuery(scrolldiv).jScrollPane({showArrows:true, scrollbarWidth:10, scrollbarMargin:20, animateTo:true});	
	scrollPane = jQuery(scrolldiv);
	activeLink = jQuery("#link1");
*/
}
function detectMacXFF2() {
  var userAgent = navigator.userAgent.toLowerCase();
  if (/firefox[\/\s](\d+\.\d+)/.test(userAgent)) {
    var ffversion = new Number(RegExp.$1);
    if (ffversion < 3 && userAgent.indexOf('mac') != -1) {
      // Execute FF/Mac code here ... 
	  jQuery("body").addClass('ffmac');
    }
  }
}
function bottombar(section) {
	if (section != 0) {
		jQuery(bodycontentdiv).hide();
		jQuery("body").addClass("landing");
	} else {
		jQuery("body").removeClass("landing");
	}
}
// These functions are just for showing and hiding parts of the page that aren't needed. 
function hidebar() { // probably not in use
	jQuery("#bottomModules").hide();
}
function showbar() { // probably not in use 
	jQuery("#bottomModules").show();
	jQuery("#pagetools").show();
}
function hidepage() {
	jQuery(bodycontentdiv).hide();
	jQuery("#pagetools").hide();
}
function showpage() {
	jQuery(bodycontentdiv).show();
}

// Helper function 
function isdefined( variable) {
    return (typeof(window[variable]) == "undefined")?  false: true;
}

// FORM FUNCTIONS 

// onfocus function 
function doLabelOn(my_input,my_label) {
	// remove label 
	if (jQuery(my_input).val()==my_label) { 
		jQuery(my_input).val('');
	}
	// change styles 
	jQuery(my_input+'_holder').addClass('focusfield');
	if (my_input != '#messageinput') {
		jQuery(my_input).removeClass('inputfield');
		jQuery(my_input).addClass('inputfield_on');
	} else jQuery(my_input).addClass('focusfield');
}
// on blur function
function doLabelOff(my_input,my_label) {
	var tempval = '';
	var templabel = '';
	// add label back if field is empty
	if (jQuery(my_input).val()=='') { 
		jQuery(my_input).val(my_label);
	} else {
		// otherwise append the label 
		tempval=jQuery(my_input).val();
		templabel=' ('+my_label+')';
		var ivalue=tempval.length;
		var ilabel=ivalue-templabel.length;
		if (tempval.substring(ilabel,ivalue) != templabel) jQuery(my_input).val(tempval+' ('+my_label+')');
	}
	// set style classes as desired 
	jQuery(my_input+'_holder').removeClass('focusfield');
	jQuery(my_input).removeClass('focusfield');
	if (my_input != '#messageinput') {
		jQuery(my_input).removeClass('inputfield_on');
		jQuery(my_input).addClass('inputfield');
	} else jQuery(my_input).removeClass('focusfield');
}
// removes label text from fields so data can be submitted cleanly 
function removeLabel(my_input,my_label) {
	tempval=jQuery(my_input).val();
	templabel=' ('+my_label+')';
	if (jQuery(my_input).val() == my_label) { 
		newval='';
	} else if (tempval.substring((tempval.length-templabel.length),tempval.length) == templabel) {
		newval = tempval.substr(0,(tempval.length-templabel.length));
	} else {
		newval = tempval;			
	}
	return newval;
}
// Copied from dsp_main.cfm
/* I think this was only used for testing. I had to comment out the method it calls inside Flash because the application wouldn't export with it anymore
function displayFlashInfo() {
	var flash;
	if(navigator.userAgent.indexOf("Safari") != -1) {
		flash = window.flashcontent1;
	} else if (navigator.appName.indexOf("Microsoft") != -1) {
		flash = window.flashcontent1;
	}else {
		flash = window.document.flashcontent2;
	}
	var flashInfo = flash.getFlashInfo();
	displayText = "";
	for(var key in flashInfo) {
		//flashForm.flashInfoDisplay.value += key + ": " + flashInfo[key] + "\n";
		displayText += key + ": " + flashInfo[key] + "\n";
	}
	alert(displayText);
}
*/
/* Old verison, before implementation of SWFObject:
function loadContent(displayNow, id, nameValuePairs) {
	var flash;
	if(navigator.userAgent.indexOf("Safari") != -1) {
		flash = window.flashcontent1;
	} else if (navigator.appName.indexOf("Microsoft") != -1) {
		flash = window.flashcontent1;
	}else {
		flash = window.document.flashcontent2;
	}
	var result = flash.contentRequest(displayNow, id, nameValuePairs);
	if (!result) {
		alert("There was an error communicating with Flash.");
	}
}
*/
function loadContent(displayNow, id, nameValuePairs) {
	var result = window.document.swfObjectContent.contentRequest(displayNow, id, nameValuePairs);
	if (!result) {
		alert("There was an error communicating with Flash.");
	}
}
function printfriendly() {
	printpage='print_friendly.cfm?fuseaction=section.home&id='+currentpage;
	mywin=window.open(printpage);
}

/* Handlers for the Search input field in view/displays/dsp_main.cfm */
function doFGFSearch() {
	//loadpage({ fuseaction: "section.home", id: 47, searchterm: jQuery("#searchfield").val() });
	loadContent(true,'47',jQuery("#searchfield").val()); 
	document.forms.search.searchfield.value = "Search";
	document.forms.search.searchfield.blur();
}
function noenter() {
	return !(window.event && window.event.keyCode == 13); 
}
function searchFocused() {
//	alert("searchFocused: " + document.forms.search.searchfield.value);
	if (document.forms.search.searchfield.value == "Search") {
		document.forms.search.searchfield.value = "";
	}
}
function searchUnfocused() {
//	alert("searchUnfocused");
	if (document.forms.search.searchfield.value == "") {
		document.forms.search.searchfield.value = "Search";
	}
}
/* End handler for search */