var togSwitch = '';
var adSwitch ='';

function rollover(element)
	{
		var testString = element.src;
		var inString = testString.match("hover");
		if (inString) {
			element.src = testString.replace("_hover","");
		}
		else
		{
			element.src = testString.replace(".gif","_hover.gif");
		}
	}

function submitenter(myfield,e)
	{
		var keycode;
		if (window.event) keycode = window.event.keyCode;
		else if (e) keycode = e.which;
		else return true;

		if (keycode == 13)
		   {
		   myfield.form.submit();
		   return false;
		   }
		else
		   return true;
	}

function echeck(str)
	{
		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){alert("Please enter a valid email address."); return false}
		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){alert("Please enter a valid email address."); return false}
		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){alert("Please enter a valid email address."); return false}
		if (str.indexOf(at,(lat+1))!=-1){alert("Please enter a valid email address."); return false}
		if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){alert("Please enter a valid email address."); return false}
		if (str.indexOf(dot,(lat+2))==-1){alert("Please enter a valid email address."); return false}	
		if (str.indexOf(" ")!=-1){alert("Please enter a valid email address."); return false}
		return true;
	}

function ValidateForm()
	{
		var emailID=document.getElementById('email');
		
		if ((emailID.value==null)||(emailID.value=="")){alert("Please enter your email address."); emailID.focus(); return false}
		if (echeck(emailID.value)==false){emailID.value=""; emailID.focus(); return false}
		document.getElementById('newsletter_signup').submit();
	}

 function bookmark ()
	{
		if (window.sidebar) {window.sidebar.addPanel(window.document.title,window.document.location,"");}
		else if(window.external) {window.external.AddFavorite(window.document.location,window.document.title); }
		else if(window.opera && window.print) {return true;}
	}

function ProcessFeed(feed,type,args)
	{
		if (type == 'blog' && feed.entry[0].title != 'NULL')
		{
			feedData = '<div class="blog_tease"><h3><a href="'+feed['channel']['link']+'" title="'+feed['channel']['title']+'">'+feed['channel']['title']+'</a></h3><h2><a href="'+feed.entry[0].link+'" title="'+feed.entry[0].title+'"><span>New:</span> '+feed.entry[0].title+'</a></h2></div>';
			
			document.write(feedData);
			feedData = '';
		}

		if (type == 'photo')
		{
			for (var i = 0; i < feed.entry.length; i++)
			{
				photoTitle = feed.entry[i].title;
				photoUrl = feed.entry[i].link;
				photoThumb = feed.entry[i].thumbnail;
				photoAuthor = feed.entry[i].author;
				
				if (photoUrl != 'NULL')
				{
					feedData = '<a href="'+photoUrl+'" class="photo clearfix" title="'+photoTitle+'"><img src="'+photoThumb+'" alt="'+photoTitle+'" title="'+photoTitle+'" />'+photoTitle+'<br />by <span>'+photoAuthor+'</span></a>';
				}

				document.write(feedData);
				feedData = '';
			}
		}

		if (type == 'top_ads')
		{
			for (var i = 0; i < feed.advertisers.length; i++)
			{
				adName = travAdvListData.advertisers[i].advertiser.name;
				adTitle = travAdvListData.advertisers[i].advertiser.title;
				adID = travAdvListData.advertisers[i].advertiser.id;

				feedData = '<div class="ad"><a href="http://'+args+'.shopping.wickedlocal.com/ROP/ads.aspx?advid='+adID+'" title="'+adTitle+'">'+adName+'</a></div>';
				
				document.write(feedData);
				feedData = '';
			}
		}
		
		if (type == 'weather')
		{
			var highTemp = forecast_json.day1hiTmpF;
			var lowTemp = forecast_json.day1loTmpF;
			
			var currentTemp = weather_json.TemperatureF;
			
			if (currentTemp != 'undefined')
			{
				if (lowTemp != ''){lowTemp = '<div class="lowTemp">Low: '+lowTemp+'&deg;</div>'}
				if (highTemp != ''){highTemp = '<div class="highTemp">High: '+highTemp+'&deg;</div>'}
				document.getElementById('weather_data_target').innerHTML = '<div class="subHeader2_left"><div class="weatherTab">Weather</div><div class="currentTemp"><a href="' + site_url + '/weather">'+currentTemp+'&deg;</a></div></div><div class="subHeader2_right" style="margin-top: 8px;">'+highTemp+lowTemp+'<div class="fullForecast"><a href="' + site_url + '/weather" title="Full Forecast">Full Forecast</a></div></div>';
			}
		}
	}
	
function SwapStyle(objId,className) 
	{
		document.getElementById(objId).className = className;
	}

