var offsetTop = -1;

function fitShadowBox()
{
	var overlay = document.getElementById('overlay');
	
	if(overlay.style.display != "none")
	{	
		var pageHeight1 = (document.height !== undefined) ? document.height : document.body.offsetHeight;
		//var pageWidth = (document.width !== undefined) ? document.width : document.body.offsetWidth;
		
		/*if (window.innerHeight && window.scrollMaxY) {// Firefox
			pageHeight = window.innerHeight + window.scrollMaxY;
			pageWidth = window.innerWidth + window.scrollMaxX;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			pageHeight = document.body.scrollHeight;
			pageWidth = document.body.scrollWidth;
		} else { // works in Explorer 6 Strict, Mozilla (not FF) and Safari
			pageHeight = document.body.offsetHeight;
			pageWidth = document.body.offsetWidth;
		}*/
		
		//var pageWidth = self.innerWidth || (document.documentElement.clientWidth || document.body.clientWidth);
		var pageHeight2 = self.innerHeight || (document.documentElement.clientHeight || document.body.clientHeight);
		
		var pageHeight = (pageHeight1 >= pageHeight2) ? pageHeight1 : pageHeight2;
		
		var shadowbox = document.getElementById('shadowbox');
		
		if(offsetTop == -1)
		{
			offsetTop = 100;
			shadowbox.style.top = "40px";
		}
		
		var scrollY = (window.pageYOffset || document.documentElement.scrollTop || 0) + offsetTop;

		shadowbox.style.top = scrollY + "px";

		overlay.style.width = "100%";
		overlay.style.height = pageHeight + "px";
		overlay.onclick = hideShadowBox;
	}
}

function shadowBoxHTML(html)
{
	document.body.style.overflowY = "hidden";
	document.body.style.overflowX = "hidden";
	document.getElementsByTagName("html")[0].style.overflowY = "hidden";
	document.getElementsByTagName("html")[0].style.overflowX = "hidden";
	
	var shadowbox = document.getElementById('shadowbox');
	var overlay = document.getElementById('overlay');
	var shadowboxContent = document.getElementById('shadowbox-content');
	
	shadowbox.style.display = "inline";
	overlay.style.display = "inline";
	shadowboxContent.style.overflow = "auto";
	
	shadowboxContent.innerHTML = unescape(html);
	
	fitShadowBox();
}

function showShadowBox(typeElm,index)
{
	document.body.style.overflowY = "hidden";
	document.body.style.overflowX = "hidden";
	document.getElementsByTagName("html")[0].style.overflowY = "hidden";
	document.getElementsByTagName("html")[0].style.overflowX = "hidden";
	
	var postElm = typeElm[index];
	
	postElm.content.resetCurrentImage();

	var shadowbox = document.getElementById('shadowbox');
	
	if(!document.getElementById('post-next'))
	{
		shadowbox.innerHTML += "<div id='post-next'><img src='/wp-content/themes/sliderstudio/img/next-post.png' alt='Next post' title='Next post' /></div>";
		shadowbox.innerHTML += "<div id='post-prev'><img src='/wp-content/themes/sliderstudio/img/previous-post.png' alt='Previous post' title='Previous post' /></div>";
	}
	

	if(typeElm[index+1])
	{
		document.getElementById('post-next').style.display = "inline";
	}
	else
	{
		document.getElementById('post-next').style.display = "none";
	}
	
	if(typeElm[index-1])
	{
		document.getElementById('post-prev').style.display = "inline";
	}
	else
	{
		document.getElementById('post-prev').style.display = "none";
	}

	document.getElementById('post-next').onclick = function() {showShadowBox(typeElm,index+1)};
	document.getElementById('post-prev').onclick = function() {showShadowBox(typeElm,index-1)};
	
	var shadowboxContent = document.getElementById('shadowbox-content');
	
	if(postElm.isGallery)
	{
		if(postElm.content.images[0] && postElm.content.images[0].charAt(0) == "%")
		{
			shadowBoxHTML("<div id='gallery-text'><h3>"+postElm.title+"</h3><div class='post-date'>"+postElm.postDate+"</div>"+postElm.content.text+"<div id='social'></div></div><div id='gallery-image'>"+postElm.content.images[0]+"</div>");
		}
		else
		{
			shadowBoxHTML("<div id='gallery-text'><h3>"+postElm.title+"</h3><div class='post-date'>"+postElm.postDate+"</div>"+postElm.content.text+"<div id='social'></div></div><div id='gallery-image'><img src="+postElm.content.images[0]+" /></div>");
		}
		
		shadowboxContent.style.overflow = "hidden";
		
		//showShadowBox(typeElm,index);
		if(postElm.content.images.length > 1)
		{
			//add next links
			navString = "<div id='gallery-nav'><div id='gallery-prev' class='gallery-nav-button' ><img src='/wp-content/themes/sliderstudio/img/previous-arrow.png' alt='Previous' title='Previous' /></div><div id='gallery-indexOf'></div>";
			navString += "<div id='gallery-next' class='gallery-nav-button' ><img src='/wp-content/themes/sliderstudio/img/next-arrow.png' alt='Next' title='Next' /></div></div>";
			shadowboxContent.innerHTML += navString;
			document.getElementById('gallery-next').onclick = function()
																	{
																		changeGalleryImage(postElm.content,"next");	
																	};
			document.getElementById('gallery-prev').onclick = function()
																	{
																		changeGalleryImage(postElm.content,"prev");
																	};
			document.getElementById('gallery-indexOf').innerHTML = "1 of " + (postElm.content.images.length);
		}
		shadowboxContent.scrollTop=0;

	}
	else if(postElm.isDemo)
	{
		shadowBoxHTML("<div id='shadow-demo'>"+postElm.content.images[0]+"<div id='social'></div></div>");	//load up first media item (should be only media item in post!)
		shadowboxContent.style.overflow = "hidden";
	}
	else
	{
		blogContent = "<h3>"+postElm.title+"</h3><div class='post-date'>"+postElm.postDate+"</div>"+postElm.fullContent+"<div id='social'></div>";
		shadowBoxHTML(blogContent);
		shadowboxContent.style.overflow = "auto";
		shadowboxContent.scrollTop=0;
		
	}
	
	//show social bookmarking icons
	showSocial(postElm.postLink,postElm.title);
	
}

function hideShadowBox()
{
	document.getElementsByTagName("html")[0].style.overflowY = "scroll";
	document.getElementsByTagName("html")[0].style.overflowX = "auto";

	var shadowbox = document.getElementById('shadowbox');
	var overlay = document.getElementById('overlay');
	var shadowboxContent = document.getElementById('shadowbox-content');
	
	shadowbox.style.display = "none";
	overlay.style.display = "none";
	shadowboxContent.innerHTML = "";
	
	//hack for popups using overlay
	var feedback = document.getElementById('feedback');
	if (feedback != null) feedback.style.display = "none";
}