
function initializeBadges()
{
	$('.fbln').mouseover(function(){
		showBadge($(this),'This frame is brand-new to our stores. Get it while it\'s hot!','new');
		$(this).addClass('frame-badge-label-new-trans');
		$(this).removeClass('frame-badge-label-new');
		$('.shownBadge').css('left','-57px');
	});
	
	$('.fbll').mouseover(function(){
		showBadge($(this),'This frame has limited availability. Call or visit your local store to see if we have it.','limited');
		$(this).addClass('frame-badge-label-limited-trans');
		$(this).removeClass('frame-badge-label-limited');
		$('.shownBadge').css('left','-50px');
	});
	
	$('.fble').mouseover(function(){
	
		showBadge($(this),'This frame can only be found at Pearle Vision&#174; stores. Lucky us!','exclusive');
		$(this).addClass('frame-badge-label-exclusive-trans');
		$(this).removeClass('frame-badge-label-exclusive');
		$('.shownBadge').css('left','-40px');
	});
	
	$('.fbln2').mouseover(function(){
		showBadge($(this),'This frame is brand-new to our stores. Get it while it\'s hot!','new');
		$(this).addClass('frame-badge-label-new-large-trans');
		$(this).removeClass('frame-badge-label-new-large');		
		$('.shownBadge').css('left','-57px');
	});
	
	$('.fbll2').mouseover(function(){
		showBadge($(this),'This frame has limited availability. Call or visit your local store to see if we have it.','limited');
		$(this).addClass('frame-badge-label-limited-large-trans');
		$(this).removeClass('frame-badge-label-limited-large');		
		$('.shownBadge').css('left','-50px');
	});
	
	$('.fble2').mouseover(function(){
	
		showBadge($(this),'This frame can only be found at Pearle Vision&#174; stores. Lucky us!','exclusive');
		$(this).addClass('frame-badge-label-exclusive-large-trans');
		$(this).removeClass('frame-badge-label-exclusive-large');		
		$('.shownBadge').css('left','-40px');
	});
	
	$('.fbln').mouseleave(function(){
		removeBadge($(this));
		$(this).removeClass('frame-badge-label-new-trans');
		$(this).addClass('frame-badge-label-new');
	});
	
	$('.fbll').mouseleave(function(){
		removeBadge($(this));
		$(this).removeClass('frame-badge-label-limited-trans');
		$(this).addClass('frame-badge-label-limited');
	});
	
	$('.fble').mouseleave(function(){
		removeBadge($(this));
		$(this).removeClass('frame-badge-label-exclusive-trans');
		$(this).addClass('frame-badge-label-exclusive');
	});
	
	$('.fbln2').mouseleave(function(){
		removeBadge($(this));
		$(this).removeClass('frame-badge-label-new-large-trans');
		$(this).addClass('frame-badge-label-new-large');
	});
	
	$('.fbll2').mouseleave(function(){
		removeBadge($(this));
		$(this).removeClass('frame-badge-label-limited-large-trans');
		$(this).addClass('frame-badge-label-limited-large');
	});
	
	$('.fble2').mouseleave(function(){
		removeBadge($(this));
		$(this).removeClass('frame-badge-label-exclusive-large-trans');
		$(this).addClass('frame-badge-label-exclusive-large');	
	});

}

function showBadge(element,txt,type){
	
	$(element).parent().append("<div class='badgeBkgnd shownBadge'><div class='badgeTxt'>"+txt+"</div></div>");
	
}

function removeBadge(element){

	$('.shownBadge').remove();
}


