var App = Class.create({
	
	tabs: ["Chat", "Fotos", "Website", "Impressum"],
	headTabs: ["placesArea", "pathArea"],
	visibleHeadArea: "notSet",
	waitForUser: false,

	initialize: function(){	
		
		Ajax.Responders.register({

			requestTime: null,
			
			onCreate: function(e){
				this.requestTime = new Date();
				$("loading").show();
			},
			
			onComplete: function(e) {
				$("loading").hide();
			},
			
			onExeption: function(e){
				alert("Just to remember: This is a beta version ;)");
			}
		});
		
		if (navigator.userAgent.match("Win") && !navigator.userAgent.match("Firefox")) {
			$("firefoxAd").show();
		} else {
			$("firefoxAd").hide();
		}

		$("closeHeadPanel").observe("click", (function(e){
			this.showArea();
			Event.stop(e);
		}).bind(this));	
		
		$("showPlacesArea").observe("click", (function(e){
			this.showArea("placesArea");
			$("placesAreaInput").select();
			Event.stop(e);
		}).bind(this));		
		
		$("share-link").observe("click", function(event){
			window.open($("share-link").href,'','scrollbars=yes,height=600,width=800,resizable=yes,toolbar=no,location=no,status=no,menubar=no');
			Event.stop(event);

		});
		
		$("headClosed").show();

		$("searchPlaces").observe("click", this.searchPlace.bind(this));			
		$("placesArea").observe("submit", this.searchPlace.bind(this));			
		
		this.initTabs();
		
		Event.observe(window, "resize", this.draw.bind(this));
		
		var pLabel = $("flickrpreview");

		pLabel.observe("mouseout", function(e) {
			
			var evt = Event.element(e);
			var hide = function(){
				pLabel.hide();
				Event.stop(e);
			};

			var pos = Position.cumulativeOffset($("flickrpreview"));
			
			var x = e.clientX - pos[0];
			var y = e.clientY - pos[1];

			if (x < 0 || x >= pLabel.offsetWidth || y < 0 || y >= pLabel.offsetHeight) {
				hide();	
			}
			
		}, false);	
		
		$("bookmarklet").observe("click", function(e){
			alert("Anleitung: \n - In Ihre Bookmarks ziehen.\n - Flickr Seite ansehen. \n - Auf Bookmarklet klicken um Code Tags einzufgen.");
			Event.stop(e);
		});
		
		var searchUser = (function(e){
			this.findPeople();
			Event.stop(e);
		}).bind(this);
		
		$("people_search").observe("click", searchUser);		
		$("people_search_link").observe("click", searchUser);		
				
		$("people_showall").observe("click", (function(e){
			this.map.author = null;
			this.map.changed(true);
			this.draw();
			Event.stop(e);
		}).bind(this));			
		
		$("photo").hide();
		$("arrow").hide();
		$("photosNotFound").hide();
		$("headOpened").hide();

		this.preview = new Preview();
		
		this.draw();

		this.map = new Map(this, $("map"));
		this.gMap = this.map.gMap;

		this.geoCoder = new GeoCoder(this.gMap);
		this.draw();
		this.showArea();
		
		$("mainContent").className = "main";
	},

	draw: function(){
	
		var win = getWindowDimensions();
		
		if (typeof this.gMap != "undefined") {
			var oldCenter = this.gMap.getCenter();
		}

		$("map").style.height = win.height + "px";
		$("map").style.width = win.width + "px";
		
		$("photoLoading").style.left = Math.round((win.width - $("photoLoading").offsetWidth) /2) + "px";
		$("photoLoading").style.top = Math.round((win.height - $("photoLoading").offsetHeight) /2) + "px";
		
		$("photoBackground").style.height = win.height + "px";
		
		var pos = Position.cumulativeOffset($("photolist_container"))[1];
		$("photolist_container").style.height = Math.min($("photolist").offsetHeight, (win.height - 46 - pos)) + "px";
		
		if (typeof this.gMap != "undefined") {
			this.gMap.checkResize();

		}

	},
	
	showArea: function(area){
		
		if (this.areaOpened == area) {area = null;}
		this.areaOpened = area;

		this.headTabs.each(function(a){
			$(a).hide();	
		});

		if (this.visibleHeadArea != area) {
			
			this.visibleHeadArea = area;

			if (area) {
				$(area).show();
				$("pathArea").hide();
				$("headClosed").hide();
				$("mainContent").hide();
				$("headOpened").show();
				
			} else { 
				
				$("pathArea").show();
				$("headClosed").show();
				$("mainContent").show(); 
				$("headOpened").hide(); 
			}
		}	
		
		this.draw();	
	},
	
	initTabs: function(){
		
		var list = $("tabs");

		this.tabs.each(function(tab){

			var link = Element.create("a",{
				innerHTML: tab,
				href: "./" + tab,
				id: "tabChooser_" + tab
			});
			
			var item = Element.create("li", {});
			
			item.appendChild(link);
			list.appendChild(item);
			
			link.observe("click", function(e){
				link.blur();
				Event.stop(e);
				showTab(tab);
			});
		});
		
		var showTab = (function(tabName){
			this.tabs.each(function (t){
				var li = $("tabChooser_"+t).parentNode;
			
				if (tabName == t) {
					$("tab_"+t).show();
					li.addClassName("active");
				} else {
					$("tab_"+t).hide();
					li.removeClassName("active");
				}
			});
			
		}).bind(this);
		
		showTab(this.tabs[1]);
		
		$("tags_showall").hide();
		$("flickrpreview").hide();		
	},
	
	searchPlace: function(e){
		
		var address = Form.getElements("placesArea")[0].value;
		
		try {
			this.showArea();
			this.geoCoder.find(address);
		} catch(e){
			Logger.error(e);	
		}
		
		Event.stop(e);
	},	
	
	findPeople: function(nikk, updateMap){
		
		if (!nikk) {
			nikk = prompt("Flickr User suchen:");
		}
			
		if (nikk) {
			var options = {
				parameters: {
					method:		"flickr.people.findByUsername",
					api_key:	"9b629a5deea5f5e9423ddab9eebca3be",
					username:	nikk,
					format:		"json"
				},
				callbackName: "jsoncallback",
				onSuccess: (function(response){
					if (response.stat == "ok"){
						this.getUserInfo(response.user.nsid, updateMap);
					} else {
						alert('Sorry, no user found called "' + nikk + '"');

					}
					this.waitForUser = false;
				}).bind(this),
				evalResponse: true
			};
			
			new Ajax.ScriptRequest("http://api.flickr.com/services/rest/", options);
		}
	},
	
	getUserInfo: function(nsid, updateMap){
			
		var options = {
		
			parameters: {
				method:		"flickr.people.getInfo",
				api_key:	"9b629a5deea5f5e9423ddab9eebca3be",
				user_id:	nsid,
				format:		"json"
			},
			
			onSuccess: (function(response){
				try {
					this.updateUser(response.person);
				} catch(e){
					Logger.error(e);

				}
			}).bind(this),
			
			callbackName: "jsoncallback"
		};
		
		new Ajax.ScriptRequest("http://api.flickr.com/services/rest/", options);
	},
	
	updateUser: function(user){
		
		$("people_icon").src = "http://www.flickr.com/images/buddyicon.jpg";

		this.map.author = user;
		this.map.page = 1;
		
		var userName = user.username._content;
		
		if((typeof user.realname == "object") && (user.realname._content)) {
			 userName += " (" + unescape(user.realname._content) + ")";
		} 
		
		$("people_real_name").update(userName);
		$("people_photo_count").update(user.photos.count._content + " photos.");
		$("people_icon").src = "http://static.flickr.com/" + user.iconserver + "/buddyicons/" + user.nsid + ".jpg";
		$("people_url").href = user.profileurl._content;
				
		this.map.changed(true);
		
		this.draw();
	}
});

var app;

Event.observe(window, "load", function() {

		if (GBrowserIsCompatible()){
			Element.remove("initStyles");
			app = new App();	

		} else {
			alert("Sorry, der Browser ist inkompatibel!");
		}

});

Event.observe(window, "unload", function() {
	GUnload();
});

