/*
	Init.Screen.js
	Copyright 2010 Avalanche Creative
	
	@job:	FIA Sports Management
	@loc:	scripts/
	@desc:	Initialiser script called in document header
			[Screen Media]
*/

//	jQuery scrollTo method. Credit goes to Aaron Schmidt.
//	*	URL: http://www.learningjquery.com/2007/09/animated-scrolling-with-jquery-12/comment-page-1#comment-13214
jQuery.fn.extend({
	scrollTo : function(speed, easing){
		return this.each(function(){
			var targetOffset = $(this).offset().top;
			$('html,body').animate({scrollTop: targetOffset}, speed, easing);
		});
	}
});



var FIA = {
	
	//	Fixes and cleans tag attributes and styles added for script-disabled browsers.
	clean: function(){
		//	Remove .noScript classes from all tags.
		$(".noScript").removeClass("noScript");
	},
	
	//	Adds a target="_blank" attribute to all anchor tags found with an attribute with rel="external" at runtime.
	addExternalLinks: function(){
		var links;
		
		//	Prototype Framework
		if(typeof(Prototype) != "undefined"){
			links	= $$('a[rel="external"]');
			links.each(function(link){
				if(link.readAttribute("href") != "" && link.readAttribute("href") != "#")
					link.writeAttribute("target", "_blank");
			});
		}
		
		//	jQuery Framework
		else if(typeof(jQuery) != "undefined"){
			$("a[rel='external']").attr("target", "_blank");
			$("a.rsswidget").attr("target", "_blank");
		}
	},
	
	
	placeholderText: function(remtitle){
		
		//	Prototype Method
		if(typeof(Prototype) != "undefined"){
			
			//	Register form listeners to empty text fields still holding default text upon submission.
			$$("form").each(function(form){
				
				//	Triggered when form's being submitted.
				form.observe("submit", function(e){
					var target	= e.target;
					var value	= "";
					var dtext	= "";
					form.getElements().each(function(f){
						value	= f.getValue();
						dtext	= f.readAttribute("dtext");
						if(f.hasClassName("prevtxt") && value == dtext)
							f.setValue("");
					});
					
				});
			});
			
			var fields	=	$$(".prevtxt");
			var title	=	"";
			fields.each(function(link){
				title	=	link.readAttribute("title");
				//	Determine whether to add placeholder text/listeners based on Title attribute
				if(title != ""){
					link.setValue(title);
					link.writeAttribute("dtext", title);
					link.addClassName("inactive");
					
					//	Triggered when element gains focus
					link.observe("focus", function(e){
						var target = e.target;
						
						// If current value matches placeholder text, clear field
						if(target.readAttribute("dtext") == target.getValue()){
							target.removeClassName("inactive");
							target.setValue("");
						}
					});
					
					//	Triggered when element loses focus
					link.observe("blur", function(e){
						var target	= e.target;
						var dtext	= target.readAttribute("dtext");
						var value	= target.getValue();
						//	If value was empty and/or left at placeholder value, restore styles
						if(dtext == value || !value){
							target.addClassName("inactive");
							target.setValue(dtext);
						}
					});
					
					//	If remtitle is TRUE, remove the Title attribute from source markup
					if(remtitle)
						link.writeAttribute("title", null);
				}
			});
		}
		
		
		
		
		//	jQuery Method
		else if(typeof(jQuery) != "undefined"){
			
			//	Clear the values from any fields that're left at their placeholder values before submitting.
			$("form").submit(function(){
				$(this).find("input").each(function(index, val){
					var text	=	$(val).data("text");
					if($(val).val() == text)
						$(val).val("");
				});
			});
			
			
			$("input").each(function(index, val){
				var obj		=	$(val);
				var title	=	obj.attr("title");
				var value	=	obj.val();
				
				
				//	Check if a Title attribute is present, and the initial value
				//	is currently either blank or matching the given title.
				if(title && (!value || value == title)){
					
					obj.data("text", title);
					obj.val(title);
					obj.addClass("inactive");
					
					//	Triggered when field recieves Input focus
					obj.focusin(function(){
						var obj		=	$(this);
						var temp	=	obj.data("text");
						var value	=	obj.val();
						if(value == temp){
							obj.removeClass("inactive");
							obj.val("");
						}
					});
					
					//	Triggered when the field loses input focus
					obj.focusout(function(){
						var obj		=	$(this);
						var temp	=	obj.data("text");
						var value	=	obj.val();
						if(value == temp || !value){
							obj.addClass("inactive");
							obj.val(temp);
						}
					});
				}
			});
		}
	},
	
	
	//	Bootstrap: Called from DOM's onLoaded handler.
	init: function(){
		FIA.clean();
		FIA.addExternalLinks();
		FIA.placeholderText();
		
		//	jQuery Scroll Pane
		if(typeof($.jScrollPane) != "undefined"){
			$(".scrollpane").jScrollPane({
				showArrows:		true, 
				scrollbarWidth:	12
			});
		}
		
		//	Setup the Image Rotator
		if(typeof($.fn.cycle) != "undefined")
			$("#slideshow ul").cycle({
				fx: "fade"
			});
		
		//	Lightboxes
		if(typeof($.prettyPhoto) != "undefined")
			$("a[rel^='prettyPhoto']").prettyPhoto({theme:"dark_rounded", hideflash:true});
	}
}





