var clicked = false;
var oldClass = "";
var _el = null;
var _cancel = null;
var _fire = null
function SetCarsButtonActive(el, cancelButtonClientID, fireButtonClientID) {
	if(_el == null)
		_el = el;
	if(_cancel == null)
		_cancel = cancelButtonClientID;
	if(_fire == null)
		_fire = fireButtonClientID;
	if(!clicked) {
		if(oldClass == "")
			oldClass = el.className;
	 	_el.className += " active";
		var fireButton = $get(_fire);
		fireButton.click();
		clicked = true;
	}
	else {
		_el.className = oldClass;
		var cancelButton = $get(_cancel);
		cancelButton.click();
		clicked = false;
	}
}

function replaceHeadingWithImage(elementId) {
	var el = $("#" + elementId);
	if(el === undefined)
		setTimeout("replaceHeadingWithImage('" + elementId + "')", 100);
	else
		doReplace(el, false);
}

function doReplace(element, isId) {
	if(isId)
		element = $("#" + element);
	var url = String.format(
		"/handlers/textimage.ashx?text={0}&font=Optima LT&max_px_width={1}&font_size={2}&color={3}&bold={4}&italic={5}&shadow={6}&small_shadow={7}&}",
		element.text(),
		isNaN(element.parent().css("width").replace("px", "")) ? isNaN(element.parent().width()) ? 800 : Math.round(element.parent().width()) : Math.round(element.parent().css("width").replace("px", "")),
		element.css("font-size").replace("px", ""),
		element.css("color").toLowerCase().indexOf("rgb") > -1 ? RGBtoHex(element.css("color")) : element.css("color"),
		element.css("font-weight") == "bold" ? true : false,
		element.css("font-style") == "italic" ? true : false,
		element.hasClass("noshadow") || element.context.nodeName.toLowerCase() == "h4" ? false : true,
		element.hasClass("small-shadow") || element.context.nodeName.toLowerCase() == "h3" ? true : false
	);
	var newWrapper = $("<div></div>");
	var newImg = $('<img src="' + url + '" alt="' + element.text() + '" />');
	var newEl = element.clone();
	var oldCss = element.getCSS("div#empty");
	newImg.css(oldCss); // Clone css styles
	newEl.css(oldCss);
	// Except size
	newImg.css("width", "auto");
	newImg.css("height", "auto");
	newEl.css("width", "auto");
	newEl.css("height", "auto");
	
	newEl.addClass("hidden");
	newImg.addClass("replaced");
	
	newWrapper.append(newEl);
	newWrapper.append(newImg);
	element.replaceWith(newWrapper);
}

$(document).ready(function() {
	var elements = new Array();
	elements[0] = "h1";
	elements[1] = "h2";
	elements[2] = "h3";
	
	for(i = 0; i < elements.length; i++) {
		var htmlElements = $(elements[i] + ":not(.noimg)");
		for(j = 0; j < htmlElements.length; j++) {
			var element = $(htmlElements[j]);
			doReplace(element, false);
		}
	}
});

function RGBtoHex(RGB) {
	if (result = /rgb\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*\)/.exec(RGB))
		return toHex(parseInt(result[1])) + toHex(parseInt(result[2])) + toHex(parseInt(result[3]));
}
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);
}


String.format = function( text ) {
    //check if there are two arguments in the arguments list
    if ( arguments.length <= 1 )
    {
        //if there are not 2 or more arguments there's nothing to replace
        //just return the original text
        return text;
    }
    //decrement to move to the second argument in the array
    var tokenCount = arguments.length - 2;
    for( var token = 0; token <= tokenCount; token++ )
    {
        //iterate through the tokens and replace their placeholders from the original text in order
        text = text.replace( new RegExp( "\\{" + token + "\\}", "gi" ), arguments[ token + 1 ] );
    }
    return text;
};