/*
 * A set of utilities for flash related tasks...
 */

fixFlash = function(){
	objObjects = document.getElementsByTagName("object");
	for(var i = 0; i < objObjects.length; i++){
		objObjects[i].outerHTML = objObjects[i].outerHTML;
	}
}




var flashboxMargin   = [50, 0, 0, 0];
var flashboxPadding  = [25, 0, 0, 0];
var flashboxDuration = 0.8;

var wndFlashbox = null;

//
// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.org
//
getPageScroll = function(){
	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll)
	return arrayPageScroll;
}



/**
 * swith out a 'click here to play link with a flash movie
 *
 * flashContainer - id or html element to contain the flash object/embed
 * movie          - string path to the movie
 * referer        - html element the user clicked to show the flash
 * movieWidth     - (optional) width of the flash element
 * movieHeight    - (optional) height of the flash element
 */
flashSwitcher = function(flashContainer, movie, referer, movieWidth, movieHeight){
	//string or object
	if(typeof flashContainer == 'string'){
		flashContainer = $(flashContainer);//get element by id
	}
	//invalid element..
	if(typeof flashContainer != 'object' || !(flashContainer.id)){
		return false; //just die
	}

	//defaults...
	if(movieWidth  == null){ movieWidth  = '320px';                                 }
	if(movieHeight == null){ movieHeight = '262px';                                 }

	var strFlash = (
		'<object' +
		'	id="fs-object-id-'+flashContainer.id+'"' +
		'	width="'+(movieWidth)+'"' +
		'	height="'+(movieHeight)+'"' +
		'	align="middle"' +
		'	codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0"' +
		'	classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"' +
		'>' +
		'	<param value="sameDomain" name="allowScriptAccess" />' +
		'	<param id="fs-object-movie-id-'+flashContainer.id+'"  value="'+(movie)+'" name="movie"/>' +
		'	<param value="high" name="quality"/>' +
		'	<param value="#000000" name="BGCOLOR"/>' +
		'	<embed' +
		'		width="'+(movieWidth)+'"' +
		'		height="'+(movieHeight)+'"' +
		'		bgcolor="#000000"' +
		'		type="application/x-shockwave-flash"' +
		'		pluginspage="http://www.macromedia.com/go/getflashplayer"' +
		'		quality="high"' +
		'		src="'+(movie)+'"' +
		'	/>' +
		'</object>'
	);

	//hide the referer like
	if(referer && referer.style){
		referer.style.display = 'none';
	}

	flashContainer.innerHTML = strFlash; //dump in the flash... in theory
										 //in theory it should also remove
										 //the link
	return false; //even though we succeded, we return false to kill the link redirect
}








//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
getPageSize = function(){
	var xScroll, yScroll;

	if (window.innerHeight && window.scrollMaxY) {
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}

	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}

	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else {
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight)
	return arrayPageSize;
}



