var galleryRoller = {
	startSizeX : 662,
	endSizeX : 986,
	startSizeY : 521,
	endSizeY : 891,
	imgType : ".png",
	status : 'small',
	init : function () {
		var animElements = document.getElementById("BowlGallery").getElementsByTagName("img");
		
		for (var i = 0; i < animElements.length; i++) {
			var y = animElements[i];
			y.style.width = galleryRoller.startSizeX + 'px';
			y.style.height = galleryRoller.startSizeY + 'px';
			//galleryRoller.imgSmall(y);
			var largeClick = document.getElementById("largeClick");
			var smallClick = document.getElementById("smallClick");
			largeClick.onclick = changeSize;
			smallClick.onclick = restoreSize;
		}
		
		function changeSize() {
			if (galleryRoller.status == 'small') {
				jQuery('#cont-main').animate( { 'paddingBottom':'356px' }, 750, 'linear' );
				jQuery('#AbsoluteSlider').fadeOut(1000);
				jQuery('#fade').cycle('pause');
				jQuery('#AbsoluteSlider').css( { 'z-index':'-10' } );
				
				jQuery('#site-info').css( { 'position':'relative' } );			
				jQuery('#site-info').animate( { 'top':'356px' }, 750, 'linear' );
				
				if ($.browser.msie && $.browser.version < 8) {
					jQuery('#sidebar-right').css( { 'marginLeft':'3px' } );					
				}
				
				jQuery('#sidebar-right').css( { 'position':'absolute' } );				
				jQuery('#sidebar-right').animate( { 'marginTop':'910px' }, 1750, 'linear', function() {
					jQuery('#fade img').nextAll().andSelf().animate( { 'width':'978px', 'height':'846px' }, 1750, 'linear');
					jQuery('#fade .slide').nextAll().andSelf().attr('cycleH', '846');
					jQuery('#fade .slide').nextAll().andSelf().attr('cycleW', '978');
					jQuery('#fade img, #fade .pics, #fade .slide').nextAll().andSelf().css( { 'width':'978px', 'height':'846px' } );
					jQuery('#fade').animate( { 'width':'978px', 'height':'846px' }, 1750, 'linear');
					jQuery('#AbsoluteSlider').animate( { 'width':'978px', 'height':'864px' }, 1750, 'linear');
					jQuery('#AbsoluteSlider').fadeOut(1000,  function() { jQuery('#AbsoluteSlider').css( { 'z-index':'1' } ) });
					jQuery('#AbsoluteSlider').fadeIn(1000, function() {
						jQuery('.gallerySizer #smallClick')
						.css( { backgroundPosition:'-40px 0px', color:'#00f' } )
						.stop().animate( { backgroundPosition:'(0px 0px)' }, { duration:1000 });
						jQuery('.gallerySizer #largeClick')
						.css( { backgroundPosition:'-80px 0px', color:'#fff' } )
						.stop().animate( { backgroundPosition:'(-40px 0px)' }, { duration:1000 });
					});
					jQuery('#fade').cycle('resume');
				
					galleryRoller.imgLarge(document.getElementById("GalleryBackground"));
		
					if (!document.getElementById("GalleryBackground").currentWidth) {
						document.getElementById("GalleryBackground").currentWidth = galleryRoller.startSizeX;
						document.getElementById("GalleryBackground").currentHeight = galleryRoller.startSizeY;
					}
						
					galleryRoller.resizeAnimation(document.getElementById("GalleryBackground"), 
													document.getElementById("GalleryBackground").currentWidth, 
													galleryRoller.endSizeX, 
													document.getElementById("GalleryBackground").currentHeight, 
													galleryRoller.endSizeY, 10, 10, 0.333);
				});
				
				galleryRoller.status = 'large';
			}
		}
		
		function restoreSize() {
			if (galleryRoller.status == 'large') {
				//var x = this.parentNode.getElementsByTagName("span");
				//x[0].style.display = 'none';
				
				jQuery('#AbsoluteSlider').fadeOut(1000);
				jQuery('#fade').cycle('pause');
				jQuery('#AbsoluteSlider').css( { 'z-index':'-10' } );
				
				if (!document.getElementById("GalleryBackground").currentWidth)
					return;
				
				galleryRoller.resizeAnimation(document.getElementById("GalleryBackground"), 
												document.getElementById("GalleryBackground").currentWidth, 
												galleryRoller.startSizeX, 
												document.getElementById("GalleryBackground").currentHeight, 
												galleryRoller.startSizeY, 8, 12, 0.333);
				galleryRoller.imgSmall(document.getElementById("GalleryBackground"));
				setTimeout(function() {
					jQuery('#fade img').nextAll().andSelf().animate( { 'width':'654px', 'height':'490px' }, 1450, 'linear');
					jQuery('#fade .slide').nextAll().andSelf().attr('cycleH', '490');
					jQuery('#fade .slide').nextAll().andSelf().attr('cycleW', '654');
					jQuery('#fade img, #fade .pics, #fade .slide').nextAll().andSelf().css( { 'width':'654px', 'height':'490px' } );
					jQuery('#fade').animate( { 'width':'654px', 'height':'490px' }, 1450, 'linear');
					jQuery('#AbsoluteSlider').animate( { 'width':'654px', 'height':'508px' }, 1450, 'linear');
					jQuery('#AbsoluteSlider').fadeOut(1000,  function() { jQuery('#AbsoluteSlider').css( { 'z-index':'1' } ) });
					jQuery('#AbsoluteSlider').fadeIn(1000, function() {
						jQuery('.gallerySizer #largeClick')
						.css( { backgroundPosition:'-40px 0px', color:'#00f' } )
						.stop().animate( { backgroundPosition:'(-80px 0px)' }, { duration:1000 });
						jQuery('.gallerySizer #smallClick')
						.css( { backgroundPosition:'0px 0px', color:'#fff' } )
						.stop().animate( { backgroundPosition:'(-40px 0px)' }, { duration:1000 });
					});
					jQuery('#fade').cycle('resume');
					
					jQuery('#sidebar-right').animate( { 'marginTop':'0px' }, 3000, 'linear');
					jQuery('#site-info').animate( { 'top':'0px' }, 750, 'linear' );
					jQuery('#cont-main').animate( { 'paddingBottom':'0px' }, 750, 'linear' );
				}, 1000);
				
				galleryRoller.status = 'small';
			}
		}
	},
	
	resizeAnimation : function (elem, startWidth, endWidth, startHeight, endHeight, steps, intervals, powr) {
		if (elem.widthChangeMemInt) 
			window.clearInterval(elem.widthChangeMemInt);
			
		var actStep = 0;
		elem.widthChangeMemInt = window.setInterval(
			function() {
				elem.currentWidth = galleryRoller.easeInOut(startWidth, endWidth, steps, actStep, powr);
				elem.currentHeight = galleryRoller.easeInOut(startHeight, endHeight, steps, actStep, powr);
				elem.style.width = elem.currentWidth + "px";
				elem.style.height = elem.currentHeight + "px";
				actStep++;
				if (actStep > steps) 
					window.clearInterval(elem.widthChangeMemInt);
			}, intervals)
	},
	
	easeInOut : function (minValue, maxValue, totalSteps, actualStep, powr) {
		var delta = maxValue - minValue;
		var stepp = minValue + (Math.pow(((1 / totalSteps) * actualStep), powr) * delta);
		return Math.ceil(stepp)
	},
	
	imgSmall : function (obj) {
		imgSrc = obj.getAttribute("src");
		var typePos = imgSrc.indexOf(galleryRoller.imgType, 0);
		var imgName = imgSrc.substr(0, typePos);
		obj.setAttribute("src", imgName + "_small" + galleryRoller.imgType);
	},
	
	imgLarge : function (obj) {
		imgSrc = obj.getAttribute("src");
		var typePos = imgSrc.indexOf("_small", 0);
		var imgName = imgSrc.substr(0, typePos);
		obj.setAttribute("src", imgName + galleryRoller.imgType);
	}
}

// Add event with wide browser support
if ( typeof window.addEventListener != "undefined" )
    window.addEventListener( "load", galleryRoller.init, false );
else if ( typeof window.attachEvent != "undefined" )
    window.attachEvent( "onload", galleryRoller.init );
else {
    if ( window.onload != null ) {
        var oldOnload = window.onload;
        window.onload = function ( e ) {
            oldOnload( e );
            galleryRoller.init();
        };
    } else {
        window.onload = galleryRoller.init;
	}
}