//Author:Aaron Buffie


//Set feed url's by location
function buildRSS(zip, city, state){
	var usBut = ['Top Stories', 'Business', 'Local News', 'Dining', 'Sports', 'Kids', 'Music'];
	var caBut = ['Top Stories', 'Business', 'Local News', 'Technology', 'Sports', 'Health', 'Entertainment'];
	if(zip.match(/[ABCEGHJKLMNPRSTVXY]\d[A-Z] \d[A-Z]\d/i)){
		topStories = 'http://d.yimg.com/bf/rss/canada';
		business = 'http://d.yimg.com/bf/rss/business';
		sports = 'http://d.yimg.com/bf/rss/sports';
		dining = 'http://d.yimg.com/bf/rss/tech';
		family = 'http://d.yimg.com/bf/rss/health';
		entertainment = 'http://d.yimg.com/bf/rss/entertainment';
		for(i = 0; i < 7; i++){
			document.getElementById('but' + i).innerHTML = '<a href="javascript:void(0);" onClick="return false;">'+caBut[i]+'</a>';
		}
	}else{
		topStories = 'http://rss.news.yahoo.com/rss/topstories';
		business = 'http://rss.news.yahoo.com/rss/business';
		sports = 'http://rss.news.yahoo.com/rss/sports';
		dining = 'http://local.yahoo.com/rss/restaurants?csz='+ userCity +' '+ userState;
		family = 'http://rss.news.yahoo.com/rss/parenting';
		entertainment = 'http://rss.news.yahoo.com/rss/music';
		for(i = 0; i < 7; i++){
			document.getElementById('but' + i).innerHTML = '<a href="javascript:void(0);" onClick="return false;">'+usBut[i]+'</a>';
		}
	}
}

//-----------Image sizeing functions
//
// 

function scaleImage(img) {//Sizing for scolling large images
	var height = "";
	var width = "";
	var newImage = "";

	
	if (img.height > img.width){
		width = "280px";
		descClass = "descRight";
		newImage = '<a href="' + img.imgLink +'" border="0" target="_blank" ><img src="' + img.src + '" border="0" align="left" width="' + width + '" /></a>';
	}else{
		width = "561px";
		//height = "266px";
		descClass = "desc";
		newImage = '<a href="' + img.imgLink +'" border="0" target="_blank" ><img src="' + img.src + '" border="0" align="center" width="' + width + '" /></a>';
	}
	return newImage;
	
} 




function scaleThumb(img) {// Sizing for standard tumbnails
	var newImage = '<a href="' + img.imgLink + '" target="_blank" border="0"><img src="' + img.src + '" border="0" height="94px" /></a>';
	
	return newImage;
} 

function scaleScrollingThumb(img) {// Sizing for scolling tumbnails
	var S = img.src;
	var height;
	var width;
	var hScale = 54 / img.height;
	var wScale = 90 / img.width;
	if (hScale > wScale){
		width = img.width * wScale; 
		height = img.height * hScale;
	}else{
		width = img.width * hScale; 
		height = img.height * hScale; 
	}
	//var im = "<img src='"+img.src+"' height='"+height+"' width='"+width+"' />"
	var I = new Image(width, height);
	I.src = S;
	return I;
}


//-------------END sizeing





function stripTags(str){
//---strip dis of all unwated tags
	str = (str.replace(/<\/?h1[^>]*>/gi, ""));
	str = (str.replace(/<\/?h2[^>]*>/gi, ""));
	str = (str.replace(/<\/?h3[^>]*>/gi, ""));
	str = (str.replace(/<\/?h4[^>]*>/gi, ""));
	str = (str.replace(/<\/?div[^>]*>/gi, ""));
	str = (str.replace(/<\/?p[^>]*>/gi, ""));
	str = (str.replace(/<\/?span[^>]*>/gi, ""));
	str = (str.replace(/<\/?table[^>]*>/gi, ""));
	str = (str.replace(/<\/?tr[^>]*>/gi, ""));
	str = (str.replace(/<\/?th[^>]*>/gi, ""));
	str = (str.replace(/<\/?td[^>]*>/gi, ""));
	str = (str.replace(/<br ?\/?>/gi, ""));
	str = (str.replace(/<hr ?\/?>/gi, ""));
	return str;

}



