

var WelcomeMessages = new Array();
WelcomeMessages[0] = "Did you catch a gitis today, " + prefMyName + "?";
WelcomeMessages[1] = "Good Wind " + prefMyName + "!";
WelcomeMessages[2] = "Go Gitis " + prefMyName + "!" ;
WelcomeMessages[3] = "Welcome " + prefMyName + "!" ;
WelcomeMessages[4] = "Hi " + prefMyName + "!" ;
WelcomeMessages[5] = "Aloha " + prefMyName + "!" ;
WelcomeMessages[6] = "More Zzzz " + prefMyName + "!" ;
var randomWelcomeMessageInt = Math.floor(Math.random()*WelcomeMessages.length);

var intRound = 0;

try{
	if(prefRefresh){}
	else{prefRefresh="NoRefresh";}
}
catch(x){
	prefRefresh = "NoRefresh";
}

try{
	if(prefFlickrSearch){}
	else{prefFlickrSearch="frisbeefreestyle";}
}
catch(u){
	prefFlickrSearch = "frisbeefreestyle";
}

try{
	if(prefShowNextButton){}
	else{prefShowNextButton=false;}
}
catch(t){
	prefShowNextButton = false;
}




function fnLoad()
{
	//alert(prefFlickrSearch);
	fnPrintHtml();
	//if()
	//{
	//	if( intRound < 10 )
	//	{
	//		intRound ++;
	//		//alert("load");
	//		fnLoad();
	//	}
	//	else{
	//		//alert("unable to resize");
	//	}
	//}
	//else{intRound = 0;}

	fnRefresh();
}

function fnRefresh()
{
	if(prefRefresh != "NoRefresh")
	{
		var sec = 30000;
		switch(prefRefresh)
		{
			case "10sec": 
				sec = 10000;
				break;
			case "20sec":
				sec = 20000;
				break;
			case "30sec":
				sec = 30000;
				break;
			case "40sec":
				sec = 40000;
				break;
			case "50sec":
				sec = 50000;
				break;
			case "60sec":
				sec = 60000;
				break;
			case "10 sec": 
				sec = 10000;
				break;
			case "20 sec": 
				sec = 20000;
				break;
			case "30 sec": 
				sec = 30000;
				break;
			case "40 sec": 
				sec = 40000;
				break;
			case "50 sec": 
				sec = 50000;
				break;
			case "60 sec": 
				sec = 60000;
				break;
		}

		if (window.focus)
		{
			window.setTimeout("fnLoad()", sec);
		}
	}
}

function fnPrintHtml()
{

	fnGel("main").innerHTML = "<span id='photo'></span><span id='text'></span>";
	
	switch(prefSource)
	{
		case "FpaDecades":
			fnPrintRandomFPAPhoto();
			break;
		case "FpaGallery":
			fnGel("photo").innerHTML = "<span>" + prefMyName + " sorry, gallery feauture is not available now</span>";
			break;
		case "FpaUserGallery":
			fnGel("photo").innerHTML = "<span>" + prefMyName + " sorry, user gallery feauture is not available now</span>";
			break;
		case "StaticImage":
			fnPrintStaticPhoto();
			break;
		case "ShrednowGallery":
			fnGel("photo").innerHTML = "<span>" + prefMyName + " sorry, Shrednow gallery feauture is not available now</span>";
			break;
		case "FrisbeeFreestyleGallery":
			fnPrintFrisbeeFreestyleRandom();
			break;
		case "FlickrSearch":
			fnPrintFlickrPhotos();
			break;
		case "MixAll":
			fnPrintMixAll();
			break;
		default:
			fnGel("text").innerHTML = "<span>" + prefMyName + " sorry, occured an error!</span>";
			break;
	}

	var html = "";
	if(prefIsWelcomeMessageEnabled)
	{
		html += "<div style=\"border: solid 1px " + prefMessageColor + "; position: absolute; top: 0px; left: 0px; z-index: 3; color: " + prefMessageColor + "; background-color: " + prefMessageBackColor + "; display: inline; font-size: 10px; padding: 1px;\">";
		html += "<span style='color: " + prefMessageColor + ";'><b>" + WelcomeMessages[randomWelcomeMessageInt] + "</b></span>";
		html += "</div>";
	}

	fnGel("text").innerHTML += html;
	////fnResizeImg();
}

