window.onload = init;

function init()
{
	
	icon_add();
	
}

function browser_get()
{
	var browser = navigator.userAgent;
	if(browser.indexOf("MSIE") != -1)
	{
		return 0;
	}
	else
	{
		return 1;
	}
}

function rss_load(rss_url,rss_format,number)
{	
	var http_request = false;
	
	if(window.XMLHttpRequest) // Mozilla, Safari,...
	{
		
		http_request = new XMLHttpRequest();
		
		if(http_request.overrideMimeType)
		{
		
			// set type accordingly to anticipated content type
			http_request.overrideMimeType('text/xml');
			//http_request.overrideMimeType('text/html');
		
		}
		
	}
	else if(window.ActiveXObject) // IE
	{

		try
		{
		
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		
		}
		catch(e)
		{
		
			try
			{
			
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
				
			}
			catch(e)
			{
			
			
			}
		
		}
	
	}
	
	if(!http_request)
	{
	
		alert('Cannot create XMLHTTP instance');
		return false;
	
	}

	http_request.onreadystatechange = function(){rss_parse(http_request,rss_format,number);};
	http_request.open('GET', rss_url, true);
	http_request.send(null);
}

function date_format(old_date,format)
{
	var new_date = new Date(old_date);
	var return_date = "";
	var month_array = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
	
	switch(format)
	{
		case 1:		return_date = (new_date.getMonth() + 1) + "/" + new_date.getDate() + "/" + new_date.getFullYear(); //MM/DD/YYYY string
					break;
		case 2:		return_date = (new_date.getMonth() + 1) + " " + new_date.getFullYear(); //MM YYYY string
					break;
		case 3:		if(new_date.getDate() < 10){return_date = "0" + new_date.getDate();}
					else{return_date = "" + new_date.getDate();} //DD number
					break;
		case 4:		if(new_date.getMonth() + 1 < 10){return_date = "0" + new_date.getMonth() + 1;}
					else{return_date = "" + new_date.getMonth() + 1;} //MM number
					break;
		case 5:		return_date = "" + month_array[new_date.getMonth() + 1]; //MMM string
					break;
		case 6:		return_date = "" + new_date.getFullYear(); //YYYY
					break;
	}
	
	return return_date;
	
}

function simple_title(str)
{
	str = str.toLowerCase();
														
	str = str.replace(/\s+/g,'');
	
	return str;
}

