﻿$j(document).ready(function() {
    $j(".flashheader").each(function(index) {
        var elid = "flashTitle" + index;
        var holder = document.createElement("div");
        holder.id = elid + "_holder";
        var text;
        
        if (typeof($j(this).children("span")[0]) !== "undefined") {
			text = escape($j(this).children("span")[0].innerHTML);
		} else {
            text = escape(this.innerHTML);
        }
        var fontSize;
        if ($j(document.body).hasClass("is_ie")) {
            fontSize = parseInt($j(this).css('fontSize'));
        } else {
            fontSize = Math.round(parseInt($j(this).css('fontSize')));
        }
		
        var tswf = new SWFObject("/flash/fontloader.swf?r=" + (Math.floor(Math.random() * 20)), elid, "1", "1", "9.0.0.0", "#000000");
        tswf.addParam("wmode", "transparent");
        tswf.addParam("quality", "high");

        tswf.addVariable("text", text);
		/*=============================*\
		| Available Font Names:			|
		|     CantoriaMTStd         	|
		|     CantoriaMTStd_Bold   		|
		|     CantoriaMTStd_BoldItalic  |
		|     CantoriaMTStd_ExtraBold   |
		|     CantoriaMTStd_ExtraBoldIt |
		|     CantoriaMTStd_Italic      |
		|     CantoriaMTStd_Light     	|
		|     CantoriaMTStd_LightItalic |
		|     CantoriaMTStd_SemiBold    |
		|     CantoriaMTStd_SemiBoldIt  |
		\*=============================*/
        tswf.addVariable("fontName", "CantoriaMTStd_SemiBold");
        tswf.addVariable("fontColor", parseColor($j(this).css("color")));
        tswf.addVariable("fontSize", fontSize);
        tswf.addVariable("fontBold", ($j(this).css("font-weight") >= 800 || $j(this).css("font-weight") == 'bold' ? 'true' : 'false'));
        tswf.addVariable("fontItalic", $j(this).css("font-style") == "italic" ? "true" : "false");
        tswf.addVariable("elid", elid);
        tswf.addVariable("callbackFunc", "setFlashTitleSize");

        var pn = this.parentNode;
		var h = $j(pn).height();
		$j(pn).css("height", "auto");
		$j(pn).html('');
        pn.appendChild(holder);

        tswf.write(holder.id);
    });
	
	$j(".sectiontitle p").each(function(index) {
        var elid = "sectionTitle" + index;
        var holder = document.createElement("div");
        holder.id = elid + "_holder";
        var text;
        
        if (typeof($j(this).children("span")[0]) !== "undefined") {
			text = escape($j(this).children("span")[0].innerHTML);
		} else {
            text = escape(this.innerHTML);
        }
        var fontSize;
        if ($j(document.body).hasClass("is_ie")) {
            fontSize = parseInt($j(this).css('fontSize'));
        } else {
            fontSize = Math.round(parseInt($j(this).css('fontSize')));
        }
		
        var tswf = new SWFObject("/flash/fontloader.swf?r=" + (Math.floor(Math.random() * 20)), elid, "1", "1", "9.0.0.0", "#000000");
        tswf.addParam("wmode", "transparent");
        tswf.addParam("quality", "high");

        tswf.addVariable("text", text);
		/*=================================*\
		| Available Font Names:				|
		|     CantoriaMTStd         		|
		|     CantoriaMTStd_Bold   			|
		|     CantoriaMTStd_BoldItalic  	|
		|     CantoriaMTStd_ExtraBold   	|
		|     CantoriaMTStd_ExtraBoldIt 	|
		|     CantoriaMTStd_Italic      	|
		|     CantoriaMTStd_Light     		|
		|     CantoriaMTStd_LightItalic 	|
		|     CantoriaMTStd_SemiBold    	|
		|     CantoriaMTStd_SemiBoldIt  	|
		|     OrgandaMN						|
		\*=================================*/
        tswf.addVariable("fontName", "OrgandaMN");
        tswf.addVariable("fontColor", parseColor($j(this).css("color")));
        tswf.addVariable("fontSize", fontSize);
        tswf.addVariable("fontBold", ($j(this).css("font-weight") >= 800 || $j(this).css("font-weight") == 'bold' ? 'true' : 'false'));
        tswf.addVariable("fontItalic", $j(this).css("font-style") == "italic" ? "true" : "false");
        tswf.addVariable("elid", elid);
        tswf.addVariable("callbackFunc", "setFlashTitleSize");

        var pn = this.parentNode;
		var h = $j(pn).height();
		$j(pn).css("height", "auto");
		$j(pn).html('');
        pn.appendChild(holder);

        tswf.write(holder.id);
    });
	
	$j('flashrotator').each(function(index)
	{
		var par = $j(this).parent();
		if($j(par).attr('id') == '')
		{
			$j(par).attr('id', 'prodRot_' + index + 'Holder');
		}
		
		var swf = new SWFObject("/flash/productRotator.swf", "prodRot_" + index, 504, 217, "#FFFFFF", "9.0.0.0");
		swf.addParam("wmode", "transparent");
		swf.addParam("quality", "high");
		
		var file = URLEncode($j(this).attr('file'));
		var backColor = $j(this).attr('backcolor');
		var backAlpha = $j(this).attr('backalpha');
		var navBackColor = $j(this).attr("navBackColor");
		var navColor = $j(this).attr("navColor");
		var navGraphicColor = $j(this).attr("navGraphicColor");
		var navHighlightColor = $j(this).attr("navHighlightColor");
		var itemHighlightColor = $j(this).attr("itemHighlightColor");
		var productTextColor = $j(this).attr("productTextColor");
		var setCount = $j(this).attr("setCount");
		
		if(productTextColor)
			swf.addVariable("productTextColor", productTextColor);
		
		if(backColor)
			swf.addVariable("backColor", backColor);
		
		if(backAlpha)
			swf.addVariable("backAlpha", backAlpha);
		
		if(navBackColor)
			swf.addVariable("navBackColor", navBackColor);
		
		if(navColor)
			swf.addVariable("navColor", navColor);
		
		if(navGraphicColor)	
			swf.addVariable("navGraphicColor", navGraphicColor);
		
		if(navHighlightColor)
			swf.addVariable("navHighlightColor", navHighlightColor);
		
		if(itemHighlightColor)
			swf.addVariable("itemHighlightColor", itemHighlightColor);
		
		if(productTextColor)
			swf.addVariable("productTextColor", productTextColor);
		
		if(setCount)
			swf.addVariable("setCount", setCount);
		
		if(file)
			swf.addVariable("file", file);
		
		swf.write($j(par).attr('id'));
		
		$j('#prodRot_' + index).focus(function() { this.blur();});
	});
	
	$j('flvideo').each(function(index)
	{
		var par = $j(this).parent();
		if($j(par).attr('id') == '')
		{
			$j(par).attr('id', 'flvideo_' + index + 'Holder');
		}
		
		var w, h, url, file;
		w = $j(this).attr('width');
		h = $j(this).attr('height');
		url = $j(this).attr('url');
		file = $j(this).attr('file');
		
		if(!url || url == '')
			return;
		
		if(!w || w == 0)
			w = 640;
		
		if(!h || h == 0)
			h = 480;
		
		var swf = new SWFObject(url, "flvideo_" + index, w, h, "#FFFFFF", "9.0.0.0");
		swf.addParam("wmode", "transparent");
		swf.addParam("quality", "high");
		
		swf.addVariable('file', URLEncode(file));
		swf.write($j(par).attr('id'));
		
		/* $j('#flvideo_' + index).focus(function() { this.blur();}); */
	});
	
});

