// JavaScript Document
$(function() {
	setGalleryPopup();
});

function adjustHeight() {
	var heightDiff = $(window).height() - $("#footer").bottom();
	if (heightDiff > 0) {
		$("#main_div").animate({height: "+=" + (heightDiff - 5)}, function() {
			// min-height can't be animated, but we don't want a fixed height either
			$(this).css('min-height', $(this).css('height'));
			$(this).css('height', 'auto');
		});
	}
}

function selectAll(formID, button)
{
	var checking;
	if (button.value == "Select All")
		checking = true;
	else
		checking = false;
		
	var form = document.getElementById(formID);
	
	var inputs = form.getElementsByTagName("input");
	
	for (var i = 0; i < inputs.length; i++)
		if (inputs[i].type == "checkbox")
			inputs[i].checked = checking;
			
	if (button)
	{
		button.value = button.value == "Unselect All" ? "Select All" : "Unselect All";
	}

}

function addToCart(courseID, obj, loc)
{
	loc = loc ? loc : 'cart.php';
	getAJAX('/ajax/sign_up.php?action=signup&id=' + courseID, obj, null, function() {location = loc});
}

function removeFromCart(courseID, obj, loc)
{
	loc = loc ? loc : 'cart.php';
	getAJAX('/ajax/sign_up.php?action=drop&id=' + courseID, obj, null, function() {location = loc});
}

// Unselect a radio group
function unselect(inputName)
{
	var inputs = document.getElementsByTagName('input');
	
	for (var i = 0; i < inputs.length; i++)
	{
		if (inputs[i].type == 'radio' && inputs[i].name == inputName)
			inputs[i].checked = false;
	}
}

function setCookie(name, value, time)
{
	if (!time) time = 60*60*24*365;
	
	var d = new Date();
	var t = new Date(d.getTime() + time);
	t = t.toGMTString();
	document.cookie = name + "=" + escape(value) + ";expires=" + t + ";path=/;domain=.extravadanza.com";
	alert(document.cookie);
}

function duplicateTableRow(rowID)
{
	var row = document.getElementById(rowID);
	var html = row.innerHTML;
	var newIndex = ++row.attributes.index.value;
	
	var newRow = document.createElement('tr');
	var newCell = document.createElement('td');
	
	newCell.innerHTML = html;
	
	var newRowContainer = row.parentNode;
	
	newRow.appendChild(newCell);
	newRowContainer.appendChild(newRow);	
	
	// Go through all the inputs looking for 
	// an array index like [0] and replace
	// it by the new index
	var inputs = newRow.getElementsByTagName('input')
	var selects = newRow.getElementsByTagName('select');

	for (var i = 0; i < inputs.length + selects.length; i++)
	{
		// see which collection we're dealing with
		var elem =  (i >= selects.length) ? inputs[i - selects.length] : selects[i];
		
		var pattern = /\[\d{1,2}\]/;
		var oldIndex = elem.name.match(pattern);
		elem.name = elem.name.replace(oldIndex, "[" + newIndex + "]");			
	}
}

function findPos(obj)
{
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
	}
	return {left:curleft, top:curtop};
}

function addOnload(func)
{
	if (typeof window.onload != 'function')
		window.onload = func;
	else
	{
		var oldOnload = window.onload;
		window.onload = function()
		{
			oldOnload();
			func();
		}
	}
}

function createElement(elName, attributes)
{
	if (!name) name = "";
	
	if (window.attachEvent) // if IE
	{
		attrText = '';
		for (var name in attributes)
			attrText += ' ' + name + '="' + attributes[name]+ '"';
			
		return document.createElement('<' + elName + attrText + '>');
	}
	else
	{
		var e = document.createElement(elName);
		for (var name in attributes)
			e[name] = attributes[name];
		
		return e;
	}
}

function setGalleryPopup() {
	$(".gallery_popup").colorbox({
		iframe: true,
		width: "960",
		height: "690",
		current: "",
		next: "",
		opacity: .7
	});	
}

function showPopupOnce(image_path) {
	var cookieName = image_path.replace(/[^a-z0-9]/gi, "");
	if (!$.cookie(cookieName)) {
		$.colorbox({
			href: image_path,
			opacity: .7
		});
		$.cookie(cookieName, 1, {
			expires: 30,
			path: "/",
			domain: ".extravadanza.ca"
		});
	}
}

(function($) {
	$.expr[':'].focus = function(elem) {
		return elem === document.activeElement && (elem.type || elem.href);
	};

	$.fn.backgroundText = function(text, opts) {
		opts = $.extend({
			idleClass: "bgtext_idle",
			focusClass: "bgtext_focus",
			overlayClass: "bgtext_overlay"
		}, opts);

		var group = this;
		
		this.each(function() {
			var $this = $(this),
				message = text || this.title,
				overlay = $("<label />", {
					text: message,
					"for": this.id,
					"class": opts.overlayClass
				})
				.appendTo("body"),
				interval;

			$this
				.data('overlay', overlay)
				.focus(function() {
					$this.removeClass(opts.idleClass).addClass(opts.focusClass);
					overlay.hide();
					interval = window.setInterval(function() {
						checkGroupInput();
					}, 100);
				})
				.blur(function() {
					window.clearInterval(interval);
					$this.removeClass(opts.focusClass).addClass(opts.idleClass);
					if ($this.val() == '') {
						reposition(overlay).show();
					}
				});
			$(window).bind('resize', function() {
				reposition(overlay);
			});

			$this.addClass(opts.idleClass);
			$(window).load(function() {
				reposition(overlay);
				if ($this.val() == '') {
					overlay.fadeIn(1000);
				}
			});

			function reposition(overlay) {
				return overlay.css({
					top: $this.offset().top + ($this.outerHeight() - overlay.height()) / 2,
					left: $this.offset().left
				});
			}
			function checkGroupInput() {
				group.not($this).each(function() {
					if (this.value != '') {
						$.data(this, 'overlay').hide();
					} else {
						$.data(this, 'overlay').show();
					}
				});
			}
		});
	};
	
	$.fn.bottom = function() {
		var offset = this.offset();
		return offset && (offset.top + this.outerHeight(true));
	}
	
})(jQuery);
