
window.onload = function() {
	initSIFR();
	initCollection();
}


function initSIFR() {
	var h1 = "#ffffff";
	var h2 = "#ffffff";

	if (document.styleSheets) {
		var css = document.styleSheets;
		for (var i=0;i<css.length;i++) {
			if (css[i].title=="custom") {
				if (css[i].cssRules) var rules = css[i].cssRules;
				else if (css[i].rules) var rules = css[i].rules;
				else return;
				for (var j=0;j<rules.length;j++) {
					if (rules[j].selectorText.toLowerCase()=="h1") h1 = parseHex(rules[j].style.color);
					if (rules[j].selectorText.toLowerCase()=="h2") h2 = parseHex(rules[j].style.color);
				}
				break;
			}
		}
	}
	
	if(typeof sIFR == "function"){
		sIFR.replaceElement("h1", named({sFlashSrc: "./sifr/futura.swf", sColor: h1, sCase: "upper", sWmode: "transparent"}));
		sIFR.replaceElement("h2", named({sFlashSrc: "./sifr/futura.swf", sColor: h2, sCase: "upper", sWmode: "transparent"}));
	};
}

function initCollection() {
	if (document.getElementById("collection")) {
		var a = document.getElementById("collection").getElementsByTagName("a");
		for (var i=0;i<a.length;i++) {
			a[i].onclick = function() {
				var a = document.getElementById("collection").getElementsByTagName("a");
				for (var i=0;i<a.length;i++) {
					if (a[i]==this) addClass(a[i],"sel");
					else remClass(a[i],"sel");
				}
				setCollectionImg(this);
				return false;
			}
		}
	}
	
}

function setCollectionImg(node) {
	if (!document.getElementById("collection-image")) {
		var o = document.createElement("div");
		o.setAttribute("id","collection-image");
		document.getElementById("collection").appendChild(o);
	} else {
		var o = document.getElementById("collection-image");
	}
	
	o.innerHTML = "";
	var img = document.createElement("img");
	img.src = node.href;
	o.appendChild(img);
	
	var childnodes = node.getElementsByTagName("div");
	for (i=0;i<childnodes.length;i++) {
		if (childnodes[i].className=="description") {
			var desc = childnodes[i].cloneNode(true);
			o.appendChild(desc);
		}
	}
	
}


function embedFlash(sID,url,w,h) {
	var str = 	'<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="'+w+'" height="'+h+'" id="'+sID+'">';
	str +=		'<param name="allowScriptAccess" value="sameDomain" />';
	str +=		'<param name="movie" value="'+url+'" />';
	str +=		'<param name="quality" value="high" />';
	str +=		'<param name="wmode" value="transparent" />';
	str +=		'<param name="bgcolor" value="#ffffff" />';
	str +=		'<embed src="'+url+'" quality="high" wmode="transparent" bgcolor="#ffffff" width="'+w+'" height="'+h+'" name="'+sID+'" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />';
	str +=		'</object>';
	document.write(str);
}


/* Helper methods */

function addClass(o,str) {
	remClass(o,str);
	o.className += " " + str;
}
function remClass(o,str) {
	o.className = o.className.replace(str,"");
	o.className = o.className.replace(/' '{2,}/gi," ");
}

function parseHex(s) {
	if (s.toLowerCase().indexOf("rgb")!=-1) {
		var a = (s.match(/\d+/g));
		s =  "#" + RGBtoHex(a[0],a[1],a[2]);
	}
	return s;
	
}
function RGBtoHex(R,G,B) {return toHex(R)+toHex(G)+toHex(B)}
function toHex(N) {
	if (N==null) return "00";
	N=parseInt(N);
	if (N==0 || isNaN(N)) return "00";
	N=Math.max(0,N);
	N=Math.min(N,255);
	N=Math.round(N);
	return "0123456789ABCDEF".charAt((N-N%16)/16) + "0123456789ABCDEF".charAt(N%16);
}
