/* ********************************************************************************************************************
**
** 	Name: 			Soda Trailers							
** 	 																						
** 	Version: 		0.1																			
** 	Author: 		Ben Sekulowicz
**
******************************************************************************************************************** */

var sodaTrailers = Class.create();

sodaTrailers.prototype = {
	
   	initialize: function() {
		// Format the URL and update the location, (javascript version)
		if (this.getIdFromUrl(document.location.href).startsWith("trailer-")) {
			document.location.href = "#/" + this.getIdFromUrl(document.location.href).gsub("trailer-", "") + "/";
		}
					
		// On page load
		Event.observe(window, "load", this.onLoadWindow.bindAsEventListener(this));
	},
	
	/* *********************************************************************************************************************
	
	 EVENTS: ON LOAD
	
	********************************************************************************************************************* */
	
	onLoadWindow: function(e) {	
		// Assign variables
		var count = 0;
			
		// Find each trailer within the page
		$$("ol#listDisplayTrailers > li").each(function(li) {			
			// Add the closed class name
			li.addClassName("jsClosed");
			
			// Attach onClick event to each header
			li.getElementsBySelector("h2").each(function(h2) {
				Event.observe(h2, "click", this.onClickHeading.bindAsEventListener(this));	
			}.bind(this));
			
			// Hide all 'extra' sections
			li.getElementsBySelector("div.expander").each(function(div) {
				div.style.display = "none";
			}.bind(this));			
			
			// If the URL returns true to this ID, expand it ...
			if (this.checkUrl(li.getAttribute("id"))) { 
				this.expand(li); 
				count ++;
			}								
		}.bind(this));
		
		// Find each trailer link
		$$("ul#listTrailers li p.linkTrailer a").each(function(a) {
			// Attach onClick event to each header
			Event.observe(a, "click", this.onClickLinks.bindAsEventListener(this));
			
			a.onclick = function() { return false; }
		}.bind(this));
		
		// Check for expanded variables
		if ((this.getIdFromUrl(document.location.href) != "") && (count == 0)) {
			alert("The trailer you are looking for could not be found or does not exist!");
		}
	},
	
	/* *********************************************************************************************************************
	
	 EVENTS: ON CLICK
	
	********************************************************************************************************************* */
	
	onClickHeading: function(e) {
		// Expand the item in the header
		this.expand(Event.findElement(e, "h2").parentNode);
	},
	
	onClickLinks: function(e) {
		// Get the URL of the item
		var url = Event.findElement(e, "a").getAttribute("href");
		
		// if the URL matche sour schema and the trailer exists
		if ($(this.getIdFromUrl(url))) { this.expand($(this.getIdFromUrl(url))); }		
	},
	
	/* ****************************************************************************************************************** */
	
	expand: function(li) {
		// Cycle through child DIVs
		li.getElementsBySelector("div.expander").each(function(div) {		
			
			// Execute the Scriptaculous effect
			Effect.toggle(div, "blind", { duration: 0.75,			
				
				// Hide the video
				beforeStart: function(o) {
					// Hide the video block
					div.getElementsBySelector("div.video").each(function(video) { video.hide(); }); 
				},
				
				// Show the video
				afterFinish: function(o) {
					// Toggle the class names
					li.toggleClassName("jsClosed");
						
					// Reveal the video block
					div.getElementsBySelector("div.video").each(function(video) { video.show(); }); 

					// Update the URL
					document.location.href = "#/" + li.id.gsub("trailer-", "") + "/";
				}
	 		});
		}.bind(this));
	},
	
	/* ****************************************************************************************************************** */
	
	checkUrl: function(id) {		
		// If the url matches the item ID passed in
		if (this.getIdFromUrl(document.location.href) == "/" + id.gsub("trailer-", "") + "/") {			
			return true;	
		}
		
		// Return false if there is a problem
		return false;
	},
	
	/* ****************************************************************************************************************** */
	
	getIdFromUrl: function(url) {
		if (url.split("#").length == 2) {
			return url.split("#")[1];
		} else {
			return "";
		}
	}
	
	/* ****************************************************************************************************************
	
	FIN
	
	**************************************************************************************************************** */
}