
var httpRequest = false;
var searchTarget = "";
var searchText = "";
var isMSIE = /*@cc_on!@*/false;

json_parse=(function(){var at,ch,escapee={'"':'"','\'':'\'','\\':'\\','/':'/',b:'\b',f:'\f',n:'\n',r:'\r',t:'\t'},text,error=function(m){throw{name:'SyntaxError',message:m,at:at,text:text};},next=function(c){if(c&&c!==ch){error("Expected '"+c+"' instead of '"+ch+"'");}
ch=text.charAt(at);at+=1;return ch;},number=function(){var number,string='';if(ch==='-'){string='-';next('-');}
while(ch>='0'&&ch<='9'){string+=ch;next();}
if(ch==='.'){string+='.';while(next()&&ch>='0'&&ch<='9'){string+=ch;}}
if(ch==='e'||ch==='E'){string+=ch;next();if(ch==='-'||ch==='+'){string+=ch;next();}
while(ch>='0'&&ch<='9'){string+=ch;next();}}
number=+string;if(isNaN(number)){error("Bad number");}else{return number;}},string=function(){var hex,i,string='',uffff;if(ch==='"'){while(next()){if(ch==='"'){next();return string;}else if(ch==='\\'){next();if(ch==='u'){uffff=0;for(i=0;i<4;i+=1){hex=parseInt(next(),16);if(!isFinite(hex)){break;}
uffff=uffff*16+hex;}
string+=String.fromCharCode(uffff);}else if(typeof escapee[ch]==='string'){string+=escapee[ch];}else{break;}}else{string+=ch;}}}
error("Bad string");},white=function(){while(ch&&ch<=' '){next();}},word=function(){switch(ch){case't':next('t');next('r');next('u');next('e');return true;case'f':next('f');next('a');next('l');next('s');next('e');return false;case'n':next('n');next('u');next('l');next('l');return null;}
error("Unexpected '"+ch+"'");},value,array=function(){var array=[];if(ch==='['){next('[');white();if(ch===']'){next(']');return array;}
while(ch){array.push(value());white();if(ch===']'){next(']');return array;}
next(',');white();}}
error("Bad array");},object=function(){var key,object={};if(ch==='{'){next('{');white();if(ch==='}'){next('}');return object;}
while(ch){key=string();white();next(':');if(Object.hasOwnProperty.call(object,key)){error('Duplicate key "'+key+'"');}
object[key]=value();white();if(ch==='}'){next('}');return object;}
next(',');white();}}
error("Bad object");};value=function(){white();switch(ch){case'{':return object();case'[':return array();case'"':return string();case'-':return number();default:return ch>='0'&&ch<='9'?number():word();}};return function(source,reviver){var result;text=source;at=0;ch=' ';result=value();white();if(ch){error("Syntax error");}
return typeof reviver==='function'?(function walk(holder,key){var k,v,value=holder[key];if(value&&typeof value==='object'){for(k in value){if(Object.hasOwnProperty.call(value,k)){v=walk(value,k);if(v!==undefined){value[k]=v;}else{delete value[k];}}}}
return reviver.call(holder,key,value);}({'':result},'')):result;};}());

function C2AjaxRequest()
{
	var ajaxResponseHandler;
	var url;
	var operation;
	var parameters;
	var data;
	
	this.init = function(url,operation,parameters,ajaxResponseHandler,data)
	{
		this.url = url;
		this.operation = operation;
		this.parameters = parameters;
		this.ajaxResponseHandler = ajaxResponseHandler;
		this.data = data;
	}
	
	this.getUrl = function()
	{
		return this.url;
	}
	
	this.getOperation = function()
	{
		return this.operation;
	}
	
	this.getParamters = function()
	{
		return this.parameters;
	}
	
	this.getAjaxResponseHandler = function()
	{
		return this.ajaxResponseHandler;
	}
	
	this.getData = function()
	{
		return this.data;
	}
}

