// take all the HR tags and wrap a div around it so we can control the styling better
// screen readers still get the nice HR tag along with browsers with javascript turned off
function replaceHR()
{
	if(!document.getElementsByTagName)
		return false;
	var hr = document.getElementsByTagName("hr");
	for(var i=0,z=hr.length;i<z;i++)
	{
		var newHR = hr[i];
		var div = document.createElement("div");
		div.className = "hrBorder";
		hr[i].parentNode.replaceChild(div,hr[i]);
		div.appendChild(newHR);
	}
}
/*----------------------*/


var curPageCont; 	//element that holds the current page
var totalPageCont;	// element that holds the total # of pages
function initImgSlide()
{
	var scrollItems = $("#scrollItems");
	
	var tempWidth = 0;
	var imgs = scrollItems[0].getElementsByTagName("img");
	if(imgs.length < 2)
		return false;
	
	document.getElementById("pagesNav").style.display = "block";
	// find the width of the elements and then position them
	$("img",scrollItems).each(function(i)
	{
	// console.log(i)
		tempWidth += this.offsetWidth;
		if(i > 0)
			this.style.left = (i * 946) + "px";
		else
			this.style.left = (i * 946) + "px";
	});
	//scrollItems.css({width:tempWidth});
	$("#nextArrow").bind("click",function(){ changePageNum(-1); return false; });
	$("#prevArrow").bind("click",function(){ changePageNum(1); return false; });
	curPageCont = $("#curPage");		// element that holds the current page
	totalPageCont = $("#totalPages");	// element that holds the total # of pages
}

function slideImages(dir)
{
	var imgs = $(".scrollImg");
	// console.log(imgs.length)
	if(dir == 1)
	{
		for(var i=imgs.length-1,z=0;i>=z;i--)
		{
			var leftPos = imgs[i].offsetLeft;
			var left = leftPos - imgs[i].offsetWidth * dir;
			$(imgs[i]).animate({left:left},speed,function() 
				{
					bindEvent("#nextArrow","click",function(){ changePageNum(-1); return false; });
					bindEvent("#prevArrow","click",function(){ changePageNum(1); return false; });					
				});
		}
	}
	else
	{
		for(var i=0,z=imgs.length;i<z;i++)
		{
			var leftPos = imgs[i].offsetLeft;
			var left = leftPos - imgs[i].offsetWidth * dir;
			$(imgs[i]).animate({left:left},speed,function() 
				{
					bindEvent("#nextArrow","click",function(){ changePageNum(-1); return false; });
					bindEvent("#prevArrow","click",function(){ changePageNum(1); return false; });					
				});
		}
	}
}
/*----------------------*/

// change the page number of the image shown
function changePageNum(dir)
{
	unbindEvent("#nextArrow","click");
	unbindEvent("#prevArrow","click");
	// find the page number and total number of pages, convert to Int
	var curPage = parseInt(curPageCont.html(),10);
	var totalPage = parseInt(totalPageCont.html(),10);
	curPage = curPage + dir;
	if(curPage > 0 && curPage <= totalPage)
	{
		if(curPage > 1)
		{
			// console.log(curPage)
			document.getElementById("prevArrow").style.display = "block";
		}
		if(curPage < 10)
			curPage = "0" + curPage
		curPageCont.html(curPage);
		slideImages(dir);
	}
	else
	{
		if(curPage == 0)
		{
			
		}
		bindEvent("#nextArrow","click",function(){ changePageNum(-1); return false; });
		bindEvent("#prevArrow","click",function(){ changePageNum(1); return false; });
	}
}
/*----------------------*/

// scroll a container vertically
var scrollTimer;
var vScrollAmount = 95;
function vScrollAction(id,dir)
{
	unbindEvent("#upArrowBtn","click");
	unbindEvent("#downArrowBtn","click");
	var el = $("#"+id);
	var topPos = parseInt(el.css("top"));
	var top = topPos;
	if(dir == 1 && Math.abs(top) < el.height() - vScrollAmount)	// scroll down
		el.animate({top:topPos-vScrollAmount},speed, function(){vScrollBinding();});
	else if(dir == -1 && Math.abs(top) > 0)	// scroll up
		el.animate({top:topPos+vScrollAmount},speed, function(){vScrollBinding();});
	else
		vScrollBinding();
		
}
/*----------------------*/

function stopTimer()
{
	// console.log("stop");
	clearInterval(scrollTimer);
}
/*----------------------*/

//
function vScrollBinding()
{
	if($("a","#vScroll").length > 5) {
		$("#upArrowBtn").bind("click",function(){ vScrollAction("vScroll",-1); return false; });
		$("#downArrowBtn").bind("click",function(){ vScrollAction("vScroll",1); return false; });
	}
	else {
		$("#upArrowBtn,#downArrowBtn").hide();
	}
}
/*----------------------*/

// add events to an element
function bindEvent(el,event,handler)
{
	el = $(el);
	el.bind(event,handler);
}
/*----------------------*/

// remove events from an element
function unbindEvent(el,event)
{
	el = $(el);
	el.unbind(event);
	if(event == "click")
	{
		el.bind("click",function(){ return false; });
	}
}
/*----------------------*/