function rss_parse(http_request,rss_format,number)
{
	if(http_request.readyState == 4)
	{
	
		if(http_request.status == 200)
		{
			var result = http_request.responseText;
			
			var xmlDoc;
			var x;
			var i;
			var j;
			var k;
			var node_array = [];
			var output = "";
				
			if(browser_get()) //All Others
			{
				xmlDoc = (new DOMParser()).parseFromString(result, "text/xml");
				
				x = xmlDoc.getElementsByTagName("item");
				
				if(x.length < number){number = x.length;}
				
				for(i=0;i<number;i++)
				{
					node_array[i] = {'title':'','link':'','pubDate':'','description':'','subtitle':'','large':'','small':''};
					for(j=1;j<x[i].childNodes.length;j++)
					{
						if(x[i].childNodes[j].tagName == "title"){node_array[i].title = x[i].childNodes[j].textContent;}
						else if(x[i].childNodes[j].tagName == "link"){node_array[i].link = x[i].childNodes[j].textContent;}
						else if(x[i].childNodes[j].tagName == "pubDate"){node_array[i].pubDate = x[i].childNodes[j].textContent;}
						else if(x[i].childNodes[j].tagName == "description"){node_array[i].description = x[i].childNodes[j].textContent;}
						else if(x[i].childNodes[j].tagName == "bsa:multimedia")
						{
							for(k=1;k<x[i].childNodes[j].childNodes.length;k++)
							{
								if(x[i].childNodes[j].childNodes[k].tagName == "bsa:image")
								{
									if(x[i].childNodes[j].childNodes[k].childNodes.length > 1){node_array[i].small = x[i].childNodes[j].childNodes[k].childNodes[1].textContent;}
									if(x[i].childNodes[j].childNodes[k].childNodes.length > 3){node_array[i].large = x[i].childNodes[j].childNodes[k].childNodes[3].textContent;}
								}
								else if(x[i].childNodes[j].childNodes[k].tagName == "bsa:subtitle")
								{
									node_array[i].subtitle = x[i].childNodes[j].childNodes[k].textContent;
								}
								k++;
							}
						}
						j++;
					}
				}
			}
			else //IE
			{
				xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
				xmlDoc.async = "false";
				xmlDoc.loadXML(result);
				
				x = xmlDoc.getElementsByTagName("item");
				
				if(x.length < number){number = x.length;}
				
				for(i=0;i<number;i++)
				{
					node_array[i] = {'title':'','link':'','pubDate':'','description':'','subtitle':'','large':'','small':''};
					for(j=0;j<x[i].childNodes.length;j++)
					{
						if(x[i].childNodes[j].tagName == "title"){node_array[i].title = x[i].childNodes[j].text;}
						else if(x[i].childNodes[j].tagName == "link"){node_array[i].link = x[i].childNodes[j].text;}
						else if(x[i].childNodes[j].tagName == "pubDate"){node_array[i].pubDate = x[i].childNodes[j].text;}
						else if(x[i].childNodes[j].tagName == "description"){node_array[i].description = x[i].childNodes[j].text;}
						else if(x[i].childNodes[j].tagName == "bsa:multimedia")
						{
							for(k=0;k<x[i].childNodes[j].childNodes.length;k++)
							{
								if(x[i].childNodes[j].childNodes[k].tagName == "bsa:image")
								{
									if(x[i].childNodes[j].childNodes[k].childNodes.length > 0){node_array[i].small = x[i].childNodes[j].childNodes[k].childNodes[0].text;}
									if(x[i].childNodes[j].childNodes[k].childNodes.length > 1){node_array[i].large = x[i].childNodes[j].childNodes[k].childNodes[1].text;}
								}
								else if(x[i].childNodes[j].childNodes[k].tagName == "bsa:subtitle")
								{
									node_array[i].subtitle = x[i].childNodes[j].childNodes[k].text;
								}
							}
						}
					}
				}
			}
			
			switch(rss_format)
			{
				case 'scoutsinthenews':				for(i=0;i<node_array.length;i++)
													{
														output += "<h3><a href=\"" + node_array[i].link.toString() + "\" target=\"_blank\">" + node_array[i].title.toString() + "</a></h3>";
														output += "<p>" + date_format(node_array[i].pubDate.toString(),1) + "</p>";
														output += "<p>" + node_array[i].description.toString() + "</p>";
													}
													break;
				case 'scoutsinthenews_widget':		for(i=0;i<node_array.length;i++)
													{
														output += "<p><a href=\"" + node_array[i].link.toString() + "\" target=\"_blank\">" + date_format(node_array[i].pubDate.toString(),1) + " - " + node_array[i].title.toString() + "</a></p>";
													}
													break;
				case 'prospeak_general':			var current_date = "";
													var new_date = 0;
													for(i=0;i<node_array.length;i++)
													{
														if(date_format(node_array[i].pubDate.toString(),2) == current_date) //check for same date
														{
															if(new_date == 1){output += "<div class=\"monthArticles\"><ul>";new_date = 0;}
															output += "<li><a href=\"" + node_array[i].link.toString() + "\">" + node_array[i].title.toString() + "</a></li>";
														}
														else //first node of next date
														{
															if(i == 0){output += "<div class=\"month\">";}
															else{output += "</ul></div></div><div class=\"month\">";}
															output += "<h2>" + node_array[i].pubDate.toString().substr(8,8) + "</h2>";
															output += "<div class=\"monthFeature\">";
															output += "<img src=\"" + node_array[i].small.toString() + "\" style=\"margin:0; padding:0;\">";
															output += "<div>";
															output += "<h4><a href=\"" + node_array[i].link.toString() + "\">" + node_array[i].title.toString() + "</a></h4>";
															output += "<p>" + node_array[i].subtitle.toString() + " <a href=\"" + node_array[i].link.toString() + "\">READ MORE</a></p>";
															output += "</div>";
															output += "</div>";
															current_date = date_format(node_array[i].pubDate.toString(),2);
															new_date = 1;
														}
														
														if(i == (node_array.length - 1)){output += "</ul></div></div>";}
													}
													break;
				case 'prospeak_editors_corner':		var current_date = "";
													var new_date = 0;
													for(i=0;i<node_array.length;i++)
													{
														output += "<div class=\"month\">";
														output += "<h2>" + node_array[i].pubDate.toString().substr(8,8) + "</h2>";
														output += "<div class=\"monthFeature\" style=\"width:100%;\">";
														output += "<h4>" + node_array[i].title.toString() + "</h4>";
														output += "<p>" + node_array[i].description.toString() + "</p>";
														output += "</div></div>";
														current_date = date_format(node_array[i].pubDate.toString(),2);
														new_date = 1;
													}
													break;
				case 'prospeak_editors_corner_home':	output += "<div id=\"editorial\">";														
														output += "<a href=\"~/link.aspx?_id=699C4272887A4CD78E9B01E6F9B66912&amp;_z=z\"><h3>Editor's Corner >></h3></a>";
														output += "<img src=\"/filestore/prospeak/images/rich_luna_small.jpg\">";
														output += "<div style=\"width:600px;\">";
														output += "<h2>" + node_array[0].title.toString() + "</h2>";
														output += "<p>" + node_array[0].description.toString() + "</p>";
														output += "</div>";
														output += "</div>";
													break;
				case 'prospeak_trailblazers':		for(i=0;i<node_array.length;i++)
													{
														output += "<div id=\"prospeak_" + simple_title(node_array[i].title.toString()) + "\"></div>";
														rss_load(node_array[i].link.toString(),'prospeak_trailblazers_2',12);
													}
													break;
				case 'prospeak_trailblazers_2':		for(i=0;i<node_array.length;i++)
													{														
														if(i == 0) //first node of next date
														{
															output += "<div class=\"month\">";
															output += "<h2>" + node_array[i].title.toString() + "</h2>";
															output += "<ul>";
															output += "<li class=\"current\"><a href=\"" + node_array[i].link.toString() + "\">" + node_array[i].pubDate.toString().substr(8,8) + "</a></li>";
														}
														else
														{
															if(i%3 == 0){output += "</ul><ul>";}
															output += "<li><a href=\"" + node_array[i].link.toString() + "\">" + node_array[i].pubDate.toString().substr(8,8) + "</a></li>";
														}
														
														if(i == (node_array.length - 1)){output += "</ul></div>"; rss_format = "prospeak_" + simple_title(node_array[i].title.toString());}
													}
													break;
				case 'museum_curators_corner':		for(i=0;i<node_array.length;i++)
													{
														output += "<div id=\"blog\">";
														output += "<h1>" + date_format(node_array[i].pubDate.toString(),3) + "</h1>";
														output += "<div id=\"date\">";
														output += "<div id=\"month\"><h2>" + date_format(node_array[i].pubDate.toString(),5).substr(0,3) + "</h2></div>";
														output += "<div id=\"year\"><p>" + date_format(node_array[i].pubDate.toString(),6).substr(0,2) + "<br/>" + date_format(node_array[i].pubDate.toString(),6).substr(2,2) + "</p></div>";
														output += "</div>";
														output += "</div>";
														output += "<div id=\"blogCONTENT\"><h2><a target=\"_self\" href=\"" + node_array[i].link.toString() + "\">" + node_array[i].title.toString() + "</a></h2><p>" + node_array[i].description.toString() + "</p></div>";
													}
													break;
			}
			
			document.getElementById(rss_format).innerHTML = output;
			
		}
		else
		{
		
			alert('There was a problem with the request.');
			
		}
		
	}
}

