function writeFlash(url,w,h,id,bg,trans) {
var html = "";
html += '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0" width="' + w + '" height="' + h + '" id="'+ id + '">\n';
html += '<param name="movie" value="' + url + '" />\n';
html += '<param name="quality" value="high" />\n';
html += '<param name="bgcolor" value="' + bg + '" />\n';
html += '<param name="scale" value="noscale" />\n';
if (trans == 1) {html += '<param name="wmode" value="transparent" />\n';}
html += '<!--[if !IE]> <-->\n';
html += '<object data="' + url + '" width="' + w + '" height="' + h + '" type="application/x-shockwave-flash" name="'+ id + '">\n';
html += '<param name="quality" value="high" />\n';
html += '<param name="scale" value="noscale" />\n';
html += '<param name="bgcolor" value="' + bg + '" />\n';
html += '<param name="pluginurl" value="http://www.macromedia.com/go/getflashplayer" />\n';
if (trans == 1) {html += '<param name="wmode" value="transparent" />\n';}
html += ' FAIL (the browser should render some flash content, not this).\n';
html += ' </object>\n';
html += '<!--> <![endif]-->\n';
html += '</object>\n';
return html;
}


function positionPreloader(elmObj) {
	elmObj.setStyles( { top: Math.round((window.getSize().y/2) - (elmObj.getSize().y/2)) + "px" });
}


function getElementsByClassName(classname, node) {
	if(!node) node = document.getElementsByTagName("body")[0];
	var a = [];
	var re = new RegExp('\\b' + classname + '\\b');
	var els = node.getElementsByTagName("*");
	for(var i=0,j=els.length; i<j; i++)
	if(re.test(els[i].className))a.push(els[i]);
	return a;
}


function assignOutboundShareLinks() {
	// check for outbound share links
	var shareLinks = $$("#sbox-content a.sociable-hovers");
	shareLinks.each(function(lsLink, index) {
		lsLink.target = "_blank";
		lsLink.href = lsLink.href + escape(location.href);
	});
	var shareLink = $$("#sbox-content a.sociable-hover");
	shareLink.each(function(lsLink, index) {
		lsLink.target = "_blank";
	});
}

function initializeVideo(type) {
	var videoId = "sbclone-video-obj";
	var videoHref;
	var divElements = $$("#sbox-content .overlay-"+type);
	Array.forEach(divElements, function(divElement) {
		divElement.id = videoId;
		videoHref = divElement.getElementsByTagName("a");
	});
	if (videoHref.length>0) {
		var video = new swfObjInit(videoId, { // leroy smith feature video
			videoId: videoId,
			videoUrl: videoHref[0]
		});
	}
}




function CheckHistory() {
	
	var hashData = location.hash;
	hashData = hashData.replace("#","");
	hashData = hashData.split("/");
	if (hashData.length<3) {
		SqueezeBox.close();
		window.doResize = true;
		if( window.delayResize == true ){
			window.delayResize == false;
			$(window).fireEvent('resize');
		}
	} else {

		if ((hashData[1]!=null)&&(hashData[1]!="")) {
			if (hashData[1]=="game") { // OPEN GAME
				window.location = "/game/index.html";
			} else if ((hashData[2]!=null)&&(hashData[2]!="")) {
				var elementObj = $(hashData[2]);
				if ((elementObj!=null)&&(typeof(elementObj)=="object")) {
					
					window.doResize = false;
					
					if (hashData[1]=="video") { // OPEN VIDEO OVERLAY
						SqueezeBox.open(elementObj, {
							handler: 'clone',
							size: {x: 780, y: 540}
						});
						setTimeout('assignOutboundShareLinks()', 250);
						setTimeout('initializeVideo("video")', 500);
					} else if (hashData[1]=="news") { // OPEN NEWS OVERLAY
						SqueezeBox.open(elementObj, {
							handler: 'clone',
							size: {x: 915, y: 540}
						});
						setTimeout('assignOutboundShareLinks()', 250);
						setTimeout('initializeVideo("news")', 500);
					} else if (hashData[1]=="drill") { // OPEN DRILL OVERLAY
						SqueezeBox.open(elementObj, {
							handler: 'clone',
							size: {x: 780, y: 540}
						});
						setTimeout('assignOutboundShareLinks()', 250);
						setTimeout('initializeVideo("video")', 500);
					} else if (hashData[1]=="wallpaper") { // OPEN WALLPAPER OVERLAY
						SqueezeBox.open(elementObj, {
							handler: 'clone',
							size: {x: 800, y: 575}
						});
					} else if (hashData[1]=="recipe") { // OPEN RECIPE OVERLAY
						SqueezeBox.open(elementObj, {
							handler: 'clone',
							size: {x: 850, y: 540}
						});
					} else if (hashData[1]=="music") { // OPEN MP3 OVERLAY
						SqueezeBox.open(elementObj, {
							handler: 'clone',
							size: {x: 420, y: 535},
							onOpen: function() {
								var insertTo = $("sbox-content").getElement(".overlay-music");
								insertTo.empty();
								var f = writeFlash("../musicplayer.swf",420,500,"flashMusic","",0);
								insertTo.innerHTML = f;
							}
						});
					} else {
						SqueezeBox.close();
						window.doResize = true;
						if( window.delayResize == true ){
							window.delayResize == false;
							$(window).fireEvent('resize');
						}
					}
				} else {
					SqueezeBox.close();
					window.doResize = true;
					if( window.delayResize == true ){
						window.delayResize == false;
						$(window).fireEvent('resize');
					}
				}
			}
		}
	}

};





