function log(msg){  if ( window.console )  console.log.call(console, msg);  }

var filterCats = new Object();
var propsPerPage = 15;
var items = [];
var states = [];
var luxCats = [];
var totalPages;
var locCats = [];
var totalProps;
var property;
var properties = [];
var propertiesShow = [];
var initCount = true;

$(function() {
	
	// Sidebar Functions
	$('.filter-expand').live('click', function(event) {
		event.preventDefault();
		$(this).closest('#filter-sidebar').find('.filter-inner-wrapper').slideDown('slow');
		$(this).closest('#filter-sidebar').find('.filter-group').addClass('active');
	});
	
	$('.filter-collapse').live('click', function(event) {
		event.preventDefault();
		$(this).closest('#filter-sidebar').find('.filter-inner-wrapper').slideUp('slow');
		$(this).closest('#filter-sidebar').find('.filter-group').removeClass('active');
	});
	
	$('.filter-reset').live('click', function(event) {
		$('div#loading-wrapper').show();
		$('div#loading-overlay').show('fast',function(){
			filterCats = new Object();
				$('.property-listing').find('li').addClass('active');		
			$('#filter-sidebar').find('#filter-state').children("option:selected").removeAttr("selected");
			$('#filter-luxury li').find('input[type="checkbox"]').each(function(){
				$(this).removeAttr("checked"); 
			});

			$('#filter-property-style').find('li.active').removeClass('active');
			$('#filter-arch-style').find('li.active').removeClass('active');
			
			totalPropsHandler();
			resetFilterCount();
			$('#filter-sidebar').find('.filter-inner-wrapper').slideUp('slow');
			$('#filter-sidebar').find('.filter-group').removeClass('active');
		});	
		return false;

	});

	$('.filter-group .filter-details').live('click', function(event) {
		event.preventDefault();
		if ( $(this).parent().hasClass('active') ) {
			$(this).closest('.filter-group').find('.filter-inner-wrapper').slideUp('slow');
			$(this).closest('.filter-group').removeClass('active');
		} else {			
			$(this).closest('.filter-group').find('.filter-inner-wrapper').slideDown('slow');
			$(this).closest('.filter-group:not("active")').addClass('active');
		}
	});
	
	$('.filter-group h3').live('click', function(event) {
		event.preventDefault();
		$(this).closest('.filter-group').find('.filter-inner-wrapper').slideDown('slow');
		$(this).closest('.filter-group:not("active")').addClass('active');
	});
	

	$('.filter-group.active h3').live('click', function(event) {
		event.preventDefault();
		$(this).closest('.filter-group').find('.filter-inner-wrapper').slideUp('slow');
		$(this).closest('.filter-group').removeClass('active');
	});
		
	$('.previous-page').click(function() {
		var current = $('.property-listing').data('currentPage');
		goToPageResults(--current, totalPages);
		if($(this).parent().hasClass('pages-wrapper-bottom')) {
			window.scrollTo(0,$("#pages-wrapper").offset().top);
		}
		return false;
	});

	$('.next-page').click(function() {
		var current = $('.property-listing').data('currentPage');
		goToPageResults(++current, totalPages);
		if($(this).parent().hasClass('pages-wrapper-bottom')) {
			window.scrollTo(0,$("#pages-wrapper").offset().top);
		}
		return false;
	});
	
	$('a#show-package-link').click(function() { 
		var curProp = $('#property-filter');
		$('div#loading-wrapper').show();
		$('div#loading-overlay').show('fast',function(){
			var theFilter = '';
			$(curProp).removeAttr('class').addClass(theFilter);
			$('#property-filter option:eq(0)').attr('selected', 'selected');
			packageFilter('property', theFilter);
			totalPropsHandler();
		});				
		$('a#show-package-link').hide();
		return false;								   
	});

	$.get('http://www.historichotels.org/en/xml/property.php', function(data) { 	
		properties[0] = 'initialize';										   
		$('item', data).each(function(i) {
			items[i] = {};
			$(this).children().each(function() {
				if (this.tagName == 'category'){		
					var tempCat = $(this).text().split(",");
					var catName = [];					
					if (tempCat.length > 1) { 
						var tempCatArray = tempCat;
						catName = [];
	
						for (x in tempCatArray){								
							var tempCatName = tempCatArray[x].toString();;
							tempCatName = tempCatName.replace(/\W/g, "");
							tempCatName = tempCatName.toLowerCase(); 
							catName.push(tempCatName);										
						}
					} else { 
						catName = []
						var tempCatName = $(this).text().replace(/\W/g, "");
						tempCatName = tempCatName.toLowerCase(); 
						catName.push(tempCatName);	
					}
					items[i][this.tagName] = catName;
				} else if (this.tagName == 'state'){
					var tempStateName = $(this).text().replace(/\W/g, "").toUpperCase();
					states.push(tempStateName);
					items[i][this.tagName] = $(this).text();		
				}else 
					items[i][this.tagName] = $(this).text();		
					
			});			
		});			
		
		$('.property-listing').each(function() {
			var q = window.location.search.substring(1);
			var totalProps;							
			if (q!='') {				
				$('div#loading-wrapper').show();
				$('div#loading-overlay').show('fast',function(){
					var query = parseQuery(q);				
					if ( $('.property-listing').attr('id')=='property-listing' ) {
						$('.property-listing li').removeClass('active').hide();
					}else {
						$('.property-listing div.property-filter').removeClass('active').hide();	
					}
					if($('#deals-results-wrapper').hasClass('deals-results-wrapper-loc'))
						filterQueryState(query);
					else
						filterQuery(query);
					totalPropsHandler();
					$('#search-results').show();
				});
			} else {
				totalProps = $('.property-listing').find('.active').length;
				if (totalProps > 0) 
					$('div#no-results').hide();			
				else
					$('div#no-results').show();	
				totalPages = Math.ceil( totalProps/propsPerPage);				
				createNavigationResults(totalPages);
				setDataAndEventsResults(totalPages);										
			}		
			
		});
	});


	// Filter Actions		
	$('.results #filter-state').change(function() {
		var curState = this;
		$('div#loading-wrapper').show();
		$('div#loading-overlay').show('fast',function(){
			var theFilter = $(curState).attr('value');
			if ( $('.property-listing').attr('id')=='property-listing' ) {
						$('.property-listing li').removeClass('active').hide();
					}else {
						$('.property-listing div.property-filter').removeClass('active').hide();	
					}
			filterItems('state', theFilter);
			totalPropsHandler();
		});	
	});
	
	$('#filter-luxury .checkbox').click(function() {
		var curCheckbox = this;
		var isChecked = false;	
		$('div#loading-wrapper').show();
		$('div#loading-overlay').show('fast',function(){
			var theFilter = $(curCheckbox).attr('value');
			if ($(curCheckbox).is(":checked")!=true) {
				luxCats.splice(luxCats.indexOf(theFilter.toLowerCase()), 1);
				theFilter='';
			}					
			if ( $('.property-listing').attr('id')=='property-listing' ) {
				$('.property-listing li').removeClass('active').hide();
			}else {
				$('.property-listing div.property-filter').removeClass('active').hide();	
			}
			filterItems('luxury_level', theFilter);			
			totalPropsHandler();
		});
	});
	
	$('.filter-link a').click(function() {
		var curProp = this;
		var filtType = $(this).parent().parent().attr('id');
		if(filtType=='filter-property-style')
			filtCat = 'property_style';			
		else if (filtType=='filter-arch-style')
			filtCat = 'arch_style';					
		
		$('div#loading-wrapper').show();
		$('div#loading-overlay').show('fast',function(){
			$(curProp).closest('ul').find('li').removeAttr('class');
			$(curProp).closest('li').addClass('active');
			var theFilter = $(curProp).attr('filter');
			if ( $('.property-listing').attr('id')=='property-listing' ) {
						$('.property-listing li').removeClass('active').hide();
					}else {
						$('.property-listing div.property-filter').removeClass('active').hide();	
					}
			filterItems(filtCat,theFilter);			
			totalPropsHandler();
		});
		
		return false;
	});
	
	$('#property-filter').change(function(event) {
		$('a#show-package-link').show();
		var curState = this;
		var filterCat = $(this).attr('class');
		$('div#loading-wrapper').show();
		$('div#loading-overlay').show('fast',function(){
			var theFilter = $(curState).attr('value');
			packageFilter(filterCat, theFilter);
			totalPropsHandler();			
		});								  										  
		
	});
	
							
	// kill 1 page "pagination" for property packages etc
	$('.results-pagination').each(function() {
		var count = $(this).find('li').length;
		if (count < 2) {
			$(this).find('li').hide();
			$(this).closest('#experiences-results-wrapper').removeClass('multi');
		} 
	});
			
});

