SM.gallery = {
	init: function(){
		var self=this;
		
		// element references
		
		// variables
		self.resizeTimer = null;
		
		// setup
		self.resizeGallery();
		// event handlers
		$(window).bind('resize', function() {
		    if (self.resizeTimer) {
				clearTimeout(self.resizeTimer);
			}
		    self.resizeTimer = setTimeout(self.resizeGallery, 100);
		});		
	},
	
	resizeGallery: function() {
	    $('#smGallery').height(function(){
			var height = $(window).height() - $('#header').outerHeight(true) - $('#footer').outerHeight(true) - $('div.galleryDivider').outerHeight(true) - $('div.clear').outerHeight(true) - 4;
			return height;
		});
	},
	
	noFlash: function(){
		$.ajax({
		    type: "GET",
		    url: "gallery.xml",
		    dataType: "xml",
		    success: parseXML
		  });
		
		function parseXML(xml){
			// CSS changes
			$("body").addClass("no-flash");	
			
			// Structure changes
			$("#header").after('<div class="headerPlaceholder">&nbsp;</div>');
			$(".galleryDivider").after('<div class="galleryDividerPlaceholder">&nbsp;</div>');
			$("#smGallery").html('');
			
			//print out the images from the xml data
			$(xml).find("url").each(function()
			{
				$("#smGallery").append('<img src="' + $(this).text() + '" />');
			});
			
			// fixing position:fixed issue in mobile OS's
			if( navigator.userAgent.match(/Android/i) ||
			 navigator.userAgent.match(/webOS/i) ||
			 navigator.userAgent.match(/iPhone/i) ||
			 navigator.userAgent.match(/iPod/i) ||
			 navigator.userAgent.match(/iPad/i)
			 ){
				/* 
				FIXME consider using iScroll to fix bugs in iphone version.
				- 	Currently the jquery has to wait for the scroll to finish before moving the header over.
				- 	iScroll is supposed to fix this, but with the current layout, iScroll randomly removes the 
					header div, and fails to make the #smgallery scrollable. :(
				*/

				/* old scrolling code for use with iScroll
					$('#smGallery img').load(function(){
						var myScroll = new iScroll('smGallery', { });
					});
				*/				
				$(window).scroll(function () {
                    var offset = $(this).scrollLeft();
					$('#header, .galleryDivider, #footer').css({
						"position":"absolute",
						left: offset
					});
                });
			} 
		}
	}
}
