//
//
//**************************************************************
//
// INIT (OVERRIDE)
//
//**************************************************************
(function() {
  var proxied = init
  init = function() {
	
	 proxied()
    initPage()
  //  return proxied.apply(this, arguments);
  };
  
  var proxied_size = setSize
  setSize = function(){
	  proxied_size();
	  setSizePage();
  }
})();
//
//
//**************************************************************
//
// GALLERY
//
//**************************************************************
function initPage(){
	initGallery();
	initLightboxHack();
}


//
//
//**************************************************************
//
// GALLERY
//
//**************************************************************
function initGallery(){
	jQuery("#gallery #arrow_left").click(scrollLeft);
	jQuery("#gallery #arrow_right").click(scrollRight);
	//
	jQuery("#gallery #images").css("left", jQuery("#gallery #arrow_left").width());
	//
	jQuery('div[id^="arrow_"]').hover(function(){scrollButtonOver(jQuery(this));},function(){ scrollButtonOut(jQuery(this));});
	//
	

	//
	galleryIntro();
}
function initLightboxHack(){
	console.log("init hack");
	var myLinks = jQuery("td.photogalleryItem a");
			jQuery.each( myLinks, function(thisIndex, thisLink){
				jQuery(thisLink).click(function(){_resetGallery(),_sizeTimer();});
				//jQuery(thisLink).prop("onclick","");
			//	jQuery(thisLink).prop("rel","");
			} )
		//Lightbox.prototype.disableKeyboardNav();
		console.log(Lightbox.prototype.keyboardAction)
		//console.log(myLightbox)
		Lightbox.prototype.keyboardAction = function(){};
}

function sizeTimer(){
	console.log("timer");
	var myImage = jQuery("#outerImageContainer img")
	myImage.css("width","auto")
	myImage.css("height","auto")
	clearTimeout(_timer_object)
	_timer_object = setTimeout(this._resizeImage,4000)
	if(!_timer_added){
		jQuery("#hoverNav a").click(function(){_resetGallery(),_sizeTimer();});
		_timer_added = true
	}
	
}
var _timer_object
var _sizeTimer = sizeTimer
var _timer_added = false
function resizeImage(){
	var myIE7 =  jQuery.browser.msie && parseInt(jQuery.browser.version, 10)==7
	var myDontAnimate = myIE7
	//resetGallery()
	//alert("jimmy")
	var myImage = jQuery("#outerImageContainer img")
	var myOuterContainer = jQuery("#outerImageContainer")
	var myImageContainer = jQuery("#imageDataContainer")
	var myLightBox = jQuery("#lightbox")
	var myWidth = myImage.width()
	var myHeight = myImage.height()
	
	
	
	var myMaxWidth = width*.85
	var myMaxHeight = height*.8
	var myScale = Math.min(myMaxWidth / myWidth, myMaxHeight / myHeight,1)
	
	var myNewWidth = Math.round(myWidth*myScale)
	var myNewHeight = Math.round(myHeight*myScale)
	var mySpeed = myIE7?0:500
	if(myNewWidth!=myWidth&&myNewHeight!=myHeight){
		myImage.animate({"width":myNewWidth,"height":myNewHeight},mySpeed)
		myOuterContainer.animate({"width":myNewWidth+20,"height":myNewHeight+20},mySpeed);
		myImageContainer.animate({"width":myNewWidth+20},mySpeed,setSize);
		myLightBox.animate({"top":((height-myNewHeight)/2)-20},mySpeed);
		//if(myHeight!=myNewHeight){
			//myLightBox.animate({"top":, "top":(height-myNewHeight)/2},mySpeed);
		//}
		jQuery("#hoverNav a").css("height","100%");
	}

}
var _resizeImage = resizeImage


function resetGallery(){
	var myImage = jQuery("#outerImageContainer img")
	myImage.css("max-width","none");
	myImage.css("max-height","none");
/*	jQuery("#outerImageContainer").css("width",myImage.width()+20);
	jQuery("#outerImageContainer").css("height",myImage.height()+20);

	jQuery("#imageDataContainer").css("width",myImage.width()+20)*/
}
var _resetGallery = resetGallery
function setSizePage(){
	setSizePageContent()
	//_resetGallery()
}
function setSizePageContent(){
	var myStrips = jQuery("#main_strip, #main_blurb");
	myStrips.css("height",height-450);
}

