/*
|----------------------------------------------------------------------------------------------------------------------------------
| Greasemonky MTV Fullscreen script
|----------------------------------------------------------------------------------------------------------------------------------
|
|	Converts all video links on http://www.mtvmusic.com/ so they play in a full(ish) screen window.
|
|  2008-10-30
|  Author Nick Taylor 2008 - nick@tangerineworks.com
|
|  Released under the GPL license
|  http://www.gnu.org/copyleft/gpl.html
|
|  --------------------------------------------------------------------
|
|  This is a Greasemonkey user script.
|
|  To install, you need Greasemonkey: http://greasemonkey.mozdev.org/
|  Then restart Firefox and revisit this script.
|  Under Tools, there will be a new menu item to "Install User Script".
|  Accept the default configuration and install.
|
|  To uninstall, go to Tools/Manage User Scripts,
|  select "MTV Full Screen", and click Uninstall.
|
|
*/

// set up the auto-loader bit
//
// ==UserScript==
// @name          MTV Full Screen
// @namespace     http://tangerineworks.com/
// @description   Pops up MTV videos in full screen window
// @include       http://www.mtvmusic.com/*
// ==/UserScript==




loadjQuery();										// generic jquery loader
run_when_ready('popupVideo()');		// popupvideo() is the function that will be run when jquery is ready



/*
|----------------------------------------------------------------------------------------------------------------------------------
| loadjQuery
|----------------------------------------------------------------------------------------------------------------------------------
|
|	creates a link in the <head> bit to the latest jquery script from :
|	http://jquery.com/src/jquery-latest.js
|
*/

function loadjQuery(){
	var jqueryLink = document.createElement('script');
	jqueryLink.src = 'http://jquery.com/src/jquery-latest.js';
	jqueryLink.type = 'text/javascript';
	document.getElementsByTagName('head')[0].appendChild(jqueryLink);
}


/*
|----------------------------------------------------------------------------------------------------------------------------------
| run_when_ready
|----------------------------------------------------------------------------------------------------------------------------------
|
|	The normal $function onload thing doesn't work here, so we loop until jquery has loaded, then 
|   run a function who's name is passed in as a parameter
|	
|
*/
function run_when_ready(functionToRun) {

	if(typeof unsafeWindow.jQuery == 'undefined') { 
		window.setTimeout(run_when_ready(functionToRun),100); 
	}else { 
		$ = unsafeWindow.jQuery; 
		eval(functionToRun);
	}
}



/*
|----------------------------------------------------------------------------------------------------------------------------------
| popupVideo
|----------------------------------------------------------------------------------------------------------------------------------
|
|	A simple onclick->popup a new window script - with id passed to the url
|	
|
*/
function popupVideo() {

	$('.videoTitle').click(function(){

		var link=$(this).attr('href');	// get the id. There's probably a better way of doing this.
		var temp1=link.split('id=');
		var id=temp1[1].split('&');

		if (id[0]>0) {
			popup(id[0])
			return false;
		}
	})
 }


/*
|----------------------------------------------------------------------------------------------------------------------------------
| popup
|----------------------------------------------------------------------------------------------------------------------------------
|
|	A simple popup script. 
|
*/
function popup(id) {

	params  = 'width='+screen.width;
	params += ', height='+screen.height;
	params += ', top=0, left=0'
	params += ', fullscreen=yes';

	newwin=window.open('http://www.weirdsky.com/mtv.php?id=' + id, "mtv" + id, params);
	if (window.focus) {newwin.focus()}
	return false;
}