//	jQuery Compatibility Method
var $j = jQuery.noConflict();


try
{
	initializeCheckoutAPI("soft", "www.softclothing.net", "/cgi-bin/UCCheckoutAPIJSON");
}
catch(e)
{
	// no search
}

$j("document").ready(function(){





	//	hash to tag based on 'id' attribute (creates scrolling animation)
	
	$j('[href^=#]').click(function (event){
		if(!($j(this).attr('rel') && $j(this).attr('rel') == 'tab'))
		{
			event.preventDefault();
			if($j(this).attr('href').slice(1))
			{
				$j('html,body').animate({scrollTop: $j('[id=' + this.hash.slice(1) +']').offset().top}, 1000);
			}
		}
	});
	
	
	
	
	
	//	popup links
	
	$j('[rel=popup]').click(function (event){
		event.preventDefault();
		window.open($j(this).attr('href'), '', 'width=800, height=650, scrollbars=yes');
	});
	
	
	
	
	
	//	popup sizing chart on item page
	
	$j('#sizing_chart_link').click(function (event){
		event.preventDefault();
		window.open($j(this).attr('href'), '', 'width=900, height=600, scrollbars=yes');
	});
	
	
	
	
	
	// image gallery
	
	$j('.gallery a').click(function(event){
		event.preventDefault();
		window.open($j(this).attr('href'), '', 'width=903,height=837,resizable=1,scrollbars=1');
	});
	
	$j('.main a').click(function(event){
		event.preventDefault();
		window.open($j(this).attr('href'), '', 'width=903,height=837,resizable=1,scrollbars=1');
	});
	
	
	
	
	
	// item form validation
	
	$j('#item_order').submit(function(event){
		var errors = validate($j(this));
		if(errors && errors.length > 0)
		{
			event.preventDefault();
			display_errors(errors);
		}
		/*
		else
		{
			// remove this 'else' after store is live.
			event.preventDefault();
			errors = new Array();
			errors[0] = 'Orders cannot yet be placed. Thank you for your patience.';
			display_errors(errors);
		}
		*/
	});
	
	function validate(form_object)
	{
		var errors = new Array();
		form_object.find('.required').each(function(){
			if(!$j(this).val())
			{
				errors.push('Please choose: ' + $j(this).attr('title'));
			}
		});
		return errors;
	}
	
	function display_errors(errors)
	{
		var errors_list = '';
		for(i = 0; i < errors.length; i ++)
		{
			errors_list += '<li>' + errors[i] + '</li>';
		}
		$j('#errors').show(250);
		$j('#errors ol').html(errors_list);
	}
	
	
	
	
	
	//	tabs
	
	$j('.tab').hide();
	$j('.tabs .menu a:first').attr('class', 'active');
	$j('.tab:first').show();
	
	$j('[rel=tab]').click(function (event) {
		event.preventDefault();
		$j('.tab').hide();
		$j('.tabs .menu a').removeAttr('class');
		$j(this).attr('class', 'active');
		$j('[id=' + this.hash.slice(1) +']').show();
	});
	
	
	
	
	
	// social bookmarks
	
	bookmarks = [
		{
			'title': 'Digg',
			'url': 'http://digg.com/submit?phase=2&url=' + document.location + '&title=' + document.title
		},
		
		{
			'title': 'Twitter',
			'url': 'http://twitter.com/home/?status=Checking out ' + document.location + 'by @softclothing'
		},
		
		{
			'title': 'Facebook',
			'url': 'http://www.facebook.com/share.php?u=' + document.location + '&h=' + document.title
		},
		
		{
			'title': 'Stumble',
			'url': 'http://www.stumbleupon.com/submit?url=' + document.location + '&title=' + document.title
		},
		
		{
			'title': 'Reddit',
			'url': 'http://reddit.com/submit?url=' + document.location + '&title=' + document.title
		},
		
		{
			'title': 'Del.icio.us',
			'url': 'http://del.icio.us/post?url=' + document.location + '&title=' + document.title
		},
		
		{
			'title': 'YahooBuzz',
			'url': 'http://buzz.yahoo.com/submit?submitUrl=' + document.location
		}
	];
	
	$j('[rel=bookmark]').click(function(event){
		event.preventDefault();
		for(i = 0; i < bookmarks.length; i ++)
		{
			if(bookmarks[i]['title'].toLowerCase() == $j(this).attr('title').toLowerCase())
			{
				window.open(bookmarks[i]['url'], '', 'height=500, width=800')
			}
		}
	});
	
	
	
	
	
	// search
	
	$j('[name=q]').val($j('[name=q]').attr('title'));
	
	$j('[name=q]').focus(function(){
		if($j(this).val() == $j(this).attr('title'))
		{
			$j(this).val('');
		}
	});
	
	$j('[name=q]').blur(function(){
		if($j(this).val() == '')
		{
			$j(this).val($j(this).attr('title'));
		}
	});
	
	var timeout;
	var query = '';
	
	$j('[name=q]').keyup(function(event){
		try
		{
			window.clearTimeout(timeout);
		}
		catch(e)
		{
			// first time
		}
		var code = (event.keyCode ? event.keyCode : event.which);
		if(code == 13 && $j(this).val() != query)
		{
			// enter pressed
			do_search($j('[name=q]').val());
		}
		else if(code != (8) && code != (13) && code != (16) && code != (17) && code != (18) && code != (35) && code != (36) && code != (46))
		{
			timeout = window.setTimeout(do_search, 750, [$j(this).val()]);
		}
	});
	
	$j('.search button').click(function(){
		if($j('[name=q]').val() != $j('[name=q]').attr('title'))
		{
			do_search($j('[name=q]').val());
		}
		else
		{
			$j('[name=q]').focus();
		}
	});
	
	$j('#end_search').click(function(event){
		end_search();
	});
	
	function do_search(q)
	{
		try
		{
			query = q;
			var searchresult = search('www.softclothing.net', query, 8, 1);
			var items = searchresult['items'];
			display_search_results(items);
		}
		catch(e)
		{
			//
		}
	}
	
	function display_search_results(items)
	{
		if(!items)
		{
			return;
		}
		$j('.search_results').show();
		if(items.length > 0)
		{
			var SEARCH_RESULT_HTML = '';
			for(var i = 0; i < items.length; i++)
			{
				var item = items[i];
				if(i == 0 || i == 4)
				{
					SEARCH_RESULT_HTML += '<div class="result_col">';
				}
				SEARCH_RESULT_HTML += '<div class="box">';
				SEARCH_RESULT_HTML += '<div class="image">';
				SEARCH_RESULT_HTML += '<a href="' + item.viewUrl + '"><img src="http://secure.ultracart.com/thumbs/' + item.thumbnailUrl + '" alt="' + item.description + '" /></a>\n';
				SEARCH_RESULT_HTML += '</div><!--/image-->';
				SEARCH_RESULT_HTML += '<div class="desc">';
				SEARCH_RESULT_HTML += '<h4><a href="' + item.viewUrl + '">' + item.description + '</a></h4>\n';
				SEARCH_RESULT_HTML += '<p>Price: $' + item.cost + '</p>\n';
				SEARCH_RESULT_HTML += '<p><a href="' + item.viewUrl + '">view item</a></p>\n';
				SEARCH_RESULT_HTML += '</div><!--/desc-->';
				if(i == 0 || i == 4)
				{
					SEARCH_RESULT_HTML += '</div><!--/result_col-->';
				}
				item = null;
			}
			$j('#search_results_content').html(SEARCH_RESULT_HTML);
		}
		else
		{
			$j('#search_results_content').html('<h2>Sorry, no results found. Please try another search.</h2>');
		}
	}
	
	function end_search()
	{
		$j('#search_results_content').html('');
		$j('.search_results').hide();
	}





}); // document.ready