function setFlashTitleSize(elid, w, h) {
    var el = $j("#" + elid)[0];
    el.style.width = w + "px";
    el.style.height = h + "px";
	el.style.marginBottom = "-6px";
	if($j(document.body).hasClass('is_ie7'))
	{
		el.parentNode.style.height = (h + 2) + "px";
		$j(el).removeAttr('width');
		$j(el).removeAttr('height');
		$j(el).css('display', 'block');
	} else {
		el.parentNode.style.height = "auto";
	}
}

function parseColor(rgb) {
    if ($j(document.body).hasClass("is_ie")) {
        return rgb.replace("#", "");
    } else {
        rgb = rgb.toLowerCase().replace(/rgb\(/, "");
        rgb = rgb.toLowerCase().replace(")", "");
        rgb = rgb.toLowerCase().replace(" ", "");
        rgb = rgb.split(',');
        return dec2hex(rgb);
    }
}

function dec2hex(dec) {
    var i, h = "", d, l;
    for (i = 0; i < dec.length; i++) {
        d = dec[i];
        l = Number(d).toString(16);
        if (l.length < 2) {
            l = "0" + l;
        }
        h += "" + l;
    }
    return h.toUpperCase();
}

function showPopupImage(imageUrl,title,designer)
{
	var atag = document.createElement('a');
	$j(atag).attr('href', imageUrl);
	$j(atag).attr('title', title + '<br /> by ' + designer);
	$j(atag).attr('class', 'popupImage');
	$j(atag).css('display', 'none');
	document.body.appendChild(atag);
	$j(atag).slimbox({}, null, function(el) {
		return (this == el) || ((this.rel.length > 8) && (this.rel == el.rel));
	});
	$j(atag).click();
	
	document.body.removeChild(atag);
}

function URLEncode(text)
{
	var SAFECHARS = "0123456789" +					// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";					// RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";

	var plaintext = text;
	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
	    if (ch == " ") {
		    encoded += "+";				// x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} // for

	return encoded;
}