function fnPrintStaticPhoto()
{
	var imageUrl = "http://freestyledisc.org/images/gallery/90-11.jpg";
	var imagePageUrl = "http://freestyledisc.org/gallery90-11.html";
	var html = "";


	html += "<div style=\"text-align: center; padding: 0; margin: 0; max-height: 300px; max-width: 400px; \">";
	//html += "<h5 style=\"text-align: left; padding: 0; margin: 0;\">Hola " + prefMyName + " !</h5>";
	html += "<a title=\"Frisbee Freestyle Players Ass.\" target=\"_blank\" ";
	html += "href=\"" + imagePageUrl + "\">";
	html += "	<img border=\"0\" src=\"" + imageUrl + "\"";
	html += "	style=\"max-height: 300px; max-width: 400px; \" ";
	html += "	alt=\"Frisbee Freestyle Players Ass.\"";
	html += "	title=\"FPA Frisbee Freestyle Players Ass.\" />";
	html += "</a>";
	html += "<div style=\"border: solid 1px " + prefMessageColor + "; position: absolute; bottom: 0px; left: 0px; z-index: 3; color: " + prefMessageColor + "; background-color: " + prefMessageBackColor + "; display: inline; white-space: no-wrap; font-size: 10px; padding: 1px;\">";
	html += "<span style='color: " + prefMessageColor + ";'><b style='color:" + prefMessageColor + "'>Credits: <a href='http://freestyledisc.org'  target='_blank' style='color: " + prefMessageColor +";'>FPA Frisbee Freestyle Players Ass.</a></b></span>";
	html += "</div>";
	html += "</div>";
	
   
	fnGel("photo").innerHTML = html;

}

function fnPrintMixAll()
{
	var randomGallery = Math.floor(Math.random()*4)+1;
	switch(randomGallery)
	{
		case 1: 
			fnPrintFrisbeeFreestyleRandom();
			break;
		case 2: 
			fnPrintRandomFPAPhoto();
			break;
		case 3:
			fnPrintFlickrPhotos();
			break;
		case 4:
			fnPrintFlickrPhotos();
			break;
	}
}

function fnPrintFlickrPhotos()
{
	var imageUrl = "";
	var imagePageUrl = "";
	var description = "";

	var rssUrl = "http://api.flickr.com/services/feeds/photos_public.gne?tags=" + encodeURI(prefFlickrSearch) + "&format=RSS_200";

	_IG_FetchXmlContent(rssUrl,
			function(response)
			{
				if (response == null){ 
					 alert("There is no data.");
					 return;
				}


				var entryLenght = response.getElementsByTagName("entry").length;

				var randomEntry = Math.floor(Math.random()*entryLenght);

				var entrySelected = response.getElementsByTagName("entry")[randomEntry];

				var imageUrl = "";
				var imageTitle = "";

				imageTitle = ""; //entrySelected.getElementsByTagName("title")[0].nodeValue;
				//alert(imageTitle);
				for (i=0;i<entrySelected.getElementsByTagName("link").length ;i++ )
				{

					if(entrySelected.getElementsByTagName("link")[i].getAttribute("type") == "image/jpeg")
					{
						imageUrl = entrySelected.getElementsByTagName("link")[i].getAttribute("href");
					}
					
				}

				imageUrl = imageUrl.replace("_o.jpg","_m.jpg");
				//alert(imageTitle);
				//alert(imageUrl)
				fnGel("photo").innerHTML = "";
				fnGel("photo").innerHTML = getHtmlPhoto(imageUrl,imageUrl,"flickr");
				window.setTimeout("fnResizeImg();",5);
				//fnResizeImg();
				//return x;
				//alert(html);
				//return html;
				//else{alert(feed);}

				// Start building HTML string that will be displayed in gadget.


				// Access the fields in the feed
				//html += "<div><b>" + feed.Title + "</b></div>";
				//html += "<div>" + feed.Description + "</div><br>";

				//alert(feed.Entry.length);
				//var intEntryLenght = (feed.Entry.length)*1;
				//var randomEntry = Math.floor(Math.random()*intEntryLenght)+1;
				//alert(randomEntry);
				//var html = "";
				//html += "intEntryLenght: " + intEntryLenght + "<br>";
				//html += "randomEntry: " + randomEntry + "<br>";
				//html += "feed.Entry[randomEntry]: " + feed.Entry[randomEntry] + "<br>";
				//html += "feed.Entry[randomEntry].Link: " + feed.Entry[randomEntry].Link + "<br>";
				
				//imageUrl = feed.Entry[randomEntry].Link;
				//imagePageUrl = feed.Entry[randomEntry].Link;
				//description = feed.Entry[randomEntry].Title;
				//alert(feed.Entry[randomEntry].Title + "\n" + feed.Entry[randomEntry].Link + "\n" + feed.Entry[randomEntry].Date);
				//for(i=0;i<feed.Entry[randomEntry].length;i++)
				//{
				//	alert(feed.Entry[randomEntry][i]);
				//}

				//html += getHtmlPhoto(imageUrl,imagePageUrl,description);
				//alert("html:\n" + html);
				//document.write(html);
			}
			//, 9, false);
			//return html;
	 // The rest of the function parameters, which are optional: the number
     // of entries to return, and whether to return summaries.
     );
	//alert(html);
	//window.setTimeout("return " + html + "; ", 1);
	//return html;
}

