function setCookie(name,value,expires,path,domain,secure){document.cookie=name+'='+escape(value)+((expires)?';expires='+expires.toGMTString():'')+';path='+((path)?path:'/')+';domain='+((domain)?domain:'boston.com')+((secure)?';secure':'');}
function getCookie(name){var dc=document.cookie,prefix=name+'=',begin=dc.indexOf('; '+prefix);if(begin==-1){begin=dc.indexOf(prefix);if(begin!=0)return null;}else begin+=2;var end=dc.indexOf(';',begin);if(end==-1)end=dc.length;return unescape(dc.substring(begin+prefix.length,end));}

var trustedTown, townDefaultOrig, townDefaultDown;

var doc = document;
var yummy;
var hlCookieName = 'hlTown';
var imgRoot = 'http://cache.boston.com/universal/site_graphics/yourtown/';
var weatherImgRoot = 'http://cache.boston.com/universal/site_graphics/yourtown/weather/YT_weather_';
var townDefault = 'yourtown';
var cookieCrumbles =  new Date('Dec 31, 2010');
var towns = { 'newton':
                     {'slogan'     : '',
                      'weatherJS'  : 'NEWY',
                      'insideSitePath' : '83',
		      'pForumID' : '8260538464668385426'},
              'needham':
                     {'slogan'     : '',
                      'weatherJS'  : 'MA16',
                      'insideSitePath' : '85',
                      'pForumID' : '5973041168854404240'},
              'waltham':
                     {'slogan'     : '',
                      'weatherJS'  : 'WALY',
                      'insideSitePath' : '86',
                      'pForumID' : '7165642765645548907'},
              'wellesley':
                     {'slogan'     : '',
                      'weatherJS'  : 'WELY',
                      'insideSitePath' : '98',
                      'pForumID' : 'bd1cfa03-b462-4ec8-8581-cef413a99ea5'},
              'melrose':
	      {'slogan'     : '',
	       'weatherJS'  : 'MRSX',
	       'insideSitePath' : '114',
	       'pForumID' : 'e4a7a7c4-271f-4615-8eef-c4bf63f88dc0'},
              'medford':
	      {'slogan'     : '',
	       'weatherJS'  : 'MEDX',
	       'insideSitePath' : '112',
	       'pForumID' : '95b4b083-46fb-4a3c-9fd3-190559cc0e61'},
              'malden':
	      {'slogan'     : '',
	       'weatherJS'  : 'MALD',
	       'insideSitePath' : '111',
	       'pForumID' : 'c8af56a8-6ba0-4319-a428-1814823f6481'},
              'norwell':
	      {'slogan'     : '',
	       'weatherJS'  : 'UINX',
	       'insideSitePath' : '115',
	       'pForumID' : 'd277d032-208d-407a-a92a-8529d936d71f'},
              'hingham':
	      {'slogan'     : '',
	       'weatherJS'  : 'UINX',
	       'insideSitePath' : '113',
	       'pForumID' : '77c46bfb-edd5-404a-838d-01a78a553b66'},
              'scituate':
	      {'slogan'     : '',
	       'weatherJS'  : 'UINX',
	       'insideSitePath' : '116',
	       'pForumID' : '132c54f4-4b19-47ee-9488-d6a8f7908e0c'},
              'somerville':
              {'slogan'     : '',
               'weatherJS'  : 'CAMB',
               'insideSitePath' : '119',
               'pForumID' : '6d30627b-c283-431f-9ba9-7d76a2c894b7'},
              'natick':
              {'slogan'     : '',
               'weatherJS'  : 'MA58',
               'insideSitePath' : '120',
               'pForumID' : 'f2f5b6fe-225e-4e16-8633-99d71ed90816'},
              'brookline':
              {'slogan'     : '',
               'weatherJS'  : 'CAMB',
               'insideSitePath' : '120',
               'pForumID' : '48321598-5f65-4e5b-b853-6d8d08ab7c8d'},
              'framingham':
              {'slogan'     : '',
               'weatherJS'  : 'FMGX',
               'insideSitePath' : '120',
               'pForumID' : 'a334c008-75ac-4555-a540-5b04281c85e6'},
			   'dedham':
              {'slogan'     : '',
               'weatherJS'  : 'MA01',
               'insideSitePath' : '132',
               'pForumID' : '16887512-c29f-4966-ad92-3317b11b32c0'},
			   'arlington':
              {'slogan'     : '',
               'weatherJS'  : 'ARLY',
               'insideSitePath' : '131',
               'pForumID' : 'd018bd02-1941-445c-8192-6040770b0520'}
            };


