/* Hide EVP */
function hidediv (div_id, extension) 
{
	x = document.getElementById(div_id); 
	if (x.className=="div_shown" || x.className=="div_shown_movie")
	{
		if(extension != 'flv')
			stopMovie();
		x.innerHTML = "";
		x.className="div_hidden";
		change_opacity(100);
		movie_playing = "false";
		x.innerHTML = "";
	}
}

/* Show EVP */
function showmoviediv(max_number, movie_url, movie_width, movie_height, autostart, fullscreen, container_width, container_height, template, div_horizontal, div_vertical)
{
	div_id = 'evp_div';
	x = document.getElementById(div_id); 

	/* Get movie array (extension, number, path) */
	movie_array = make_array(movie_url);

	
	// Determine if settings are passed by link or default

	if(!movie_width || movie_width=="undefined" || movie_width=="")
		movie_width = evp_movie_width;
	if(!movie_height || movie_height=="undefined" || movie_height=="")
		movie_height = evp_movie_height;

	if(!autostart || autostart=="undefined" || autostart=="")
	{
		if(evp_movie_auto_play=='on')
			autostart = 'true';
		else
			autostart = 'false';
	}
	if(!fullscreen || fullscreen=="undefined" || fullscreen=="")
	{
		if(evp_movie_full_screen=='on')
			fullscreen = 'true';
		else
			fullscreen = 'false';
	}

	if(!container_width || container_width=="undefined" || container_width=="")
		container_width = evp_width;

	if(!container_height || container_height=="undefined" || container_height=="")
		container_height = evp_height;

	if(!template || template=="undefined" || template=="")
		template = template_selected;

	template_code = template_array[template];
	
	if(!div_horizontal || div_horizontal=="undefined" || div_horizontal=="")
		div_horizontal = evp_horizontal;
	if(!div_vertical || div_vertical=="undefined" || div_vertical=="")
		div_vertical = evp_vertical;

	// If anything is playing stop it and clear the code. 
	if(!movie_playing || movie_playing == "false")
		movie_playing = "true";
	else
	{
		if(movie_array[2] != 'flv')
			stopMovie();
		else
			sendEvent('stop');
		x.innerHTML = "";
	}

	// Get window size for positioning div
	evp_windows_size(div_horizontal, div_vertical);

	x.className="div_shown_movie";
	xclass = getElementsByClass("div_shown_movie");
	xclass[0].style.width = container_width+'px';
	xclass[0].style.height = container_height+'px';
	xclass[0].style.background = evp_bgcolor+" url('"+evp_bgimage+"') "+evp_bgrepeat;
	xclass[0].style.border =  evp_borderwidth+'px solid '+evp_bordercolor;
	xclass[0].style.left = position_left+'px';
	xclass[0].style.top = position_top+'px';
	xclass[0].style.color = evp_textcolor;

	change_opacity(30);

	filler = '';
	filler = filler+stringreplace(movie_array, max_number, movie_width, movie_height, autostart, fullscreen, container_width, container_height, template, div_horizontal, div_vertical, template_code);

	filler = stripslashes(filler);

	x.innerHTML = filler;

	// Create movie code
	if(movie_array[2] == 'flv')
		movie_code_flv(movie_url, movie_width, movie_height, autostart, fullscreen);
	else
		movie_code_other(movie_url, movie_width, movie_height, autostart, fullscreen)
}


/* Create the movie array from the url */
function make_array(array_url)
{
	new_array = array_url.split(".");		

	find_number = parseInt(new_array[new_array.length-2].substring(new_array[new_array.length-2].length-3));
	find_number_length = 3;

	if(isNaN(find_number))
	{
		find_number_length = 2;
		find_number = parseInt(new_array[new_array.length-2].substring(new_array[new_array.length-2].length-2));
	}

	if(isNaN(find_number))
	{
		find_number_length = 1;
		find_number = parseInt(new_array[new_array.length-2].substring(new_array[new_array.length-2].length-1));
	}

	if(isNaN(find_number))
	{
		find_number_length = 0;
		find_number = '';
	}

	var base_url = '';

	for(var i=0; i < new_array.length-2; i++)
	{
		base_url = base_url + new_array[i] + ".";
	}

	base_url = base_url + new_array[new_array.length-2].substring(0,new_array[new_array.length-2].length-find_number_length);

	current_number = find_number;
	extension_url = new_array[new_array.length-1];

	return [base_url, current_number, extension_url];
}