/* --------------------------------------------------------------------------------------------------------------------------------------------------------------------------- */

function createNavigationResults(totalPages) {
	var navHTML = '';
	for (var i = 0; i < totalPages; i++) {
		navHTML += '<li class="'+(i+1)+'"><a href="#pages-wrapper">'+(i+1)+'</a></li>';
	}
	if (totalPages >1) 
		$('.results-pagination').html(navHTML);
	else
		$('.results-pagination').html('');
	$('.prev-page').hide();
	
	goToPageResults(1, totalPages);
	
	// need to rewrite to use links instead of select
	$('.resultsNav li a').live('click',function() {
		if($(this).parent().parent().parent().hasClass('pages-wrapper-bottom')) {
		  window.scrollTo(0,$("#pages-wrapper").offset().top); 	
		}
		goToPageResults($(this).parent().attr('class'), totalPages);
		return false;
	});
	
}

function setDataAndEventsResults(totalPages) {
	$('.property-listing').data('currentPage',1);
	if ( $('.property-listing').attr('id')=='property-listing' )
		$('.property-listing li.active:gt(' + (propsPerPage-1) + ')').hide();
	else
		$('.property-listing div.property-filter.active:gt(' + (propsPerPage-1) + ')').hide();
}

function goToPageResults(pageNumber, totalPages) {
	var currentPage = parseInt(pageNumber);
	pageNumber = pageNumber.toString();
	pageNumber = pageNumber.replace(" current","");
	var propsNoFeatured;
	$('.results-pagination li').removeClass('current');			
	if (pageNumber == 1) $('.previous-page').hide();	else $('.previous-page').show();					
	if ((pageNumber == totalPages) || (totalPages==0)) $('.next-page').hide(); else $('.next-page').show();
	$('.results-pagination li.'+pageNumber).addClass('current');
	$('.property-listing').data('currentPage', pageNumber);
	$('#goTo').val(pageNumber);
	
	//Each special offers page may have varying numbers of featured offers
	//Each page should still show a maximum number of offers are orignially specified in propsPerPage
	//All featured offers should remain visible on all pages.
	var featuredCount = $('.featured').length;
	if(pageNumber > 1){
		var propsNoFeatured = propsPerPage - featuredCount
	} else { 
		propsNoFeatured = propsPerPage;
	}
	var from = (pageNumber - 1) * propsNoFeatured;
	var to = from + (propsNoFeatured - 1);
	if(pageNumber > 1){
		from = from  + featuredCount;
		to = to + featuredCount;
	}
	
	if ( $('.property-listing').attr('id')=='property-listing' ) {
		$('.property-listing li.active').show();
		$('.property-listing li.active:lt(' + (from) + ')').not(".featured").hide();
		$('.property-listing li.active:gt(' + (to) + ')').not(".featured").hide();
		$('.results-pagination li').show();
		$('.results-pagination').each(function(){
			$('li:lt(' + (currentPage-3) + ')',this).hide();
			$('li:gt(' + (currentPage+1) + ')',this).hide();
		});
		
	} else {
		$('.property-listing div.property-filter.active').show();
		$('.property-listing div.property-filter.active:lt(' + (from) + ')').not(".featured").hide();
		$('.property-listing div.property-filter.active:gt(' + (to) + ')').not(".featured").hide();
		$('.results-pagination li').show();
		$('.results-pagination').each(function(){
			$('li:lt(' + (currentPage-3) + ')',this).hide();
			$('li:gt(' + (currentPage+1) + ')',this).hide();
		});;
	}
	updateFilterCount();
}

