function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function becomeAFan(what) {
	switch(what) {
		case 'show':
			var xy = $('facebook_button').getCoordinates();
			$('facebook_fanbox').setStyles({
				'display': 'block',
				'z-index': 1000,
				'left': xy['left']
			});
			break;
		case 'hide':
			$('facebook_fanbox').setStyles({
				'display': 'none'
			});
			break;
	}
}

function getCategory(ids, limit, el, docs) {
	$('vidList').innerHTML = "<p>Loading…</p>";
	var vids = 'cat='+ids;
	if(docs == 1) { vids = 'docs='+ids; }
	var getCategory = new Request({
			url: '/get-category',
			method: 'get',
			onSuccess: function(response, x) {
				$('vidList').innerHTML = response;
				var js = $$('#vidList .vidListBox h1 a')[0].get('href');
				$('vidDescript').innerHTML = '';
				var h1 = $$('#vidList .vidListBox h1')[0].clone();
				var p = $$('#vidList .vidListBox p')[0].clone();
				h1.inject($('vidDescript'));
				p.inject($('vidDescript'));
				$$('#vidDescript h1 a')[0].dispose();
				$('vidHdCat').innerHTML = $$('#vidCats li a')[el].get('text').substr(2);
				eval(js);
			}
		}).send(vids+'&limit='+limit);
}

function getVideo(id) {
	if($('player_outer')) {
		//alert(typeof($('player')));
		var fade = new Fx.Tween($('player_outer'), {
			onComplete: function() {
				var myRequest = new Request({
					url: '/get-video',
					method: 'get',
					onSuccess: function(response, x) {
						var data = response.split(";");
						
						var size = $('vidBox') ? $('vidBox').getSize() : $('vidFeature').getSize();
						var h = size.y;
						var w = size.x;
						//var h = $('vidFeature') ? 288 : 235;
						//var w = $('vidFeature') ? 512: 400;
						
						// SET URI
						var uri = new URI();
						uri.set('fragment', 'vid='+id);
						uri.go();
						
						$('player_outer').set('html', '<video id="player" width="'+w+'" height="'+h+'"></video>');
						
						jwplayer("player").setup({
					        flashplayer: "/assets/flash/html5_player/player.swf",
					        width: w,
					        height: h,
					        levels: [
					            { file: data[0] },
					            { file: data[0]+'.mp4' }
					        ],
					        image: data[1],
					        dock: true,
					        skin: '/assets/flash/html5_player/skin/glow.zip',
					        stretching: 'fill',
					        plugins: {
					        	"sharing-3": {
					                  link: "http://www.backushospital.org/multimedia#vid="+id
					             },
					            "gapro-2": { accountid: "UA-3315259-1", "trackstarts": true, "trackpercentage": true, "trackseconds": true, "idstring": "Video: " + data[2] }
					        }
					    });
						
						/*
						var so = new SWFObject("/assets/flash/player.swf","mpl",w,h,"8");
						so.addParam("allowfullscreen","true");
						so.addVariable("width", w);
						so.addVariable("height", h);
						so.addVariable("file", data[0]);
						so.addVariable("image", data[1]);
						so.addVariable('stretching','fill');
						so.addParam("allowscriptaccess", "always");
						so.addVariable("plugins", "gapro-1"); 
						so.addVariable("gapro.accountid","UA-3315259-1");
						so.addVariable("gapro.trackstarts","true");
						so.addVariable("gapro.trackpercentage","true");
						so.addVariable("gapro.tracktime","true");
						so.addVariable("gapro.idstring", "Video: " + data[2]);
						so.addVariable("provider", "HTTP");
        				so.addVariable("http.startparam", "10");
						so.write("player");
						*/
						
						var scroll = new Fx.Scroll(window, {
						    offset: {
						        'x': 0,
						        'y': -100
						    }
						}).toElement($('vidBox'));
						
						// Insert the vid description
						if($('vidDescript')) {
							$('vidDescript').innerHTML = '';
							var h1 = new Element('h1', {'html': data[2]});
							var p = new Element('p', {'html': data[3]});
							//alert(h1);
							h1.inject($('vidDescript'));
							p.inject($('vidDescript'));
						}
						
						var fade = new Fx.Tween($('player_outer')).start('opacity', 0, 1);
						init_hoverThumb();
					}
				}).send('vid='+id);
			}
		}).start('opacity', 1, 0);
	
	} else {
		var url = '';
		var scripts = $$('head script');
		for(i=0; i < scripts.length; i++) {
			var src = scripts[i].getProperty('src');
			var js = /core\.js(\?.*)?$/;
			var jsmatch = src.match(js);
			if(jsmatch != null && $defined(jsmatch) && $defined(jsmatch[1]) && jsmatch[1] != 0) {
				url = jsmatch[1];
				break;
			}
		}
		
		url = url.split("=")[1];
		window.location = "/" + url + "?vid=" + id;
	}
	
}

function init_hoverThumb() {
	$$('.hoverthumb').each(function(el) {
		el.addEvents({
			'mouseover': function() {
				this.setStyle('opacity', 0.7);
			},
			'mouseleave': function() {
				this.setStyle('opacity', 1);
			}
		});
	});
}

function play_podcast() {
	document.getElementById("mp3player").SetVariable("player:jsPlay", "");
}

window.addEvent('domready', function() {
	
	//if($('facebook_button')) {
	//	$('facebook_button').addEvents({
	//		'mouseenter': function() {
	//			//becomeAFan('show');
	//		}
	//	});
	//}
	
	// Add hover opacity events to all thumbnails
	init_hoverThumb();
	
	$$('#play_podcast').each(function(el) {
		el.addEvent('click', function() {
			play_podcast();
		});
	});
	
	if($('textlist') && $$('a.smooth').length > 0) {
		$$('a.smooth').each(function(el) {
			el.addEvent('click', function(e) {
				var e = new Event(e).stop;
				var anch = this.getProperty('href');
				var scroll = new Fx.Scroll('textlist').toElement(anch.substring(1, anch.length));
			});
		});
	}
	
	new SmoothScroll({duration: 1000}, window);
	
});