/* Create movie code for FLV */
function movie_code_flv(movie_url, movie_width, movie_height, autostart, fullscreen)
{
	var so = new SWFObject(root_dir+'wp-content/plugins/evp/mediaplayer.swf','mpl',movie_width,movie_height,'7');
	so.addParam("allowfullscreen",fullscreen);
	so.addVariable("autostart",autostart);
	so.addVariable("enablejs","true");
	so.addVariable("javascriptid","mpl");
	so.addVariable("file",movie_url);
	so.write('player');
}

/* Create movie code for non FLV */
function movie_code_other(movie_url, movie_width, movie_height, autostart, fullscreen)
{
	movie_code = "<OBJECT id='MediaPlayer' name='MediaPlayer' width='"+movie_width+"' height='"+movie_height+"' CLASSID='CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6'    type='application/x-oleobject'>\n";
	movie_code = movie_code + "<PARAM NAME='URL' VALUE='"+movie_url+"'>\n";
	movie_code = movie_code +  "<PARAM NAME='SendPlayStateChangeEvents' VALUE='True'>\n";
	movie_code = movie_code +  "<PARAM NAME='AutoStart' VALUE='"+autostart+"'>\n";
	movie_code = movie_code +  "<PARAM NAME='stretchToFit' VALUE='false'>\n";
	movie_code = movie_code +  "<PARAM name='uiMode' value='full'>\n";
	movie_code = movie_code +  "<PARAM name='PlayCount' value='1'>\n";
	movie_code = movie_code +  "<PARAM NAME='AutoRewind' VALUE='0'>\n";
	movie_code = movie_code +  "<embed type='application/x-mplayer2' pluginspage='http://www.microsoft.com/Windows/MediaPlayer/' id='MediaPlayer' name='MediaPlayer' autostart='"+autostart+"' width='"+movie_width+"' height='"+movie_height+"' transparentstart='1' SHOWSTATUSBAR='1' loop='0' controller='true' src='"+movie_url+"' stretchToFit='false'></embed></OBJECT>\n";
	var y = document.getElementById('player');
	y.innerHTML = movie_code;
}


/* Stop non FLV movie */
function stopMovie()
{
	var IE = document.all?true:false

	if(IE)
		MediaPlayer.controls.stop();
}


/* Play Full Screen for non FLV movie */
function playFullscreen() 
{
	// Check if the video is playing. If it is, change
	// to fullscreen. If not, start checking to see when it is.
	if (3 == MediaPlayer.playState) 
	{
		MediaPlayer.fullscreen = 'true';
	}
	else 
	{
		MediaPlayer.controls.play();
		if (!stateTimer) stateTimer = window.setInterval( checkState, 500 );
	}
}

/* Check if Video has started for non FLV movie */
function checkState() 
{
	// Check periodically to see if the video has
	// started. If so, destroy the timer and change to fullscreen
	if (3 == MediaPlayer.playState) 
	{
		window.clearInterval( stateTimer );
		stateTimer = null;
		MediaPlayer.fullscreen = 'true';
	}
}


/* Function to find elements by class used for finding and controlling the page class */
function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp('(^|\\s)'+searchClass+'(\\s|$)');
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

/* Change opacity */
function change_opacity(opacity_level)
{
	el = getElementsByClass('page');
	el[0].style.filter = 'alpha(opacity='+opacity_level+')';
	el[0].style.opacity = (opacity_level/100); 
	el[0].style.MozOpacity = (opacity_level/100); 
	el[0].style.KhtmlOpacity = (opacity_level/100);
}

