$S = $$;
//
// Class: generic DOM functions.
// ****************************************************************************
//  author: Casey Flynn
//  usage:  var ntsDOM = new novatexDOM({color: "black"});
//
var novatexDOM = new Class({
	
	//
	// Function: Initalize Novatex DOM class
	// *************************************
	//
	initialize: function(options) {
		this.options = options; //save our opitons.
		
		// always handle external links
		this.externalLinks();
		
		// always handle list Hover
		this.listHover();
		
		// always fix any png files
		$$('img.fixPNG').each(function(png){fixPNG(png)});
		
	},
	
	//
	// Function: Generic externalLinks function.
	// ******************************
	// author: Casey Flynn
	// usage: novatexDOM::externalLinks();
	// notes: LEGACY.. Need to move this away from using rel
	//		  and use css selectors instead.
	externalLinks: function() {

		if (!document.getElementsByTagName) return;
		
		var anchors = document.getElementsByTagName("a");
		
		for (var i=0; i<anchors.length; i++) {
			var anchor = anchors[i];
			
			if (anchor.getAttribute("href")){
				rel = anchor.getAttribute("rel");
				
				if(rel) {
					s = rel.split(" ");
					for(var x=0; x<s.length;x++) {
						if(s[x] == "external") {
							anchor.target = "_blank";
							break;
						}
					}
				}
			}
		}
	},
		
	//
	// Function: fixPNG
	// ****************************************************
	//	this will make transparent pngs show up correctly in IE. This function 
	//	is based almost entirely on the function found here: 
	//	<http://homepage.ntlworld.com/bobosola/pnginfo.htm>
	//
	// Developer notes ::
	// Aaron Newton, <aaron [dot] newton [at] cnet [dot] com>
	//
	fixPNG: function (myImage) 
	{
		try {
			var arVersion = navigator.appVersion.split("MSIE");
			var version = parseFloat(arVersion[1]);
			if ((version >= 5.5) && (version < 7) && (document.body.filters)){
				myImage = $(myImage);
				var vis = myImage.visible();
				if(!vis) myImage.show();
				var width = $(myImage).offsetWidth;
				var height = $(myImage).offsetHeight;
				if(!vis) myImage.hide();
				var imgID = (myImage.id) ? "id='" + myImage.id + "' " : "";
				var imgClass = (myImage.className) ? "class='" + myImage.className + "' " : "";
				var imgTitle = (myImage.title) ? "title='" + myImage.title	+ "' " : "title='" + myImage.alt + "' ";
				var imgStyle = "display:inline-block;" + myImage.style.cssText;
				var strNewHTML = "<span " + imgID + imgClass + imgTitle
										+ " style=\"" + "width:" + width 
										+ "px; height:" + height 
										+ "px;" + imgStyle + ";"
										+ "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
										+ "(src=\'" + myImage.src + "\', sizingMethod='scale');\"></span>";
				myImage.outerHTML = strNewHTML;
			}
		} catch(e) {}
	},

	listHover:function(){
		$$('ul.hover').each(function(e){
			this.overAppend(e,'li');
		},this);
	},
	
	//
	// overAppend: Appends over on tags that don't support hover
	// *********************************************
	// author: casey flynn
	// usage: novatexDOM::handle_over('view_id','TAG');
	//
	overAppend:function (v, t) {
		this.list = $ES(t,v);
		this.list.each(function(e){
			e.addEvent('mouseover', function(event){e.addClass('over');});
            e.addEvent('mouseout', function(event){e.removeClass('over');});
		},this);
	},
	
	//
	// imgFadeSwitch
	// *************************************
	// author: casey flynn
	// usage: novatexDOM.imgFadeSwitch('view_id');
	// notes: view container must have an img tag and at least 1 anchor
	imgFadeSwitch: function(v) {
		this.v = v;
		$ES('a',v).each(function(a){
			a.onclick = function() {
				i = $E('img', this.v);
				i.effect('opacity',{duration: 1000,onComplete:
					function(){
						i.setProperty('src',a.getProperty('href'));
						$E('img', this.v).effect('opacity',{duration: 1000}).custom(0,1);
					}.bind(this)
				}).custom(1,0);
				return false;	
			}.bind(this);
		},this);
	}
});



