﻿function findPos(Obj)
{
	var cureft = curtop = 0;
	if (Obj.offsetParent)
	{
		curleft = Obj.offsetLeft
		curtop = Obj.offsetTop
		
		while (Obj = Obj.offsetParent)
		{
			curleft += Obj.offsetLeft
			curtop += Obj.offsetTop
		}
	}
	return [curleft,curtop];
}


function getTarget(e) 
{
    var objEvent;
    if (!e) {
        objEvent = window.event;
    } else {
        objEvent = e;
    }
    if (objEvent.srcElement) {
        target = objEvent.srcElement;
    }
    if (objEvent.target) {
        target = objEvent.target;
    }
    return target;
}


function findOwnerDIV(element) 
{
    node = element;
    while (node) {
        if (node.nodeType == 1 && node.nodeName == "DIV") 
        {
            return node;
        }
        node = node.parentNode;
    }
    return null;
}


function compareOwnerNode(element, compareelement) 
{
    if (element != compareelement)
    {
        node = element;
        while (node)
        {
            if (node == compareelement)
            {
                return true;
            }
            node = node.parentNode;
        }
    }
    else
    {
        return true;
    }
    
    return false;
}



function StringBuilder(value)
{
	this.strings = new Array("");
	this.append(value);
}

StringBuilder.prototype.append = function (value)
{
	if (value)
	{
		this.strings.push(value);
	}
}

StringBuilder.prototype.clear = function ()
{
	this.strings.length = 1;
}

StringBuilder.prototype.toString = function ()
{
	return this.strings.join("");
}



var _modalSetup = false;
function setupModalWindow()
{
    if (_modalSetup == false)
    {
        var s = "";
        s = s + "<div id=\"modalWindow\" style=\"position: fixed; left: 0; top: 0; z-index: 999; background-color: white; display: none; border: 1px solid #777777;\"><div style=\"padding: 6px;\" class=\"backgroundRed\"><table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" style=\"width: 100%\"><tr><td><span id=\"modalWindowTitle\" style=\"color: #ffffff; font-weight: bold;\"></span></td><td align=\"right\" nowrap=\"nowrap\"><span class=\"awhite\" onclick=\"hideModalWindow();\">close [X]</span></td></tr></table></div><div id=\"modalWindowLoading\" style=\"position: relative; top: 40%; display: none; width: 98%; text-align: center;\"><img src=\"images/loading.gif\" /><br /><b id=\"modalloadtext\">Loading</b></div><div id=\"modalWindowContent\"></div></div>";
        s = s + "<div id=\"modalBackground\" style=\"position: fixed; left: 0; top: 0; width: 100%; height: 100%; z-index: 998;background-color:#ffffff; display: none; opacity: 0.50; filter: alpha(opacity=50)\"></div>";
        
        var div = document.createElement("div");
        div.innerHTML = s;
        document.body.appendChild(div);
        
        _modalSetup = true;
    }
}


function showModalWindow(Title, Content, Height, Width)
{
    setupModalWindow();
    
    document.getElementById('modalWindowTitle').innerHTML = Title;
    document.getElementById('modalWindowContent').innerHTML = "Loading...";
    document.getElementById('modalWindow').style.height = Height + 'px';
    document.getElementById('modalWindow').style.width = Width + 'px';
    document.getElementById('modalWindow').style.display = 'block';
    $('#modalBackground').fadeIn('fast');
    
    // call once to center everything
	modalWindowOnWindowResize();
    
    if (window.attachEvent)
		window.attachEvent('onresize', modalWindowOnWindowResize);
	else if (window.addEventListener)
		window.addEventListener('resize', modalWindowOnWindowResize, false);
	else
		window.onresize = modalWindowOnWindowResize;
	
	if (document.all)
		document.documentElement.onscroll = modalWindowOnWindowResize;

		
	//Apppend content
	document.getElementById('modalWindowContent').style.display='none';
	document.getElementById('modalWindowContent').innerHTML = Content;
	
	//See if the content is an iframe
	if (Content.indexOf("iframe") > 0)
	{
	    document.getElementById('modalWindowLoading').style.display='block';
	    $("iframe").bind("load", function(e){
            document.getElementById('modalWindowLoading').style.display='none';
            document.getElementById('modalWindowContent').style.display='block';
        });
	}
	else
	{
	    document.getElementById('modalWindowContent').style.display='block';
	}
}


function modalWindowOnWindowResize()
{
	var left = window.XMLHttpRequest == null ? document.documentElement.scrollLeft : 0;
	var top = window.XMLHttpRequest == null ? document.documentElement.scrollTop : 0;
	var div = document.getElementById('modalWindow');
	
	div.style.left = Math.max((left + (getWindowWidth() - div.offsetWidth) / 2), 0) + 'px';
	div.style.top = (Math.max((top + (getWindowHeight() - div.offsetHeight) / 2), 0) - 80) + 'px';
}


function hideModalWindow()
{
    
    if (document.getElementById('modalBackground'))
    {
        $('#modalBackground').fadeOut('fast');
    }
    
    if (document.getElementById('modalWindow'))
    {
        document.getElementById('modalWindow').style.display = 'none';
    	
	    if (window.detachEvent)
		    window.detachEvent('onresize', modalWindowOnWindowResize);
	    else if (window.removeEventListener)
		    window.removeEventListener('resize', modalWindowOnWindowResize, false);
	    else
		    window.onresize = null;
    }
}



