
//DEPENDS:
//jQuery (obviously)
//wcscompare (for comparitor)

function strip(name) {
	return name.replace(/\s+/g, '').replace(/-/g, '');
}

String.prototype.trim = function() {
	a = this.replace(/^\s+/, '');
	return a.replace(/\s+$/, '');
};

//var definedSliders = new Array();

//////////////////////////////
//  Lister Page functions
//////////////////////////////

function submitCompareForm() {
	var compareForm = document.getElementById("CompareForm");
	if (compareForm == null) om_submit("compare");
	else compareForm.submit(); 
}

function om_submit (action, args) {
	var $form = $("#SearchForm");
	for (arg in args) {
		//if exists, update
		$existing = $form.find("input[@name='" + arg + "']");
		if ($existing.length != 0) {
			$existing.attr("value", args[arg]);
		} else { //else add
			var element = document.createElement("input");
			element.type = "hidden";
			element.name = arg;
			element.value = args[arg];
			$form.append(element);
		}
	}
	$form.find("input[@name='action']").attr("value", action);
	comparitor.save(); //save product comparison selections
	$form.submit();
	return false;
}

$(document).ready(function() {

	$("span.addcompare input[@type='checkbox']")
		.click(function() {
			if (comparitor.count() >= 5 && this.checked) {
				alert('You have selected more than 5 products. Please reduce the number of products to be compared');
				return false;  //disallow check
			}
			comparitor.setElement(this.id, this.checked);
			return true;
		})
		.each(function() {
			if (comparitor.isChecked(this.id) && comparitor.count() <= 5) {
				this.checked = true;					
			} else {
				this.checked = false;
			}
		});
	
	$(".compare input[type='submit']")
		.click(function() {
			if (comparitor.count() < 2) {
				alert('Please select at least two products to compare');
				return false;
			}
			else if (comparitor.count() > 5){
				alert("You have selected more than 5 products. Please reduce the number of products to be compared");
				return false;
			} else {
				submitCompareForm();
			}
		});
	
	$(".sortOptions, .perPageOptions").change(function() {
		$(this).parents("form").submit();
		return false;
	});
		
	$(".addtocompare").show();
	
	$("div.left-col-links ul").each(function() {
		if ($(this).find("li").size() > 5) {
			$(this).find("li:gt(4)").hide();
			
			var option = $(this).prev().text().trim().toLowerCase();
			$(this).append('<li class="showmemorelink"><a title="Click to see more ' 
							+ option 
							+ ' options" href="#catattributes">see more ' 
							+ option 
							+ ' options</a></li>');
		}
	});

	$("li.showmemorelink").click(function() {
		$(this).prevAll().show();
		$(this).remove();
	});

});


//////////////////////////////
//  Left Nav functions
//////////////////////////////

function initLeftNav(isSearch) {
	if ($("#layeredrefinecol").length) { // might not exist
		var tootipPos = { 
			x:$("#layeredrefinecol").offset().left + 40,
			positionTooltip:function (event,x,y) {
				$('div#tooltip').css({"top":$(event.target).offset().top + 25, "left":x});
			}
		};
		$(".facetTooltip").tooltip(tootipPos);
		$(".facetToolTipText").tooltip(tootipPos);
	}

}