function C2Ajax() 
{
	var ajaxRequestId;
	var ajaxRequests;
	var self=this;

	this.init = function()
	{
		this.ajaxRequestId = 0;
		this.ajaxRequests = new Array();
	}

	this.makeRequest = function(url,operation,parameters,ajaxResponseHandler,data)
	{
		this.ajaxRequestId++;
		var ajaxRequest = new C2AjaxRequest;
		ajaxRequest.init(url,operation,parameters,ajaxResponseHandler,data);
		this.ajaxRequests[this.ajaxRequestId] = ajaxRequest;
		httpRequest = false;
		if (window.XMLHttpRequest) 
		{ // Mozilla, Safari,...
			httpRequest = new XMLHttpRequest();
			if (httpRequest.overrideMimeType) 
			{
				httpRequest.overrideMimeType('text/xml');
			}
		} 
		else if (window.ActiveXObject) 
		{ // IE
			try 
			{
				httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch (e) 
			{
				try 
				{
					httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
				} 
				catch (e) 
				{
				}
			}
		}
		if (!httpRequest) 
		{
			alert('An error has occured. Please refresh the page'); // Cannot create XMLHTTP instance
			return false;
		}
		httpRequest.onreadystatechange = this.handleResponse;
		if (parameters != '') parameters += '/';
		parameters += 'op='+operation+'/__='+encodeURIComponent((new Date()).getTime()) + '/ajaxRequestId='+ this.ajaxRequestId;
		httpRequest.open('GET', url + parameters, true);
		httpRequest.send(null);
	}
	
	this.handleResponse = function()
	{
		if (httpRequest.readyState == 4) 
		{
			if (httpRequest.status == 200) 
			{
				var result = httpRequest.responseText.toString();
				if (result)
				{	
					var resultObj = json_parse(result);
					if (resultObj)
					{
						var requestId = resultObj.ajaxRequestId;
						if (requestId && requestId>0)
						{
							var ajaxRequest = self.ajaxRequests[requestId];
							if (ajaxRequest)
							{
								var ajaxResponseHandler = ajaxRequest.getAjaxResponseHandler();
								if (ajaxResponseHandler) ajaxResponseHandler(resultObj,ajaxRequest);
							}
						}
					}
				}
			}
		}	
	}
}

var ajaxc = new C2Ajax();
ajaxc.init();

function searchFocus()
{
	document.searchForm.search.className="searchFocus";
	if (document.searchForm.search.value == searchText) document.searchForm.search.value="";
}

function searchBlur()
{
	document.searchForm.search.className="search";
	if (document.searchForm.search.value == "") document.searchForm.search.value=searchText;
}

function inputFocus(o,defaultText)
{
	var defaultText = (defaultText == null) ? "" : defaultText;
	var className = o.className;
	if (className.length <= 5 || className.substring(className.length-5) !== "Focus")
	{
		o.className = className + "Focus";
		if (o.value == defaultText) o.value="";
	}
}

function inputBlur(o,defaultText)
{
	var defaultText = (defaultText == null) ? "" : defaultText;
	var className = o.className;
	if (className.length > 5 && className.substring(className.length-5) == "Focus")
	{
		o.className = className.substring(0,className.length-5);
		if (o.value == "") o.value=defaultText;
	} 
}

function searchSubmit()
{
	if (document.searchForm.search.value == searchText) return false;
	searchQuery = document.searchForm.search.value;
	searchQuery = escape(searchQuery.replace(/\/|\\/g,""));	
	if (searchQuery != "") 
	{	
		window.location = searchTarget+searchQuery;	
	}
	return false;
}

function getCookie( check_name ) {
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f
	
	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		a_temp_cookie = a_all_cookies[i].split( '=' );		
		
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
	
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}	

function setCookie( name, value, expires, path, domain, secure ) 
{
	var today = new Date();
	today.setTime( today.getTime() );
	
	if ( expires )
	{
	expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	
	document.cookie = name + "=" +escape( value ) +
	( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
	( ( path ) ? ";path=" + path : "" ) + 
	( ( domain ) ? ";domain=" + domain : "" ) +
	( ( secure ) ? ";secure" : "" );
}

function addLoadEvent(func) 
{
  var oldonload = window.onload;
  if (typeof window.onload != 'function') 
  {
    window.onload = func;
  } else 
  {
    window.onload = function() 
    {
      if (oldonload) 
      {
        oldonload();
      }
      func();
    }
  }
}

function isMouseReallyOut(e,elementName)
{
	if (!e) var e = window.event;
	var tg = (window.event) ? e.srcElement : e.target;
	if (tg.id != elementName) return false;
	var reltg = (e.relatedTarget) ? e.relatedTarget : e.toElement;
	while (reltg != tg && reltg.nodeName != 'BODY')
		reltg= reltg.parentNode
	if (reltg== tg) return false;
	return true;
}

function return2br(st) 
{
	return st.replace(/(\r\n|[\r\n])/g, "<br />");
}

function addQuoteSlashes(st) 
{
	return st.replace(/(\")/g, "&quot;");
}

function showEmailForm(show)
{
	if (self.onShowEmailForm) onShowEmailForm(show);
	if (show == true)
	{
		var emailDiv = document.getElementById("emailDiv");
		emailDiv.style.display="block";
		centerControl(emailDiv,400,emailDiv.offsetHeight);
	} else
	{
		var emailDiv = document.getElementById("emailDiv");
		emailDiv.style.display = "none";
	}
}

function findPos(obj)
{
 var curleft = 0;
 var curtop = 0;
 if (obj.offsetParent)
 {
  while (obj.offsetParent)
  {
   curleft += obj.offsetLeft-obj.scrollLeft;
   curtop += obj.offsetTop-obj.scrollTop;
   var position='';
   if (obj.style&&obj.style.position) position=obj.style.position.toLowerCase();
   if ((position=='absolute')||(position=='relative')) break;
   while (obj.parentNode!=obj.offsetParent) {
    obj=obj.parentNode;
    curleft -= obj.scrollLeft;
    curtop -= obj.scrollTop;
   }
   obj = obj.offsetParent;
  }
 }
 else {
     if (obj.x)
      curleft += obj.x;
  if (obj.y)
      curtop += obj.y;
    }
 return {left:curleft,top:curtop};
}

function getEventPosition(e)
{
	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;
	}
	return {x:posx,y:posy};
}

function centerControl(ctrl, width, height)
{
   var divLeft, divTop;
   if(ctrl != null)
   {
     if(ctrl.style.display != 'none')
     {
       if(document.documentElement != null)
       {
       	 if (document.body.scrollTop > 0)
       	 {
	         divLeft = ((document.documentElement.clientWidth - width) / 2) + document.body.scrollLeft;
	         divTop = ((document.documentElement.clientHeight - height) / 2) + document.body.scrollTop;	     	
	     } else
	     {
	         divLeft = ((document.documentElement.clientWidth - width) / 2) + document.documentElement.scrollLeft;
	         divTop = ((document.documentElement.clientHeight - height) / 2) + document.documentElement.scrollTop;
	     }
       }
       else
       {
         divLeft = ((document.body.clientWidth - width) / 2) + document.body.scrollLeft;
         divTop = ((document.body.clientHeight - height) / 2) + document.body.scrollTop;
       }
       ctrl.style.left = divLeft + 'px';
       ctrl.style.top = divTop + 'px';
     }
   }
}
 
var draggedElement;
function startDrag(e){
	 // determine event object
	 if(!e){var e=window.event};
	 // determine target element
	 if(!e) return;
	 var targ=e.target?e.target:e.srcElement;
	 if(!targ) return;
	 var targParent=targ.parentNode;
	 if (!targParent) return;
	 draggedElement=null;
	 if (targParent.className=='stdPopupDiv') {
	 	draggedElement = targParent;
	 } else {
	 	targParent=targParent.parentNode;
	 	if (!targParent) return;
		if (targParent.className=='stdPopupDiv') {
			draggedElement = targParent;
		} else
		{
			return;
		}
	 }
	 var o=draggedElement;
	 // calculate event X,Y coordinates
    offsetX=e.clientX;
    offsetY=e.clientY;
	 // assign default values for top and left properties
	 if(!o.style.left){o.style.left='0px'};
	 if(!o.style.top){o.style.top='0px'};
	 // calculate integer values for top and left properties
    coordX=parseInt(o.style.left);
    coordY=parseInt(o.style.top);
    drag=true;
	 // move div element
	 document.onmousemove=dragDiv;
}
// continue dragging
function dragDiv(e){
	if(!drag){return};
	var o=draggedElement;
	if(!e){var e=window.event};
	var targ=e.target?e.target:e.srcElement;
	// move div element
	o.style.left=coordX+e.clientX-offsetX+'px';
	o.style.top=coordY+e.clientY-offsetY+'px';
	return false;
}
// stop dragging
function stopDrag(){
 drag=false;
}
function initDragEvents(){
 document.onmousedown=startDrag;
 document.onmouseup=stopDrag;
}
addLoadEvent(initDragEvents);

function changeGalleryStripBorderColor(divContainer,newBorderColor)
{
	var o=divContainer.childNodes[0];
	for (i=0;i<o.childNodes.length;i++) o.childNodes[i].style.borderColor=newBorderColor;
}

function textAreaCharCounter(textarea, maxChars,charLeftText)
{
	charLeft = maxChars-textarea.value.length;
	if (charLeft < 0) charLeft = 0;
	if (charLeftText) charLeftText.innerHTML= ''+charLeft;
	if(textarea.value.length <= maxChars) return;
	textarea.value = textarea.value.substr(0, maxChars);
}

function inputCharCounter(inputElement, maxChars,charLeftText)
{
	charLeft = maxChars-inputElement.value.length;
	if (charLeftText) charLeftText.innerHTML= ''+charLeft;
}
function validateColorValue(o,withNumberSign,defaultValue)
{
	var strPattern = /^[0-9a-f]{6}$/i; 
	if (withNumberSign) strPattern = /^#[0-9a-f]{6}$/i; 
	if (!strPattern.test(o.value)) o.value = defaultValue; 
}
function showSearchMenu()
{
	var searchMenu = document.getElementById('searchMenu');
	var searchPos = findPos(document.getElementById('searchMenuBtn'));
	searchMenu.style.display = "block";
	var searchMenuLeft = parseInt(searchPos.left);
	var searchMenuTop = parseInt(searchPos.top) + 20;
	searchMenu.style.left = searchMenuLeft + "px";
	searchMenu.style.top = searchMenuTop + "px";
}
function hideSearchMenu()
{	
	var searchMenu = document.getElementById('searchMenu');
	searchMenu.style.display = "none";
}
function searchMenuSelect(o)
{
	document.getElementById('searchMenuBtn').innerHTML = o.innerHTML;
	searchTarget = searchTargets[parseInt(o.id.substring(16))];
	hideSearchMenu();
}
function showMenu(menuId)
{
	var menu = document.getElementById(menuId+'Menu');
	var btnPos = findPos(document.getElementById(menuId+'MenuBtn'));
	menu.style.display = "block";
	var menuLeft = parseInt(btnPos.left);
	var menuTop = parseInt(btnPos.top) + 20;  // calculate 20 from height of item
	menu.style.left = menuLeft + "px";
	menu.style.top = menuTop + "px";
}
function hideMenu(menuId)
{	
	var menu = document.getElementById(menuId+'Menu');
	menu.style.display = "none";
}
function menuSelect(o,menuId)
{
	document.getElementById(menuId+'menuBtn').innerHTML = o.innerHTML;
	//onMenuSelect(parseInt(o.id.substring(10+strlen(menuId))));   -- strlen, pass onMenuSelect
	hideMenu(menuId);
}
function adjustFeaturedGalleryHeight(divId) 
{
	var t  = document.getElementById(divId+"Title");
	var tl = document.getElementById(divId+"TitleLink");
	if (t && tl && tl.offsetHeight>24) 
	{
		t.style.top = (-tl.offsetHeight - 12) + "px";
		t.style.height = (tl.offsetHeight) + "px";
	}
}
function adjustFeaturedGalleriesHeight(divId,divCount)
{
	for (divIter=0;divIter<divCount;divIter++) 
	{
		adjustFeaturedGalleryHeight(divId+divIter);
	}
}
function getButtonHtml(buttonText,isLarge,id,buttonClass,buttonStyle,isSubmit,onClick,additionalHtml,buttonLink)
{
	var buttonHtml = "<button " + ((id=="")?"":("id='"+id+"'"));
	buttonHtml += " class='"+((isLarge)?"largeButton":"stdButton")+" "+buttonClass+"'";
	buttonHtml += ((buttonStyle=="")?"":" style='"+buttonStyle+"'");
	buttonHtml += ((isSubmit)?" type='submit'":"");
	if (buttonLink!="") onClick += "window.location='"+buttonLink+"';return false;";
	buttonHtml += ((onClick=="")?"":" onclick=\""+onClick+"\"");
	buttonHtml += " "+additionalHtml+">"+buttonText+"</button>";	
	return buttonHtml;
}
function writeStdPopup(popupId,title,message,additionalHtml,withOkButton)
{
	var popupHtml = '<div class="stdPopupDivTop"><div class="stdPopupTitle">'+title+'</div></div>';
	popupHtml += '<form id="'+popupId+'Form" class="stdPopupForm">';
	popupHtml += '<div class="stdPopupMsg">'+message+'</div>' + additionalHtml;
	if (withOkButton) popupHtml += getButtonHtml("OK",true,"","","margin-left:160px;",false,"deleteStdPopup('"+popupId+"');return false;","","");	
	popupHtml += '</form><div class="stdPopupDivBottom">&nbsp;</div>';
	this.popupDiv=document.createElement("div");
	document.body.appendChild(this.popupDiv);
	this.popupDiv.id = popupId;
	this.popupDiv.className = "stdPopupDiv";
	this.popupDiv.innerHTML = popupHtml;
	this.popupDiv.style.display = "block";
	centerControl(popupDiv,400,popupDiv.offsetHeight);	
}
function hideStdPopup(popupId)
{
	var popupDiv = document.getElementById(popupId);
	if (popupDiv != null)
	{
		popupDiv.style.display = "none";						
	}				
}
function deleteStdPopup(popupId)
{
	var popupDiv = document.getElementById(popupId);
	if (popupDiv != null)
	{
		document.body.removeChild(popupDiv);
	}
}
var contextMenuLinkToSpace = "";
var contextMenuAddLinkId = "";
function onBrowseXLImageContextMenu(e,linkToSpace,addLinkId)
{
	contextMenuLinkToSpace = linkToSpace;
	contextMenuAddLinkId = addLinkId;
	var contextMenu = document.getElementById('imageContextMenu');
	eventPos = getEventPosition(e);
	var posX=eventPos.x;
	var posY=eventPos.y;
	contextMenu.style.left=posX+'px';
	contextMenu.style.top=posY+'px';
	contextMenu.style.display='block';
}
function contextMenuOpenInNew()
{
	window.open(contextMenuLinkToSpace);
}
function contextMenuAddToGallery()
{
	var contextMenuAddLink = document.getElementById(contextMenuAddLinkId);
	if (contextMenuAddLink.href==document.location+"#")
		contextMenuAddLink.onclick();
		else window.location = contextMenuAddLink.href;
}
function writeContactProPopup(professionalId)
{
	var popupHtml = "<input type='hidden' id='proId' value='"+ professionalId +"'></input> \
	<div class='contactProLine'><div class='contactProLabel'>Name</div><input class='contactProInput' id='name'></input></div> \
	<div class='contactProLine'><div class='contactProLabel'>Phone</div><input class='contactProInput' id='phone'></input></div> \
	<div class='contactProLine'><div class='contactProLabel'>Email</div><input class='contactProInput' id='email'></input></div> \
	<div class='contactProLine'><div class='contactProLabel'>Message</div><textarea class='contactProTextArea' id='message'></textarea></div> \
	<div class='contactProLine'><div class='contactProLabel'>Best Time to Call</div><textarea class='contactProTextArea' id='timeToCall' style='height:40px;'></textarea></div> \
	<div class='contactProLine' style='padding-top:10px;'>" + getButtonHtml("Cancel",true,"","","margin-left:70px;",false,"deleteStdPopup('contactPro');return false;","","") + getButtonHtml("Submit",true,"","","margin-left:70px;",false,"contactProSubmit();return false;","","") + "</div><div class='floatSeparator'></div>";
	
	writeStdPopup("contactPro","Send Message","",popupHtml,false);
}
function contactProSubmit()
{
	var params = '/proId='+contactProForm.proId.value;
	params += '/name='+contactProForm.name.value;
	params += '/phone='+contactProForm.phone.value;
	params += '/email='+contactProForm.email.value;
	params += '/message='+encodeURIComponent(encodeURIComponent(contactProForm.message.value));
	params += '/timeToCall='+encodeURIComponent(encodeURIComponent(contactProForm.timeToCall.value));
	ajaxc.makeRequest('http://www.houzz.com/contactPro', 'c',params,contactProAjaxHandler,'');
	deleteStdPopup('contactPro');	
}
function contactProAjaxHandler(resultObj,ajaxRequest)
{
	if (resultObj.success == "true") 
	{
		writeStdPopup("confirmContactPro","Send Message","Your message has been sent.","",true);
	} else
	{
		writeStdPopup("confirmContactPro","Send Message",resultObj.error,"",true);
	}				
}
			