function pngfix()
{

	if(navigator.appVersion.indexOf("MSIE 6") != -1)
	{
		
		var tag_array = new Array("a","b","br","div","embed","font","form","frame","h","h1","h2","h3","h4","h5","h6","i","img","iframe","li","p","select","span","strong","table","td","textarea","ul");

		var img_array = document.getElementsByTagName("img");
		
		var bg_array = new Array();
		var bg_path;
		var temp_path;
		
		var i = 0;
		var j = 0;
		
		var img_width = 0;
		var img_height = 0;
		
		for(i=0;i<img_array.length;i++) //Test for images
		{
		
			if(img_array[i].src.indexOf(".png") != -1)
			{
			
				img_width = img_array[i].offsetWidth;
				img_height = img_array[i].offsetHeight;
				
				img_array[i].style.width = img_width + "px";
				img_array[i].style.height = img_height + "px";
				
				img_array[i].style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true,sizingMethod='none',src=\'" + img_array[i].src + "\')";
				
				img_array[i].src = "/filestore/global/spacer.png";
			
			}
		
		}
		
		for(i=0;i<tag_array.length;i++) //Test for background images
		{
			
			bg_array = document.getElementsByTagName(tag_array[i]);
			
			for(j=0;j<bg_array.length;j++)
			{
				
				if(bg_array[j].style.backgroundImage.indexOf(".png") != -1)
				{
					
					bg_path = bg_array[j].style.backgroundImage;
					
					temp_path = bg_path.substr(4,bg_path.length - 2);
					
					bg_array[j].style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true,sizingMethod='none',src=\'" + temp_path + "\')";
					bg_array[j].style.backgroundImage = "";
					
				}
				
			}
			
		}
	
	}

}