function totalPropsHandler() {
	totalProps = $('.property-listing').find('.active').length;
	if (totalProps > 0) {
		$('div#num-results').show();
		$('span#result-count').html(totalProps);
		$('div#no-results').hide();
	} else {
		$('div#num-results').hide();
		$('div#no-results').show();
		$('.next-page').hide();
		$('.prev-page').hide();
	}
	totalPages = Math.ceil( totalProps/propsPerPage);
	createNavigationResults(totalPages);
	setDataAndEventsResults(totalPages);
	updateLink();
	$('div#loading-overlay').hide();
	$('div#loading-wrapper').hide();
}

function updateFilterCount() {
	if(properties[0]!='initialize') {
		$('.filter-group').each(function() {
			if ( $(this).children().children().hasClass('filter-count') ) {
				var filterCount = $(this).children().children('.filter-count');
				var filterType = $(this).children().children('.filter-count').attr('id').substr(7).replace('-','_');
				if ( !$(filterCount).children('li').hasClass('active') || initCount == true ) {
					$(filterCount).children('li').each(function(){ 
						var updateID = $(this).children().attr('filter');
						var updateSpan = $(this).children().children('span');
						var count = 0;
						$.each(propertiesShow,function(k){											   												   
							var curPropertyItem = propertiesShow[k];					
							if(items[curPropertyItem][filterType]==updateID) {
								count++;	
							}
						});
						$(updateSpan).html(count);
					}); 
				}
			}
		});
	}
	initCount = false;
}

