var link = "";
function checkZip(){
	var zip = document.getElementById("zipField").value
	var curHREF = document.getElementById("sort_distance").href;
	if(zip.length<5){
		//Check If IE7
		var brVer = navigator.appVersion
		var ie7 = brVer.search(/MSIE 7/);
		if(ie7>0){
			document.getElementById("sort_distance").href="#";
			alert("please enter a 5 digit zip code");
			exit;
		}
		
		var objLink = document.getElementById("sort_distance").href;
		if(objLink.substring(objLink.length-1,objLink.length)!="#"){
			link = document.getElementById("sort_distance").href;
		}
		
		var y = window.prompt("please enter a zip code",'');
		if(y!='' && y!=null && y.length==5){
			document.getElementById("sort_distance").href="#";
			var zipPos = link.search(/&zip=false/);
			if(zipPos>0){
				window.location = link.substring(0,zipPos)+"&zip="+y;
			}
			else{
				window.location = link+"&zip="+y;
			}
		}
		else{
			document.getElementById("sort_distance").href="#";
		}
	}
	else{
		endPos = curHREF.search(/&zip=false/)
		if(endPos>0){
			document.getElementById("sort_distance").href=curHREF.substring(0,endPos)+"&zip="+zip;
		}
		document.getElementById("sort_distance").href=curHREF.substring(0,curHREF.length-5)+zip;
	}
}

function updateDistance(e,obj){
	var characterCode; //literal character code will be stored in this variable

	if(e && e.which){ //if which property of event object is supported (NN4)
		e = e;
		characterCode = e.which; //character code is contained in NN4's which property
	}
	
	if(characterCode == 13){ //if generated character code is equal to ascii 13 (if enter key)var link = document.getElementById("distance").href;
		if(obj.value.length==5){
			if(link==""){
				link = window.location;
			}
			link = String(link);
			var zipPos = link.search(/&zip=/);
			var extraParams='';
			if(link.length>zipPos+10){
				extraParams += link.substring(zipPos+10,link.length);
			}
			if(zipPos>0){
				document.getElementById("sort_distance").href = link.substring(0,zipPos)+"&zip="+obj.value+extraParams;
			}
			else{
				document.getElementById("sort_distance").href = link+"&sort=distance&sort_d=ASC&zip="+obj.value;
			}
			window.location = document.getElementById("sort_distance").href;
		}
	}
	if (window.event && window.event.keyCode == 13){
		if(link==""){
			link = window.location;
		}
		link = String(link);
		var zipPos = link.search(/&zip=/);
		if(zipPos>0){
			document.getElementById("sort_distance").href = link.substring(0,zipPos)+"&zip="+obj.value;
		}
		else{
			document.getElementById("sort_distance").href = link+"&sort=distance&sort_d=ASC&zip="+obj.value;
		}
		window.location = document.getElementById("sort_distance").href;
	}
}
function zipFocus(obj){
	obj.value='';
	obj.style.color = "#000000";
}
function zipBlur(obj){
	if (obj.value.length<5){
		obj.value = "zip";
		obj.style.color="#999999";
	}
}

function refineSearch(){
	var filters = new Array("condition","price_low","price_high","handed","listing_type");
	for(i=0; i<filters.length; i++){
		var key = getKey.indexOf(filters[i]);
		var element = document.getElementById( filters[i] );
		if(element){
			if(key>=0){
				if(element.value.length>0){
					getData[key] = element.value;
				}
				else{
					getData[key]="";
					getKey[key]="";
				}
			}
			else{
				if (element.type=="checkbox"){
					if(element.checked == true){
						getKey[getKey.length]=filters[i];
						getData[getData.length] = element.value;
					}
				}
				else{
					getKey[getKey.length]=filters[i];
					getData[getData.length] = element.value;
				}
			}
		}
		else{
			alert(key);
			getData[key] = "";
		}
	}
	var filterStr = constructUrl();
	window.location = curURL.substring(0,first)+"?"+filterStr.substring(0,filterStr.length-1);
}

function constructUrl(){
	var filterStr = '';
	for(i=0;i<getKey.length;i++){
		if(getData[i].length>0 ){
			if(getKey[i]=="search"){
				filterStr += getKey[i]+"="+getData[i]+"&";
			}
			else{
				filterStr += getKey[i]+"="+getData[i]+"&";
			}
		}
		else{
			if(getKey[i]=="search"){
				filterStr += getKey[i]+"="+getData[i]+"&";
			}
		}
	}
	return filterStr;
}

