// declarations
var animation_speed = 1250;

function ImagePreloader(images, callback) {
	
	// initialize variables
	this.images_loaded = 0;
	this.num_images = images.length;
	this.callback = callback;
	
	// load the images
	for (var i=0; i<images.length; i++) {
		this.load(images[i]);
	}
}

ImagePreloader.prototype.load = function(image) {
	
	var img = new Image();
	var o = this;
	
	img.onload = function() {
		o.loaded();
	};
	
	img.src = image;
};

ImagePreloader.prototype.loaded = function() {
	
	this.images_loaded++;
	
	if (this.images_loaded == this.num_images) {
		if (typeof this.callback == 'function') {
			this.callback();
		}
	}
};

CityLife = {
	load : function(base_url, pages) {
		
		//preload images
		var images_url = base_url + '/images/';
		var images = new Array();
		for (var i=0; i<pages.length; i++) {
			
			// add the page background to the preloader
			images.push(images_url + pages[i] + "_bg.jpg");
			// add link to the page
			
			$("#" + pages[i] + "-link").attr('href', '#/' + pages[i]).click(function(event) {
				CityLife.changePage($(this).attr("id").split("-")[0]);
			});
		}
		
		new ImagePreloader(images, function() {
			$("#loading").fadeOut(1000, function() {
				$("#wrap").show();
				$("#content").html('');
				var current_page = location.hash.slice(2);
				if ($.inArray(current_page, pages) > -1) {
					CityLife.changePage(current_page)
				} else {
					CityLife.changePage('index');
				}
				setTimeout(function() {
					// fade in the footer and navigation after the page changed
					$("#navigation").fadeIn(1000);
					$("#footer").fadeIn(1000);
				}, animation_speed / 2);
				poll = setInterval(function() {
					if (current_page != location.hash.slice(2)) {
						current_page = location.hash.slice(2);
						CityLife.changePage(current_page);
					}
				}, 250);
			});
		});
	},
	changePage : function(page) {
		
		if (!page) {
			page = 'index';
		}
		$("#navigation a.selected").removeClass("selected");
		$("#" + page + "-link").addClass("selected");
		$("#content").fadeOut(animation_speed, function() {
			$("#content").html($("#" + page + "-page").html());
			if (typeof CityLife.pages[page] == 'function') {
				CityLife.pages[page]();
			}
			$("#content").fadeIn(animation_speed);
		});
	},
	changeSubPage : function(page) {
		$("#content-text .leftinner").fadeOut(animation_speed, function() {
			$("#content-text .leftinner").html("#" + page + "-page").fadeIn(animation_speed);
		});
	},
	pages : {
		who : function() {
			$("#who-tab").tabs({ fx: { opacity: 'toggle' } });
		},
		media : function() {
			$("#media-tab").tabs();
			var click_func = function() {
				var src = $('img', this).attr('alt');
				var img = new Image();
				img.onload = function() {
					var height = this.clientHeight;
					var width = this.clientWidth;
					var content = "<img src='" + src + "' />";
					TINY.box.show(content,0,width,height,1);
				};
				img.src = src;
			};
			$("#media-tab .thumbnail").each(function() {
				$(this).click(click_func);
			});
		}
	}
};