function resetFilterCount() {
		$('.filter-group').each(function() {
			if ( $(this).children().children().hasClass('filter-count') ) {
				var filterCount = $(this).children().children('.filter-count');
				var filterType = $(this).children().children('.filter-count').attr('id').substr(7).replace('-','_');
				$(filterCount).children('li').each(function(){ 
					var updateID = $(this).children().attr('filter');
					var updateSpan = $(this).children().children('span');
					var count = 0;
					$.each(items,function(k){
						var curPropertyItem = items[k];
						var arrStyles = curPropertyItem[filterType].split(",");
						var styleMatch = false;
						$.each(arrStyles, function(key, value) { 
							if ( value == updateID ){
								styleMatch = true;
							}
						});
						if(styleMatch==true){
							count++;
						}
						/*if(items[curPropertyItem][filterType]==updateID) {
							count++;	
						}*/
					});
					$(updateSpan).html(count);
				}); 
			}
		});
	initCount = false;
}


/* --------------------------------------------------------------------------------------------------------------------------------------------------------------------------- */

// live filter functions
function filterItems(newfiltercat, theFilter) {
	var dup = false;
	properties = [];
	switch(newfiltercat) {
		case 'luxury_level':
			theFilter = theFilter.toLowerCase();	
			if(theFilter!='')
				luxCats.push(theFilter);
			luxCats = $.uniqueArray(luxCats);	
			filterCats[newfiltercat] = luxCats;
			break;
		default:
			filterCats[newfiltercat] = theFilter;
			break;	
	}
	$.each(items, function(j) { 
		var itemmatched=true;
		var matcheditems = [];
		property = j;
		// Loops through filterCats array to see if the current item matches the current filter category
		$.each(filterCats, function(k,val1) {
			var tempMatch = items[j][k];				
			if (tempMatch != undefined) {
				tempMatch = tempMatch.toLowerCase();
				if(k=='luxury_level') {
					var luxMatch=false;
					for(x in luxCats) {
						if ( tempMatch == luxCats[x] )
							luxMatch=true;
					}	
					if( (luxMatch==true) || (luxCats.length==0) )
						matcheditems.push(true);
					else
						matcheditems.push(false);			
				} else if (k=='state') {		
					var tempCityState = items[j]['city'].toLowerCase() + ', ' + items[j]['state_name'].toLowerCase();
					//log(items[j]['state'].toLowerCase() + ' ' + [val1]);
					var tempVal1 = [val1].toString();
					if ( tempVal1.length==2) {
						if ( (items[j]['state'].toLowerCase()==tempVal1.toLowerCase()) ) {
							matcheditems.push(true);	
						} else {
							matcheditems.push(false);											
						}
					} else {
						if ( (items[j]['state'].toLowerCase()==[val1]) || (tempCityState.indexOf([val1])!= -1) || (items[j]['state_name'].toLowerCase().indexOf([val1])!= -1) || (items[j]['city'].toLowerCase().indexOf([val1])!= -1) || (items[j]['property_name'].toLowerCase().indexOf([val1])!= -1) || ([val1] == '') ) {
							matcheditems.push(true);	
						} else {
							matcheditems.push(false);											
						}
					}
				} else if ( tempMatch == [val1] || [val1] == '' ){
					matcheditems.push(true);
				} else {
					matcheditems.push(false);			
				}
			} else {
				matcheditems.push(false);
			}
		})
		properties[j]=matcheditems;
	});	
	matchItems();
}

