function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

jQuery.noConflict();

jQuery.preloadImages = function()
{
  for(var i = 0; i<arguments.length; i++)
  {
    jQuery("<img>").attr("src", arguments[i]);
  }
}

// preload images first (can run before page is fully loaded)
jQuery.preloadImages("/images/tooltip-top.png", "/images/tooltip-bottom.png", "/images/tooltip-top-alt.png", "/images/tooltip-bottom-alt.png", "/images/tooltip-top.gif", "/images/tooltip-bottom.gif", "/images/tooltip-top-alt.gif", "/images/tooltip-bottom-alt.gif");

jQuery(document).ready(function($) {
	
	// UPDATE SELECT BOXES
	$('#container a.form_updater').click(function () {
		//Get the value from the rel attribute
		selectID = $(this).attr("rel");
		window.console.log("selectID:"+selectID);
		//Get the select menu
		selectItem = $('#id_fragrance option');
		//Iterate through the options
		//If the value matches the selectID, update it to SELECTED
		for (var i=0; i < selectItem.length; i++) {
			currentValue = selectItem[i].getAttribute("value");
			selectItem[i].removeAttribute("selected");
			
			window.console.log("currentValue:"+currentValue);
			
			if (currentValue == selectID) {
				selectItem[i].selected = true;
			};
		}
		
		//Show User Choice Has Been Selected
		
		// First, clearn any previou choices 
		$('.selected-msg').remove()
		var oSpan = document.createElement('span');
		var updateText = document.createTextNode(" (Selected)");
		$(oSpan).append(updateText);
		$(oSpan).addClass("selected-msg hidden")
		$(this).prev().append($(oSpan))
		$(oSpan).fadeTo(100, 0, function() {
			jQuery(this).removeClass("hidden")
		}).fadeTo(500, 1)
		return false;
	});
	
	
	// TOOLTIPS
	$('#container a.tooltip').hover(function () {
		
		//get the message that is to be displayed from the title property
		msg = $(this).attr("title");
		$(this).attr('title', '');
		
		$(this).parent().css("z-index", "2");
		
		// Create our tooltip elements
		oDiv = document.createElement('div');
		oP = document.createElement('p');
		oSpan = document.createElement('span');
		oText = document.createTextNode(msg);
		
		// Here we split the code depending whether this is a form updater tooltip or standard tooltip
		
		//form_updater classes needs right alignment
		if (this.className == 'tooltip form_updater') {
			oDiv.className = 'tooltip-msg-alt';
		}
		
		// Standard tooltip (left-aligned)
		else {
			oDiv.className = 'tooltip-msg';
		}
				
		// Append description tooltip and insert into document		
		$(oSpan).append(oText);
		$(oP).append(oSpan);
		$(oDiv).append(oP);
		$(oDiv).insertAfter($(this));
		
		// Now that div is inserted, get height and offset from top
		// Hide div to prepare for animation
		objHeight = $(oDiv).height();
		$(oDiv).css('display', 'none');
		
		//Form Updating tooltip needs slightly different positioning
		if(this.className !== 'tooltip form_updater') {
			objHeight = objHeight - 10;
			objHeight = '-' + objHeight + 'px';
			$(oDiv).css('top', objHeight)	
		}
		
		//Animate DIV via opacity
		$(oDiv).animate({
			opacity: 'show'
		}, 500)
		
	}, function () {
		$(this).parent().css("z-index", "1");
		$(this).attr('title', msg);
		$(oDiv).remove();
	});
	
	// Rollover For Add to Cart Button
	$('#addtobag').hover(function() {
		this.setAttribute("src", "/images/add-to-bag-on.png");
	}, function () {
		this.setAttribute("src", "/images/add-to-bag.png");
	});
	
	// Fudge the Homepage column heights to match
	if ($('#col-1').height() > $('#col-2').height()) {
		$('#col-2').height($('#col-1').height());
	}
	else {
		$('#col-1').height($('#col-2').height());
	}
	
});