// SWF Object classes
var ShowFeatureVideo = new Class({ // show the feature video for first time users
	initialize: function() {
		var myCookie;
		this.urlParam = location.hash;
		this.urlParam = this.urlParam.replace("#","");
		this.urlParam = this.urlParam.split("/");
		if (this.urlParam.length<3) {
			var previousCookie = Cookie.read('Jordan');
			if (previousCookie!="LeroySmith") {
				location.href="#/video/leroy-feature";
			}
		} else {
			CheckHistory();
		}
		myCookie = Cookie.write('Jordan', 'LeroySmith', {duration: 999, path: "/"});
	}
});




var swfObjInit = new Class({
	Implements: [Options],
	options: {
		videoId: null,
		videoWidth: 780,
		videoHeight: 463,
		videoUrl: null,
		videoFullscreen: true,
		videoScriptAccess: "always",
		videoVersion: 9,
		videoFlashVars: {},
		basePath: "."
	},
	initialize: function(source, options){
		// read in params
		this.source = source;
		this.setOptions(options);

		var so = new SWFObject(this.options.videoUrl, "leroy-"+this.options.videoId, this.options.videoWidth, this.options.videoHeight, this.options.videoVersion);
		so.addParam("base", this.options.basePath);
		so.addParam("wmode", "transparent");
		so.addParam("allowfullscreen", "true");
		for (var key in this.options.videoFlashVars) {
			so.addVariable(key, this.options.videoFlashVars[key]);
		}
		so.write(this.options.videoId);

	}
});

var ieRolloverSetup = new Class({
	initialize: function() {
		if(Browser.Engine.trident && Browser.Engine.version < 7 ){
			var r = $$(".rollover");
			r.getParent("a").addEvent("mouseenter",function(){
				$$(this).getElement(".rollover").addClass("rolloverOver");
			}).addEvent("mouseleave",function(){
				$$(this).getElement(".rollover").removeClass("rolloverOver");
			});
			r.addEvent("click",function(){
				var u = $$(this).getParent("a").getProperty("href") + "";
				if(u.contains('#'))
				{
					window.location.hash = $$(this).getParent("a").getProperty("href");
				}
				else
				{
					window.location.href = u;
				}
				eval($$(this).getParent("a").getProperty("onclick"));
			});
		}
	}	
});


// Shuffle class
var Shuffle = new Class({ 
	Implements: [Options],
	options: {
		items: '.module',
		version: '0.1',
		spacer: 10,
		preload: 'preloader'
	},
	initialize: function(options){
		this.setOptions(options);
		this.myModules = $$(this.options.items);
		this.currentIndex = 0;

		this.windowSize = window.getSize();
		if (this.windowSize.x < 960) this.windowSize.x = 960; // 1000px minimum width
		if( Browser.Platform.win && Browser.Engine.name == "webkit" ){
			this.windowSize.x = 960;
		}
		
		window.addEvent('resize', function(e) {
			if( Browser.Platform.win && Browser.Engine.name == "webkit" ){
				return false;
			}
			else if(window.doResize == true){
				this.windowSize = window.getSize();
				if (this.windowSize.x < 960) this.windowSize.x = 960; // 1000px minimum width
				this.displayModules();
			}
			else if( window.doResize == false ){ window.delayResize = true }
		}.bind(this));	

		this.displayModules();

	},
	displayModules: function(){
		this.matrix = new Array();
		this.x = this.options.spacer;
		this.y = this.options.spacer;
		this.smallElmHeight = 0;
		this.row = 1;

		this.myModules.each(function(myModule, index){

			// get the height of the smallest element in a row
			if(this.smallElmHeight > myModule.getSize().y || this.smallElmHeight == 0){
				this.smallElmHeight = myModule.getSize().y;
			}

			// adding another element to a row will cause a horizontal scroll
			if((this.x + myModule.getSize().x) > this.windowSize.x ){
				this.y += (this.smallElmHeight + this.options.spacer);
				this.x = this.options.spacer;

				this.row++;
				this.smallElmHeight = 0;
			}

			myModule.setStyles({
				top: this.y,
				left: this.x,
				visibility: 'visible'
			});

			this.matrix[index] = { coordinate: { x: this.x, y: this.y }, size : myModule.getSize(), row: this.row };

			this.x += (this.matrix[index].size.x + this.options.spacer);

			while(!this.hitTest(index)){
				this.matrix[index].coordinate.y += this.options.spacer;
				myModule.setStyle('top', this.matrix[index].coordinate.y);
			}
		}.bind(this));


		this.myModules.each(function(myModule, index){
			myModule.setStyles({
				visibility: 'visible'
			});
		}.bind(this));

		var preloaderObj = $("preloader");
		preloaderObj.setStyles( {
			visibility: 'hidden'
		});
		var navObj = $("menu");
		navObj.setStyles( {
			visibility: 'visible'
		});
	},
	hitTest: function(myIndex){
		var curElmInfo = this.matrix[myIndex];
		var curElemWidth = this.matrix[myIndex].size.x + this.matrix[myIndex].coordinate.x;
		var curElemHeight = this.matrix[myIndex].size.y + this.matrix[myIndex].coordinate.y;

		for(i = 0, len = this.matrix.length; i < len; i++){

			var matrixItem = this.matrix[i];

			//not the element we are checking
			if(myIndex != i){
				var elmTotaly = matrixItem.coordinate.y + matrixItem.size.y;
				var elmTotalx = matrixItem.coordinate.x + matrixItem.size.x;

				// check y
				if((elmTotaly > curElmInfo.coordinate.y)  && curElemHeight > matrixItem.coordinate.y){

					// check x
					if((elmTotalx > curElmInfo.coordinate.x) && curElemWidth > matrixItem.coordinate.x){
						return false;
					}

				}
				
			}
		}
		return true;
	}
});