function filterQuery(query) {	
	var locFilter = query['property'];
	if(locFilter != undefined) {
		locFilter = locFilter.replace(/\+/g, " ");	
		
		if (locFilter == 'Hotel or Location')
			locFilter = '';
		locFilter = locFilter.toLowerCase();
	} else {
		if( query['state'] != undefined )
			locFilter = query['state'];
	}
	filterCats['state'] = locFilter;	
	var luxFilter = query['luxury'];
	var luxFiltCat=[];
	if((luxFilter!='') && (luxFilter!= undefined)) {
		luxFiltCat = luxFilter.split(',');
		for (x in luxFiltCat) {
			luxCats.push(luxFiltCat[x]);
			luxCats = $.uniqueArray(luxCats);				
		}
		filterCats['luxury_level'] = luxCats;	
	}
	var archFilter = query['architecture'];
	if((archFilter!='') && (archFilter != undefined))
		filterCats['arch_style'] = archFilter;
	var propStyleFilter = query['propstyle'];
	if((propStyleFilter!='') && (propStyleFilter != undefined))
		filterCats['property_style'] = propStyleFilter;

	$.each(items, function(j) { 
		var itemmatched=true;
		var matcheditems = [];
		property = j;
		// Loops through filterCats array to see if the current item matches the current filter category
		$.each(filterCats, function(k,val1) {
			var tempMatch = items[j][k];	
			if (tempMatch != undefined) {
				tempMatch = tempMatch.toLowerCase();
				if(k=='luxury_level') {
					var luxMatch=false;
					for(x in luxCats) {
						if ( tempMatch == luxCats[x] )
							luxMatch=true;
					}	
					if( (luxMatch==true) || (luxCats.length==0) )
						matcheditems.push(true);
					else
						matcheditems.push(false);			
				} else if (k=='state') {
					var tempCityState = items[j]['city'].toLowerCase() + ', ' + items[j]['state_name'].toLowerCase();
					var tempCityStateName = items[j]['city'].toLowerCase() + ', ' + items[j]['state'].toLowerCase();
					//log(items[j]['state'].toLowerCase() + ' ' + [val1]);
					var tempVal1 = [val1].toString();
					if ( tempVal1.length==2) {
						if ( (items[j]['state'].toLowerCase()==[val1]) ) {
							matcheditems.push(true);	
						} else {
							matcheditems.push(false);											
						}
					} else {
						if ( (items[j]['state'].toLowerCase()==[val1]) || (tempCityState.indexOf([val1])!= -1) || (tempCityStateName.indexOf([val1])!= -1) || (items[j]['state_name'].toLowerCase().indexOf([val1])!= -1) || (items[j]['city'].toLowerCase().indexOf([val1])!= -1) || (items[j]['property_name'].toLowerCase().indexOf([val1])!= -1) || ([val1] == '') ) {
							matcheditems.push(true);	
						} else {
							matcheditems.push(false);											
						}
					}
				} else if ( tempMatch == [val1] || [val1] == '' ){
					matcheditems.push(true);
				} else {
					matcheditems.push(false);			
				}
			} else {
				matcheditems.push(false);
			}
		});
		properties[j]=matcheditems;
	});	
	
	matchItems();
	
	if( (locFilter != '') && (locFilter!=undefined) ) {
		$('#filter-sidebar #filter-state').parent('.filter-inner-wrapper').show();
		$('#filter-sidebar #filter-state').parent().parent('.filter-group').addClass('active');	
	}
	if( (luxFilter != '') && (luxFilter!=undefined) ) {
		$('#filter-sidebar #filter-luxury').parent('.filter-inner-wrapper').show();
		$('#filter-sidebar #filter-luxury').parent().parent('.filter-group').addClass('active');
		for(x in luxCats) {
			if(luxFiltCat[x]!=undefined) {
				var testsub = luxFiltCat[x].toString();
				testsub = testsub.substring(0,4);
				if ( testsub!='func') {		
					$('ul#filter-luxury').children('li.'+luxCats[x]).children('input').attr('checked','checked');
				}
			}
		}
	}
	if( (propStyleFilter != '') && (propStyleFilter!=undefined) ) {
		$('#filter-sidebar #filter-property-style').parent('.filter-inner-wrapper').show();
		$('#filter-sidebar #filter-property-style').parent().parent('.filter-group').addClass('active');
		$('ul#filter-property-style').children('li[filter*="'+propStyleFilter+'"]').addClass('active');
	}
	if( (archFilter != '') && (archFilter!=undefined) ) {
		$('#filter-sidebar #filter-arch-style').parent('.filter-inner-wrapper').show();
		$('#filter-sidebar #filter-arch-style').parent().parent('.filter-group').addClass('active');
		$('ul#filter-arch-style').children('li[filter*="'+archFilter+'"]').addClass('active');
	}			
}

