$(function(){
	// This is the enabler for the plug-in name jquery.ba-hashchage.js 
	//which forces IE to deal with the hashchange event that exists for HTML5
	$(window).bind( 'hashchange', function() {
	  	//get hash for the current location
		var hash = location.hash;
	
		// This hopefully loads the right meta tags
		/*$.post('getmeta.php', { hash: hash, tempTitle: tempTitle }, function(data) {
			$('title').remove();
			$('meta').remove();
			$('head').append(data);
		});*/
		
	  	//force the right content to not display if the page doesn't exist
		$.ajax({
			cache: false,
			crossDomain: true,
			statusCode: {
				404: function() {
					$("right-content").css("display","none");
					$("#main-content").css({
					  "width":"650px",
					  "margin-top":"45px"
					});
				}
			}
		});
		//display the ajax-loader.gif while the page is loading
		$("#loading-img").show();
		//get the location of the page, not sure this is being used
		var page = window.location.href;
		//when the page loads and if a hash exists, cut off the #! and break the string into parts to 
		//setup the urls for each section to load. 
		if (hash != "") {
			$("#custom-tweet-button").html('<a href=\"https://twitter.com/share?url=https://www.salemhealth.org/home.php' + encodeURIComponent(hash) + '\" target=\"_blank\"></a>');
			$("#facebook").html('<iframe src="https://www.facebook.com/plugins/like.php?href=https%3A%2F%2Fwww.salemhealth.org%2Fhome.php' + encodeURIComponent(hash) + '&amp;layout=button_count&amp;show_faces=false&amp;width=80&amp;action=like&amp;font=arial&amp;colorscheme=light&amp;height=21" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:80px; height:21px;" allowTransparency="true"></iframe>');
			var noHashBang = hash.substr(2);
			$("#linkedin").html('<script src="http://platform.linkedin.com/in.js" type="text/javascript"></script><script type="IN/Share" data-url="http://www.salemhealth.org/' + hash + '" data-counter="right"></script>');
			var url = "/" + hash.slice(2);
			var directory = url.split(".",1);
			//alert(hash.slice(2).replace(".","/"));
			// This is the google analytics code.  First it creates an analytics friendly url
			// then pushes that URL to analytics as a page
			var googleAnalyticsURL =  hash.slice(2).replace(".","/");
			_gaq.push(['_setAccount', 'UA-3976065-17']);
			_gaq.push(['_trackPageview', googleAnalyticsURL]);
			
			
			var leftDefault = "www.salemhealth.org/" + directory + "/leftnav/home.php";
			var rightDefault = "www.salemhealth.org/" + directory + "/rightnav/home.php";
			var breadDefault = "www.salemhealth.org/" + directory + "/breadcrumbs/home.php";
			var leftnav = url.replace(".","/leftnav/") + ".php";
			var content = url.replace(".","/content/") + ".php";
			var right = url.replace(".","/rightnav/") + ".php";
			var breadcrumbs = url.replace(".","/breadcrumbs/") + ".php";
			//make homepage go away and move social stuff over to right-content
			$("#home").css("display","none");
			$("#footer-wrap").slideUp(500);
			$("#expand").show();
			$("#shrink").hide();
			$("#social-triggers").css({
				"left":"720px",
				"top":"5px"
			});
			$("#social-wrap-home").css({
				"left":"715px",
				"top":"40px"
			});
			//bring in content page and hide the loading image
			$("#page").fadeIn(500, function() {
				$("#loading-img").hide();
			});
			if (hash.match(/sh.home/gi) != null || hash.match(/whp.home/gi) != null || hash.match(/wvh.home/gi) != null) {
				$("#footer-wrap").slideDown(500);
				$("#expand").hide();
				$("#shrink").show();
			}
			//if there is a question mark in the url, do string manipulation to handle those
			//this comes up for Messenger, Messenger Express, CHEC Recipes, etc.
			if (page.match(/\?/gi) != null) {
				var contenturl = url.replace(".","/content/");
				var leftnavurl = url.replace(".","/leftnav/");
				var righturl = url.replace(".","/rightnav/");
				var breadurl = url.replace(".","/breadcrumbs/");
				var story = contenturl.replace("?",".php?");
				var left = leftnavurl.replace("?",".php?");
				var right = righturl.replace("?",".php?");
				var bread = breadurl.replace("?",".php?");
				$.ajax({
					type: "GET",
					url: story,
					success: function() {
						$("#main-content").load(story, function () {
							$.ajax({
								type: "GET",
								url: "getTitle.php",
								success: function (data) {
										tempTitle = data;
										$('title').remove();
										$('head').append('<title>' + tempTitle + '</title>');								
										document.title = tempTitle;
								},
								statusCode: {
									404: function() {
										tempTitle = $('h1:first').html();
										$('title').remove();
										$('head').append('<title>' + tempTitle + '</title>');
										document.title = tempTitle;
									}
								}
							});

						});
					},
					error: function() {
						window.location.replace("http://www.salemhealth.org/#!errors.404"); 
					},
					statusCode: {
						404: function() {
							window.location.replace("http://www.salemhealth.org/#!errors.404");
						}
					}
				});
				$('#left-nav').load(left);
				$.ajax({
					type: "GET",
					url: right,
					success: function() {
						$("#right-content").css("display","block");
						//$("#ad-space").css("display","block");
						$("#right-info").load(right);
						if (hash.match(/whp\./gi) != null) {
							$("#whp-ad").load("/adverts/whp.php", function(event) {
								$(this).css("display","block");
							})
							$("#sh-ad,#wvh-ad,#ad-space").css("display","none");
						}
						else if (hash.match(/sh\./gi) != null) {
							$("#sh-ad").load("/adverts/sh.php", function(event) {
								$(this).css("display","block");
							})
							$("#whp-ad,#wvh-ad,#ad-space").css("display","none");
						}
						else if (hash.match(/wvh\./gi) != null) {
							$("#wvh-ad").load("/adverts/wvh.php", function(event) {
								$(this).css("display","block");
							});
							$("#sh-ad,#whp-ad,#ad-space").css("display","none");
						}
						else {
							$("#ad-space").load("/adverts/sh.php", function(event) {
								$(this).css("display","block");
							});
							$("#whp-ad,#wvh-ad,#sh-ad,#ad-space").css("display","none");
						}
					},
					error: function() {
						$("#right-content").css("display","none");
						//$("#ad-space").css("display","none");
						$("#main-content").css({
						  "width":"650px",
						  "margin-top":"45px"
						});
					},
					statusCode: {
						404: function() {
							$("#right-content").css("display","none");
							//$("#ad-space").css("display","none");
							$("#main-content").css({
							  "width":"650px",
							  "margin-top":"45px"
							});
						}
					}
				});
				$('#architecture').load(bread);
				window.scrollTo(0,-50);
			}
			else {
				//load the necessary URLs for each content section, however, if 
				//the file doesn't exist for the #right-content, we don't want
				//it to display so we can have Subsite Homepages. We also widen
				//the width of #main-content. This just gives us more real estate 
				//for pages like Willamette Health Partners, CHEC, etc.
				$('#left-nav').load(leftnav);
				/*$('#main-content').load(content, function () {
					tempTitle = $('h1:first').html();
					$('title').remove();
					$('head').append('<title>' + tempTitle + '</title>');
				});*/
				$.ajax({
					type: "GET",
					url: content,
					success: function() {
						$("#main-content").load(content, function () {
							$.ajax({
								type: "GET",
								url: "js/getTitle.php",
								data: ({hash : hash}),
								success: function (data) {
										tempTitle = data;
										$('title').remove();
										$('head').append('<title>' + tempTitle + '</title>');
										document.title = tempTitle;								
								},
								statusCode: {
									404: function() {
										tempTitle = $('h1:first').html();
										$('title').remove();
										$('head').append('<title>' + tempTitle + '</title>');
										document.title = tempTitle;
									}
								}
							});

						});
					},
					error: function() {
						//This IF is a band-aid for weird things going on with the community pages in IE7
						window.location.replace("http://www.salemhealth.org/#!errors.404");
					},
					statusCode: {
						404: function() {
							//This IF is a band-aid for weird things going on with the community pages in IE7
							window.location.replace("http://www.salemhealth.org/#!errors.404");
						}
					}
				});
				$.ajax({
					type: "GET",
					url: right,
					success: function() {
						$("#right-content").css("display","block");
						//$("#ad-space").css("display","block");
						$("#right-info").load(right);
						if (hash.match(/whp\./gi) != null) {
							$("#whp-ad").load("/adverts/whp.php", function(event) {
								$(this).css("display","block");
							})
							$("#sh-ad,#wvh-ad,#ad-space").css("display","none");
						}
						else if (hash.match(/sh\./gi) != null) {
							$("#sh-ad").load("/adverts/sh.php", function(event) {
								$(this).css("display","block");
							})
							$("#whp-ad,#wvh-ad,#ad-space").css("display","none");
						}
						else if (hash.match(/wvh\./gi) != null) {
							$("#wvh-ad").load("/adverts/wvh.php", function(event) {
								$(this).css("display","block");
							});
							$("#sh-ad,#whp-ad,#ad-space").css("display","none");
						}
						else {
							$("#ad-space").load("/adverts/sh.php", function(event) {
								$(this).css("display","block");
							});
							$("#whp-ad,#wvh-ad,#sh-ad").css("display","none");
						}
						$("#main-content").css({
						  "width":"450px",
						  "margin-top":"45px"
						});
					},
					error: function() {
						$("#right-content").css("display","none");
						//$("#ad-space").css("display","none");
						$("#main-content").css({
						  "width":"650px",
						  "margin-top":"45px"
						});
					},
					statusCode: {
						404: function() {
							$("#right-content").css("display","none");
							//$("#ad-space").css("display","none");
							$("#main-content").css({
							  "width":"650px",
							  "margin-top":"45px"
							});
						}
					}
				});
				$('#architecture').load(breadcrumbs);
				window.scrollTo(0,-50);
			}
		} else {
			$("#page").fadeOut(250);
			$("#home").fadeIn(500, function() {
				$("#loading-img").hide();
				$("#footer-wrap").slideDown(500);
				$("#expand").hide();
				$("#shrink").show();
			});
			$("#social-triggers").css({
				"left":"690px",
				"top":"5px"
			});
			$("#social-wrap-home").css({
				"left":"685px",
				"top":"40px"
			});
		}
	});

	//This is what actually triggers the whole site, no big deal.
	$(window).trigger( 'hashchange' );
});