function fnPrintFrisbeeFreestyleRandom()
{
	var randomGallery = Math.floor(Math.random()*5)+1;
	var randomPhoto = Math.floor(Math.random()*6) +1;

	var imageUrl = "http://www.freestyle-frisbee.com/random_images/";
	imageUrl += randomGallery + "/image" + randomPhoto + ".jpg";
	var imagePageUrl = imageUrl; //no html page to link;

	//alert(imageUrl);
	//return getHtmlPhoto(imageUrl,imagePageUrl,"www.freestyle-frisbee.com");
	fnGel("photo").innerHTML = getHtmlPhoto(imageUrl,imagePageUrl,"www.freestyle-frisbee.com");
	window.setTimeout("fnResizeImg();",5);
}

function fnPrintRandomFPAPhoto()
{
	var randomGalleryInt = Math.floor(Math.random()*4)+1;
	var randomPhoto = Math.floor(Math.random()*12) +1;
	var randomGallery = "00";
	
	switch(randomGalleryInt)
	{
		case 1: 
			randomGallery = "70"; 
			break;
		case 2: 
			randomGallery = "80"; 
			break;
		case 3: 
			randomGallery = "90"; 
			break;
		case 4: 
			randomGallery = "00"; 
			break;
	}
	
	var imageUrl = "http://freestyledisc.org/images/gallery/";
	imageUrl += randomGallery + "-" + randomPhoto + ".jpg";
	var imagePageUrl = "http://freestyledisc.org/gallery" + randomGallery + "-" + randomPhoto + ".html";

	//return getHtmlPhoto(imageUrl,imagePageUrl,"Frisbee Freestyle Players Ass.");
	fnGel("photo").innerHTML = getHtmlPhoto(imageUrl,imagePageUrl,"FPA: Freestyle Players Ass. www.freestyledisc.org");
	window.setTimeout("fnResizeImg();",5);
}

function getHtmlPhoto(imageUrl,imagePageUrl, altText)
{
	var	html= "";
	html += "<div style=\"vertical-align: middle; text-align: center; padding: 0; margin: 0; max-height: 300px; max-width: 400px; \">";
	//html += "<h5 style=\"text-align: left; padding: 0; margin: 0;\">Hola " + prefMyName + " !</h5>";
	html += "<a title=\"" + altText + "\" target=\"_blank\" ";
	html += "href=\"" + imagePageUrl + "\">";
	html += "	<img id='mainImage' border=\"0\" src=\"" + imageUrl + "\"";
	//html += "	style=\"max-height: 300px; max-width: 400px; \" ";
	html += "	alt=\"" + altText + "\"";
	html += "	title=\"" + altText +"\" />";
	html += "</a>";
	//html += "<div style=\"position: absolute; top: 30px; left: 0px; width: 16px; height: 16px; z-index: 2; \">";
	//html += "<a href=\"javascript:fnLoad();\" alt=\"Next Random Photo\" title=\"Next Random Photo\">";
	//html += "	<img border=\"0\" alt=\"Next Random Photo\" src=\"http://www.freestyledisc.org/gallery/images/next.gif\" ";
	//html += " alt=\"Next Random Photo\" />";
	if(prefShowNextButton && prefRefresh == "NoRefresh" )
	{
		html += "<input type='button' onClick='fnLoad()' name='next' value='next' style='position: absolute; ";
		if(prefIsWelcomeMessageEnabled)
			html += "top: 15px; ";
		else{
			html += "top: 0px; ";
		}
		html += "left: 0px; z-index: 2; padding: 0px; margin: 0px; border: solid 1px " + prefMessageColor + "; color: " + prefMessageColor + "; background-color: " + prefMessageBackColor + "; font-size: 10px;'";
		//html += "<a/>";
		//html += "</div>";
	}
	html += "</div>";
	//html += "<div style=\"border: solid 1px " + prefMessageColor + "; position: absolute; top: 400px; left: 0px; z-index: 3; color: " + prefMessageColor + "; background-color: " + prefMessageBackColor + "; display: inline; white-space: no-wrap; font-size: 10px; padding: 1px;\">";
	//html += "<span style='color: " + prefMessageColor + ";'><b style='color:" + prefMessageColor + "'>Credits: <a href='http://freestyledisc.org'  target='_blank' style='color: " + prefMessageColor +";'>FPA Frisbee Freestyle Players Ass.</a></b></span>";
	//html += "</div>";

	return html;
}