function bakeCookie(town){
  setCookie(hlCookieName,town,cookieCrumbles);
};

function eatCookie(){
  var yummy = getCookie(hlCookieName);
};

// This will change once final ZVents URLs are known

function knowThyTown(){
if (trustedTown && (trustedTown != ''))
  {townDefault = trustedTown;}
else
  {var townTravel = getCookie(hlCookieName);
  if (townTravel)
    townDefault = townTravel;
  else
    townDefault = townDefault;}
}
// End changing code
var townList = towns[townDefault];

// Title Management
function writePageTitle(){
document.title =  townDefault + " - Hyperlocal - Boston.com"
};

// Town Name Write Out
function writeTownName(el, domClass){
if (townDefault != 'yourtown')
  document.writeln('<' + el + ' class=\"' + domClass + '\"><a href=\"http:\/\/www.boston.com\/yourtown\/' + townDefault.toLowerCase() + '\/\">' + townDefault + '<\/a><\/' + el + '>')
else
  document.writeln('<' + el + ' class=\"' + domClass + '\"><a href=\"http:\/\/www.boston.com\/yourtown\/">Your Town<\/a><\/' + el + '>');
};

// Town Image Write Out
function writeTownImage(domClass){
if (townDefault != 'yourtown')
  document.writeln('<div class=\"' + domClass + '\"><a href=\"http:\/\/www.boston.com\/yourtown\/'+ townDefault + '\/\" class=\"imageLink\" ><img src=\"' + imgRoot +  townDefault.toLowerCase() + '_photo.jpg\" \/ alt=\"' + townDefault + ' cityscape\"><\/a><\/div>')
else
  document.writeln('<div class=\"' + domClass + '\"><a href=\"http:\/\/www.boston.com\/yourtown\/" class=\"imageLink\"><img src=\"' + imgRoot +  'yourtown_photo.jpg\" \/ alt=\"Boston.com Your Town\"><\/a><\/div>')
};

// Town Slogan Write Out
function writeTownWeatherScript(){
if (townDefault != 'yourtown')
  {var townList = towns[townDefault.toLowerCase()];
   document.writeln('<script src=\"http:\/\/weather.boston.com\/' + townList['weatherJS'] + '\/js\"><\/script>')};
};


// Inside Town Include  Write Out for Dynamic Assembly tool
function writeInsideTownInclude(){
if (townDefault != 'yourtown')
  {var townList = towns[townDefault.toLowerCase()];
   document.write('<script src="http:\/\/www.boston.com\/dynamicassembly\/sitepath' + townList['insideSitePath'] + '\/js_output.js"><\/script>')};
   
};

function doWeather(){
if (townDefault == 'yourtown')
  document.write('<li class=\"weather\"><a href=\"http:\/\/weather.boston.com\/">Weather<\/a><\/li>')
else
{var weatherLoc = weather["location"];
var weatherCur = weather["current"];
var weatherDeg = weatherCur["temperature"];
var townList = towns[townDefault.toLowerCase()];
var weatherURL = 'http://weather.boston.com/?code=' + townList['weatherJS'];
document.write('<li class=\"icon\"><a href=\"' + weatherURL + '\" class="\imageLink\"><img src=\"' + weatherImgRoot + weatherCur["icon"] + '\" alt=\"' + weatherCur["description"]+ ' weather icon\" title=\" Current Condition: ' + weatherCur["description"]+ '\"  \/><\/a><\/li>');
document.write('<li class=\"degrees\"><a href=\"'+ weatherURL + '\">' + weatherDeg["_"]  +'\&deg\;<\/a><\/li>');}
};

function showWhat(divid)
{if(document.getElementById(divid).style.display == 'none'){document.getElementById(divid).style.display = 'block';}}

function hideWhat(divid)
{if(document.getElementById(divid).style.display == 'block'){document.getElementById(divid).style.display = 'none';}}

function keywordForm(inputId){
    if (document.getElementById(inputId).value != "" && document.getElementById(inputId).value != null){
      return document.getElementById(inputId).value.toLowerCase();
    }
    else{return "User_Blank"}
}