function getAjaxResults() {
	var postUrl = '.action?color='+$('#filter_colors').val()+'&brand='+$('#filter_brand').val()+'&shape='+$('#filter_shapes').val()+'&price='+$('#filter_price').val()+'&department='+$('#department').val()+'&material='+$('#filter_materials').val();
	//alert($('#filterForm_totFilter').is(':checked'));	
	
	if(document.getElementById('filterForm_totFilter').checked == true)
	//if($('#filterForm_totFilter').is(':checked'))
	//if($('#filterForm_totFilter').attr('checked'))
	{
		postUrl = postUrl + '&totFilter=true';
	}	
	

	$.ajax({
  		url:'/eyewear/frames-catalog-ajax'+postUrl,
  		success: function(data) {
    	$('#resultListRep').html(data);
    		$('#topPaging').html($('#bottomPaging').html());
  		}
	});
}

function updateDepartment(ele,dept){
	document.getElementById("department").value = dept;
	$('#brandbar ul li.dspI a').removeClass("nounderline");
	$(ele).addClass("nounderline");	
	
	updateCriteria();
}

function updateCriteria() {
	var filterset = new FilterSet();
	//filterset.styles = document.getElementById("filter_styles"); 

	filterset.colors = document.getElementById("filter_colors");
	filterset.materials = document.getElementById("filter_materials");
	filterset.shapes = document.getElementById("filter_shapes");
	filterset.brands = document.getElementById("filter_brand");
	filterset.prices = document.getElementById("filter_price");
	//var brand = document.getElementById("brand").value;

	var dept = document.getElementById("department").value;

	var uri = "NarrowCriteria.action?department=" + escape(dept);
	filterset.adjustFilterData(uri);
	getAjaxResults();
}

function FilterSet(styles, colors, materials, shapes, brands, prices) {
	this.styles = styles;
	this.colors = colors;
	this.materials = materials;
	this.shapes = shapes;
	this.brands = brands;
	this.prices = prices;
}

function FilterSet(){
	
}

FilterSet.prototype.adjustFilterData = function(baseURL) {
	var url = baseURL /* + this.getOptionParam("style", this.styles) no styles now  */  
		+ this.getOptionParam("color", this.colors)
		+ this.getOptionParam("material", this.materials)
		+ this.getOptionParam("shape", this.shapes)
		+ this.getOptionParam("price", this.prices)
		+ this.getOptionParam("brand", this.brands);
	requestData(url, this);
}

FilterSet.prototype.handleResponseText = function(xmlReq, responseText) {
	//alert("Received response " + responseText);
	var criteria = JSON.parse(responseText);
	//this.setDropdownOptions("Styles", this.styles, criteria.styles);
	this.setDropdownOptions("Colors", this.colors, criteria.colors);
	this.setDropdownOptions("Materials", this.materials, criteria.materials);
	this.setDropdownOptions("Shapes", this.shapes, criteria.shapes);
	this.setDropdownOptions("Prices", this.prices, criteria.prices);
	//this.setDropdownOptions("Brands", this.brands, criteria.brands);
}

FilterSet.prototype.setDropdownOptions = function(name, dropdown, valarray) {
	var selectedval = dropdown.options[dropdown.selectedIndex].value;
	dropdown.options.length = 0
	valarray.sort(); 
	
	this.addOption(dropdown, "-All " + name + "-", "")
	
	for (var i = 0; i < valarray.length; i++) {
		this.addOption(dropdown, valarray[i].value, valarray[i].key)

		if (valarray[i].key == selectedval) {
			dropdown.selectedIndex = i+1;
		}
	}
}

FilterSet.prototype.addOption = function(dropdown, text, value) {
	var oOption = document.createElement("option");
	oOption.text=text;
	oOption.value=value;
	if (navigator.appName == "Microsoft Internet Explorer") {
		dropdown.add(oOption)
	} else {
		dropdown.add(oOption, null)
	}
}

FilterSet.prototype.handleHttpFailure = function(xmlReq) {
	//alert("HTTP call failed: " + xmlReq.status);
}

FilterSet.prototype.getOptionParam = function(optionName, selectElement) {
	if (selectElement == undefined){
		return "";
	}
	if (selectElement.value.length > 0 && selectElement.value != "-1") {
		return "&" + optionName + "=" + encodeURIComponent(selectElement.value);
	} 
	return "";
}
