// http://getfirebug.com/firebug/firebugx.js
if (!window.console || !console.firebug)
{
    var names = ["log", "debug", "info", "warn", "error", "assert", "dir", "dirxml",
    "group", "groupEnd", "time", "timeEnd", "count", "trace", "profile", "profileEnd"];

    window.console = {};
    for (var i = 0; i < names.length; ++i)
        window.console[names[i]] = function() {}
}

var Comments = {
    formTemplate: null,
	sort: null,

	_replyOpen: false,

    openReply: function(id) {
		Comments.closeReply();
		
		var el = $$('.comment' + id + ' .comment-replies').first();
		var t = new Template(Comments.formTemplate);

		if(this.sort == 'DESC') {
			new Insertion.Top(el, t.evaluate({id: id}));
		}
		else {
			new Insertion.Bottom(el, t.evaluate({id: id}));
		}
		
		Form.focusFirstElement(document.commentreply);
		Comments._replyOpen = true;
	},

    closeReply: function() {
		if(Comments._replyOpen) {
			Element.remove(document.commentreply);
			Comments._replyOpen = false;
		}
	},
	
    onSubmit: function(form) {
		if(form.email) {
			form.email.value = 'pass_test1';
		}
	}

};


if(window.jQuery) {
	jQuery(webtoolsJQuery);
}
else {
	Event.observe(window, 'load', webtoolsPrototype);
}

function webtoolsJQuery() {
 	function forceOneClick() {
		// disable submit button after press
		jQuery('div.form-wrapper form').find(':submit,:image').click(function(e) {
				e.preventDefault();
				this.disabled = true;
				jQuery(this).parents('form').submit();
		});
	}

	forceOneClick();

    // REQUIRES media plugin:
    // http://www.malsup.com/jquery/media/
    jQuery('.hero-media').each(function() {
	var el = $(this);
	var p = el.parent('.heroshots');
	var width = p.width();
	var height = p.height();
	jQuery(this).media({ width: width,
			     height: height,
			     flashvars: { clicktag: this.rel }});
    })
}

function webtoolsPrototype() {
	function forceOneClick() {
		// disable submit button after press
		document.getElementsByClassName('form-wrapper').each(function(o) {
				inputs = o.getElementsByTagName('input');
				for(var i=0; i<inputs.length; i++) {
					var el = inputs[i];
					if(el.type == 'submit' || el.type == 'image') {
						Event.observe(el, 'click', function(e) {
								Event.stop(e);
								el.disabled = true;
								Element.up(el, 'form').submit();
						});
					}
				};
			});
	}

	forceOneClick();

    function gensym() {
	return 'O' + Math.floor(Math.random()*1000000);
    }

    // REQUIRES swfplugin:
    // http://code.google.com/p/swfobject/
    $$('.hero-media').each(function(o) {
	var id = gensym();
	var p = o.up('.heroshots');
	var width = p.getWidth();
	var height = p.getHeight();
	o.id = id;

	swfobject.embedSWF(o.href, id, width, height, "5");
    });

}


// deprecated
function LeaveHttps() {
	var href = window.location.href;
	if(href.substring(0, 5) == 'https') {
		var links = document.getElementsByTagName('a');
		for (var i = 0, len = links.length; i < len; i++) {
			var a = links[i];
			if(a.href.substring(0, 5) == 'https') {
				a.href = "http" + a.href.substring(5);
			}
		}
	}
}

/* ------------------------------------------------------------e
The following code is commented-out because it interacts strangely with IE:

var Event = Event || {};
if (Event.observe) { Event.observe(window, 'load', LeaveHttps); }
else if (jQuery) { jQuery(LeaveHttps); }
------------------------------------------------------------ */