function writeTopNav(){
var navSep = '<li class\=\"sep\">\&nbsp;<\/li>';
if (townDefault != 'yourtown')
	{if (townDefault == 'newton' || townDefault == 'Newton')
		{var townDefaultOrig = 'Newton'; var townDefaultDown = townDefault.toLowerCase();}
	else
		{var townDefaultDown = townDefault.toLowerCase(); townDefaultOrig = townDefault.toLowerCase();}
  
	var townList = towns[townDefaultDown];
	  	
		document.write('<li id=\"nHome\"><a href=\"http:\/\/www.boston.com\/yourtown\/' + townDefaultDown + '\/\">home<\/a><\/li>' + navSep + 
		'<li id=\"nNews\"><a href=\"http:\/\/www.boston.com\/yourtown\/' + townDefaultDown + '\/news\/\">news</a>' + 
		navSep + '<li id=\"nEvents\"><a href=\"http:\/\/townevents.boston.com\/search?swhere=' + townDefaultOrig + ' ,ma\">events<\/a><\/li>' + navSep + '<li id=\"nSchools\"><a href=\"http:\/\/www.boston.com\/yourtown\/' + townDefaultDown + '\/schools\/\">schools<\/a><\/li>' + navSep + '<li id=\"nSports\"><a href=\"http:\/\/www.boston.com\/yourtown\/' + townDefaultDown + '\/sports\/\">sports<\/a><\/li>' + navSep +  '<li id=\"nDiscuss\"><a href=\"http:\/\/www.boston.com\/community\/forums.html\?plckForumPage\=Forum\&amp;plckForumId\=Cat\:Your\+TownForum\:'+ townList['pForumID'] +'\">discussions<\/a><\/li>' + navSep + 
		//'<li id=\"nRE\"><a href=\"http:\/\/re.boston.com\/sales\/List_Listings.asp?RoS=S&amp;CityName=' + townDefaultDown + '&amp;ST=MA\">real estate<\/a><\/li>' + navSep + 
		'<li id=\"nMarket\"><a href=\"http:\/\/www.boston.com\/yourtown\/' + townDefaultDown+ '\/marketplace\/\">marketplace<\/a><\/li>' + navSep);
		
} 
else
  document.write('<li><a href\=\"http:\/\/www.boston.com\/yourtown\/\">home<\/a><\/li>' + navSep + '<li><a href\=\"http:\/\/www.boston.com\/yourtown\/help\/contact_us\/\">contact us<\/a><\/li>' + navSep + '<li><a href\=\"http:\/\/www.boston.com\/yourtown\/advertise\/\">advertise<\/a><\/li>');}

function writeBottomNav(){
  var navSep = '<li class=\"listPipe\">|<\/li>';
  if (townDefault != 'yourtown')
    {if (townDefault == 'newton' || townDefault == 'Newton')
      {var townDefaultOrig = 'Newton'; var townDefaultDown = townDefault.toLowerCase();}
   else
      {var townDefaultDown = townDefault.toLowerCase(); townDefaultOrig = townDefault.toLowerCase();}
     var townList = towns[townDefaultDown];
     document.write('<li><a href=\"http:\/\/www.boston.com\/yourtown\/' + townDefaultDown + '\/\">Home<\/a><\/li>' + navSep + '<li><a href=\"http:\/\/www.boston.com\/yourtown\/' + townDefaultDown + '\/news\/\">News</a></li>' + navSep + '<li><a href=\"http:\/\/townevents.boston.com\/search?swhere=' + townDefault + ' ,ma\">Events<\/a><\/li>' + navSep + '<li><a href=\"http:\/\/www.boston.com\/yourtown\/' + townDefaultDown + '\/schools\/\">Schools<\/a><\/li>' + navSep + '<li><a href=\"http:\/\/www.boston.com\/yourtown\/' + townDefaultDown + '\/sports\/\">Sports<\/a><\/li>' + navSep + '<li><a href=\"http:\/\/www.boston.com\/yourtown\/wiki\/' + townDefault + '\/\">City Guide<\/a><\/li>' + navSep + '<li><a href=\"http:\/\/www.boston.com\/community\/forums.html\?plckForumPage\=Forum\&amp;plckForumId\=Cat\:Your\+TownForum\:' + townList['pForumID'] + '\">Discussions<\/a><\/li>' + navSep + '<li><a href=\"http:\/\/www.boston.com\/yourtown\/' + townDefaultDown + '\/marketplace\/\">Marketplace<\/a><\/li>');
    }
else
  {document.write('<li><a href=\"http:\/\/www.boston.com\/yourtown\/\">Home<\/a><\/li>' + navSep + '<li><a href=\"http:\/\/www.boston.com\/yourtown\/help\/contact_us\/\">Contact Us<\/a><\/li>' + navSep + '<li><a href=\"http:\/\/www.boston.com\/yourtown\/advertise\/\">Advertise<\/a><\/li>');}  }