var Swiff = {
	
	count: 0,
	
	callBacks: {},
	
	/*
	Function: Swiff.create
		creates a flash object with supplied parameters.

	Arguments:
		source - the swf path.
		properties - an object with key/value pairs. all options are optional. see below.
		where - the $(element) to inject the flash object.

	Properties:
		width - int, the width of the flash object. defaults to 0.
		height - int, the height of the flash object. defaults to 0.
		onLoad - string, sets a variable in the actionsctipt called onLoad. Used to fire a function when flash is loaded. defaults to none.
		id - string, the id of the flash object. defaults to 'Swiff-Object-num_of_object_inserted'.
		wmode - string, transparent or opaque.
		bgcolor - string, hex value for the movie background color.

	Returns:
		the object element, to be injected somewhere.
	*/
	
	create_old: function(source, properties, where){
		if (!Swiff.fixed) Swiff.fix();
		var props = Object.extend({
			width: 0, height: 0, onLoad: 'Class.empty', id: 'Swiff-Object-'+Swiff.count++, wmode: 'transparent', bgcolor: '#ffffff'
		}, properties || {});
		var load = source + '?onLoad=' + props.onLoad;
		var html =
			'<object width="'+props.width+'" height="'+props.height+'" id="'+props.id+'" type="application/x-shockwave-flash" data="'+load+'">'+
				'<param name="allowScriptAccess" value="sameDomain" />'+'<param name="movie" value="'+load+'" />'+
				'<param name="bgcolor" value="'+props.bgcolor+'" />'+'<param name="scale" value="noscale" />'+
				'<param name="salign" value="lt" />'+'<param name="wmode" value="'+props.wmode+'">'+
			'</object>';
		
		var element = new Element('div').setHTML(html).getElementsByTagName('object')[0];
		if (where) $(where).appendChild(element);
		return element;
	},
	
	create: function(source, properties, where){
		if (!Swiff.fixed) Swiff.fix();
		var props = Object.extend({
width: 0, height: 0, onLoad: 'Class.empty', id: 'Swiff-Object-'+Swiff.count++, wmode: 'transparent', bgcolor: '#ffffff', flashVars: ''
		}, properties || {});
		var load = source + '?onLoad=' + props.onLoad;

		document.writeln('<object width="'+props.width+'" height="'+props.height+'" id="'+props.id+'" type="application/x-shockwave-flash" data="'+load+'" flashVars="'+props.flashVars+'">');
		document.writeln('<param name="allowScriptAccess" value="sameDomain" />'+'<param name="movie" value="'+load+'" />');
		document.writeln('<param name="bgcolor" value="'+props.bgcolor+'" />'+'<param name="scale" value="noscale" />');
		document.writeln('<param name="salign" value="lt" />'+'<param name="wmode" value="'+props.wmode+'">');
		document.writeln('<param name="flashVars" value="'+props.flashVars+'" />');
		document.writeln('</object>');
	},
	
	//from swfObject, fixes bugs in ie+fp9
	fix: function(){
		
		Swiff.fixed = true;
		
		window.addEvent('beforeunload', function(){
			__flash_unloadHandler = Class.empty;
			__flash_savedUnloadHandler = Class.empty;
		});

		window.addEvent('unload', function(){
			if (!window.ie) return;
			var objects = $each(document.getElementsByTagName("object"), function(swf){
				swf.style.display = 'none';
				for (var p in swf){
					if (typeof swf[p] == 'function') swf[p] = Class.empty;
				}
			});
		});
	},
	
	/*
	Function: Swiff.getVersion
		gets the major version of the flash player installed.

	Returns: 
		a number representing the flash version installed, or 0 if no player is installed.
	*/
	
	getVersion: function(){
		if(navigator.plugins && navigator.mimeTypes.length){
			var x = navigator.plugins["Shockwave Flash"];
			if(x && x.description) var version = x.description;
		} else if (window.ActiveXObject){
			try {
				var x = new ActiveXObject("ShockwaveFlash.ShockwaveFlash");
				var version = x.GetVariable("$version");
			} catch(e){}
		}
		if (version) return version.replace(/\w+\s+/gi, '').toInt();
		else return 0;
	},
	
	/*
	Function: Swiff.remote
		Calls an ActionScript function from javascript. Requires ExternalInterface.

	Returns: 
		Whatever the ActionScript Returns
	*/
	
	remote: function(object, name){
		var value = object.CallFunction("<invoke name=\"" + name + "\" returntype=\"javascript\">" + __flash__argumentsToXML(arguments, 2) + "</invoke>");
		return eval(value);
	}

};

