$(document).ready(function () {
  // create handler for change-region submission
  YAHOO.util.Event.addListener("change-region", "submit", function(e) {
  	YAHOO.util.Event.preventDefault(e);
  	submitSearchBy(document.getElementById("city-state-or-postal-code").value, '') 
  });
});

/* We may have been called with a redirect. */
var redirectParam = $(document).getUrlParam('redirect');
var changeRegionRedirect = function() {
  return (redirectParam != undefined ? '&redirect=' + redirectParam : '');
}

/**
 * Displays a dialog for when the user hasn't chosen a city yet, but has selected to go to a category location.
 */
var categoryAlias;
function displayCityChoiceRequest(catName, catAlias) {
	categoryAlias = catAlias;
	var panel =  new YAHOO.widget.Panel("redirect-explanation",
                                      { width: "280px",
                                      height: "125px",
                                      fixedcenter:true, 
						              close:true,  
						              draggable:false,  
						              modal:true, 
						              visible:false,
						              zindex:4
                                      } ); 
                                      
  panel.setBody("<div style='position:absolute;top:25px;left:50px;width:190px;height:75px;vertical-align:middle;'>" +
  					"Where are you? " + 
  					"Tell us your city, state or zip and we'll forward you to your destination.<br/>" +
					"<input type='text' value='' name='set-location' id='set-location' style='width:150px' onkeyup='if(event.keyCode==13){submitSearchBy(this.value);}return false;'/> " +
					"<a href='#' onclick='submitSearchBy(document.getElementById(\"set-location\").value);return false;'>" +
					"<img src='/images/goBtn.png' /></a>" +
				"</div>"
				);
  panel.render(document.body);
  panel.show(); 
}

/**
 * Displays a dialog if there is no city found
 */
function displayCityNoInfoRequest(searchBy, category) {
  
    var panel =  new YAHOO.widget.Panel("search-by-noinfo",
                                      { width: "500px",
                                      height: "120px",
                                      fixedcenter:true, 
						              close:true,  
						              draggable:false,  
						              modal:true, 
						              visible:false,
						              zindex:4
                                      } );  
  
  panel.setBody("<div style='position:absolute;top:25px;left:30px;width:440px;vertical-align:middle;'>"+
  					"<b>We don't have anything listed in <i>" + searchBy + "</i> yet.</b><br/><br/>" + 
  					"Do you want to add an <a style='color:#009999;font-weight:bold;' href='/submit/event/?categoryAlias=" + categoryAlias + "'>event</a>, <a style='color:#009999;font-weight:bold;' href='/submit/attraction/?categoryAlias=" + categoryAlias + "'>place</a>, or <a style='color:#009999;font-weight:bold;' href='/submit/attraction/?categoryAlias=" + categoryAlias + "'>resource</a> there?<br>" + 
					"Or would you like to see what's within " + 
					"<select name='searchByRadius' id='searchByRadius'>" + 
						"<option value='10'>10</option>" +
						"<option value='25'>25</option>" + 
						"<option value='50'>50</option>" + 
						"<option value='100'>100</option>" + 
					"</select>" + " of <i>" + searchBy + "</i>? " +
					"<a href='#' onclick='submitSearchBy(\"" + searchBy + "\", document.getElementById(\"searchByRadius\").value);return false;'><img src='/images/goBtn.png' /></a>" +
				"</div>"
				);
  panel.render(document.body);
  panel.show(); 
}

/**
 * If an error occured when changing the region this dialog shows.
 */
function displaySearchByError() {
  var panel =  new YAHOO.widget.Panel("error-explanation",
                                      { width: "280px",
                                      height: "125px", 
                                      fixedcenter:true, 
						              close:true,  
						              draggable:false,  
						              modal:true, 
						              visible:false,
						              zindex:4
                                      } );                         
  panel.setBody("<div style='position:absolute;top:25px;left:30px;width:220px;height:75px;vertical-align:middle;'>" +
  					"We are unable to process your request at this time. Please make sure that you have entered in a proper city, state or zip and try again." +
  				"</div>"
  				);
  panel.render(document.body);
  panel.show(); 
}

/**
 * Submit the ajax call to change the region.
 * @param {Object} searchBy 
 * @param {Object} radius
 */
function submitSearchBy(searchBy, radius, eventAlias) {
	var conn = YAHOO.util.Connect.asyncRequest("POST", "/data/location-finder.json", 
		handleSearchBy, 
		"query=" + searchBy + "&radius=" + radius + "&eventalias=" + eventAlias); 
}