function icon_add()
{
	
	var current_url = "" + window.location.href;

	var split_url = new Array();
	
	split_url = current_url.split("/");

	var current_domain = split_url[2];

	var i;
	
	var j;
	
	if(document.getElementById("middle-element"))
	{
		var middle_element = document.getElementById("middle-element");
		
		var a_array = middle_element.getElementsByTagName("a");
		
		var temp_href = "";
		
		var temp_extension = "";
		
		var split_href = new Array();
		
		var split_file = new Array();
		
		var split_extension = new Array();
		
		var link_image = "";
		
		for(i=0;i<a_array.length;i++)
		{

			temp_href = "" + a_array[i].href;
			
			if(temp_href != "")
			{
			
				temp_href = temp_href.toLowerCase();
		
				link_image = "html";
							
				split_href = temp_href.split("/");
		
				if(split_href[split_href.length - 1].indexOf(".") != -1 && a_array[i].innerHTML.toLowerCase().indexOf("<img") == -1 && a_array[i].href.toLowerCase().indexOf("javascript") == -1)
				{
				
					split_file = split_href[split_href.length - 1].split(".");
		
					temp_extension = split_file[split_file.length - 1];
		
					if(temp_extension.indexOf("?") != -1){split_extension = temp_extension.split("?"); temp_extension = split_extension[0];}
					if(temp_extension.indexOf("#") != -1){split_extension = temp_extension.split("#"); temp_extension = split_extension[0];}
				
					switch(temp_extension)
					{
					
						case 'doc':		
						case 'docx':		
						case 'rtf':
						case 'txt':		link_image = "word";
										break;
						case 'pdf':		link_image = "PDF";
										break;
						case 'swf':		
						case 'fla':		
						case 'swd':
						case 'flv':		link_image = "flash";
										break;
						case 'xls':		
						case 'xlsx':		
						case 'csv':
						case 'xlt':
						case 'xlw':		link_image = "excel";
										break;
						case 'ppt':		
						case 'pptx':		
						case 'pps':		link_image = "powerpoint";
										break;
						case 'zip':		
						case 'gzip':		
						case 'rar':		link_image = "zip";
										break;
						default:		link_image = "html";
										break;
							
					}
					
				}
				
				if(temp_href.indexOf("mailto") != -1 && a_array[i].innerHTML.toLowerCase().indexOf("<img") == -1 && a_array[i].href.toLowerCase().indexOf("javascript") == -1)
				{
					
					link_image = "MailTo";
					
				}
		
				if(temp_href.indexOf(current_domain) == -1 && a_array[i].innerHTML.toLowerCase().indexOf("<img") == -1 && a_array[i].href.toLowerCase().indexOf("javascript") == -1)
				{
		
					a_array[i].innerHTML = a_array[i].innerHTML + " <img src=\"/filestore/global/link-" + link_image + ".gif\" border=\"0\" style=\"padding:0;\">";
					
				}
				else if(link_image != "html" && a_array[i].innerHTML.toLowerCase().indexOf("<img") == -1 && a_array[i].href.toLowerCase().indexOf("javascript") == -1)
				{
					
					a_array[i].innerHTML = a_array[i].innerHTML + " <img src=\"/filestore/global/link-" + link_image + ".gif\" border=\"0\" style=\"padding:0;\">";
					
				}
				
			}
		
		}
		
	}
	
	pngfix();
	
}

