function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

function changeTab(attr){
	document.getElementById('popular').style.display = "none";
	document.getElementById('emotional').style.display = "none";
	document.getElementById('physical').style.display = "none";
	document.getElementById(attr).style.display = "block";
	document.getElementById("popularLink").className = "tab_desel";
	document.getElementById("emotionalLink").className = "tab_desel";
	document.getElementById("physicalLink").className = "tab_desel";
	document.getElementById(attr+"Link").className = "tab_sel";
}
//javascript:showAbout('size');" onmouseout="javascript:hideAbout('size');"></a>
//_about
function showAbout(varname){
	var pos = findPos(document.getElementById(varname));
	document.getElementById(varname+"_about").style.left = (pos[0]+180)+"px";
	document.getElementById(varname+"_about").style.top = (pos[1]-30)+"px";
	document.getElementById(varname+"_about").style.display = "block";
}
function hideAbout(varname){	
	document.getElementById(varname+"_about").style.display = "none";
}

function dm_rollover(target, id){
	//alert(target+ " " +id);
	var pos = findPos(document.getElementById("dog_match_link_"+id));
	var pos2 = findPos(document.getElementById("dog_match_right"));

var screenSize = getScreenSize();
	if(document.getElementById("dog_match_img_"+id).width){
		if(document.getElementById("dog_match_img_"+id).width == 55){
			var tileOffset = 56;
		} else if(document.getElementById("dog_match_img_"+id).width == 130){
			var tileOffset = 131;
		} else if(document.getElementById("dog_match_img_"+id).width == 230){
			var tileOffset = 231;
		}
	}
	
	var offsetLeft = (pos[0]-pos2[0]+tileOffset);
	if((pos[0]+tileOffset+210) > screenSize[0]){
		offsetLeft = (pos[0]-pos2[0]-(179));
	}
		var offsetY = pos[1]-pos2[1];
	
//		var offsetLeft = (pos[0]+tileOffset);
//		var offsetY = pos[1]-tileOffset-4;
	document.getElementById("dm_rollover_"+id).style.top = offsetY+"px";
	document.getElementById("dm_rollover_"+id).style.left = offsetLeft+"px";
	document.getElementById("dm_rollover_"+id).style.display = "block";

}

function dm_rollout(target, id){
	//alert(target+ " " +id);
	document.getElementById("dm_rollover_"+id).style.display = "none";
}

function changeImgSize(){
	var value = document.dm_options_form.image_size.value;
	tileSize = value;
	sortDM();
}

function sortDM(){
	var str="tileSize="+tileSize;
	for(i=0; i<window.attrList.length; i++){
		var minRange = window[window.attrList[i]+"Range"].minRange;
		var maxRange = window[window.attrList[i]+"Range"].maxRange;
		if(minRange == 1 && maxRange == 5){} else {
			str+="&"+window.attrList[i]+"Min="+minRange;
			str+="&"+window.attrList[i]+"Max="+maxRange;
		}
	}
		var so = new SWFObject("gfx/dog_match_loader.swf","preloader","100%","100%", "8", "#FFFFFF", true);
		so.addParam('wmode', 'transparent');
		so.addVariable("msgType", "loading");
		so.write("dog_match_loader");
		xmlhttpPost(str, "php/dm_results.php");	
}