function fnResizeImg()
{
	var _resizeWidth  = 400;
	var _resizeHeight = 295;
	var _resizeClass  = "news-img";

	var imgArray = document.getElementsByTagName( "IMG" );

	var imgObj = fnGel("mainImage");

	try{
		if ( imgObj.id == ""){}
		if ( imgObj.width == ""){}
	}
	catch(s){
		//alert(s);
		return false;
	}
			
	if ( imgObj.id == "mainImage" )
	{

		if(imgObj.width < 50 || imgObj.height < 50)
		{
			//alert("setTimeout bef " + imgObj.width + " * " +imgObj.height + " = " + perc);
			window.setTimeout("fnResizeImg()", 1);
			return false;
			//alert("setTimeout aft " + imgObj.width + " * " +imgObj.height + " = " + perc);
			//alert("resized " + imgObj.width + " * " +imgObj.height + " = " + perc);
		}
		else if(imgObj.width > _resizeWidth)
		{
			var perc = imgObj.width/_resizeWidth;
			var h = imgObj.height / perc;
			var w = _resizeWidth;
		
			//alert("image: " + imgObj.width + " * " + imgObj.height + " resize w perc:" + perc + " = w:" + w + " * h:" + h );
			if(perc > 1.7 || imgObj.width < 100 || imgObj.height < 100)
			{
				//alert("return true - a " + imgObj.width + " * " + imgObj.height + " = " + perc);
				//alert("return true - a " + imgObj.width + " * " +imgObj.height + " = " + perc);
				return true;
			}			
			//alert("resizing a " + imgObj.width + " * " +imgObj.height + " to " + w + " * " + h + " = " + perc);
			//imgObj.width = w + "px";
			//imgObj.height = h + "px";
			imgObj.style.width = w + "px";
			//imgObj.style.height = h + "px";
			//alert("resized a " + imgObj.width + " * " +imgObj.height + " = " + perc);
			return false;
		}
		else if(imgObj.height > _resizeHeight)
		{
			var perc = imgObj.height/_resizeHeight;
			var h = _resizeHeight;
			var w =  imgObj.width / perc;
			
			//alert("image: " + imgObj.width + " * " + imgObj.height + " resize h perc:" + perc + " = w:" + w + " * h:" + h );
			if(perc > 1.7 || imgObj.width < 100 || imgObj.height < 100)
			{
				//alert("return true - b " + imgObj.width + " * " +imgObj.height + " = " + perc);
				//alert("return true - b " + imgObj.width);
				return true;
			}		
			//alert("resizing b " + imgObj.width + " * " +imgObj.height + " to " + w + " * " + h + " = " + perc);
			//imgObj.width = w + "px";
			//imgObj.height = h + "px";
			///imgObj.style.height = h + "px";
			imgObj.style.width = w + "px";
			//alert("resized b " + imgObj.width + " * " + imgObj.height + " = " + perc);
			return false;
		}
		else
		{
			//alert("return false a " + imgObj.width + " * " +imgObj.height + " = " + perc);
			return false;
		}
		
		//alert("return false b " + imgObj.width + " * " +imgObj.height + " = " + perc);
		return false;
	}

	//alert("return false b " + imgObj.width + " * " +imgObj.height + " = " + perc);
	return false;
}