//
//
//**************************************************************
//
// GALLERY INTRO
//
//**************************************************************
function galleryIntro(){
	var myGallery = jQuery("#gallery");
	
	var myHeight = myGallery.height();
	var myTop = parseInt(myGallery.css('top'), 10);
	
	var myImages =  jQuery("#gallery #images");
	//
	//-------------------------------------------------------
	// Hide Images 
	//-------------------------------------------------------
/*	myImages.animate({
		opacity: 0
	  }, 0);
	//
	//-------------------------------------------------------
	// Hide Arrows
	//-------------------------------------------------------
	jQuery('div[id^="arrow_"]>img').animate({
			opacity:0
			},0);
	 //
	//-------------------------------------------------------
	// Collapse Gallery
	//-------------------------------------------------------
	  myGallery.css("height",1);
	  myGallery.css("top",myTop+myHeight/2);
	  myGallery.animate({
		opacity: 0
	  }, 0);
	  myGallery.css("visibility","visible");*/
	  //
	//-------------------------------------------------------
	// Listen for loaded event
	//-------------------------------------------------------
	window.onload = function(){
		//
		//-------------------------------------------------------
		// Insert Stylesheet
		//-------------------------------------------------------
		var ss = document.createElement("link"); 
		ss.type = "text/css"; 
		ss.rel = "stylesheet"; 
		ss.href = "/stylesheets/custom_lightbox.css"; 
		document.getElementsByTagName("body")[0].appendChild(ss); 
		 //
		//-------------------------------------------------------
		// Add Custom Elements
		//-------------------------------------------------------
		var myCloseButton = jQuery("#bottomNavClose")
		myCloseButton.append('<div id="close" class="button">Close</div>');
		initButtons();
		//
		//-------------------------------------------------------
		// Positioning hack - too bad I can't edit the javascript
		//-------------------------------------------------------
	/*	jQuery("#lightbox").css("margin-top","-40px");

		//
		 myGallery.animate({
			opacity:1,
		  height:myHeight,
		  top:myTop
	  },750,galleryRevealComplete);*/
	 };
	 
}
function galleryRevealComplete(){
	showImages();
}
function showImages(){
	var myImages =  jQuery("#gallery #images");
	myImages.animate({
		opacity: 1
	  }, 250,imagesRevealComplete);
}
function imagesRevealComplete(){
	//
	//-------------------------------------------------------
	// Hide Arrows
	//-------------------------------------------------------
	jQuery('div[id^="arrow_"]>img').animate({
			opacity:1
			},150);
}
//
//
//**************************************************************
//
// GALLERY INTERACTION
//
//**************************************************************
function scrollButtonOver(thisElement){
	thisElement.children('img').animate({
		opacity: .4
	  }, 200);
}
function scrollButtonOut(thisElement){
	thisElement.children('img').animate({
		opacity: 1
	  }, 200);
}
function scrollLeft(){
	scrollGallery(1)
}
function scrollRight(){
	scrollGallery(-1)
}
function scrollGallery(thisDir){
	//
	var myNavWidth = jQuery("#gallery #arrow_left").width();
	var myImages =  jQuery("#gallery #images");
	myImages.stop();
	var myImagesWidth =myImages.width();
	var myLeft = myImages.position().left;
	var myWindowWidth = jQuery("#images_container").width();
	//
	var myScrollDistance = (myWindowWidth-(myNavWidth*2))*thisDir;
	var myScrollDestination = myLeft+myScrollDistance
	//
	if(thisDir==1){
		myScrollDestination = Math.min(myNavWidth,myScrollDestination);
	}else{
		
		myScrollDestination = Math.max(-(myImagesWidth-myWindowWidth)-myNavWidth,myScrollDestination);

	}

	myImages.animate({
		left: myScrollDestination
	  }, 1000, scrollGalleryComplete);

}
function scrollGalleryComplete(){
}


