/*
* Contains common javascript extensions
* Prerequisites:
*    jquery.js
*
* @date 30/12/2008
* @author Andrew Lowndes
*/

//Record the page view for Google Analytics
$(document).ready(function() {
	try {
		var pageTracker = _gat._getTracker("UA-10825451-1");
		pageTracker._trackPageview();
	} catch(err) {}
});

//Extend jQuery functionality
$.fn.wait = function(time, type) {
	time = time || 1000;
	type = type || "fx";
	return this.queue(type, function() {
		var self = this;
		setTimeout(function() {
			$(self).dequeue();
		}, time);
	});
};

//Functions
$("a").ready(function() {
	$("a.blank_window").click(function(i) {
		window.open($(this).attr("href"),"_blank");
		return false;
	});
});

$(".start_hidden").ready(function() {
	$(".start_hidden").hide();
});

//Enhance IE6 to include .pngs
if ($.browser.msie && $.browser.version == "6.0") {
	$(document).ready(function() {
		$("body *").each(function(i){
			var bgIMG = $(this).css('background-image');
			
			if(bgIMG.indexOf(".png")!=-1){
				var iebg = bgIMG.split('url("')[1].split('")')[0];
				$(this).css('background-image', 'none');
				$(this).get(0).runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + iebg + "',sizingMethod='scale')";
			}
		});
	});
}

//enhance applets by allowing them to be launched
$(".jogl_applet").ready(function() {
	$(".jogl_applet").each(function(i) {
		var appletContent = $(this).find("noscript").html();
		var appletContainer = $(this);
		
		$(this).html("<a href=\"#\" class=\"applet_launcher\">Play</a>");
		
		$(this).find(".applet_launcher").click(function(i) {
			appletContainer.html(appletContent);
			return false;
		});
	});
});

//make page sections into tab-like pages
$(".page").ready(function() {
	var pages = $(document).find(".page");
	pages.hide();
	pages.filter(":first").show();
	
	$(".tabbed_menu").ready(function() {
		var bookmarks = $(".tabbed_menu").find("a");
		
		bookmarks.click(function(event) {
			pages.hide();
			$($(this).attr("href") +"_page").show();
			return false;
		});
	});
});


//Provide nice anims for source code
$(".source_container").ready(function() {
	$(".source_container").each(function(i) {
		var source_code = $(this).find(".source_code");
		var copy_button = $(this).find(".copy_button");
		var print_button = $(this).find(".print_button");
		
		copy_button.click(function() {
			copyToClipboard(source_code.text());
			return false;
		});
		
		//Note: doesnt work on Google Chrome for unknown reason
		print_button.click(function() {
			var holder = source_code.clone();
			var print_window = window.open();
			var print_document = print_window.document;
			print_window.opener = null;
			print_document.write("<html><head><title>Print Source Code</title>");
			print_document.write("<link rel=\"stylesheet\" href=\"/project.css\" type=\"text/css\" media=\"all\" />");
			print_document.write("</head><body><div class=\"source_code print_source_full_screen\">");
			print_document.write(holder.html());
			print_document.write("</div></body></html>");
			print_document.close();
			print_window.print();
			print_window.close();
			return false;
		});
	});
});

/**
* Copy some text onto the clipboard
* NOTE: only works with IE and FF
* param string to copy
*/
function copyToClipboard(copyStr){
	try {
		window.clipboardData.setData("Text",copyStr); //IE
		alert("Text successfully copied to clipboard!");
	} catch(e){
		try{
			var securityEnabled = true;
			
			if ((netscape) && (netscape.security) && (netscape.security.PrivilegeManager)) { //FF
				try {
					netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
				} catch(e) {
					alert("Your browser is not configured to use this feature!\n\nTo configure on Firefox:\n1. Go into 'about:config'  by typing it in the location bar\n2. Look for the config value called 'signed.applets.codebase_principal_support' \n3. Doubleclick this value to set it from \"false\" to \"true\"");
					securityEnabled = false;
				}
			}
		
			if (securityEnabled) { 
				var clipboard,transferer,supportString,clipboardInterface;
				clipboard=Components.classes["@mozilla.org/widget/clipboard;1"].createInstance(Components.interfaces.nsIClipboard);
				transferer=Components.classes["@mozilla.org/widget/transferable;1"].createInstance(Components.interfaces.nsITransferable);
				clipboardInterface=Components.interfaces.nsIClipboard;
			
				transferer.addDataFlavor("text/unicode");
				supportString=Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
				supportString.data=copyStr;
				transferer.setTransferData("text/unicode",supportString,copyStr.length*2);
				
				clipboard.setData(transferer,null,clipboardInterface.kGlobalClipboard);
				alert("Text successfully copied to clipboard!");
			}
		}catch(e){
			alert("Your browser does not support this feature!");
		}
	}
}