function matchItems() {
	propertiesShow = [];
	$.each(properties, function(j) { 
		matcheditems=properties[j];
		var itemmatched=true;
		for (x in matcheditems) {
			if(matcheditems[x]==false)
				itemmatched=false;			
		}	
		if(itemmatched) {
			propertiesShow.push(j);				
		}
	});
	matchItemsShow();
}

function matchItemsShow() {	
	if(propertiesShow.length ==items.length) {
		if ( $('.property-listing').attr('id')=='property-listing' ) {
				$('.property-listing li').addClass('active');
		} else {
				$('.property-listing div.property-filter').addClass('active');	
		}
	} else {
		$.each(propertiesShow, function(j) { 
			if ( $('.property-listing').attr('id')=='property-listing' ) {
					$('.property-listing li').eq(propertiesShow[j]).addClass('active');
			} else {
					$('.property-listing div.property-filter:eq('+propertiesShow[j]+')').addClass('active');	
			}
		});
	}
}


function filterQueryState(query) {
	var locFilter = query['state'];
	$('.property-filter').each(function() { 
		if( (locFilter != '') && (locFilter!=undefined) ) {
			if ( ($(this).hasClass(locFilter))) {				
				$(this).addClass('active');
				$(this).show();
			} else {
				$(this).removeClass('active');
				$(this).hide();
			}
		}
	});	
}

function packageFilter(newfilterid, theFilter) {
	$('#property-filter-wrapper div.property-filter').each(function() {
		if ( !($(this).hasClass(theFilter)) && theFilter!='' ) {
			$(this).removeClass('active');
			$(this).hide();
		} else {
			$(this).addClass('active');
			$(this).show();	
		}
	});		
}

function updateLink() {
	var listURL = '/hotels-resorts/results.php';
	var mapURL ='/hotels-resorts/';
	var url = '';
	$.each(filterCats, function(k,val1) { 
		var newParam = '';
		switch(k) {
			case 'state':
				if ( (filterCats['city']==undefined) || (filterCats['city']=='') )
					newParam = 'property='+[val1];
				else
					newParam = 'property='+filterCats['city']+', '+[val1];
					
				break;
			case 'luxury_level':						
				var luxString = [val1].toString();
				if (luxString.indexOf('function')>0)
					luxString = luxString.substr(0,luxString.indexOf('function'));
				newParam = 'luxury='+luxString;
				break;
			case 'property_style':
				newParam = 'propstyle='+[val1];
				break;
			case 'arch_style':
				newParam = 'architecture='+[val1];
				break;
			default:
				break;
		}
		url += '&'+newParam;
	});
	if(url!='') {
		url = url.replace(/ /g,"+");	
		url = url.replace(/,/g,"%2C");
		listURL += '?' + url;
		mapURL += '?' + url;
	}
	$('li#results-list a').attr('href',listURL);
	$('li#results-map a').attr('href',mapURL);		
}


/* --------------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
function parseQuery(str) {
	str=str.replace('%C2%AE','');
	var vars = str.split("&");
	var pairs = {};
	for (var i=0;i<vars.length;i++) {
		var p = vars[i].split("=");
		pairs[p[0]] = this.urlDecode(p[1]);
	}
	return pairs;
}

function urlDecode(encodedString) {
	var output = encodedString;
	var binVal, thisString;
	var myregexp = /(%[^%]{2})/;
	while ((match = myregexp.exec(output)) !== null && match.length > 1 && match[1] !== '') {
		binVal = parseInt(match[1].substr(1),16);
		thisString = String.fromCharCode(binVal);
		output = output.replace(match[1], thisString);
	}
	return output;
}

if (!Array.indexOf) {
  Array.prototype.indexOf = function (obj, start) {
    for (var i = (start || 0); i < this.length; i++) {
      if (this[i] == obj) {
        return i;
      }
    }
    return -1;
  }
}

jQuery.expr[':'].Contains = function(a, i, m) { 
  return jQuery(a).text().toUpperCase().indexOf(m[3].toUpperCase()) >= 0; 
};