function doZventsCatLinks(){
  if (townDefault != 'yourtown')
    var searchTown = '&amp;swhere\=' + townDefault + ',Ma';
  else
    var searchTown = '';
  document.write('<li><a href=\"http://townevents.boston.com/search?cat=1' + searchTown + '\">Arts &amp; Crafts</a></li><li><a href=\"http://townevents.boston.com/search?cat=2' + searchTown + '\">Community</a></li><li><a href=\"http://townevents.boston.com/search?cat=6' + searchTown + '\">Food &amp; Dining</a></li><li><a href=\"http://townevents.boston.com/search?cat=8' + searchTown + '\">Performing Arts</a></li><li><a href=\"http://townevents.boston.com/search?cat=1049' + searchTown + '\">Shopping</a></li><li><a href=\"http://townevents.boston.com/search?cat=9' + searchTown + '\">Sports &amp; Rec</a></li><li><a href=\"http:\/\/townevents.boston.com\/search?cat=&amp;has_kids=1&amp;new=n&amp;search=true&amp;sort=0&amp;srad=10&amp;srss=10&amp;ssi=0&amp;st=event&amp;svt=text&amp;swhat=&amp;swhen=&amp;swhere=' + searchTown + '&trim=1\">Family Events<\/a><\/li>')
}

function setFormTown(formId){document.getElementById(formId).value=townDefault;}

function doTTDTown(){
if (townDefault == 'yourtown')
  document.write('');
else 
  document.write('in ' + townDefault);
};

function doTTDTownLink(){
    if (townDefault == 'yourtown')
	document.write('');
    else
	document.write('<a href=\"http:\/\/townevents.boston.com\/search?swhere=' + townDefault + ' ,ma\">Things to do in ' + townDefault + '<\/a>');
 };

function writeWkSourceID() { 
	switch(townDefault) { 
	case 'Newton' :  
		var wkSourceID = "2082389852" ;
		break; 
	case 'needham' : 
		var wkSourceID = "901489237" ;
		break; 
	case 'waltham' : 
		var wkSourceID = "1332419771" ;
		break; 
	case  'wellesley' :  
		var wkSourceID = "1333668495" ;
		break; 
	case  'medford' :  
		var wkSourceID = "1482337630" ;
		break; 
  	case  'melrose' :  
		var wkSourceID = "334477259" ;
		break; 

	case 'malden' : 
		var wkSourceID = "53130554" ; 
		break; 
	case 'scituate' : 
		var wkSourceID = "1430414847" ;
		break; 
	case 'norwell' : 
		var wkSourceID = "280086812" ;  	
		break; 
	case 'hingham' : 
		var wkSourceID = "1890820663" ; 
	default:  		
		var wkSourceID = "2082389852"; 
	} 
		
	document.write('<input id="wkSourceID" type="hidden" value="' + wkSourceID +'" name="s.collapseFilter" />');	
};

// Town MyGroceryDeals

function doGroceryDealsHP(){
    if (townDefault == 'yourtown')
	document.write('');
    else
	document.write('<div id=\"mygrocery\"><div id=\"mygroceryBG\"><form method=\"get\" action=\"http:\/\/guest.microsite.mygrocerydeals.com\/index.cfm\" target=\"_blank\" onsubmit=\"s_linkType=\'o\';s_linkName=\'YourTown_' + townDefault +'_HP_My_Grocery_Search\';s_lnk=s_co(this);s_gs(\'nytbglobe\')\"><fieldset><ol><li id=\"mgheader\"><h3 class=\"sectionHeader\"><a href=\"http:\/\/www.mygrocerydeals.com\">Local Grocery Deals</a></h3></li><label>Get the latest coupons from stores in your area:<\/label><li id=\"zip\"><input name=\"zip\" type=\"text\" value=\"Zip Code\" onblur=\"if(this.value==\'\')this.value=\'Zip Code\';\" onfocus=\"if(this.value==\'Zip Code\')this.value=\'\';\" \/><input type=\"hidden\" name=\"vendor_id\" value=\"boston.microsite\"><\/li><li id=\"goButton\"><input align=\"middle\" type=\"submit\" hidefocus=\"hidefocus\" value=\"GO\" class=\"form-button\" id=\"Go\"\/><\/li><\/ol><\/fieldset><\/form><\/div><\/div>');
};

