var scaleSWF = {
	percent : .8,
	flashes : new Array(),
	aspectRatio : 0,
	
	getWindowSize : function() {
		var w = 0;
		var h = 0;

		if(!window.innerWidth) { //IE
			if(!(document.documentElement.clientWidth == 0)) { //strict mode
				w = document.documentElement.clientWidth;
				h = document.documentElement.clientHeight;
			} else { //quirks mode
				w = document.body.clientWidth;
				h = document.body.clientHeight;
			}
		} else { //w3c
			w = window.innerWidth;
			h = window.innerHeight;
		}
		return {width:w,height:h};
	},
	
	calcSWFwidth : function() {
		if (this.percent > 1) {
			alert('Has to be a decimal value of percent (ie. .1 = 10%, .5 = 50%)');
			return;
		}
		var screen = this.getWindowSize();
		return Math.floor(Number(screen.width) * this.percent);
	},
	
	calcSWFheight : function() {
		if (this.percent > 1) {
			alert('Has to be a decimal value of percent (ie. .1 = 10%, .5 = 50%)');
			return;
		}
		var screen = this.getWindowSize();
		return Math.floor(Number(screen.height) * this.percent);
	},
	
	calcSWFratioToWidth : function(ratio) {
		var width = this.calcSWFwidth();
		return Math.floor(Number(width) * ratio);
	},
	
	calcSWFratioToHeight : function(ratio) {
		var height = this.calcSWFheight();
		return Math.floor(Number(height) * ratio);
	},
	
	getRatio : function(h, w) {
		if (w != 0 && h != 0) {
			if (Number(w) > Number(h)) { // Scale to width //
				var ratio = (h/w); type = 'width_scale';
			} else { // Scale to height //
				var ratio = (w/h); type = 'height_scale';
			}
			if (type == 'width_scale') {
				wh = this.calcSWFwidth();
				ht = this.calcSWFratioToWidth(ratio);
			} else {
				wh = this.calcSWFratioToHeight(ratio);
				ht = this.calcSWFheight();
			} 
			
			// Make sure the height will still fit in the screen.. If not we will resize it again using the height as the basis
			if (Number(ht) > Number(this.getWindowSize().height)) {
				var ratio = (w/h);
				wh = this.calcSWFratioToHeight(ratio);
				ht = this.calcSWFheight();
			}
			return {width:wh,height:ht};
		}
	},
	
	makeRELattribute : function(obj) {
		var tmp = $(obj).attr('rel').split(';');
		var swfHeight = 0; var swfWidth = 0;
		var str = 'prettyPhoto;';
		for (i=0;i<tmp.length;i++) {
			if (tmp[i].indexOf('swfWidth') >= 0) {
				swfWidth = tmp[i].substring(tmp[i].indexOf('swfWidth') + 9, tmp[i].length)
				str += 'swfWidth:' + swfWidth + ';';
			}
			if (tmp[i].indexOf('swfHeight') >= 0) {
				swfHeight = tmp[i].substring(tmp[i].indexOf('swfHeight') + 10, tmp[i].length)
				str += 'swfHeight:' + swfHeight + ';';
			}
			
			var aspect = this.getRatio(swfHeight,swfWidth);

			if (tmp[i].indexOf('width') >= 0) {
				str += 'width:' + aspect.width + ';';
			}
			if (tmp[i].indexOf('height') >= 0) {
				str += 'height:' + aspect.height + ';';
			}
			
			if (tmp[i].indexOf('flashvars') >= 0) {
				str += 'flashvars:' + tmp[i].substring(tmp[i].indexOf('flashvars') + 10, tmp[i].length) + ';';
			}
		}
		return str;
	},
	
	modifySWFlink : function(obj) {
		if (!obj) { return; }
		var str = this.makeRELattribute(obj);
		if (obj.setAttribute) { 
			obj.setAttribute('rel',str);
		} else {
			obj.rel = str;
		}
		this.flashes.push(obj);
	},
	
	resizeCall : function() {
		var arr = this.flashes;
		var len = arr.length;
		this.flashes = new Array();
		for (i=0;i<len;i++) {
			this.modifySWFlink(arr[i]);
		}
		//history.go(0); reloads the page to recalc the sizes.. not perfect..
	}
}