function global_bookmark()
{
	
	var url = "" + window.location.href;
	
	var title = "" + document.title;
	
	if(window.sidebar) // firefox
	{
		window.sidebar.addPanel(title, url, "");
	}
	else if(document.all)// ie
	{
		window.external.AddFavorite(url, title);
	}
	else
	{
		alert("This browser does not support this functionality.\nPlease bookmark this page using \"CTRL+D\" or \"CMD+D\".");	
	}
	
}

function widget_video_play(flv_url)
{
	
	var encoded_url = encode_url(flv_url);

	var url = "/filestore/global/swf/video_player.swf?url=" + encoded_url;
	
	flash_obj = "<embed width=\"212\" height=\"131\" src=\"" + url + "\" quality=\"high\" allowScriptAccess=\"always\" allowFullScreen=\"true\" wmode=\"transparent\" pluginspage=\"http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash\" type=\"application/x-shockwave-flash\"></embed>";

	document.getElementById("widget_video_player").innerHTML = flash_obj;
	
}

function middle_video_play(flv_url)
{
	
	var encoded_url = encode_url(flv_url);

	var url = "/filestore/global/swf/video_player.swf?url=" + encoded_url;
	
	flash_obj = "<embed width=\"515\" height=\"318\" src=\"" + url + "\" quality=\"high\" allowScriptAccess=\"always\" allowFullScreen=\"true\" wmode=\"transparent\" pluginspage=\"http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash\" type=\"application/x-shockwave-flash\"></embed>";

	document.getElementById("middle_video_player").innerHTML = flash_obj;
	
}

function encode_url(url)
{
	
	url = encodeURIComponent(url);
	url = url.replace(".","%2E");
	url = url.replace("/","%2F");
	
	return url;
	
}

function rss_list_load(list,div_id)
{
	
	if(document.getElementById(div_id))
	{
		
		document.getElementById(div_id).innerHTML = list;
		
	}
	
}

function image_swap()
{
	var url = "" + window.location.href;
	var i;
	var split_url = new Array();
	split_url = url.split("/");
	var current_page = split_url[split_url.length - 1];
	var current_dir = current_page.replace(/.aspx/,"").toLowerCase();
	
	if(filestore_dir)
	{
		var filestore_path = filestore_dir + current_dir + "/";
		var alphabet = new Array("a","b","c","d","e","f","g","h","i","j");
		
		for(i=0;i<alphabet.length;i++)
		{
			if(document.getElementById(current_dir + "_" + alphabet[i]))
			{
				document.getElementById(current_dir + "_" + alphabet[i]).src = filestore_path + alphabet[i] + image_index + ".jpg";
			}
		}
		
		if(image_index >= total_images){image_index = 1;}
		else{image_index++;}
		
		setTimeout(image_swap,2000);
	}
	
}

function prospeak_months(content)
{
    document.getElementById('prospeak_content_div').innerHTML = content;
}