/* Strip slashes */
function stripslashes(str) 
{
	str=str.replace(/\\'/g,'\'');
	str=str.replace(/\\"/g,'"');
	str=str.replace(/\\\\/g,'\\');
	str=str.replace(/\\0/g,'\0');
	return str;
}


// Build nav_matrix
function evp_nav_matrix_build(movie_array, max_number, movie_width, movie_height, autostart, fullscreen, container_width, container_height, template, div_horizontal, div_vertical)
{
	evp_matrix = '<div class="movie-clips-container">';

	j = 1;
	for(i=0; i<max_number; i++)
	{
		movie_url_nav = movie_array[0]+(i+1)+'.'+movie_array[2];
		movie_url_nav = 'javascript:showmoviediv(\''+max_number+'\',\''+movie_url_nav+'\', \''+movie_width+'\', \''+movie_height+'\', \''+autostart+'\', \''+fullscreen+'\', \''+container_width+'\', \''+container_height+'\', \''+template+'\' ,\''+div_horizontal+'\', \''+div_vertical+'\')';
		if(j<4)
		{
			evp_matrix = evp_matrix+'<a href="'+movie_url_nav+'" class="movie-clips" style="background: url(\''+evp_nav_image_matrix+'\') top left no-repeat;" />';
			evp_matrix = evp_matrix+(i+1);
			evp_matrix = evp_matrix+'</a>';
			j++;
		}
		else
		{
			evp_matrix = evp_matrix+'<a href="'+movie_url_nav+'" class="movie-clips" style="background: url(\''+evp_nav_image_matrix+'\') top left no-repeat; margin-right:0px;" />';
			evp_matrix = evp_matrix+(i+1);
			evp_matrix = evp_matrix+'</a>';
			evp_matrix = evp_matrix+'<br clear=both>';
			j = 1;
		}
	}
	evp_matrix = evp_matrix+'</div>';
	return evp_matrix;
} 

// Build nav_numbers
function evp_nav_numbers_build(movie_array, max_number, movie_width, movie_height, autostart, fullscreen, container_width, container_height, template, div_horizontal, div_vertical)
{
	evp_numbers ='';
	evp_numbers_min = (movie_array[1]-1);
	evp_numbers_plus = (max_number-movie_array[1]);

	/* determine how many loops to take to diplay x number before the current position */
	evp_limit_calc(evp_numbers_plus);
	if(evp_limit>evp_numbers_min)
		evp_limit=evp_numbers_min;

	for(i=1;i<=evp_limit; i++)
	{
		evp_numbers = evp_numbers+'<a href="javascript:showmoviediv(\''+max_number+'\',\''+movie_array[0]+(movie_array[1]-evp_limit-1+i)+'.'+movie_array[2]+'\', \''+movie_width+'\', \''+movie_height+'\', \''+autostart+'\', \''+fullscreen+'\', \''+container_width+'\', \''+container_height+'\', \''+template+'\' ,\''+div_horizontal+'\', \''+div_vertical+'\')" class="evp_nav_numbers" style="margin-right:3px;">'+(movie_array[1]-evp_limit-1+i)+'</a>';
	}

	evp_numbers = evp_numbers+'<span class="evp_nav_numbers" style="margin-right:0px;">'+movie_array[1]+'</span>';

	evp_limit_calc(evp_numbers_min);
	for(i=1;i<=evp_numbers_plus && i<=evp_limit; i++)
	{
		evp_numbers = evp_numbers+'<a href="javascript:showmoviediv(\''+max_number+'\',\''+movie_array[0]+(movie_array[1]+i)+'.'+movie_array[2]+'\', \''+movie_width+'\', \''+movie_height+'\', \''+autostart+'\', \''+fullscreen+'\', \''+container_width+'\', \''+container_height+'\', \''+template+'\' ,\''+div_horizontal+'\', \''+div_vertical+'\')" class="evp_nav_numbers" style="margin-left:3px;">'+(movie_array[1]+i)+'</a>';
	}

	return evp_numbers;
}


/* Get the nav_numbers' maximum amount of numbers to display on either side of the current clip number */
function evp_limit_calc(evp_numbers_mm)
{
	switch(evp_numbers_mm)
	{
		case 0:
		evp_limit = 4;
		break 

		case 1:
		evp_limit = 3;
		break 

		default:
		evp_limit = 2;
		break 
	}
	return evp_limit;
}


// Replace strings in the template
function stringreplace(movie_array, max_number, movie_width, movie_height, autostart, fullscreen, container_width, container_height, template, div_horizontal, div_vertical, template_code)
{
	// Define the string to be worked on
	str = template_code;

	/* Movie Clip */
	str = str.replace(/%evp_movie%/, '<div class="movie_div" id="player"><a href="http://www.macromedia.com/go/getflashplayer">Get the Flash Player</a> to see this movie.</div>');

	/* Movie Clip Nav Matrix */
	str = str.replace(/%nav_matrix%/gi, evp_nav_matrix_build(movie_array, max_number, movie_width, movie_height, autostart, fullscreen, container_width, container_height, template, div_horizontal, div_vertical));

	/* Movie Clip Nav First */
	if(movie_array[1]!=1)
	{
		str = str.replace(/%nav_first_image%/gi, '<a href="javascript:showmoviediv(\''+max_number+'\',\''+movie_array[0]+'1'+movie_array[2]+'\', \''+movie_width+'\', \''+movie_height+'\', \''+autostart+'\', \''+fullscreen+'\', \''+container_width+'\', \''+container_height+'\', \''+template+'\' ,\''+div_horizontal+'\', \''+div_vertical+'\')" title="first clip"><img src="'+evp_nav_image_first+'" alt="first" style="border: 0px;" /></a>');

		str = str.replace(/%nav_first%/gi, '<a href="javascript:showmoviediv(\''+max_number+'\',\''+movie_array[0]+'1.'+movie_array[2]+'\', \''+movie_width+'\', \''+movie_height+'\', \''+autostart+'\', \''+fullscreen+'\', \''+container_width+'\', \''+container_height+'\', \''+template+'\' ,\''+div_horizontal+'\', \''+div_vertical+'\')" title="first clip">&laquo;</a>');
	}
	else
	{
		str = str.replace(/%nav_first_image%/gi, '<img src="'+evp_nav_image_first+'" alt="first" style="border: 0px;" />');

		str = str.replace(/%nav_first%/gi, '&laquo;');
	}


	/* Movie Clip Nav Last */
	if(movie_array[1]!=max_number)
	{
		str = str.replace(/%nav_last_image%/gi, '<a href="javascript:showmoviediv(\''+max_number+'\',\''+movie_array[0]+max_number+'.'+movie_array[2]+'\', \''+movie_width+'\', \''+movie_height+'\', \''+autostart+'\', \''+fullscreen+'\', \''+container_width+'\', \''+container_height+'\', \''+template+'\' ,\''+div_horizontal+'\', \''+div_vertical+'\')" title="last clip"><img src="'+evp_nav_image_last+'" alt="last" style="border: 0px;" /></a>');

		str = str.replace(/%nav_last%/gi, '<a href="javascript:showmoviediv(\''+max_number+'\',\''+movie_array[0]+max_number+'.'+movie_array[2]+'\', \''+movie_width+'\', \''+movie_height+'\', \''+autostart+'\', \''+fullscreen+'\', \''+container_width+'\', \''+container_height+'\', \''+template+'\' ,\''+div_horizontal+'\', \''+div_vertical+'\')" title="last clip">&raquo;</a>');
	}
	else
	{
		str = str.replace(/%nav_last_image%/gi, '<img src="'+evp_nav_image_last+'" alt="last" style="border: 0px;" />');

		str = str.replace(/%nav_last%/gi, '&raquo;');
	}

	/* Movie Clip Nav Previous */
	if(movie_array[1]!=1)
	{
		str = str.replace(/%nav_previous_image%/gi, '<a href="javascript:showmoviediv(\''+max_number+'\',\''+movie_array[0]+(movie_array[1]-1)+'.'+movie_array[2]+'\', \''+movie_width+'\', \''+movie_height+'\', \''+autostart+'\', \''+fullscreen+'\', \''+container_width+'\', \''+container_height+'\', \''+template+'\ ,\''+div_horizontal+'\', \''+div_vertical+'\')" title="previous clip"><img src="'+evp_nav_image_previous+'" alt="last" style="border: 0px;" /></a>');

		str = str.replace(/%nav_previous%/gi, '<a href="javascript:showmoviediv(\''+max_number+'\',\''+movie_array[0]+(movie_array[1]-1)+'.'+movie_array[2]+'\', \''+movie_width+'\', \''+movie_height+'\', \''+autostart+'\', \''+fullscreen+'\', \''+container_width+'\', \''+container_height+'\', \''+template+'\' ,\''+div_horizontal+'\', \''+div_vertical+'\')" title="previous clip">&lsaquo;</a>');
	}
	else
	{
		str = str.replace(/%nav_previous_image%/gi, '<img src="'+evp_nav_image_previous+'" alt="last" style="border: 0px;" />');

		str = str.replace(/%nav_previous%/gi, '&lsaquo;');
	}

	/* Movie Clip Nav Next */
	if(movie_array[1]!=max_number)
	{
		str = str.replace(/%nav_next_image%/gi, '<a href="javascript:showmoviediv(\''+max_number+'\',\''+movie_array[0]+(movie_array[1]+1)+'.'+movie_array[2]+'\', \''+movie_width+'\', \''+movie_height+'\', \''+autostart+'\', \''+fullscreen+'\', \''+container_width+'\', \''+container_height+'\', \''+template+'\' ,\''+div_horizontal+'\', \''+div_vertical+'\')" title="next clip"><img src="'+evp_nav_image_next+'" alt="last" style="border: 0px;" /></a>');

		str = str.replace(/%nav_next%/gi, '<a href="javascript:showmoviediv(\''+max_number+'\',\''+movie_array[0]+(movie_array[1]+1)+'.'+movie_array[2]+'\', \''+movie_width+'\', \''+movie_height+'\', \''+autostart+'\', \''+fullscreen+'\', \''+container_width+'\', \''+container_height+'\', \''+template+'\' ,\''+div_horizontal+'\', \''+div_vertical+'\')" title="next clip">&rsaquo;</a>');
	}
	else
	{
		str = str.replace(/%nav_next_image%/gi, '<img src="'+evp_nav_image_next+'" alt="last" style="border: 0px;" />');

		str = str.replace(/%nav_next%/gi, '&rsaquo;');
	}

	/* Movie Clip Nav Numbers */
	str = str.replace(/%nav_numbers%/gi, evp_nav_numbers_build(movie_array, max_number, movie_width, movie_height, autostart, fullscreen, container_width, container_height, template, div_horizontal, div_vertical));

	/* EVP Close Button */
	str = str.replace(/%evp_close%/gi, "<a href='javascript:hidediv(\"evp_div\", \""+movie_array[2]+"\")'><img border='0' src='"+evp_close_button+"' /></a>");


	/* Replace %br% with \r */
	str = str.replace(/%br%/gi,'\r');

	return str;
}


// Prepare the form for a new template
function evp_new_template()
{
	new_template_name = document.template_editor.evp_new_template.value;
	if(new_template_name != "")
	{
		document.template_editor.evp_templates_selected.options[0].selected = true;
		textarea = document.getElementById('evp_template_html');
		textarea.value = '';
	}
	else
		alert("Please enter a name for your new template");
}

// Get the template display for the current selected template
function display_evp_template_html()
{
	textarea = document.getElementById('evp_template_html');
	selection = document.template_editor.evp_templates_selected;
	key = selection.options[selection.selectedIndex].value;
	if(key == 'new')
	{
		textarea.value = '';
		document.template_editor.evp_new_template.focus();
	}
	else
	{
		document.template_editor.evp_new_template.value ="";

		str = js_array[key];
		str = str.replace(/%br%/gi,'\r');

		textarea.value = '';
		textarea.value = str;
	}
}

// Prepare form for deletion of selected template and submit
function evp_delete_template()
{
	selection = document.template_editor.evp_templates_selected;
	key = selection.options[selection.selectedIndex].value;

	var response = confirm("Are you sure you wish to delete: "+key);

	if (response == true)
  	{
		document.template_editor.evp_save_template.value = "delete";
		document.template_editor.evp_save_template.click();
	}
	
}

function evp_windows_size(div_horizontal, div_vertical)
{
	if (parseInt(navigator.appVersion)>3)
	{
		if (navigator.appName=="Netscape")
		{
			winW = window.innerWidth-16;
			winH = window.innerHeight-16;
		}
		if (navigator.appName.indexOf("Microsoft")!=-1)
		{
			winW = document.body.offsetWidth-20;
			winH = document.body.offsetHeight-20;

			winH = (0.55*winW);
		}
	}

	position_left = ((winW/2)-(evp_width/2)+parseInt(div_horizontal));
	position_top = ((winH/2)-(evp_height/2)+parseInt(div_vertical));
}
