
var ESLFlix = {};

ESLFlix.preloadVideo = null;
ESLFlix.currentRequest = null;
ESLFlix.currentCategory = "";
ESLFlix.currentVideo = "";
ESLFlix.webpath = "/";
ESLFlix.ishome = false;


ESLFlix.playVideo = function( id ) {
	ESLFlix.loadVideo( id, true );
}

ESLFlix.loadVideo = function( id, autoPlay ) {
	if ( id == ESLFlix.currentVideo )
		return;
	
	ESLFlix.currentVideo = id;
	
	var el = document.getElementById( "media" );
	if ( el ) {
		var html = "";
		if ( el.scrollHeight )
			html += "<div style=\"height:" + (el.scrollHeight - 3) + "px;\">";
		else
			html += "<div>";
		if ( ESLFlix.ishome )
			html += "<h1>Welcome</h1><p>loading...</p>";
		else
			html += "<p>loading...</p>";
		html += "</div>";
		el.innerHTML = html;
		window.scrollTo( 0, 0 );

		// cancel out any pending requests:
		if ( ESLFlix.currentRequest != null ) {
			YAHOO.util.Connect.abort( ESLFlix.currentRequest );
			ESLFlix.currentRequest = null;
		}
		
		// ask the server for details:
		// success callback:
		var handleSuccess = function( o ) {
		
			YAHOO.log( "ESLFlix.playVideo success", "info" );
			var result = {};
			
			if ( o.responseText != undefined ) {
				YAHOO.log( "ESLFlix.playVideo raw response: " + o.responseText, "debug" );
				result = eval( '(' + o.responseText + ')' );
			}
			var html = "";
			
			if ( ESLFlix.ishome )
				html += "<h1>Welcome</h1>";
			else if ( result.name )
				html += "<h1>" + result.name + "</h1>";
			
			if ( result.video ) {
				var vf = ESLFlix.webpath + "dimg/videos/" + ESLFlix.currentCategory + "/" + ESLFlix.currentVideo + "." + result.video;
				var ff = ESLFlix.webpath + (ESLFlix.ishome ? "videoPlayer_sm.swf" : "videoPlayer_lg.swf");
				ff += "?vv=.9a";
				ff += "&id=" + ESLFlix.currentVideo + "&vf=" + vf;
				if ( result.poster ) {
					var pf = ESLFlix.webpath + "dimg/video_posters/" + ESLFlix.currentCategory + "/" + ESLFlix.currentVideo + "." + result.poster;
					ff += "&pf=" + pf;
				}
				if ( o.argument )
					ff += "&ap=1";
				var fe;
				if ( ESLFlix.ishome )
					fe = new FlashEmbed( ff, 8, "360", "300", "#ffffff" );
				else
					fe = new FlashEmbed( ff, 8, "486", "406", "#ffffff" );
				html += fe.getHTML();
			}
			else if ( result.embed )
				html += result.embed;
			
			var elt = document.getElementById( "media" );
			if ( elt ) {
				elt.innerHTML = html;
			}
		}
		
		// failure callback:
		var handleFailure = function( o ) {
			YAHOO.log( "ESLFlix.playVideo failed", "warn" );
			
			var elt = document.getElementById( "media" );
			if ( elt ) {
				elt.innerHTML = "<p>An error occurred while trying to access the video - please try again later.</p>";
			}
		}
		
		var callback = { success:handleSuccess, failure:handleFailure, argument:autoPlay };
		
		var vars = "l=videos&rtfl=name,poster,video,embed&id=" + id;
		
		ESLFlix.currentRequest = YAHOO.util.Connect.asyncRequest( 'POST', ESLFlix.webpath + 'servlet/GetData', callback, vars );

	}
}

ESLFlix.emailRE = /^((?:(?:(?:\w[\.\-\+]?)*)\w)+)\@((?:(?:(?:\w[\.\-\+]?){0,62})\w)+)\.(\w{2,6})$/;
ESLFlix.contactFromFields = [ "name", "email", "addr1", "addr2", "city", "state", "country", "zip", "phone", "comments" ];

