dojo.require("dijit.Tooltip")

google.setOnLoadCallback(function(){

	var init = function () {
		h1 = document.getElementsByTagName('h1')[0];
		dojo.connect(h1, 'onclick', function() { window.location.href = '/'; });
		setTimeout(initOverLabels, 40);
	};


	var setScreenClass = function () {
		// If the window size is too narrow, we need to use some style rules to help it out.
		dojo.removeClass(document.body, 'narrow');
		dojo.removeClass(document.body, 'wide');
		var fmt = document.documentElement.clientWidth;
		var cls = ( fmt <= 900 ) ? 'narrow' : 'wide' ;
		dojo.addClass(document.body, cls);
	};


	dojo.addOnLoad(init);
	dojo.addOnLoad(setScreenClass);
	dojo.connect(window, 'onresize', setScreenClass);
});

function initOverLabels () {
	dojo.query("label").forEach(function(node) {
		if (dojo.hasClass(node, "overlabel")) {
			// Skip labels that do not have a named association
			// with another field.
			var id = node.htmlFor || node.getAttribute('for');
			var field = dojo.byId(id);
			if (id && field) {
				// Change the applied class to hover the label 
				// over the form field.
				dojo.addClass(node, "overlabel-apply");

				// Hide any fields having an initial value.
				if (field.value !== '') {
					hideLabel(field.getAttribute('id'), true);
				}

				// Set handlers to show and hide labels.
				dojo.connect(field, "onfocus", function() {
					hideLabel(this.getAttribute('id'), true);
				});
				dojo.connect(field, "onblur", function() {
					if (this.value === '') {
						hideLabel(this.getAttribute('id'), false);
					}
				});

				// Handle clicks to LABEL elements (for Safari).
				dojo.connect(node, "onclick", function() {
					var id = this.getAttribute('for');
					var field = dojo.byId(id);
					if (id && field) {
						field.focus();
					}
				});
			}
		}
	});
}

function hideLabel (field_id, hide) {
	dojo.query('label').forEach(function(node) {
		var field_for = node.htmlFor || node.getAttribute('for');
		if (field_for == field_id) {
			node.style.textIndent = (hide) ? '-1000em' : '0';
			return true;
		}
	});
}

function removeFromCart (id, ref) {
	// Just let href do the work if we don't have a callback link to to fancy ajax with
	if (typeof(ref) == 'undefined') {
		window.location.href = "/cart.php?remove=" + id;
	} else {
		var cart = dojo.byId('topcart');
		cart.innerHTML = '<p style="height:'+(cart.clientHeight-25)+'px; margin: 0; padding: 10px;">Updating your cart...</p>';
		dojo.xhrGet({
				url: "/cart.php",
				content: {id: id, action: 'Remove from Cart'},
				load: function(data) {
					cart.innerHTML = data;
					dojo.parser.parse(cart);
				}
			});
		ref.parentNode.parentNode.innerHTML = '<input type="hidden" name="id" value="'+id+'" /><p><input type="text" name="qty" id="qty" value="1"/> <input type="submit" name="action" value="Add to Cart" /></p>';
		return false;
	}
}

function addToCart (ref) {
	var cart = dojo.byId('topcart');
	cart.innerHTML = '<p style="height:'+(cart.clientHeight-25)+'px; margin: 0; padding: 10px;">Updating your cart...</p>';
	if (typeof(ref) != 'undefined') {
		dojo.xhrGet({
				url: "/cart.php",
				content: {id: ref.id.value, action: ref.action.value, qty: ref.qty.value},
				load: function(data) {
					cart.innerHTML = data;
					dojo.parser.parse(cart);
				}
			});
		ref.innerHTML = '<p><a class="button" href="/cart.php?action=Remove%20from%20Cart&id='+ref.id.value+'" onclick="return removeFromCart('+ref.id.value+', this);">Remove from Cart</a></p>';
		return false;
	}
}

function add_image(file) {
	var ta = document.getElementsByTagName('textarea')[0];
	var str = '\n(caption):/images/content/'+file+'(right)\n';

	// IE support
	if (document.selection) {
		ta.focus();
		sel = document.selection.createRange();
		sel.text = str;
	// MOZILLA support
	} else if (ta.selectionStart || ta.selectionStart == '0') {
		var startPos = ta.selectionStart;
		var endPos = ta.selectionEnd;
		ta.value = ta.value.substring(0, startPos) + str + ta.value.substring(endPos, ta.value.length);
	} else {
		ta.value += str;
	}
}
