// Automagic blog content replacement and pop up stuff

function replace_swfs ()
{
	var main = document.getElementById("main");
	var img = main.getElementsByTagName("img");
	var i;
	var n = img.length;

//	nk_debug("replace_swfs()", n);

	for (i = 0; i < n; i++) {
		if (img[i] != null && img[i] != undefined) { // wtf?
//			nk_debug("replace_swfs()", i, typeof img[i]);
			replace_swf(img[i]);
		}
	}
}

function replace_swf (img)
{
	var src = img.getAttribute("src").replace("http://www.whatscookin.com", "").replace(nk_blog_base + "/..", "");
	var href = window.location.href

	nk_debug("replace_swf()", src);
	
	if (/company\/overview\/\d\d\d\d\_293\_01\.jpg/.test(src)) {
		replace_swf_company_overview(img);
	} else if (/\d\d\d\d\/\d\d\d\d\_293\_01\.jpg/.test(src)) {
		replace_swf_project(img);
	} else {
		replace_swf_byname(img);
	}
}

function replace_swf_company_overview (img)
{
	var src = img.getAttribute("src").replace("http://www.whatscookin.com", "").replace(nk_blog_base + "/..", "");
	var so;
	var w;
	var h;

	nk_debug("replace_swf_company_overview()", src);

	// set div background to original image

	img.parentNode.style.background = "url(" + src + ") top left no-repeat";

	// get width and height from original image

	w = img.getAttribute("width");
	h = img.getAttribute("height");

	// replace image with portfolio swf

	so = new SWFObject("/assets/Flash/company/overview/company.swf", "company", w, h, "8", "#E5EFF5");
	so.addParam("wmode", "transparent");
	so.write(img.parentNode);
}

function hide_swf_company_overview_bg ()
{
	nk_debug("hide_swf_company_overview_bg()");

	var portfolio = document.getElementById("company");

	portfolio.parentNode.style.background = "#E5EFF5";
}

function replace_swf_project (img)
{
	var src = img.getAttribute("src").replace("http://www.whatscookin.com", "").replace(nk_blog_base + "/..", "");
	var so;
	var w;
	var h;
	var project;

	// yank out project number

	project = src.substr(24, 4);

	nk_debug("replace_swf_project()", project);

	// set div background to original image

	img.parentNode.style.background = "url(" + src + ") top left no-repeat";

	// get width and height from original image

	w = img.getAttribute("width");
	h = img.getAttribute("height");

	// replace image with portfolio swf
	
	so = new SWFObject("/assets/Flash/portfolio/portfolio.swf", "portfolio", w, h, "8", "#E5EFF5");
	so.addParam("wmode", "transparent");
	so.addVariable("project", project);
	so.write(img.parentNode);
}

function hide_swf_project_bg ()
{
	nk_debug("hide_swf_project_bg()");

	var portfolio = document.getElementById("portfolio");

	portfolio.parentNode.style.background = "#E5EFF5";
}

function replace_swf_byname (img)
{
	var src = img.getAttribute("src").replace("http://www.whatscookin.com", "").replace(nk_blog_base + "/..", "");
	var src2;
	var i;
	var j;
	var so;
	var w;
	var h;
	var div;

	if (src.indexOf("/assets/Image") == -1) return  ;                              //   is this one of our replacement candidates?
	k = src.lastIndexOf(".");                                                      //     find location of extension
	src2 = src.replace("/assets/Image", "/assets/Flash").substring(0,k) + ".swf";  //     convert image URL into swf URL

	for (i = 0; i < swflist.length; i++) {                                         //     loop through our list of swfs
		if (src2 == swflist[i]) {                                                  //       did we find a match?
			nk_debug("replace_swf_byname()", src2);
			w = img.getAttribute("width");	                                       //         grab image width
			h = img.getAttribute("height");                                        //         grab image height
			so = new SWFObject(src2, "swf" + j, w, h, "8", "#FFFFFF");             //         build the SWFObject
			so.addParam("wmode", "transparent");                                   //         setup so options
			so.write(img.parentNode);                                              //         jam the swf into the image parent
			break;                                                                 //         break out of inner loop
		}
	}
}

function open_project_popup (project)
{
	nk_debug("open_project_popup()", project);

	open_popup("/assets/Popup/portfolio/" + project, 800, 600);
}

function open_studio_popup (a)
{
	var img = a.firstChild;
	var src = img.getAttribute("src").replace("http://www.whatscookin.com", "").replace(nk_blog_base + "/..", "");
	var parts = src.replace("/assets/Image/studio/190_", "").replace(".jpg", "").split("_");
	var what = parts[0].toLowerCase();
	var which = parts[1];
	var url;

	nk_debug("open_studio_popup()", src);

	url = "/assets/Popup/studio/" + what;

	if (what != "designs") url += "_" + which;

	open_popup(url, 700, 500);
}

function open_popup (url, w, h)
{
	nk_debug("open_popup()", url, w, h);

	wid = window.open(url, "popup","resizable=no,status=no,scrollbars=no,height=" + h + ",width=" + w);

	wid.focus();
}