function xmlhttpPost(strURL, submitTo) {
	strURL = submitTo+"?"+strURL;
	
	var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
	self.xmlHttpReq.open('POST', strURL, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() {
		if(self.xmlHttpReq.readyState==4){
            updatepage(self.xmlHttpReq.responseText);
		}
    }

    self.xmlHttpReq.send(strURL);
}

function updatepage(stri){
//	alert(stri);
	document.getElementById("dog_match_loader").style.display = "none";
	document.getElementById('dog_tiles').innerHTML = stri;
}





window.onresize = function(){
	resizeMatch();
}

window.onload = function(){
	if(document.getElementById('dog_match_wrap')){
	resizeMatch();
	tileSize = document.dm_options_form.image_size.value;
	checkPresets();
   }
}

function getScreenSize(){
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  return [myWidth,myHeight];
}
function resizeMatch(){
	var screenSize = getScreenSize();
  	myWidth = screenSize[0];
	document.getElementById("dog_match_right").style.width = (myWidth-273)+"px";
	document.getElementById("dog_match_loader").style.width = (myWidth-243)+"px";
	document.getElementById("dog_match_wrap").style.width = (myWidth-47)+"px";	
	document.getElementById("dog_match_header").style.width = (myWidth-47)+"px";
}

sliderWid = 120;
thumbWid = 9;
lastX = 0;
dragging = false;
window.attrList = [];
activeSlideType = "";
resortOnLoad = false;

function SlideObject(target, attr, preMin, preMax){
	this.target = target;
	this.attr = attr;
	window[attr+"Range"] = new Object();
	
/*	if(checkCookies === true){
	var tmpVal = readCookie(attr);
	if(tmpVal){
		var values = tmpVal.split(",");
		if(parseInt(values[0])){preMin = values[0];}
		if(parseInt(values[1])){preMax = values[1];}
	}
	}
*/
	if(preMin == ""){window[attr+"Range"].minRange = 1;} else {window[attr+"Range"].minRange = preMin;}
	if(preMax == ""){window[attr+"Range"].maxRange = 5;} else {window[attr+"Range"].maxRange = preMax;}
	window.attrList.push(attr);
	document.getElementById(target).innerHTML = "<span id=\""+attr+"selbg\" class=\"sliderselbg\"></span><span id=\""+attr+"thumb1\" class=\"sliderthumb\"><img src=\"gfx/slider_thumb_left.jpg\" width=\"9\" height=\"14\" /></span><span id=\""+attr+"thumb2\" class=\"sliderthumb2\"><img src=\"gfx/slider_thumb_right.jpg\" width=\"9\" height=\"14\" /></span>";
	document.getElementById(attr+"thumb1").style.left = "1px";
	document.getElementById(attr+"thumb2").style.left = "113px";	
	
	document.getElementById(attr+"thumb1").onmousedown = function(){
		dragging = true;
		activeSlide = attr;
		activeSlideType = 1;
		activeParent = target;
		minRange = 1;
		document.getElementById("dog_match_loader").style.display = "block";
		var so = new SWFObject("gfx/dog_match_loader.swf","preloader","100%","100%", "8", "#FFFFFF", true);
		so.addParam('wmode', 'transparent');
		so.addVariable("msgType", "release");
		so.write("dog_match_loader");
		
		if(document.getElementById(attr+"thumb2").style.left){
			maxRange = cssToInt(document.getElementById(attr+"thumb2").style.left)-thumbWid-1;
		} else {
			maxRange = 106;	
		}
		initTracking();
		return false;
	}
	document.getElementById(attr+"thumb2").onmousedown = function(){
		dragging = true;
		activeSlide = attr;
		activeSlideType = 2;
		activeParent = target;
		
		document.getElementById("dog_match_loader").style.display = "block";
		var so = new SWFObject("gfx/dog_match_loader.swf","preloader","100%","100%", "8", "#FFFFFF", true);
		so.addParam('wmode', 'transparent');
		so.addVariable("msgType", "release");
		so.write("dog_match_loader");
		
		if(document.getElementById(attr+"thumb1").style.left){
			minRange = cssToInt(document.getElementById(attr+"thumb1").style.left)+thumbWid+1;
		} else {
			minRange = 11;	
		}
		maxRange = 113;

		initTracking();
		return false;
	}
	document.getElementById(target).onmousedown = function(){
		if(dragging === false){
		activeSlide = attr;
		activeParent = target;
		document.onmouseup = trackBar;
		return false;
		}
	}
	var tmpMin = 0;
	var tmpMax = 5;
	
	createCookie(attr, preMin+","+preMax,1);
	
	if(parseInt(preMin)){
		tmpMin = preMin;
	}
	if(parseInt(preMax)){
		tmpMax = preMax;	
	}
	if(tmpMin > 1 || tmpMax < 5){
		activeSlide = attr;
		resortOnLoad = true;
		lockSliders(preMin, preMax);	
	}
	
}
function checkPresets(){
	if(resortOnLoad === true){
		document.getElementById("initial_load_msg").style.display = "none";
		sortDM();
	}
}
function cssToInt(thestyle){
	var endPos = thestyle.indexOf('px');
	var returnNum = parseInt(thestyle.substring(0,endPos));
	return returnNum;
}

function trackBar(e){
	var posActive = getPos(e);
	var slide1Pos = cssToInt(document.getElementById(activeSlide+"thumb1").style.left);
	var slide2Pos = cssToInt(document.getElementById(activeSlide+"thumb2").style.left);
	var compare1 = posActive - slide1Pos;
	var compare2 = slide2Pos - posActive;

	if(compare1 < compare2){
		activeSlideType = 1;
	} else {
		activeSlideType = 2;	
	}
	lastX = posActive;
	cancelTrack();
	document.onmouseup = null;
}
function initTracking(){
	document.onmousemove = trackMouse;
	document.onmouseup = cancelTrack;
}


function lockSliders(toMin, toMax){
	if(activeSlideType == ""){ var tmpActiveSlideType = 1; var flag = true; lastX = "f";} else { var tmpActiveSlideType = activeSlideType;  var flag = false;}
	
if(tmpActiveSlideType == 1){
		if(lastX < 14 || toMin == 1){
			document.getElementById(activeSlide+"thumb"+tmpActiveSlideType).style.left = "1px";
			window[activeSlide+"Range"].minRange = 1;
		} else if(lastX < 39 || toMin == 2){
			document.getElementById(activeSlide+"thumb"+tmpActiveSlideType).style.left = "26px";
			window[activeSlide+"Range"].minRange = 2;	
		} else if(lastX < 65 || toMin == 3){
			document.getElementById(activeSlide+"thumb"+tmpActiveSlideType).style.left = "52px";
			window[activeSlide+"Range"].minRange = 3;
		} else if(lastX < 91 || toMin == 4){
			document.getElementById(activeSlide+"thumb"+tmpActiveSlideType).style.left = "78px";
			window[activeSlide+"Range"].minRange = 4;
		} else {
			document.getElementById(activeSlide+"thumb"+tmpActiveSlideType).style.left = "103px";
			window[activeSlide+"Range"].minRange = 5;
		}
} 
if(tmpActiveSlideType == 2 || flag === true){
		if(flag === true){tmpActiveSlideType = 2;}
		if(lastX > 100 || toMax == 5){
			document.getElementById(activeSlide+"thumb"+tmpActiveSlideType).style.left = "113px";
			window[activeSlide+"Range"].maxRange = 5;
		} else if(lastX > 75 || toMax == 4){
			document.getElementById(activeSlide+"thumb"+tmpActiveSlideType).style.left = "88px";
			window[activeSlide+"Range"].maxRange = 4;
		} else if(lastX > 49 || toMax == 3){
			document.getElementById(activeSlide+"thumb"+tmpActiveSlideType).style.left = "62px";
			window[activeSlide+"Range"].maxRange = 3;
		} else if(lastX > 23 || toMax == 2){
			document.getElementById(activeSlide+"thumb"+tmpActiveSlideType).style.left = "36px";
			window[activeSlide+"Range"].maxRange = 2;
		} else {
			document.getElementById(activeSlide+"thumb"+tmpActiveSlideType).style.left = "11px";
			window[activeSlide+"Range"].maxRange = 1;
		}
	}
	alignSel();
	createCookie(activeSlide,window[activeSlide+"Range"].minRange+","+window[activeSlide+"Range"].maxRange,1);
}

function cancelTrack(){
	lockSliders("false", "false");
	//document.getElementById('debug').innerHTML = activeSlide+"Range = "+window[activeSlide+"Range"].minRange+", "+window[activeSlide+"Range"].maxRange;
		///TMP FOR DEMO
		document.getElementById("initial_load_msg").style.display = "none";
		sortDM();
		//showRndDogs();

	
	dragging = false;
	document.onmousemove = null;
	document.onmouseup = null;
}


function getPos(e){
	var posx = 0;
	var posy = 0;
	if (!e) var e = window.event;
	if (e.pageX || e.pageY) 	{
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY) 	{
		posx = e.clientX + document.body.scrollLeft
			+ document.documentElement.scrollLeft;
		posy = e.clientY + document.body.scrollTop
			+ document.documentElement.scrollTop;
	}
	var posAry = findPos(document.getElementById(activeParent));
	posx-=posAry[0];
	return posx-(Math.round(thumbWid/2));	
}
function trackMouse(e) {
	var posActive = getPos(e);
	
	if(posActive>=minRange && posActive <= maxRange){
		var asnX = posActive;
	} else if(posActive<=minRange){
		var asnX = minRange;
	} else {
		var asnX = maxRange;
	}
		document.getElementById(activeSlide+"thumb"+activeSlideType).style.left = asnX+'px';
		alignSel();
		lastX = asnX;
		return false;
//	document.getElementById(activeSlide+"selbg").style.left = 
//	document.getElementById('debug').innerHTML = cssToInt(document.getElementById(activeSlide+"thumb1").style.left)+","+thumbWid+"<BR>"+eval(cssToInt(document.getElementById(activeSlide+"thumb1").style.left)+thumbWid)+'px';	
	//document.getElementById('debug').innerHTML += temp +" = "+posx+" ,";	

	// posx and posy contain the mouse position relative to the document
	// Do something with this information
}

function alignSel(){
		var selWid = cssToInt(document.getElementById(activeSlide+"thumb2").style.left) - eval(cssToInt(document.getElementById(activeSlide+"thumb1").style.left)+thumbWid);
		document.getElementById(activeSlide+"selbg").style.left = eval(cssToInt(document.getElementById(activeSlide+"thumb1").style.left)+thumbWid)+'px';
		document.getElementById(activeSlide+"selbg").style.width = eval(selWid+1)+'px';	
}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
			} 
		while (obj = obj.offsetParent);
	return [curleft,curtop];
	}
}

