$(document).ready(function() {

	galCB = $("#galcontainer").html();
	$("#galcontainer").remove();
	$("div.RightWrap").append(galCB);
	$("#gallery").wrap("<div id='galleryWrap'></div>");
	$("#galleryWrap").css({ width:"466px", height:"135px", marginLeft:"10px" });
	src69 = "/portals/_default/Skins/crest/images/gallery_boundary.gif";
	$('#galleryWrap').css('backgroundImage','url(' + src69 +')'); 

	var adminTest;
	$("img").each(function(i) {								 
		src = $(this).attr("src");
		if (src == "/images/action_settings.gif"){ adminTest = true; }
	});
	
	if (adminTest == true){
		$("#slideContainer").css("overflow","visible");
		$("#gallery").css("height","auto");
		$("#slideControlDevelopment").css("position","relative");
		
	} else {

		var imageLinks = new Array();
	
		$("div.lifestyleGalleryImage").each(function(i) { 
			imageLinks[i] = $(this).html();
	
		});
		
		$("#deleteGallery").remove();
	
		$(imageLinks).each(function(i) {
			$("#slideControlDevelopment").append(imageLinks[i]);								   
		});	
		$("#slideControlDevelopment a").addClass("thickbox");
		
		$("#slideControlDevelopment a").each(function(i) { 
			var href = $(this).attr("href") + "&KeepThis=true&TB_iframe=true&height=600&width=900"									   
			$(this).attr("href",href);
		});
	
		var slideNumber = 0;
		var galleryImgWidth = 137;
		$("#slideControlDevelopment").find("img").each(function(i) {
			slideNumber = slideNumber + 1;
		});	 
		
	
		var slideControlWidth = (slideNumber + 1) * galleryImgWidth;
		
		$("#slideControlDevelopment").css("width",slideControlWidth);
	
		$("#galleryRightDevelopment").click(function () { 
			currentLeftPos = $("#slideControlDevelopment").css ("left");
		
			var currentLeftPos = currentLeftPos.substr(0,(currentLeftPos.length - 2));
			
			currentLeftPos = currentLeftPos * 1;
			currentLeftPos = Math.round(currentLeftPos);
			var testPos = currentLeftPos/galleryImgWidth;
			if (currentLeftPos > ((slideControlWidth*-1)+(2*galleryImgWidth)) && ( testPos == parseInt(testPos) ) ){
				currentLeftPos = currentLeftPos - galleryImgWidth;
				currentLeftPos = currentLeftPos + "px";

				$("#slideControlDevelopment").animate({ 
					left: currentLeftPos
				  }, 750 );			
			}
			
		});
		$("#galleryLeftDevelopment").click(function () { 
			currentLeftPos = $("#slideControlDevelopment").css ("left");
			var currentLeftPos = currentLeftPos.substr(0,(currentLeftPos.length - 2));
			
			currentLeftPos = currentLeftPos * 1;
			currentLeftPos = Math.round(currentLeftPos);
			var testPos = currentLeftPos/galleryImgWidth;
			if (currentLeftPos < 0 && ( testPos == parseInt(testPos) ) ){
				currentLeftPos = currentLeftPos + galleryImgWidth;
				currentLeftPos = currentLeftPos + "px";

				$("#slideControlDevelopment").animate({ 
					left: currentLeftPos
				  }, 750 );
			}	
		});
		
		$("#slideContainer").css("width","403px");
		$("#gallery").css("width","449px");
		$("#gallery").css("margin-left","10px");
		$("#gallery").css("top","30px");
		$("#gallery").css("position","relative");
	}

});