﻿// Convert object name string or object reference into a valid element object reference
function g$(obj) {
	var theObj;
	if (typeof obj == "string") {
		theObj = document.getElementById(obj);
	} else {
		// pass through object reference
		theObj = obj;
	}
	return theObj;
}

// Retrieve the padding around an object
function GetObjectPadding(obj) {
	var elem = g$(obj);

	var l = 0;
	var r = 0;
	var t = 0;
	var b = 0;
	if (elem.currentStyle) {
		if (elem.currentStyle.paddingLeft)
			l = parseInt(elem.currentStyle.paddingLeft, 10);
		if (elem.currentStyle.paddingRight)
			r = parseInt(elem.currentStyle.paddingRight, 10);
		if (elem.currentStyle.paddingTop)
			t = parseInt(elem.currentStyle.paddingTop, 10);
		if (elem.currentStyle.paddingBottom)
			b = parseInt(elem.currentStyle.paddingBottom, 10);
	}
	else if (window.getComputedStyle) {
		l = parseInt(window.getComputedStyle(elem, null).paddingLeft, 10);
		r = parseInt(window.getComputedStyle(elem, null).paddingRight, 10);
		t = parseInt(window.getComputedStyle(elem, null).paddingTop, 10);
		b = parseInt(window.getComputedStyle(elem, null).paddingBottom, 10);
	}
	if (isNaN(l) == true) l = 0;
	if (isNaN(r) == true) r = 0;
	if (isNaN(t) == true) t = 0;
	if (isNaN(b) == true) b = 0;

	return { l: (l), r: (r), t: (t), b: (b) };
}

// Retrieve the rendered size of an element
function GetObjectSize(obj) {
	var elem = g$(obj);
	var w = 0;
	var h = 0;
	if (elem.offsetWidth) {
		w = elem.offsetWidth; h = elem.offsetHeight;
	} else if (elem.clip && elem.clip.width) {
		w = elem.clip.width; h = elem.clip.height;
	} else if (elem.style && elem.style.pixelWidth) {
		w = elem.style.pixelWidth; h = elem.style.pixelHeight;
	}

	w = parseInt(w, 10);
	h = parseInt(h, 10);

	// remove any original element padding
	var padding = GetObjectPadding(elem);
	w -= (padding.l + padding.r);
	h -= (padding.t + padding.b);

	return { w: (w), h: (h) };
}
function WriteFlashCountWidth(obj,flash,objW){
	obj = g$(obj);
	objW = g$(objW);
	objW = !!objW ? objW : obj;
	var size =  GetObjectSize(objW);
	var w = (size.w > 0 && !isNaN(size.w)) ? size.w : 100;
	var h = parseInt(w * 0.7);
	obj.innerHTML=WriteFlash(flash,h);
}
function WriteFlash(url,w){
	return ['<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0"',
		'width="100%" height="',
		w,
		'px">',
		'<param name="movie" value="',
		url,
		'">',
		'<param name="wmode" value="transparent">',
		'<param name="quality" value="high">',
		'<embed src="',
		url,
		'" wmode="transparent" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="100%" height="',
		w,
		'px"></object>'].join("");
}