//	jQuery Compatibility Method

try
{
	google.load('search', '1');
	initializeCheckoutAPI("43452", "www.generousgems.com", "/cgi-bin/UCCheckoutAPIJSON");
}
catch(e)
{
	// no search
}

var $j = jQuery.noConflict();
$j("document").ready(function(){




	// Email hidden from bots
	
	var supportEmail = 'sales@generousgems.com';
	
	$j('.support_email_link').text(supportEmail);
	$j('.support_email_link').attr('href', 'mailto:' + supportEmail);
	
	
	
	// Google search
	
	var hasSearched = null;
	
	$j('.search button').click(function(event){
		event.preventDefault();
		executeSearch();
	});
	
	$j('.search form').submit(function(event){
		event.preventDefault();
	});
	
	try
	{
		if($j('#q').val() == '' || $j('#q').val() == 'Search...')
		{
			$j('#q').val('Search...');
		}
		else
		{
			executeSearch();
		}
	}
	catch(e)
	{
		// oops!
	}
	$j('#q').focus(function(){
		if($j('#q').val() == 'Search...')
		{
			$j(this).val('');
		}
		
	});
	
	$j('#q').blur(function(){
		if($j(this).val() == '')
		{
			$j('#q').val('Search...');
		}
	});
	
	var timeout = false;
	
	$j('#q').keypress(function(){
		try
		{
			window.clearTimeout(timeout);
		}
		catch(e)
		{
			// no timeout yet
		}
		timeout = window.setTimeout(executeSearch, 500);
	});
	
	$j('#results_close a').click(function(event){
		event.preventDefault();
		endSearch();
		$j('#q').val('Search...');
	});
	
	function endSearch()
	{
		$j('.primary').show();
		$j('#results').hide();
		$j('#results_content').html('<!--empty-->\n');
		$j('#json_results_content').html('<!--empty-->\n');
	}
	
	function executeSearch()
	{
		$j('.primary').hide();
		$j('#results').show();
		if($j('#q').val().replace(/ /g, '') == '' || $j('#q').val() == 'Search...')
		{
			if(hasSearched)
			{
				endSearch();
			}
			else
			{
				$j('#results_content').html('<h4>Please type something into the search box, and submit</h4>');
				window.setTimeout(endSearch, 2500);
			}
		}
		else
		{
			hasSearched = true;
			JSONSearch($j('#q').val());
			webSearch = new google.search.WebSearch();
			webSearch.setSiteRestriction('generousgems.com');
			webSearch.setResultSetSize(google.search.Search.LARGE_RESULTSET);
			webSearch.setSearchCompleteCallback(this, searchComplete, [webSearch]);
			webSearch.execute($j('#q').val());
		}
	}
	
	function searchComplete(searcher)
	{
		var results = searcher.results;
		if(results.length < 1)
		{
			$j('#results_content').html('<h4>Sorry, there no Google results for your current search.</h4>');
		}
		else
		{
			var theHTML = '<h3>Google search results:</h3>\n';
			for (var i = 0; i < results.length; i++) {
				var result = results[i];
				theHTML += '<h4><a href="' + result.unescapedUrl + '">' + result.titleNoFormatting + '</a></h4>\n';
				if(result.content.length > 140)
				{
					theHTML += '<p>' + result.content.substring(0, 140).replace(/\.\.\./g, '').replace(/<b>/g, '<strong>').replace(/<\/b>/g, '</strong>') + '&hellip;</p>\n';
				}
				else
				{
					theHTML += '<p>' + result.content.replace(/\.\.\./g, '').replace(/<b>/g, '<strong>').replace(/<\/b>/g, '</strong>') + '</p>\n';
				}
				
				if(result.url.length > 55)
				{
					theHTML += '<p class="url">' + result.url.substring(0, 55) + '&hellip;</p>\n';
				}
				else
				{
					theHTML += '<p class="url">' + result.url + '</p>\n';
				}
			}
			$j('#results_content').html(theHTML);
		}
	}
	
	
	
	
	function JSONSearch(query)
	{
		try
		{
			var searchresult = search('www.generousgems.com', query, 15, 1);
			var items = searchresult.items;
			if(!items)return;
			if(items.length < 1)
			{
				$j('#json_results_content').html('<h4>Sorry, no items matched your search</h4>');
			}
			else
			{
				var theHTML = '<h3>Item search results:</h3>\n';;
				for (var i = 0; i < items.length; i++) {
					var item = items[i];
					theHTML += '<h4><a href="' + item.viewUrl + '">' + item.description + '</a></h4>\n';
					theHTML += '<dl>';
					theHTML += '<dt><a href="' + item.viewUrl + '"><img src="http://secure.ultracart.com/thumbs/' + item.thumbnailUrl + '" alt="' + item.description + '" /></a></dt>\n';
					theHTML += '<dd>';
					if(item.cost > 0.00)
					{
						theHTML += '<strong class="price">Price: $' + item.cost + '</strong>';
					}
					else
					{
						theHTML += '<strong class="price"><small><a href="' + item.addToCartUrl + '">Add to Cart to view price</a></small></strong>';
					}
					theHTML += '<strong class="view_add"><a href="' + item.viewUrl + '"><img src="/assets/images/search/search_view_item.png" alt="View Item" /></a> <a href="' + item.addToCartUrl + '"><img src="/assets/images/search/search_add_to_cart.png" alt="Add to Cart" /></a></strong>';
					theHTML += '</dd>';
					theHTML += '</dl>';
				}
				$j('#json_results_content').html(theHTML);
			}
		}
		catch(e)
		{
			// item search failed
		}
	}
	
	
	
	// Dropdown menus
	
	$j('.header h5').hover(function(){
		child = $j('#' + $j(this).attr('class'));
		$j(this).mouseleave(function(){
			child.hide();
		});
		child.show();
		child.mouseenter(function(){
			child.show();
		});	
		child.mouseleave(function(){
			child.hide();
		});		
	});
	
	
	
	// Item Tabs
	
	$j('.tabs .menu a').click(function(event){
		event.preventDefault();
		$j('.tab').hide();
		$j('#' + $j(this).attr('href').slice(1)).show();
		$j('.tabs .menu a').removeClass('active');
		$j(this).attr('class', 'active');
	});
	
	$j('.tabs .menu a:first').click();
	
	
	
	// hash to tag based on 'id' attribute (creates scrolling animation)
	
	$j('[href^=#]').click(function (event) {
		if($j(this).parent().parent().parent().attr('class') != 'tabs' && $j(this).attr('href').slice(1))
		{
			$j('html,body').animate({scrollTop: $j('[id=' + this.hash.slice(1) +']').offset().top}, 1000);
		}
	});
	
	
	
	// Widen the content area to match the size of the cart form
	
	if($j('.ucShoppingBackground'))
	{
		
		if($j('.ucShoppingBackground').outerWidth() > 840)
		{
			var cartFormWidth = $j('.ucShoppingBackground').outerWidth();
			$j('.content_outer').width(cartFormWidth + 50);
			$j('.content_outer').css({'background' : 'transparent'});
			$j('.content_inner').width(cartFormWidth + 40);
			$j('.content_inner').css({'background' : 'transparent'})
			$j('.content').width(cartFormWidth + 20);
			$j('.cart').width(cartFormWidth);
		}
	}




}); // Ends Ready Function