var getKey = new Array();
var getData = new Array();
var curURL = "";
curURL+=window.location;
var first = curURL.indexOf("?");
var get = curURL.substring(first+1,curURL.length);
var entries = get.split("&");
for(i=0;i<entries.length;i++){
	var entry = entries[i].split("=");
	getKey[getKey.length] = entry[0];
	getData[getData.length] = entry[1];
}

function pageJump(obj){
	var key = getKey.indexOf("page");
	if(key>0){
		getData[key]=obj.value;
	}
	else{
		getKey[getKey.length] = "page";
		getData[getData.length] = obj.value;
	}
	var getStr = constructUrl();
	window.location = curURL.substring(0,first)+"?"+getStr.substring(0,getStr.length-1);
}
function perPage(obj){
	var key = getKey.indexOf("per_page");
	if(key>0){
		getData[key]=obj.value;
	}
	else{
		getKey[getKey.length] = "per_page";
		getData[getData.length] = obj.value;
	}
	var getStr = constructUrl();
	window.location = curURL.substring(0,first)+"?"+getStr.substring(0,getStr.length-1);
}
function sortOrder(obj){
	var key = getKey.indexOf("sort");
	if(key>0){
		getData[key]=obj.value;
	}
	else{
		getKey[getKey.length] = "sort";
		getData[getData.length] = obj.value;
	}
	var getStr = constructUrl();
	window.location = curURL.substring(0,first)+"?"+getStr.substring(0,getStr.length-1);
}
var enlargeIDS = new Array();
var enlargeINTS = new Array();
var enlargeOPEN = new Array();
var enlargeON = new Array();
var loadedImages = new Array();
var lastID = null;
var enlargeDebug = false;
function getIndex(picID){
	for(i=0;i<enlargeIDS.length;i++){
		if(enlargeIDS[i] = picID){
			return i;
		}
	}
}
function clearAllOpen(id){
	lastID = id
	if(1==0){
		for(i=0;i<enlargeIDS.length;i++){
			if(id!=enlargeIDS[i]){
				window.clearInterval(enlargeINTS[i]);
				enlargeINTS[i] = undefined;
				if(document.getElementById('enlarge_'+enlargeIDS[i]).style.display!='none' || 1==1){
					//hide thumb
					Effect.Fade(
						'enlarge_'+enlargeIDS[i],
						{
							duration:0
						}
					);
				}
			}
		}
	}
	if(1==1){
		while(enlargeOPEN.length>0){
			
			picID = enlargeOPEN.shift();
			if(picID!=undefined && picID!=false && picID!=id){
				//clear intervals
				window.clearInterval(enlargeINTS[getIndex(picID)]);
				enlargeINTS[getIndex(picID)] = undefined;
				
				document.getElementById('enlarge_'+picID).style.display='none';
			}
		}
	}
	
}
function closeEnlarge(picID){
	if(enlargeINTS[getIndex(picID)]!=undefined){
		window.clearInterval(enlargeINTS[getIndex(picID)]);
		enlargeINTS[getIndex(picID)] = undefined;
	}
	enlargeINTS[getIndex(picID)] = window.setInterval('closeAction('+picID+')',750);
}
function closeAction(picID){
	if(enlargeON[getIndex(picID)] == undefined || enlargeON[getIndex(picID)] == false){
		if(enlargeOPEN[getIndex(picID)]!=undefined && enlargeOPEN[getIndex(picID)]!=false){
			window.clearInterval(enlargeINTS[getIndex(picID)]);
			enlargeINTS[getIndex(picID)] = undefined;
			enlargeOPEN[getIndex(picID)]=false;
			
			document.getElementById('enlarge_'+picID).style.display='none';
		}
	}
}
function openEnlarge(picID){
	enlargeIDS[enlargeIDS.length]=picID;
	
	clearAllOpen(picID);
	
	if(enlargeINTS[getIndex(picID)]!=undefined){
		window.clearInterval(enlargeINTS[getIndex(picID)]);
		enlargeINTS[getIndex(picID)] = undefined;
	}
	enlargeOPEN[getIndex(picID)]=picID;
	
	document.getElementById('enlarge_'+picID).style.display='block';
	
}
function onEnlarge(picID){
	enlargeON[getIndex(picID)] = picID;
}
function offEnlarge(picID){
	enlargeON[getIndex(picID)] = false;
}
