////////////////////////////////////////////////////
//
// CONTENT.JS
//
//	This page defines javascript for the content
//	area.
//
// Created 05/05/2009 by Chris Jones
//
////////////////////////////////////////////////////

// Declare global variables


// ======
// Events
// ======

/***************************************************
 * This function is called when the 'ready' event 
 * triggers in the master file. It sets up all DOM
 * elements in the content area.
 ***************************************************/
function runContentJavascriptReady(){
	
	// Load any flash elements
	// -----------------------
	jQ(".flashreplace").each(function(i,thisFlash){
		
		if(jQ.flash.available && jQ.flash.hasVersion(9.1)) {
			// Store resize properties for the flash element
			jQ(thisFlash).attr('fratio', jQ(thisFlash).attr('fheight') / jQ(thisFlash).attr('fwidth'));
			
			//Setup the HTML options for this Flash element
			var flashVarsAttr = jQ(thisFlash).attr('fflashvars') ? jQ(thisFlash).attr('fflashvars') : '{}';
			if (flashVarsAttr.substr(0,1) != "{"){flashVarsAttr = "{" + flashVarsAttr + "}";}
			eval('var flashVarsArray = ' + flashVarsAttr);
			
			var flashOptions = {
				swf: jQ(thisFlash).attr('fsrc') ? jQ(thisFlash).attr('fsrc') : '',
				width: '100%',
				height: jQ(thisFlash).attr('fheight') ? jQ(thisFlash).attr('fheight') : '',
				params: {
					flashvars: flashVarsArray, 
					align: jQ(thisFlash).attr('falign') ? jQ(thisFlash).attr('falign') : 'middle',
					bgcolor: jQ(thisFlash).attr('fbgcolor') ? jQ(thisFlash).attr('fbgcolor') : '#000000',
					quality: jQ(thisFlash).attr('fquality') ? jQ(thisFlash).attr('fquality') : 'high',
					wmode: jQ(thisFlash).attr('fwmode') ? jQ(thisFlash).attr('fwmode') : 'transparent',
					allowscriptaccess: 'sameDomain',
					allowfullscreen: 'true'
				}
			}
			
			jQ(thisFlash).flash(flashOptions);
//			jQ(thisFlash).children('.flashdescription')
//						 .insertAfter(jQ(thisFlash).find('param:last'))
//						 .removeClass('flashdescription');
			jQ(thisFlash).removeAttr('fsrc');
			jQ(thisFlash).removeAttr('fwidth');
			jQ(thisFlash).removeAttr('fheight');
			jQ(thisFlash).removeAttr('falign');
			jQ(thisFlash).removeAttr('fflashvars');
			jQ(thisFlash).removeAttr('fbgcolor');
			jQ(thisFlash).removeAttr('fquality');
			jQ(thisFlash).removeAttr('fwmode');
			jQ(thisFlash).removeAttr('crossrefimagealt');
		
		} else {
			jQ(thisFlash).children('img.crossrefimage').attr('alt', jQ(thisFlash).attr('crossrefimagealt'));
			jQ(thisFlash).children('img.crossrefimage').show().css('display','block');
		}

	});
	
	
	// Add any drop shadows
	// --------------------
	jQ(".RDCMS_image_dropshadow img").wrap("<span class='shadow1'><span class='shadow2'>" +
     												  "<span class='shadow3'></span></span></span>")
	
	
	// Apply slideToggle events to show/hide pairs
	// -------------------------------------------
	// Hide all non-triggers in pairings
	jQ(".RDCMS_show_hide_pair").children("*:not(.RDCMS_trigger)").each(function(){
		jQ(this).css("height", jQ(this).height()+"px"); 
		jQ(this).hide(); 
	});
	// Change the cursor type for the triggers
	jQ(".RDCMS_show_hide_pair").children(".RDCMS_trigger").css('cursor', 'pointer');
	// Bind click event to trigger
	jQ(".RDCMS_show_hide_pair").children(".RDCMS_trigger").bind("click", function(){
		// Trigger click on expanded pairs that aren't this pairing
/*		jQ(".RDCMS_trigger_expanded").not(jQ(this)).trigger("click");	*/
		// Change the expanded class on this pair's trigger
		if(jQ(this).hasClass(".RDCMS_trigger_expanded")){
			jQ(this).removeClass("RDCMS_trigger_expanded");
		} else {
			jQ(this).addClass("RDCMS_trigger_expanded");
		}
		// Toggle the siblings off the trigger element
		jQ(this).siblings().slideToggle(500);
	});
	
	
	// Set up page specific onReady functions/events
	// ---------------------------------------------
	if (typeof window.initPageSpecific == "function"){
		initPageSpecific();
	}

	// Set up page generic onReady functions/events
	// ---------------------------------------------
	if (typeof window.initPageGeneric == "function"){
		initPageGeneric();
	}

}

/***************************************************
 * This function is called when the 'onload' event 
 * triggers in the master file. It sets up all DOM
 * elements in the content area.
 ***************************************************/
function runContentJavascriptOnLoad(){
	
	// Setup the height of the flash elements
	// (Fixes a bug whereby a flash elements height is
	//	set incorrectly when objects are loaded in a 
	//  unusual manner)
	resizeFlash();
	
	// Set up image reflections
	// ------------------------
	jQ("*[class *= 'RDCMS_imagereflect']").each(function(){
		var height = 0.1;
		var opacity = 0.7;
		var classes = jQ(this).attr('class').split(" ");
		for(var i=0; i < classes.length; i++) {
			if (classes[i].substring(0,19) == 'RDCMS_imagereflect_'){
				var reflectVar = classes[i].split("_");
				if (reflectVar.length >= 3) {height = str2num(reflectVar[2])/100;} 
				if (reflectVar.length >= 4) {opacity = str2num(reflectVar[3])/100;} 
			}
		}
		jQ(this).find('img').reflect({height: height, opacity: opacity} );
	});

	// Set up page specific onLoad functions/events
	// --------------------------------------------
	if (typeof window.onLoadPageSpecific == "function"){
		onLoadPageSpecific();
	}

	// Set up page generic onLoad functions/events
	// --------------------------------------------
	if (typeof window.onLoadPageGeneric == "function"){
		onLoadPageGeneric();
	}
	
}


// =========
// FUNCTIONS
// =========

/***************************************************
 * This function calls prettyPhoto API for the 
 * passed image url
 ***************************************************/
function prettyPhotoPublic(urls, index){
	urls = urls.split("|");
	jQ.prettyPhoto.open(urls, null, null, index);
} 

/***************************************************
 * This function sets the height of Flash elements
 ***************************************************/
function resizeFlash(){
	jQ(".flashreplace  object").each(function(i,thisFlash){
		var flashWidth = jQ(thisFlash).width();
		var ratio = jQ(thisFlash).parent().attr('fratio');
		jQ(thisFlash).css('height', ratio*flashWidth);
	});
} 

/***************************************************
 * This function adds reflections to reflective
 * images.
 ***************************************************/
function resizeImageReflection(){
	jQ('.RDCMS_imagereflect').find('canvas').each(function(){
		jQ(this).width(jQ(this).siblings('img').eq(0).width());
	});
} 