//	FIA Player Picker
//	*	So big, it's probably gonna warrant it's own namespace.
var PP	=	{
	
	pp:			null,
	width:		731,
	height:		390,
	base:		"http://fiaustralia.com/wp-content/themes/fia/media/",
	swf:		"picker.swf",
	noflash:	"noflash.png",
	
	getFlashPicker:	function(){
		var	out	=	"\n";
			out	+=	"<object id=\"pp_flash\" type=\"application/x-shockwave-flash\" data=\"" + (this.base + this.swf) + "\" width=\"" + this.width + "\" height=\"" + this.height + "\">\n";
			out	+=	"	<param name=\"allowScriptAccess\" value=\"always\" />\n";
			out	+=	"	<param name=\"movie\" value=\"" + (this.base + this.swf) + "\" />\n";
			out	+=	"	\n";
			out	+=	"	<img src=\"" + (this.base + this.noflash) + "\" alt=\"This content requires Flash\" usemap=\"#getFlash\" />\n";
			out	+=	"	<map name=\"getFlash\" id=\"getFlash\">\n";
			out	+=	"		<area shape=\"rect\" coords=\"200,205,530,318\" href=\"http://get.adobe.com/flashplayer/\" alt=\"Click to Download Adobe Flash Player\" />\n";
			out	+=	"	</map>\n";
			out	+=	"</object>\n";
		return out;
	},
	
	
	//	Profile Window
	profileStart:	"http://fiaustralia.com/profiles/",
	profileBase:	"http://fiaustralia.com/",
	
	getProfileWindow: function(start){
		var start	=	(typeof(start) != "undefined") ? start : this.profileStart;
		var	out		=	"\n";
			out		+=	"<div id=\"pp_charts\">\n";
			out		+=	"	<div id=\"pp_loading\"></div>\n";
			out		+=	"	<object id=\"pp_profile\" border=\"0\" type=\"text/html\" data=\"" + start + "\" width=\"733\" height=\"309\"></object>\n";
			out		+=	"</div>\n";
		return out;
	},
		setPlayer:	function(value){
			var target	=	PP.profileBase + value + "?i=1";
			
			//	Since IE doesn't appear to reload an object tag once the "data" attribute's been changed, a more brute force approach is needed.
			//	*	Thanks to Ghodmode for keeping me from pulling my hair out.
			//		URL:	http://www.dynamicdrive.com/forums/showthread.php?t=47346#post205496
			if($.browser.msie || $.browser.webkit)
				PP.reincarnate(target);
			
			//	Otherwise, simply change the data property of the embedded chart to load the new player stats.
			else	$("#pp_profile").attr("data", target);
		},
		
		reincarnate:	function(value){
			$("#pp_profile").remove();
			$("#pp_charts").append("<object id=\"pp_profile\" border=\"0\" type=\"text/html\" data=\"" + value + "\" width=\"733\" height=\"309\"></object>");
			/* Yeah, I know this code's starting get illogical. But that's what hotfixes look like, so bite me. */
		},
	
	
	//	Initializer Method
	init:	function(){
		this.pp	=	$("#pp");
		this.pp.removeClass("noScript");
		this.pp.html("");
		this.pp.html(this.getFlashPicker());
		$(this.getProfileWindow()).insertAfter(this.pp);
	}
}


//	PP class's setPlayer method aggregated from Global Scope for more reliable access.
//	*	Hey, I don't know what AS2's limitations are.
function setPlayer(name){
	PP.setPlayer(name);
}


//	Triggered when DOM's finished loading
jQuery(document).ready(function(){
	FIA.init();
	PP.init();
});