$(document).ready(function() {
	$('#creditcard-payment-form').submit(function(){
		if($(this).hasClass('submitting')){
			return false;	
		}
		
		$(this).addClass('submitting');
	});
	
	$('.clicknclear').click(function() {
		if (!($(this).attr('rel'))) {
			$(this).attr('rel', $(this).val());
			$(this).val('');
		}
	}).blur(function() {
		if ($(this).val().length < 1) {
			$(this).val($(this).attr('rel'));
		}
	});

  $('.submit').change(function (e) {
    var elem = $('#' + e.currentTarget.name);
    var form = elem.parents('form:first');
    form.submit();
  });

  if (typeof jQuery.fn.facebox == 'function') {
    $('a.enlarge').facebox();
  }
	
  if (typeof jQuery.fn.popupwindow == 'function') {
	$('a.popup').attr('rel', 'popupwindow').popupwindow({
		popupwindow: {height: 600, width:750, createnew:0, resizable:0, toolbar:0, scrollbars:1, center:1}
	});
  }

  if (typeof jQuery.fn.autocomplete == 'function') {
	// Overwrite render item
	$.ui.autocomplete.prototype._renderItem = function(ul, item) {
		var rx = new RegExp('\\b(' + this.term + ')', 'gi') ;
		var t = item.label.replace(rx, "<strong>" + '$1' + "</strong>");
		return $( "<li></li>" )
			.data( "item.autocomplete", item )
			.append( "<a>" + t + "</a>" )
			.appendTo( ul );
	};

	$('#search_q').autocomplete({
		source: '/service/item-suggest.php',
		minLength: 3,
		select: function(event, ui) {
			$('#search_q').val(ui.item.label);
			if (ui.item.id) {
				$('#search_item_id').val(ui.item.id);
				$('#search_item_id').parents('form:first').submit();
			}
		}
	});
  }
	
	
		/* TABS */
	//When page loads...
	$(".tab_content").hide(); //Hide all content
	if (location.hash && location.hash.match(/^#tab/)) {
		// Activate selected tab 
		var selectedTab = location.hash;
		// Selected tab link (add active to surrounding li) - <ul class="tabs"><li><a href="#tabX">My Tab</a></li></ul>
		$("ul.tabs li a[href='" + selectedTab + "']").parent().addClass("active").show();
		// Selected tab content
		$(selectedTab).addClass('active').show();
	} else {
		// Activate first tab
		$("ul.tabs li:first").addClass("active").show(); //Activate first tab
		$(".tab_content:first").show(); //Show first tab content
	}

	//On Click Event
	$("ul.tabs li").click(function() {
		$("ul.tabs li").removeClass("active"); //Remove any "active" class from other <li>
		$(this).addClass("active"); //Add "active" class to selected tab <li>
		$(".tab_content").hide(); //Hide all tab content 

		var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content
		$(activeTab).show().fadeIn('slow'); //Fade in the active ID content
		return false;
	});

  $(".tab-fire").click(function () {
  	var target = $(this).attr('href');

	// Activate the tab
  	$("ul.tabs li").has("a[href='" + target + "']").click();

	// Scroll to the destination
	var destination = $(target).offset().top;
	$("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination}, 1000, function() {
		window.location.hash = target;
	});
  	return false;
  });

	
	
});


// hover fix for IE 6

sfHover = function(){
	var sfEls = document.getElementById("nav").getElementsByTagName("LI");
	for(var i=0; i<sfEls.length; i++){
		sfEls[i].onmouseover=function(){
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function(){
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);