function getWindowWidth()
{
	var width =
		document.documentElement && document.documentElement.clientWidth ||
		document.body && document.body.clientWidth ||
		document.body && document.body.parentNode && document.body.parentNode.clientWidth ||
		0;
		
	return width;
}

function getWindowHeight()
{
    var height =
		document.documentElement && document.documentElement.clientHeight ||
		document.body && document.body.clientHeight ||
  		document.body && document.body.parentNode && document.body.parentNode.clientHeight ||
  		0;
  		
  	return height;
}



function searchFinder(address)
{
    var geocoder = new GClientGeocoder();
    if (geocoder && address != "")
    {
        geocoder.getLatLng(address, function(point)
        {
            if (!point){
                showModalWindow("Invalid Location", "<div style=\"padding: 12px;\"><span class=\"headerRed\"><img src=\"images/warning32.png\" align=\"left\" style=\"border-right: white 6px solid;\">Sorry, but we didn't understand the address entered.</span><br>We accept addresses in the following forms:<br><br><ul><li>121 West 15th ave, New York, NY</li><li>New York, NY</li><li>New York, NY 10023</li><li>10023</li></ul><br>Also, it's possible we don't have a listing for " + address + ". In that case, you should try adding a zip, or try a larger nearby city.</div>", 240, 440);
            }
            else{
                var appqry = "";
                if (document.getElementById("ckDefaultLocation")){
                    if (document.getElementById("ckDefaultLocation").checked == true){
                        appqry = "&MakeDefault=1";
                    }
                }
                
                window.location = "Finder.aspx?Latitude=" + point.lat() + "&Longitude=" + point.lng() + appqry;
            }
        });
    }
    else
    {
        showModalWindow("Invalid Location", "<div style=\"padding: 12px;\"><span class=\"headerRed\">Sorry, but we didn't understand the location you entered.</span><br>We accept locations in the following forms:<br><br><ul><li>121 West 15th ave, New York, NY</li><li>New York, NY</li><li>New York, NY 10023</li><li>10023</li></ul><br>Also, it's possible we don't have a listing for the address, city or state. In that case, you should try adding a zip, or try a larger nearby city.</div>", 240, 440);
    }
}


function searchFinderCategory(address, categoryid)
{
    var geocoder = new GClientGeocoder();
    if (geocoder && address != "")
    {
        geocoder.getLatLng(address, function(point)
        {
            if (!point)
            {
                showModalWindow("Invalid Location", "<div style=\"padding: 12px;\"><span class=\"headerRed\"><img src=\"images/warning32.png\" align=\"left\" style=\"border-right: white 6px solid;\">Sorry, but we didn't understand the address entered.</span><br>We accept addresses in the following forms:<br><br><ul><li>121 West 15th ave, New York, NY</li><li>New York, NY</li><li>New York, NY 10023</li><li>10023</li></ul><br>Also, it's possible we don't have a listing for " + address + ". In that case, you should try adding a zip, or try a larger nearby city.</div>", 240, 440);
            }
            else
            {
                window.location = "Finder.aspx?Latitude=" + point.lat() + "&Longitude=" + point.lng() + "&CategoryID=" + categoryid;
            }
        });
    }
    else
    {
        showModalWindow("Invalid Location", "<div style=\"padding: 12px;\"><span class=\"headerRed\">Sorry, but we didn't understand the location you entered.</span><br>We accept locations in the following forms:<br><br><ul><li>121 West 15th ave, New York, NY</li><li>New York, NY</li><li>New York, NY 10023</li><li>10023</li></ul><br>Also, it's possible we don't have a listing for the address, city or state. In that case, you should try adding a zip, or try a larger nearby city.</div>", 240, 440);
    }

}





function addComment(locationid)
{
    showModalWindow("Add a new comment", "<div style=\"padding: 12px;\"><iframe src=\"Location_AddComment_Popup.aspx?norefresh=1&LocationID=" + locationid + "\" width=100% height=\"400px\" frameborder=\"0\" align=\"middle\">loading...</iframe></div>", 440, 440);
}


function createBookmark(url, title)
{
    if (document.all)
    {
        window.external.AddFavorite(url,title);
    }
}


function addPhoto(locationid)
{
    showModalWindow("Add a new photo", "<div style=\"padding: 12px;\"><iframe src=\"Location_AddImage_Popup.aspx?LocationID=" + locationid + "\" width=100% height=\"240\" frameborder=0 align=middle>loading...</iframe></div>", 280, 500);
}


function submitLocationRating(locationid, rating, ratingpostid)
{
    if (rating != 0)
    {
        if (document.getElementById(ratingpostid))
        {
            document.getElementById(ratingpostid).innerHTML = 'Thank you!';
        }
        
        GDownloadUrl("restHandler.ashx?method=SubmitLocationRating&LocationID=" + this.locationid + "&Rating=" + rating, function(data, responseCode)
        { });
    }
}


function checkHotelRates(locationid)
{
    setupModalWindow();
    document.getElementById("modalloadtext").innerHTML="Checking prices...";
    showModalWindow("Compare prices and availability", "<div style=\"padding: 12px;\"><iframe src=\"Location_HoteRates_Popup.aspx?&LocationID=" + locationid + "\" width=100% height=\"340px\" frameborder=\"0\" align=\"middle\">loading...</iframe></div>", 380, 420);
    setTimeout(function () { document.getElementById("modalloadtext").innerHTML="Loading..."; }, 5000);
}


function externalLinkTo(url)
{
    window.open(url);return false;
}


function innerContent(content)
{
    try
    {
        document.write(content);
    }
    catch(err)
    {}
}