showFlashbox = function(href, width, height){
	var container = 'http://www.harvest.org/hobs/themes/common/containers/flashbox.php?path=';

	if(width == null){
		width = (window.screen.width-40);
	}
	if(height == null){
		height = (window.screen.width-40)*(9/16);
	}


	wndFlashbox = window.open(
		container+href    ,
		'Flashbox'        ,
		'menubar=no,'     +
		'toolbar=no,'     +
		'location=no,'    +
		'directories=no,' +
		'personalbar=no,' +
		'status=no,'      +
		'resizable=no,'   +
		'scrollbars=no,'  +
		'dependent=no,'   +
		'width='          + ( width ) + ',' +
		'height='         + ( height )
	);
	return false;


	/*var objFlashboxO = $('flashbox-overlay');
	var objFlashbox  = $('flashbox');
	var objFlashboxC = $('flashbox-container');

	var arrPageSize = getPageSize();
	var arrPageScroll = getPageScroll();

	if(width == null || width == 0){
		width = arrPageSize[2]-200;
	}
	if(height == null || height == 0){
		height = arrPageSize[3]-flashboxMargin[0];
	}

	objFlashboxO.style.height = arrPageSize[1];


	objFlashbox.style.top = arrPageScroll[1]+'px';
	objFlashbox.style.height = arrPageSize[3]+'px';

	new Effect.Appear(objFlashboxO, {duration: flashboxDuration, to: 0.8 });
	new Effect.Appear(objFlashbox,  {duration: flashboxDuration, to: 1.0 });

	populate_container = function(){

		var objObj = document.createElement("object");
		objObj.setAttribute('classid'  ,'clsid:d27cdb6e-ae6d-11cf-96b8-444553540000');
		objObj.setAttribute('codebase' ,'http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0');
		objObj.setAttribute('width'    , width);
		objObj.setAttribute('height'   , height);
		objObj.setAttribute('id'       , 'FlashboxFlash');
		objObj.setAttribute('align'    , 'middle');

		var objParam1 = document.createElement("param");
		objParam1.setAttribute('name'  , 'allowScriptAccess');
		objParam1.setAttribute('value' , 'sameDomain');

		objObj.appendChild(objParam1);

		var objParam2 = document.createElement("param");
		objParam2.setAttribute('name'  , 'movie');
		objParam2.setAttribute('value' , href);

		objObj.appendChild(objParam2);

		var objParam3 = document.createElement("param");
		objParam3.setAttribute('name'  , 'quality');
		objParam3.setAttribute('value' , 'high');

		objObj.appendChild(objParam3);

		var objParam4 = document.createElement("param");
		objParam4.setAttribute('name'  , 'wmode');

		//firefox wmode bug... oh well if you change your UA string
		if(navigator.userAgent.indexOf("Firefox")!=-1){
			objParam4.setAttribute('value' , 'window');
		}else{
			objParam4.setAttribute('value' , 'transparent');
		}

		objObj.appendChild(objParam4);

		var objParam5 = document.createElement("param");
		objParam5.setAttribute('name'  , 'bgcolor');
		objParam5.setAttribute('value' , '#000000');

		objObj.appendChild(objParam5);

		var objEmbed = document.createElement("embed");
		objEmbed.setAttribute('src'               , href);
		objEmbed.setAttribute('quality'           , 'high');
		objEmbed.setAttribute('wmode'             , 'opaque');

		//firefox wmode bug... oh well if you change your UA string
		if(navigator.userAgent.indexOf("Firefox")!=-1){
			objEmbed.setAttribute('wmode'             , 'window');
		}else{
			objEmbed.setAttribute('wmode'             , 'transparent');
		}



		objEmbed.setAttribute('bgcolor'           , '#000000');
		objEmbed.setAttribute('width'             , width);
		objEmbed.setAttribute('height'            , height);
		objEmbed.setAttribute('name'              , 'FlashboxFlash');
		objEmbed.setAttribute('align'             , 'middle');
		objEmbed.setAttribute('allowScriptAccess' , 'sameDomain');
		objEmbed.setAttribute('type'              , 'application/x-shockwave-flash');
		objEmbed.setAttribute('pluginspage'       , 'http://www.macromedia.com/go/getflashplayer');

		objObj.appendChild(objEmbed);

		objFlashboxC.appendChild(objObj);
		objFlashboxC.innerHTML = objFlashboxC.innerHTML;
	}

	setTimeout(populate_container, flashboxDuration*1100);*/

}

/*
hideFlashbox = function(){
	var objFlashboxO = $('flashbox-overlay');
	var objFlashbox  = $('flashbox');
	var objFlashboxC = $('flashbox-container');

	new Effect.Fade(objFlashbox,  {duration: flashboxDuration, to: 0 });
	new Effect.Fade(objFlashboxO, {duration: flashboxDuration, to: 0 });
	new Effect.Fade(objFlashboxC, {duration: flashboxDuration, to: 0 });
	setTimeout(
		function(){
			objFlashboxC.innerHTML = '';
		},
		flashboxDuration*2100
	)


}


initFlashbox = function(){
	var objBody = document.getElementsByTagName("body").item(0);


	var objOverlay = document.createElement("div");
	objOverlay.setAttribute('id','flashbox-overlay');
	objOverlay.style.display = 'none';
	objOverlay.onclick = function() { hideFlashbox(); }
	objBody.appendChild(objOverlay);

	var objFlashbox = document.createElement("div");
	objFlashbox.setAttribute('id','flashbox');
	objFlashbox.style.display = 'none';
	objBody.appendChild(objFlashbox);

	objFlashbox.innerHTML = (
		'<a class="close" href="#" onclick="hideFlashbox(); return false;">Close</a>' +
		'<div id="flashbox-container"></div>'
	);
}

*/
Event.observe(window, 'load', fixFlash, false);