function doGroceryDealsMP(){
    if (townDefault == 'yourtown')
        document.write('');
    else
        document.write('<div id=\"mygrocery\"><div id=\"mygroceryBG\"><form method=\"get\" action=\"http:\/\/guest.microsite.mygrocerydeals.com\/index.cfm\" target=\"_blank\" onsubmit=\"s_linkType=\'o\';s_linkName=\'YourTown_' + townDefault +'_Marketplace_My_Grocery_Search\';s_lnk=s_co(this);s_gs(\'nytbglobe\')\"><fieldset><ol><li id=\"mgheader\"><h3 class=\"sectionHeader\"><a href=\"http:\/\/www.mygrocerydeals.com\">Local Grocery Deals</a></h3></li><label>Get the latest coupons from stores in your area:<\/label><li id=\"zip\"><input name=\"zip\" type=\"text\" value=\"Zip Code\" onblur=\"if(this.value==\'\')this.value=\'Zip Code\';\" onfocus=\"if(this.value==\'Zip Code\')this.value=\'\';\" \/><input type=\"hidden\" name=\"vendor_id\" value=\"boston.microsite\"><\/li><li id=\"goButton\"><input align=\"middle\" type=\"submit\" hidefocus=\"hidefocus\" value=\"GO\" class=\"form-button\" id=\"Go\"\/><\/li><\/ol><\/fieldset><\/form><\/div><\/div>');
};



function loadPoints(design_id, element_id, proxy, options) {
	var pointsdata = "";
	var s_leadin;
	var id = "#" + element_id;
	var pointscall = proxy + design_id;

	$(document).ready(function() {
		$.get(pointscall, { },
		function(data){
			$(data).find('item').each(function(){
				var title = $(this).find('title').text();
				var url = $(this).find('url').text();
				var sourcename = $(this).find('sourcename').text();
				var description = $(this).find('description').text();
				var show_leadin = $(this).find('showdescription').text();
				s_leadin = show_leadin;
				var date = $(this).find('date').text();
				var tmpDate = new Date(date);
				var image = $(this).find('image').text();
				var show_image = 1;
				
				if (show_leadin != 1) {
					pointsdata += '<li><a href="' + url + '"><b>' + title + '</b></a> <span class="attr">' + sourcename + ', ' + (parseInt(tmpDate.getMonth())+1) + '/' + tmpDate.getDate() + '/' + tmpDate.getFullYear() + '</span></li>';
				}
				else {
					if (show_image == 1 && image) { 
					
						pointsdata += '<div class="padTop4"></div><div class="img100h3Left"><a href="' + url + '">' + '<img src="' + image + '" /></a><h3 style="text-transform:capitalize;"><a href="' + url + '">' + title + '</a></h3><div class="tt"><span>' + description + ' <span class="attr">' + sourcename + ', ' + (parseInt(tmpDate.getMonth())+1) + '/' + tmpDate.getDate() + '/' + tmpDate.getFullYear() + '</span></span></div><div class="cf"></div></div><div class="padTop4"></div>';
					 
					} else { 
						pointsdata += '<div class="padTop4"></div><div class="img100h3Left"><h3><a href="' + url + '">' + title + '</a></h3><div class="tt"><span>' + description + ' <span class="attr">' + sourcename + ', ' + (parseInt(tmpDate.getMonth())+1) + '/' + tmpDate.getDate() + '/' + tmpDate.getFullYear() + '</span></span></div><div class="cf"></div></div><div class="padTop4"></div>';
					}
				}

			});

		if (s_leadin != 1) {
			pointsdata = '<ul class="linklist">' + pointsdata + '</ul>';
		}

		$(id).append(pointsdata);

	  });

  });

 }