var PageController = {
	initialVisit : true,
	currentHash : '',
	deeplinkValue1 : '',
	deeplinkValue2 : '',
	homepageSliderHTML: '',
	workpageImgHTML: '',
	RSSFeedHTML: '',
	FeedHTML: '',
	 
	loadXML : function()
	{
		jQuery.ajax({
		   	type: 'GET',
		   	url: 'xml/homepage.xml',
		   	dataType: 'xml',
			async: false,
		   	success: function(xml){
				var htmlContent;
				PageController.homepageSliderHTML = '';
				jQuery(xml).find('item').each(function(idx){
					
						PageController.homepageSliderHTML +='<img src="images/home/' + jQuery(this).find('image').text() + '" id="image'+idx+'" title="' + jQuery(this).find('title').text() + '" alt="' + jQuery(this).find('title').text() + '" />'; 
					
				});
			}
		});
		
		jQuery.ajax({
		   	type: 'GET',
		   	url: 'xml/work.xml',
		   	dataType: 'xml',
			async: false,
		   	success: function(xml){
				var htmlContent;
				PageController.workpageImgHTML = '';
				jQuery(xml).find('item').each(function(idx){
					jQuery(this).find('portfolio').each(function(inidx)
					{
						if(jQuery(this).attr('name') == 'video')
						{
							PageController.workpageImgHTML +='<div><a href="javascript:void(PageController.showVideo('+idx+','+inidx+'))"><img src="images/work/' + jQuery(this).find('image').text() + '" title="' + jQuery(this).find('tag').text() + '" alt="' + jQuery(this).find('tag').text() + '" /></a></div>';
						}
						else
						{
							if(jQuery(this).attr('type') == 'url')
							{
								PageController.workpageImgHTML +='<div><a href="'+ jQuery(this).find('url').text() +'" target="_blank"><img src="images/work/' + jQuery(this).find('image').text() + '" title="' + jQuery(this).find('tag').text() + '" alt="' + jQuery(this).find('tag').text() + '" /></a></div>';
							}
							else
							{
								PageController.workpageImgHTML +='<div><img src="images/work/' + jQuery(this).find('image').text() + '" title="' + jQuery(this).find('tag').text() + '" alt="' + jQuery(this).find('tag').text() + '" /></div>';
							}
						}
					});					
				});
			}
		});
		
		
		
		if(this.initialVisit)
		{
			setInterval('PageController.checkParentHash()', 250);
		}
	},
	
	showVideo : function(hasVideo,inVideo)
	{
		var htmlContent;
		
		jQuery.ajax({
		   	type: 'GET',
		   	url: 'xml/work.xml',
		   	dataType: 'xml',
			async: false,
		   	success: function(xml){								
				jQuery(xml).find('item').each(function(idx){
					jQuery(this).find('portfolio').each(function(inidx)
					{
						if(idx == hasVideo)
						{
							if (inidx == inVideo)
							{
							htmlContent ='<iframe width="640" height="360" src="'+ jQuery(this).find('video').text() +'?rel=0" frameborder="0" allowfullscreen></iframe>';
							}
						}
					});					
				});
			}
		});
		//var myVideo = "images/work/video_" + hasVideo + "_" + inVideo + ".mov";
		jQuery('#blackoverlay').show();
		jQuery('#video-container').show();
		var pageHeight = jQuery(window).height();
		var pageWidth = jQuery(window).width();
		jQuery('#blackoverlay').css('height', pageHeight + 'px');
		jQuery('#blackoverlay').css('width', pageWidth + 'px');
		var videowidth = jQuery('#video-container').width();
		var pageWidth1 = (pageWidth/2);
		var pageWidth2 = (videowidth/2);
		var pageHeight1 = (pageHeight/2);
		var pageHeight2 = (videowidth/2)
		var newwidth = pageWidth1 - pageWidth2;
		var newwidth1 = pageWidth1;
		var newwidth2 = pageHeight1 -pageHeight2 + 100;
		jQuery('#video-container').css('top', newwidth2 + 'px');	
		jQuery('#video-container').css('left', newwidth + 'px');
		<!--var htmlCode1 = QT_GenerateOBJECTText(myVideo, '640px', '360px', '', 'SCALE', 'tofit', 'obj#ID', 'movie1', 'obj#name', 'movie1', 'emb#ID', 'movieEMBED1', 'emb#name', 'movie1', 'AUTOPLAY', 'True', 'EnableJavaScript', 'False', 'postdomevents', 'False');-->
		//var htmlCode1 = '<iframe width="640" height="360" src="http://www.youtube.com/embed/aR8sKR2Os4k?rel=0" frameborder="0" allowfullscreen></iframe>'
		jQuery('#videobox').html(htmlContent);
	},
	
	hideVideo: function()
	{
		jQuery('#video-container').hide();
		jQuery('#blackoverlay').hide();
		jQuery('#videobox').html("");
	},
	
	showFeed: function(index)
	{
		PageController.setHash('/RSSFeed/');
		jQuery.ajax({
		   	type: 'GET',
		   	url: 'xml/work.xml',
		   	dataType: 'xml',
			async: false,
		   	success: function(xml){
				var htmlContent;
				PageController.FeedHTML = '';
				jQuery(xml).find('item').each(function(idx){					
					jQuery(this).find('portfolio').each(function(inidx)
					{
						if(index == idx)
						{								
							if(jQuery(this).attr('name') == 'video')
							{
								PageController.FeedHTML +='<div class="feedDiv"><div class="feedTitle">'+ jQuery(this).find('title').text() +'</div><a href="javascript:void(PageController.showVideo('+idx+','+inidx+'))"><img src="images/work/' + jQuery(this).find('image').text() + '" title="' + jQuery(this).find('tag').text() + '" alt="' + jQuery(this).find('tag').text() + '" /></a></div>';
							}
							else
							{
								if(jQuery(this).attr('type') == 'url')
								{
									PageController.FeedHTML +='<div class="feedDiv"><div class="feedTitle">'+ jQuery(this).find('title').text() +'</div><a href="'+ jQuery(this).find('url').text() +'" target="_blank"><img src="images/work/' + jQuery(this).find('image').text() + '" title="' + jQuery(this).find('tag').text() + '" alt="' + jQuery(this).find('tag').text() + '" /></a></div>';
								}
								else
								{
								PageController.FeedHTML +='<div class="feedDiv"><div class="feedTitle">'+ jQuery(this).find('title').text() +'</div><img src="images/work/' + jQuery(this).find('image').text() + '" title="' + jQuery(this).find('tag').text() + '" alt="' + jQuery(this).find('tag').text() + '" /></div>';
								}
							}							
						}
					});
										
				});
			}
		});
		
		htmlCode = '<div id="rsscontent">';
		htmlCode += '<a href="javascript:void(PageController.setHash(\'/home/\'))"><div id="worklogo"></div></a>';
		htmlCode += '<div id="work-nav">';
		htmlCode += '<ul>';
		htmlCode += '<li><a href="javascript:void(PageController.setHash(\'/work/\'))" ><div class="work" id="worknav1"></div></a></li>';
		htmlCode += '<li><a href="javascript:void(PageController.showAboutWork())"><div class="aboutus" id="aboutnav1"></div></a></li>';
		htmlCode += '<li><a href="javascript:void(PageController.showContactWork())"><div class="contactus" id="contactnav1"></div></a></li>';
		htmlCode += '<li><a href="javascript:void(PageController.setHash(\'/Feed/\'))"><div class="rssactive"></div></a></li>';
		htmlCode += '</ul>';
		htmlCode += '</div>';
		htmlCode += '<div class="rss_icon"><a href="xml/work.xml"><img src="images/rss.png" alt="" title="" /></a></div>';
		htmlCode += '<div id="contact-detail-work"><img src="images/contact_detail.png" alt="" border="0px" usemap="#Map1" title="" />';
		htmlCode += '<map name="Map" id="Map1"><area shape="rect" coords="1,60,150,76" href="mailto:richie@richiegleason.com" /></map>';
		htmlCode += '</div>';
		htmlCode += '<div id="about-detail-work"><img src="images/about_detail.png" border="0px" alt="" title="" /></div>';
		htmlCode += '</div>';
		htmlCode += '<div id="feed">';
		htmlCode += PageController.FeedHTML;
		htmlCode += '</div>';
		jQuery('#wrapper').html(htmlCode);
		jQuery('#rsscontent').fadeIn(1000);
	},
	
	showHome : function()
	{
		_gaq.push(['_trackEvent', 'Website', '/home']);
		
		var htmlCode;
		htmlCode = '<div id="home">';
		htmlCode += PageController.homepageSliderHTML;
		htmlCode += '</div>';
		htmlCode += '<div id="maincontent">';
		htmlCode += '<a href="javascript:void()"><div id="logo"></div></a>';
		htmlCode += '<div id="nav">';
		htmlCode += '<ul>';
		htmlCode += '<li><a href="javascript:void(PageController.showWork())"><div class="work" id="worknav"></div></a></li>';
		htmlCode += '<li><a href="javascript:void(PageController.showAbout())"><div class="aboutus" id="aboutnav"></div></a></li>';
		htmlCode += '<li><a href="javascript:void(PageController.showContact())" ><div class="contactus" id="contactnav"></div></a></li>';
		htmlCode += '<li><a href="javascript:void(PageController.setHash(\'/Feed/\'))"><div class="rss"></div></a></li>';
		htmlCode += '</ul>';
		htmlCode += '</div>';
		htmlCode += '<div id="contact-detail"><img src="images/contact_detail.png" alt="" border="0px" usemap="#Map" title="" />';
		htmlCode += '<map name="Map" id="Map">';
		htmlCode += '<area shape="rect" coords="1,63,152,75" href="mailto:richie@richiegleason.com" />';
		htmlCode += '</map>';
		htmlCode += '</div>';
		htmlCode += '<div id="about-detail"><img src="images/about_detail.png" border="0px" alt="" title="" /></div>';
		htmlCode += '</div>';
		htmlCode += '<div id="workimg">';
		htmlCode += PageController.workpageImgHTML;
		htmlCode += '</div>';
		htmlCode += '<div id="workcontent">';
		htmlCode += '<a href="javascript:void(PageController.showMain())"><div id="worklogo"></div></a>';
		htmlCode += '<div id="work-nav">';
		htmlCode += '<ul>';
		htmlCode += '<li><a href="javascript:void(PageController.Work())" ><div class="workactive" id="worknav1"></div></a></li>';
		htmlCode += '<li><a href="javascript:void(PageController.showAboutWork())"><div class="aboutus" id="aboutnav1"></div></a></li>';
		htmlCode += '<li><a href="javascript:void(PageController.showContactWork())"><div class="contactus" id="contactnav1"></div></a></li>';
		htmlCode += '<li><a href="javascript:void(PageController.setHash(\'/Feed/\'))"><div class="rss"></div></a></li>';
		htmlCode += '</ul>';
		htmlCode += '</div>';
		htmlCode += '<div id="contact-detail-work"><img src="images/contact_detail.png" alt="" border="0px" usemap="#Map1" title="" />';
		htmlCode += '<map name="Map" id="Map1"><area shape="rect" coords="1,60,150,76" href="mailto:richie@richiegleason.com" /></map>';
		htmlCode += '</div>';
		htmlCode += '<div id="about-detail-work"><img src="images/about_detail.png" border="0px" alt="" title="" /></div>';
		htmlCode += '</div>';
		jQuery('#wrapper').html(htmlCode);
		HomePageSlideshow.startSlideshow();
		this.deeplinkValue1 = "music";
		PageController.hidAll();
	},
	
	hidAll : function()
	{
		jQuery('#wrapper').click(function(){
			jQuery('#about-detail').hide();
			jQuery('#contact-detail').hide();
			jQuery('#about-detail-work').hide();
			jQuery('#contact-detail-work').hide();
			jQuery('#aboutnav').removeClass('aboutusactive').addClass('aboutus');
			jQuery('#contactnav').removeClass('contactusactive').addClass('contactus');
			jQuery('#aboutnav1').removeClass('aboutusactive').addClass('aboutus');
			jQuery('#contactnav1').removeClass('contactusactive').addClass('contactus');
		});
		
	},
	
	showMain : function()
	{
		jQuery('#workcontent').fadeOut(1500);
		jQuery('#workimg').fadeOut(1000);
		jQuery('#home').delay(1500).fadeIn(1000);
		jQuery('#maincontent').delay(1500).fadeIn(1000);
		
	},
	
	showContact : function()
	{
		jQuery('#about-detail').hide();
		jQuery('#contact-detail').toggle();
		jQuery('#aboutnav').removeClass('aboutusactive').addClass('aboutus');
		//jQuery('#contactnav').removeClass('contacus').addClass('contactusactive');
		jQuery('#contactnav').toggleClass("contactusactive");
		
	},
	
	showAbout : function()
	{
		jQuery('#contact-detail').hide();
		jQuery('#about-detail').toggle();
		jQuery('#aboutnav').toggleClass("aboutusactive");
		jQuery('#contactnav').removeClass('contactusactive').addClass('contactus');
	},
	
	showContactWork : function()
	{
		jQuery('#about-detail-work').hide();
		jQuery('#contact-detail-work').toggle();
		jQuery('#aboutnav1').removeClass('aboutusactive').addClass('aboutus');
		jQuery('#contactnav1').toggleClass("contactusactive");
		
	},
	
	showAboutWork : function()
	{
		jQuery('#contact-detail-work').hide();
		jQuery('#about-detail-work').toggle();
		jQuery('#aboutnav1').toggleClass("aboutusactive");
		jQuery('#contactnav1').removeClass('contactusactive').addClass('contactus');
	},
	
	Work : function()
	{
		jQuery('#contact-detail-work').hide();
		jQuery('#about-detail-work').hide();
		jQuery('#aboutnav1').removeClass('aboutusactive').addClass('aboutus');
		jQuery('#contactnav1').removeClass('contactusactive').addClass('contactus');
	},
	
	showWork : function()
	{
		jQuery('#home').fadeOut(500);
		jQuery('#maincontent').fadeOut(500);
		jQuery('#workcontent').delay(500).fadeIn(500);
		jQuery('#workimg').show();
		jQuery("#workimg div img").each(function() {
				var width  = jQuery(this).width();
        		var height = jQuery(this).height();
				var parentWidth  = jQuery(window).width();
				var parentHeight = jQuery(window).height();
				var rawidth = width / parentWidth;
				var raheight = height/parentHeight;				
						newWidth  = parentWidth-10;
						newHeight = (newWidth/width*height)-10;				
				jQuery(this).css("width",newWidth);
				jQuery(this).css("height",newHeight);
				jQuery(this).css("margin","5px 5px 0px 5px");
			});
		jQuery(window).resize(function(){
				jQuery("#workimg div img").each(function() {
					var width  = jQuery(this).width();
					var height = jQuery(this).height();
					var parentWidth  = jQuery(window).width()-10;
					var parentHeight = jQuery(window).height()-10;
					var rawidth = width / parentWidth;
					var raheight = height/parentHeight;
					
							newWidth  = parentWidth;
							newHeight = newWidth/width*height;
					
					jQuery(this).css("width",newWidth);
					jQuery(this).css("height",newHeight);
					jQuery(this).css("margin","5px 5px 0px 5px");
				});
			})
	},
		
	setHash : function(hashValue)
	{
		if(hashValue == '/')
		{
			window.location.hash = '';
		}
		else
		{
			window.location.hash = '#' + hashValue;
		}
	},
	
	
	checkParentHash : function()
	{		
		var htmlCod;
		
		if(this.currentHash != window.location.hash)
		{
			this.currentHash = window.location.hash;
			var temp = this.currentHash.split('#');
			var hashValue = (temp.length > 1) ? temp[1] : '';
			var deeplinks = hashValue.split('/');
			
			//	This strips out empty values
			deeplinks = jQuery.grep(deeplinks,function(n,i){
				return(n);
			});
			
			this.deeplinkValue1 = (deeplinks[0] != null) ? deeplinks[0] : '';
			this.deeplinkValue2 = (deeplinks[1] != null) ? deeplinks[1] : null;
			
			if(this.deeplinkValue1 == '' )
			{
				this.showHome();
			}
			else if(this.deeplinkValue1 == 'Feed')
			{
				_gaq.push(['_trackEvent', 'Website', '/Feed']);
				
				jQuery.ajax({
					type: 'GET',
					url: 'xml/work.xml',
					dataType: 'xml',
					async: false,
					success: function(xml){
						var htmlContent;
						PageController.RSSFeedHTML = '';
						jQuery(xml).find('item').each(function(idx){
							jQuery(this).find('portfolio').each(function(inidx)
							{
								if(jQuery(this).attr('level') == 'first' )
								{
									if(jQuery(this).attr('name') == 'video')
									{
										PageController.RSSFeedHTML +='<div class="feedDiv"><div class="feedTitle">'+ jQuery(this).find('title').text() +'</div><div><a href="javascript:void(PageController.showVideo('+idx+','+inidx+'))"><img src="images/work/' + jQuery(this).find('image').text() + '" /></a></div></div>';
									}
									else
									{
										PageController.RSSFeedHTML +='<div class="feedDiv"><div class="feedTitle">'+ jQuery(this).find('title').text() +'</div><div><a href="javascript:void(PageController.showFeed('+idx+'))"><img src="images/work/' + jQuery(this).find('image').text() + '" /></a></div></div>';
									}
								}
							});					
						});
					}
				});
		
				htmlCode = '<div id="rsscontent">';
				htmlCode += '<a href="javascript:void(PageController.setHash(\'/home/\'))"><div id="worklogo"></div></a>';
				htmlCode += '<div id="work-nav">';
				htmlCode += '<ul>';
				htmlCode += '<li><a href="javascript:void(PageController.setHash(\'/work/\'))" ><div class="work" id="worknav1"></div></a></li>';
				htmlCode += '<li><a href="javascript:void(PageController.showAboutWork())" ><div class="aboutus" id="aboutnav1"></div></a></li>';
				htmlCode += '<li><a href="javascript:void(PageController.showContactWork())"><div class="contactus" id="contactnav1"></div></a></li>';
				htmlCode += '<li><a href="javascript:void(PageController.setHash(\'/Feed/\'))"><div class="rssactive"></div></a></li>';
				htmlCode += '</ul>';
				htmlCode += '</div>';
				htmlCode += '<div class="rss_icon"><a href="xml/work.xml"><img src="images/rss.png" alt="" title="" /></a></div>';
				htmlCode += '<div id="contact-detail-work"><img src="images/contact_detail.png" alt="" border="0px" usemap="#Map1" title="" />';
				htmlCode += '<map name="Map" id="Map1"><area shape="rect" coords="1,60,150,76" href="mailto:richie@richiegleason.com" /></map>';
				htmlCode += '</div>';
				htmlCode += '<div id="about-detail-work"><img src="images/about_detail.png" border="0px" alt="" title="" /></div>';
				htmlCode += '</div>';
				htmlCode += '<div id="feed">';
				htmlCode += PageController.RSSFeedHTML;
				htmlCode += '</div>';
				jQuery('#wrapper').html(htmlCode);
				jQuery('#rsscontent').fadeIn(1000);
				PageController.hidAll();
				
			}
			else if(this.deeplinkValue1 == 'home')
			{
				this.showHome();
			}
			else if(this.deeplinkValue1 == 'work')
			{
				this.showHome();
				PageController.showWork();
			}			
		}
		else	//	No hash value, so show intro or home
		{
			if(this.deeplinkValue1 == '')	//	Intro page
			{
				
				this.showHome();
			}
		}
	}
}

jQuery(document).ready(function() 
{
	PageController.loadXML();
});


