function init(){
	if(document.getElementById("gallery_preview")){
		setupGallery('gallery_preview','hp_gallery','hp_gall_frame','hp_gal_lbtn','hp_gal_rbtn');
	}
	if(document.getElementById("gallery_preview2")){
		setupGallery('gallery_preview2','hp_gallery2','hp_gall_frame2','hp_gal_lbtn2','hp_gal_rbtn2');
	}
	if(document.getElementById("tpl_galleryhead")){
		setGalleryPage();
	}
}

var gallitemwidth = 81;
function setupGallery(tpl_a,tpl_b,tpl_c,tpl_d,tpl_e){
	var scrollgallleft = 0;
	var gallout = document.getElementById(tpl_a);
	var gallin = document.getElementById(tpl_b);
	var gallframe = document.getElementById(tpl_c);
	if(gallout.offsetWidth > gallin.offsetWidth){
		var thewidth = gallout.offsetWidth;
		gallin.style.width = thewidth+"px";
		var finwidth = thewidth - 72;
		gallframe.style.width = finwidth+"px";
	}
	gallframe.style.overflow = "hidden";
	var tlbtn = document.getElementById(tpl_d);
	var trbtn = document.getElementById(tpl_e);
	tlbtn.style.display = trbtn.style.display = "block";
	targetHeight = (gallframe.offsetHeight / 2) - (tlbtn.offsetHeight / 2);
	tlbtn.style.top = trbtn.style.top = targetHeight+"px";
	tlbtn.onclick = function(){scrollgall("right", gallframe,tlbtn,trbtn,scrollgallleft);}
	trbtn.onclick = function(){scrollgall("left", gallframe,tlbtn,trbtn,scrollgallleft);}
	testGallLimits(tlbtn,trbtn,gallframe,scrollgallleft);
}
function testGallLimits(tlbtnxx,trbtnxx,gallframexy, scrollgallleftxy){
	var tlbtnxxloc = tlbtnxx;
	var trbtnxxloc = trbtnxx;
	var gallframexyloc = gallframexy;
	var scrollgallleftloc = scrollgallleftxy;
	if(scrollgallleftloc <= 0){
		tlbtnxxloc.className = "inactive";
	}else{
		tlbtnxxloc.className = "";
	}
	if((scrollgallleftloc + gallframexyloc.offsetWidth) >= gallframexyloc.scrollWidth){
		trbtnxxloc.className = "inactive";
	}else{
		trbtnxxloc.className = "";
	}
}
function scrollgall(direct, gallframexx, tlbtnxx, trbtnxx, scrollgallleftxx){
	var directloc = direct;
	var gallframexxloc = gallframexx;
	var tlbtnxxloc = tlbtnxx;
	var trbtnxxloc = trbtnxx;
	var maxitems = Math.floor(gallframexxloc.offsetWidth / gallitemwidth);
	var scrollgallleftloc = scrollgallleftxx;
	
	if(direct == "left"){
		if((scrollgallleftloc + gallframexxloc.offsetWidth) < gallframexxloc.scrollWidth){
			tlbtnxxloc.onclick = null;
			trbtnxxloc.onclick = null;
			
			var newscrollL = 0;
			if((scrollgallleftloc+(gallitemwidth*maxitems)+gallframexxloc.offsetWidth) <= gallframexxloc.scrollWidth){
				newscrollL = scrollgallleftloc+(gallitemwidth*maxitems);
			}else{
				newscrollL = gallframexxloc.scrollWidth - gallframexxloc.offsetWidth;
			}
			lscroll = new Tween(gallframexxloc,'scrollLeft',Tween.strongEaseInOut,scrollgallleftloc,newscrollL,1);
			lscroll.start();
			lscroll.onMotionFinished = function(){
				scrollgallleftloc = newscrollL;
				tlbtnxxloc.onclick = function(){scrollgall("right",gallframexxloc, tlbtnxxloc, trbtnxxloc, scrollgallleftloc);}
				trbtnxxloc.onclick = function(){scrollgall("left",gallframexxloc, tlbtnxxloc, trbtnxxloc, scrollgallleftloc);}
				testGallLimits(tlbtnxxloc,trbtnxxloc,gallframexxloc, scrollgallleftloc);}
		}
	}else{
		if(scrollgallleftloc > 0){
			tlbtnxxloc.onclick = null;
			trbtnxxloc.onclick = null;
			
			var newscrollR = 0;
			if((scrollgallleftloc - gallframexxloc.offsetWidth) > 0){
				newscrollR = Math.floor(scrollgallleftloc / gallitemwidth);
				newscrollR = (newscrollR - maxitems) * gallitemwidth;
			}else{
				newscrollR = 0;
			}
			rscroll = new Tween(gallframexxloc,'scrollLeft',Tween.strongEaseInOut,scrollgallleftloc,newscrollR,1);
			rscroll.start();
			rscroll.onMotionFinished = function(){
				scrollgallleftloc = newscrollR;
				tlbtnxxloc.onclick = function(){scrollgall("right",gallframexxloc, tlbtnxxloc, trbtnxxloc, scrollgallleftloc);}
				trbtnxxloc.onclick = function(){scrollgall("left",gallframexxloc, tlbtnxxloc, trbtnxxloc, scrollgallleftloc);}
				testGallLimits(tlbtnxxloc,trbtnxxloc,gallframexxloc, scrollgallleftloc);}
		}
	}
}
function setGalleryPage(){
	document.getElementById("tpl_galleryhead").style.display = "block";
	document.getElementById("tpl_galleryhead").onclick = hideshowGallery;
	document.getElementById("tpl_gallerybody").style.display = "none";
}
function hideshowGallery(){
	if(document.getElementById("tpl_gallerybody").style.display == "none"){
		document.getElementById("tpl_gallerybody").style.display = "block";
		document.getElementById("tpl_gallbtn").className = "selected";
		document.getElementById("sfc_mainbodycontent").style.display = "none";
	}else{
		document.getElementById("tpl_gallerybody").style.display = "none";
		document.getElementById("tpl_gallbtn").className = "";
		document.getElementById("sfc_mainbodycontent").style.display = "block";
	}
}