ESLFlix.sendContactRequest = function() {

	// cancel out any pending requests:
	if ( ESLFlix.currentRequest != null ) {
		YAHOO.util.Connect.abort( ESLFlix.currentRequest );
		ESLFlix.currentRequest = null;
	}
	
	// pre-filter:
	var badmsg = "";
	var nm = document.forms["contact"]["name"].value;
	if ( nm.length == 0 )
		badmsg = "Please enter your name.";
	var em = document.forms["contact"]["email"].value;
	if ( em.length == 0 ) {
		if ( badmsg.length > 0 )
			badmsg += "<br />";
		badmsg += "Please enter your email address.";
	}
	else if ( !em.match(ESLFlix.emailRE) ) {
		if ( badmsg.length > 0 )
			badmsg += "<br />";
		badmsg += "Please enter a valid email address.";
	}
	
	var elt = document.getElementById( "result_msg" );
	if ( elt ) {
		if ( badmsg.length > 0 ) {
			elt.innerHTML = "<p>" + badmsg + "</p>";
			return;
		}
		else {
			elt.innerHTML = "<p>Your request is being submitted - please wait a moment.</p>";
		}
	}
	
	// ask the server for details:
	// success callback:
	var handleSuccess = function( o ) {
	
		YAHOO.log( "ESLFlix.sendContactRequest success", "info" );
		var result = {};
		var form = document.forms["contact"];
		
		if ( o.responseText != undefined ) {
			YAHOO.log( "ESLFlix.sendContactRequest raw response: " + o.responseText, "debug" );
			result = eval( '(' + o.responseText + ')' );
		}
		var html = "";
		
		if ( result.success ) {
			html += "<p>Your request has been submitted - we will be in touch with you shortly!</p>";
			for ( var n = 0; n < ESLFlix.contactFromFields.length; n++ ) {
				var f = ESLFlix.contactFromFields[ n ];
				var inp = form[ f ];
				if ( inp ) {
					if ( inp.type == "text" || inp.type == "textarea" ) {
						inp.value = "";
					}
					else if ( inp.type.indexOf("select") != -1 ) {
						inp.selectedIndex = 0;
					}
				}
			}
		}
		else
			html += "<p>" + unescape(result.message).replace("\n","<br />") + "</p>";
		
		var elt = document.getElementById( "result_msg" );
		if ( elt ) {
			elt.innerHTML = html;
		}
		
		// enable the form fields:
		for ( var n = 0; n < ESLFlix.contactFromFields.length; n++ ) {
			var f = ESLFlix.contactFromFields[ n ];
			var inp = form[ f ];
			if ( inp )
				inp.disabled = false;
		}
	}
	
	// failure callback:
	var handleFailure = function( o ) {
		YAHOO.log( "ESLFlix.sendContactRequest failed", "warn" );
		
		var elt = document.getElementById( "result_msg" );
		if ( elt ) {
			elt.innerHTML = "<p>An error occurred while trying to send your request - please try again later.</p>";
		}
		
		// enable the form fields:
		var form = document.forms["contact"];
		for ( var n = 0; n < ESLFlix.contactFromFields.length; n++ ) {
			var f = ESLFlix.contactFromFields[ n ];
			form[ f ].disabled = false;
		}
	}
	
	var callback = { success:handleSuccess, failure:handleFailure };
	
	var vars = "list=requests&fmt=json";
	var form = document.forms["contact"];
	
	for ( var n = 0; n < ESLFlix.contactFromFields.length; n++ ) {
		var f = ESLFlix.contactFromFields[ n ];
		var inp = form[ f ];
		if ( inp ) {
			if ( inp.type == "text" || inp.type == "textarea" || inp.type == "hidden" ) {
				if ( inp.value ) {
					vars += "&" + f + "=" + escape( inp.value );
				}
			}
			else if ( inp.type.indexOf("select") != -1 ) {
				vars += "&" + f + "=" + escape( inp.options[inp.selectedIndex].value );	
			}
			inp.disabled = true;
		}
	}
	
	ESLFlix.currentRequest = YAHOO.util.Connect.asyncRequest( 'POST', ESLFlix.webpath + 'servlet/HandleForm', callback, vars );

}


ESLFlix.loadfunctions = new Array();

ESLFlix.addLoadFunction = function( f ) {
	ESLFlix.loadfunctions.push( f );
}

ESLFlix.onLoad = function() {
	for ( var n = 0; n < ESLFlix.loadfunctions.length; n++ )
		ESLFlix.loadfunctions[n]();
	
	if ( ESLFlix.preloadVideo )
		ESLFlix.loadVideo( ESLFlix.preloadVideo, false );
}

window.onload = ESLFlix.onLoad;