/*
Script: Swiff.Uploader.js
	Contains <Swiff.Uploader>

Author:
	Valerio Proietti, <http://mad4milk.net>

License:
	MIT-style license.
*/

/*
Class: Swiff.Text
	creates flash textField objects. Takes care of object creation and injection.

Arguments:
	swf - the path to Swiff.Text.Swf
	where - where to inject the swf.
	text - the text to write to the textField object.
	options - options for the textField display.
*/

Swiff.TextField = new Class({
	
	setOptions: function(options){
		this.options = Object.extend({
			color: '#111111',
			size: '30',
			bold: false,
			italic: false,
			underline: false,
			spacing: false,
			shadow: false,
			font: false,
			hover: false,
			display: 'block'
		}, options || {});
	},
	
	setShadow: function(param){
		if ($type(param) != 'object') param = {};
		var shadow = Object.extend({
			distance: '2',
			angle: '45',
			color: '#000000',
			opacity: '0.5',
			blur: '2',
			strength: '1'
		}, param || {});
		
		return shadow;
	},
	
	initialize: function(swf, where, text, options){

		if (Swiff.getVersion() < 8) return false;
		
		this.where = $(where);
		
		this.instance = 'Swiff'+Swiff.count++;
		var cb = Swiff.callBacks[this.instance] = {};
		cb.loaded = this.loaded.bind(this);
		
		this.txt = text;
		this.setOptions(options);
		
		if (this.options.shadow) this.shadow = this.setShadow(this.options.shadow);
		if (this.options.hover.shadow) this.options.hover.shadow = this.setShadow(this.options.hover.shadow);
		
		this.color = this.options.color.replace('#', '0x');
		
		this.object = Swiff.Base(swf, {
			width: '100%',
			height: '1',
			id: this.instance,
			onLoad: 'Swiff.callBacks.'+this.instance+'.loaded'
		});
		this.where.appendChild(this.object);
	},
	
	loaded: function(){
		this.setStyles(this.options);
		this.setText(this.txt);
		this.render(this.shadow);
		
		if (this.options.hover){
			this.object.onmouseover = function(){
				this.setStyles(this.options.hover);
				this.render(this.options.hover.shadow);
			}.bind(this);
			
			this.object.onmouseout = function(){
				this.setStyles(this.options);
				this.render(this.shadow);
			}.bind(this);
		}
	},
	
	setStyles: function(obj){
		if (obj.color) obj.color = obj.color.replace('#', '0x');
		Swiff.remote(this.object, 'setStyles', obj.color, obj.size, obj.bold, obj.italic, obj.underline, obj.spacing, obj.font);
	},
	
	setText: function(text){
		Swiff.remote(this.object, 'setText', text);
	},
	
	render: function(shadow){
		if (shadow){
			Swiff.remote(this.object, 'makeShadow',
				shadow.distance, shadow.angle, shadow.color.replace('#', '0x'), shadow.opacity, shadow.blur, shadow.strength
			);
		}
		var size = Swiff.remote(this.object, 'render', this.instance, shadow);
		this._setSize.pass([size[0], size[1]], this).delay(10);
	},
	
	_setSize: function(width, height){
		if (this.shadow) height = height.toInt() + this.shadow.distance.toInt() + this.shadow.blur.toInt();
		this.object.height = height;
		if (this.options.display == 'inline') this.object.width = width;
	}
	
});