function getImageSrc(img){
	var img_array = [];
	img_array = img.split(" ");
	for (i=0; i < img_array.length; i++) {
		if(img_array[i].search("src") != -1){
			var src = img_array[i];
			src = src.replace(/src?=?"/g,"");
			src = src.replace(/"/g,"");
			src = src.replace(/'/g,"");
			var srcEnd = src.indexOf("\?");
			if(srcEnd > -1){
				src = (src.substring(0, srcEnd));
				return src;
			}else{
				return src;
			}
		}
	}
} 

function getImageLink(img){
	var img_array = [];
	img_array = img.split(" ");
	for (i=0; i < img_array.length; i++) {
		if(img_array[i].search("href") != -1){
			var link = img_array[i];
			link = link.replace(/href?=?"/g,"");
			linkStart = link.indexOf("\*http:\/\/") + 1;
			if(linkStart == -1){
				linkStart = 0;
			}
			var linkEnd = link.indexOf("\"");
			if (linkEnd){
				link = (link.substring(linkStart, linkEnd));
				return link;
			}
			return link;
			
		}
	}
}
function getLink(link){
	link = link.replace(/href?=?"/g,"");
	linkStart = link.indexOf("\*http:\/\/") + 1;
	if(linkStart == -1){
		linkStart = 0;
	}
	var linkEnd = link.length;
	if (linkEnd){
		link = (link.substring(linkStart, linkEnd));
		return link;
	}
	return link;	
}

function getText(str){
	str = (str.replace(/<\/?a[^>]*>/gi, ""));
	str = (str.replace(/<img[^>]*\/?>/gi, ""));
	str = (str.replace(/<a.*[^>]>/gi, ""));
	str = (str.replace(/<\/?a>/gi, ""));
	str = (str.replace(/<img.*[^>]>/gi, ""));
	str = (str.replace("AP -", ""));
	return str;
}



function viewWindow(i){
	large_img = scaleImage(singleImgFeed[i].img);
	var html ="";
	html += '<div class="largeImg">' + large_img + '</div>';
	html += '<div class="' + descClass + '"><p>' + singleImgFeed[i].description + '</p></div>';
	document.getElementById("view").innerHTML = html;
	
}



//Change zip cookie functions
var zipCode;


var zipId = "zip_code";
var zipBox = "zipBox";


function setCookie(c_name, expiredays){
	var usReg = /^\d{5}?$/;
	var caReg = /[ABCEGHJKLMNPRSTVXY]\d[A-Z] \d[A-Z]\d/i;
	var zipbox = document.getElementById("setZipCode");
	zip = zipbox.value.toUpperCase();
	if (!zip.match(usReg)){
		if(!zip.match(caReg)){
			alert("invalid zip");
			return false;
		}else{
			//Zip vaild
		}
	}else{
		//Zip vaild
	}
	var locFound;
	$.getScript(DEFAULT_URL+"/gajet/setLoc.php?zipcode=" + escape(zip), function(){		
		
			zipbox.value = "";
			document.getElementById(zipBox).style.display = "none";
			//loadContentWeather(zipC);
			
				if(RSScontent == "but0"){
					rssLoadImg.innerHTML = "";
					rssLoadImg.appendChild(rssLoading);
					getFeed(topStories, 'preload', 15);}
				
				if(RSScontent == "but1"){
					rssLoadImg.innerHTML = "";
					rssLoadImg.appendChild(rssLoading);
					getFeed(topStories, 'none', 15);
					getFeed(business, 'strait', 15);}
				
				if(RSScontent == "but2"){
					rssLoadImg.innerHTML = "";
					rssLoadImg.appendChild(rssLoading);				
					getFeed(topStories, 'none', 15);
					loadContent("local|" + userCity + "|description|" + userState);}
				
				if(RSScontent == "but3"){
					rssLoadImg.innerHTML = "";
					rssLoadImg.appendChild(rssLoading);
					getFeed(dining, 'strait', 15);
					getFeed(topStories, 'none', 15);}
				
				if(RSScontent == "but4"){
					rssLoadImg.innerHTML = "";
					rssLoadImg.appendChild(rssLoading);
					getFeed(topStories, 'none', 15);
					getFeed(sports, 'strait', 15);}
				
				if(RSScontent == "but5"){
					rssLoadImg.innerHTML = "";
					rssLoadImg.appendChild(rssLoading);
					getFeed(topStories, 'none', 15);
					getFeed(family, 'strait', 15);}
			
				if(RSScontent == "but6"){
					rssLoadImg.innerHTML = "";
					rssLoadImg.appendChild(rssLoading);
					getFeed(topStories, 'none', 15);
					getFeed(entertainment, 'strait', 15);}
					
		
		
	});
}


function getCookie(c_name){
	if (document.cookie.length > 0){
		var c_zip = document.cookie.indexOf(c_name + "=");
		if (c_zip != -1){
			c_zip = c_zip + c_name.length + 1;
			var z_end = document.cookie.indexOf(",",c_zip);
			zipC = document.cookie.substring(c_zip, z_end);
		
			var c_city = document.cookie.indexOf(zipC + ",");
			c_city = c_city + zipC.length + 1;
			var c_end = document.cookie.indexOf(",",c_city);
			userCity = document.cookie.substring(c_city, c_end);
		
			var c_state = document.cookie.indexOf(userCity + ",");
			c_state = c_state + userCity.length + 1;			
			var s_end = document.cookie.indexOf(",", c_state);
			userState = document.cookie.substring(c_state, s_end);
			
			var c_country = document.cookie.indexOf(userState + ",");
			c_country = c_country + userState.length + 1;			
			var co_end = document.cookie.indexOf(";", c_country);
			if(co_end == -1){co_end = document.cookie.length;}
			userCountry = document.cookie.substring(c_country, co_end);
			
			
			zipC = unescape(zipC);
			userCity = unescape(userCity);
			userState = unescape(userState);
			$('#weatherBox').load(DEFAULT_URL+'/gajet/weather/weather.php?unit=0&co='+ userCountry +'&city='+ escape(userCity) +'&state='+ userState);
	
		}
	}
		document.getElementById(zipId).innerHTML =  zipC;
		buildRSS(zipC, userCity, userState);
					
		return zipC;
}
	
function convert(degree){
$('#weatherBox').load(DEFAULT_URL+'/gajet/weather/weather.php?unit='+ degree +'&co='+ userCountry +'&city='+ escape(userCity) +'&state='+ userState);	
	
}