function ProcessTopAds(topAdsObj,topAdsType){
	taTarget = document.getElementById('top_ads_' + topAdsType);
	switch (topAdsType) {
		default: {
			for (var i=0; i < topAdsObj.length; i++) {
				taContent =  '<' + 'div class="ad">' + "\n";
				taContent += '	<' + 'a href="#" id="ad_' + topAdsType + '_' + topAdsObj[i].ad_num + '_title' + '" onclick="ToggleAd(\'ad_' + topAdsType + '_' + topAdsObj[i].ad_num + '\'); this.blur(); return false;" class="ad-title">' + topAdsObj[i].ad_title + '<' + '/a>' + "\n";
				taContent += '	<' + 'div class="ad-content" id="ad_' + topAdsType + '_' + topAdsObj[i].ad_num + '" style="display: none;">' + topAdsObj[i].ad_text + '<' + '/div>' + "\n";
				taContent += '<' + '/div>' + "\n\n";
				taTarget.innerHTML += taContent;
			}
			break;
		}
	}
}

function ToggleTopAds(topAdsType) {
	togTarget = document.getElementById('top_ads_' + topAdsType);
	togTitleTarget = document.getElementById('top_ads_title_' + topAdsType);
	togTarget.style.display = 'block';
	togTitleTarget.className = 'active';
	if (togSwitch != topAdsType) {
		if (togSwitch != '') {
			document.getElementById('top_ads_' + togSwitch).style.display = 'none';
			document.getElementById('top_ads_title_' + togSwitch).className = '';
		}
		togSwitch = topAdsType;
	}
}

function ToggleAd(adID) {
	adTarget = document.getElementById(adID);
	adTargetTitle = document.getElementById(adID + '_title');
	if (adTarget.style.display == 'none') {
		adTarget.style.display = 'block';
		adTargetTitle.className = 'active-ad';
	}
	else {
		adTarget.style.display = 'none';
		adTargetTitle.className = '';
	}
	if (adSwitch != adID) {
		if (adSwitch != '') {
			document.getElementById(adSwitch).style.display = 'none';
			document.getElementById(adSwitch + '_title').className = '';
		}
		adSwitch = adID;
	}
}

/*
 * Javascript Humane Dates
 * Copyright (c) 2008 Dean Landolt (deanlandolt.com)
 * Re-write by Zach Leatherman (zachleat.com)
 * 
 * Adopted from the John Resig's pretty.js
 * at http://ejohn.org/blog/javascript-pretty-date
 * and henrah's proposed modification 
 * at http://ejohn.org/blog/javascript-pretty-date/#comment-297458
 * 
 * Licensed under the MIT license.
 */

function humane_date(date_str) {
    var time_formats = [
    [60, 'just now'],
    [90, '1 minute'],
    // 60*1.5
    [3600, 'minutes', 60],
    // 60*60, 60
    [5400, '1 hour'],
    // 60*60*1.5
    [86400, 'hours', 3600],
    // 60*60*24, 60*60
    [129600, '1 day'],
    // 60*60*24*1.5
    [604800, 'days', 86400],
    // 60*60*24*7, 60*60*24
    [907200, '1 week'],
    // 60*60*24*7*1.5
    [2628000, 'weeks', 604800],
    // 60*60*24*(365/12), 60*60*24*7
    [3942000, '1 month'],
    // 60*60*24*(365/12)*1.5
    [31536000, 'months', 2628000],
    // 60*60*24*365, 60*60*24*(365/12)
    [47304000, '1 year'],
    // 60*60*24*365*1.5
    [3153600000, 'years', 31536000],
    // 60*60*24*365*100, 60*60*24*365
    [4730400000, '1 century'],
    // 60*60*24*365*100*1.5
    ];

    var time = ('' + date_str).replace(/-/g, "/").replace(/[TZ]/g, " "),
    dt = new Date,
    seconds = ((dt - new Date(time) + (dt.getTimezoneOffset() * 60000)) / 1000),
    token = ' ago',
    i = 0,
    format;

    if (seconds < 0) {
        seconds = Math.abs(seconds);
        token = '';
    }

    while (format = time_formats[i++]) {
        if (seconds < format[0]) {
            if (format.length == 2) {
                return format[1] + (i > 1 ? token: '');
                // Conditional so we don't return Just Now Ago
            } else {
                return Math.round(seconds / format[2]) + ' ' + format[1] + (i > 1 ? token: '');
            }
        }
    }

    // overflow for centuries
    if (seconds > 4730400000)
    return Math.round(seconds / 4730400000) + ' centuries' + token;

    return date_str;
};