var handleSearchBy =  {
    success : function(o) {		
		// set the cookie domain
		var parentDomain = window.location.hostname;
		var re = new RegExp("parentsconnect.*\.com");
		var pcCookieDomain = parentDomain.match(re);
		if (pcCookieDomain == null || pcCookieDomain == "") {
			pcCookieDomain = parentDomain;
		}
		
		var oJSON = eval("(" + o.responseText + ")");
		if (typeof oJSON !== "object") {
			return;			
		}
		// if there is a city then redirect to the city url
		if (oJSON.status == "-1") {
			displayCityNoInfoRequest(oJSON.query, oJSON.cat);
		} else if (redirectParam != null && redirectParam != "" && oJSON.alias != "") {
			//<!-- Dependencies -->  
			//<script type="text/javascript" src="http://yui.yahooapis.com/2.5.1/build/yahoo/yahoo-min.js"></script> 
			//<!-- Source File --> 
			//<script type="text/javascript" src="http://yui.yahooapis.com/2.5.1/build/cookie/cookie-beta-min.js"></script>
			YAHOO.util.Cookie.remove("regionAlias");
			YAHOO.util.Cookie.remove("regionId");
			YAHOO.util.Cookie.remove("regionName");
			YAHOO.util.Cookie.remove("regionZipcode");
			YAHOO.util.Cookie.set("regionAlias", oJSON.alias, {expires: new Date("January 12, 2025"), domain: pcCookieDomain, path: "/"});
			YAHOO.util.Cookie.set("regionId", oJSON.id, {expires: new Date("January 12, 2025"), domain: pcCookieDomain, path: "/"});
			YAHOO.util.Cookie.set("regionName", unescape(oJSON.name), {expires: new Date("January 12, 2025"), domain: pcCookieDomain, path: "/"});
			YAHOO.util.Cookie.set("regionZipcode", oJSON.zipcode, {expires: new Date("January 12, 2025"), domain: pcCookieDomain, path: "/"});
			YAHOO.util.Cookie.set("regionZipcodeStart", oJSON.zipcodeStart, {expires: new Date("January 12, 2025"), domain: pcCookieDomain, path: "/"});
			YAHOO.util.Cookie.set("regionZipcodeEnd", oJSON.zipcodeEnd, {expires: new Date("January 12, 2025"), domain: pcCookieDomain, path: "/"});
			YAHOO.util.Cookie.set("regionZipcodeStart2", oJSON.zipcodeStart2, {expires: new Date("January 12, 2025"), domain: pcCookieDomain, path: "/"});
			YAHOO.util.Cookie.set("regionZipcodeEnd2", oJSON.zipcodeEnd2, {expires: new Date("January 12, 2025"), domain: pcCookieDomain, path: "/"});
			window.location = redirectParam.replace(/:region/, oJSON.alias);
		} else if (oJSON.url != null && oJSON.url != "" && oJSON.alias != null && oJSON.alias != "") {			
			YAHOO.util.Cookie.remove("regionAlias");
			YAHOO.util.Cookie.remove("regionId");
			YAHOO.util.Cookie.remove("regionName");
			YAHOO.util.Cookie.remove("regionZipcode");
			YAHOO.util.Cookie.set("regionAlias", oJSON.alias, {expires: new Date("January 12, 2025"), domain: pcCookieDomain, path: "/"});
			YAHOO.util.Cookie.set("regionId", oJSON.id, {expires: new Date("January 12, 2025"), domain: pcCookieDomain, path: "/"});
			YAHOO.util.Cookie.set("regionName", unescape(oJSON.name), {expires: new Date("January 12, 2025"), domain: pcCookieDomain, path: "/"});
			YAHOO.util.Cookie.set("regionZipcode", oJSON.zipcode, {expires: new Date("January 12, 2025"), domain: pcCookieDomain, path: "/"});
			YAHOO.util.Cookie.set("regionZipcodeStart", oJSON.zipcodeStart, {expires: new Date("January 12, 2025"), domain: pcCookieDomain, path: "/"});
			YAHOO.util.Cookie.set("regionZipcodeEnd", oJSON.zipcodeEnd, {expires: new Date("January 12, 2025"), domain: pcCookieDomain, path: "/"});
			YAHOO.util.Cookie.set("regionZipcodeStart2", oJSON.zipcodeStart2, {expires: new Date("January 12, 2025"), domain: pcCookieDomain, path: "/"});
			YAHOO.util.Cookie.set("regionZipcodeEnd2", oJSON.zipcodeEnd2, {expires: new Date("January 12, 2025"), domain: pcCookieDomain, path: "/"});
			window.location = oJSON.url;
		} else if (oJSON.url != null && oJSON.url != "") {
			YAHOO.util.Cookie.remove("regionAlias");
			YAHOO.util.Cookie.remove("regionId");
			YAHOO.util.Cookie.remove("regionName");
			YAHOO.util.Cookie.remove("regionZipcode");
			YAHOO.util.Cookie.set("regionAlias", oJSON.alias, {expires: new Date("January 12, 2025"), domain: pcCookieDomain, path: "/"});
			YAHOO.util.Cookie.set("regionId", oJSON.id, {expires: new Date("January 12, 2025"), domain: pcCookieDomain, path: "/"});
			YAHOO.util.Cookie.set("regionName", unescape(oJSON.name), {expires: new Date("January 12, 2025"), domain: pcCookieDomain, path: "/"});
			YAHOO.util.Cookie.set("regionZipcode", oJSON.zipcode, {expires: new Date("January 12, 2025"), domain: pcCookieDomain, path: "/"});
			YAHOO.util.Cookie.set("regionZipcodeStart", oJSON.zipcodeStart, {expires: new Date("January 12, 2025"), domain: pcCookieDomain, path: "/"});
			YAHOO.util.Cookie.set("regionZipcodeEnd", oJSON.zipcodeEnd, {expires: new Date("January 12, 2025"), domain: pcCookieDomain, path: "/"});
			YAHOO.util.Cookie.set("regionZipcodeStart2", oJSON.zipcodeStart2, {expires: new Date("January 12, 2025"), domain: pcCookieDomain, path: "/"});
			YAHOO.util.Cookie.set("regionZipcodeEnd2", oJSON.zipcodeEnd2, {expires: new Date("January 12, 2025"), domain: pcCookieDomain, path: "/"});
			window.location = oJSON.url;
		} else { // if oJSON.status == "-2" or for any other case, see LocationFinderController
			displaySearchByError();
		}

    }, 
    failure : function(o) {
        displaySearchByError();
    } 	
}

function highlightDealLink(element) {
	YAHOO.util.Dom.setStyle(element,'color','#0000CC');
	YAHOO.util.Dom.setStyle(element,'text-decoration','underline');
}

function unhighlightDealLink(element) {
	YAHOO.util.Dom.setStyle(element,'color','#333333');
	YAHOO.util.Dom.setStyle(element,'text-decoration','none');	
}