var tags = ["5pointz", "abstract", "aemkei", "air", "alien", "alster", "anchovy", "antalya", "architecture", "arselectronica", "art", "baby", "balloon", "bar", "bbq", "beach", "beer", "berlin", "bigben", "birthday", "black", "blue", "boat", "boy", "brooklyn", "buildings", "café", "canal", "car", "cars", "castle", "cat", "child", "chimney", "church", "city", "colors", "concert", "containership", "copenhagen", "crane", "crime", "denmark", "design", "dmy", "dorn", "dredge", "dusk", "exhibition", "fans", "fashion", "fish", "flag", "flat", "fleamarket", "flight", "flower", "food", "fun", "geotagged", "germany", "girl", "glasses", "graffiti", "green", "guns", "hackday", "hacker", "hamburg", "harbour", "hotel", "house", "indaface", "interactive", "japan", "kids", "lamp", "light", "likemind", "litter", "living", "london", "man", "marriage", "metro", "mizubitchy", "mobile", "model", "mountain", "münzuburg", "nagoya", "newyork", "night", "nyc", "office", "old", "orange", "painting", "park", "parliament", "party", "people", "photobooth", "pink", "poeple", "portrait", "ps1", "punch", "relocation", "roof", "room", "schanze", "sea", "seagull", "ship", "sights", "sign", "signs", "sky", "skyline", "snow", "soccer", "stairs", "stats", "statue", "street", "streetart", "streets", "students", "sumaato", "sunset", "tags", "thames", "tokyo", "traffic", "tree", "tube", "turkey", "turret", "tv", "ubilabs", "usa", "view", "wall", "wapping", "water", "weimar", "wm", "wohlwillstrasse", "wohnzimmerwm"];

Element.store = function(element){
	
	element = $(element);
	element.stored = document.createElement("div");
	
	//document.body.appendChild(element.stored);
	
	var html = element.innerHTML;
	
	$A(element.childNodes).each(function(child){
		
		element.stored.appendChild(child);
	});
	
	element.update(html);
	
	//Element.hide(element.stored);
	return element;
};

Element.restore = function(element){
	
	element = $(element);
	
	if (element.stored){
		
		// empty element
		element.update();
		
		// attach old childs
		$A(element.stored.childNodes).each(function(child){
		
			element.appendChild(child);
		});
	}
	// Edited by Max: it seems that there is no functionality to remove items from the stored-stack if they were
	// restored. This command sets a null-pointer to a restored element to prevent it from being restored again.
	// Perhaps there is a nicer way but this works fine at a glance.
	element.stored = null;
};

var triplib = {
	
	initLinks: function(links){
		
		var clickAction = function(link, action){
			
			Event.observe(link, "click", function(event){
				
				Event.stop(event);
				action();
			});
		};
		
		links.each(function(link){
			
			if (link.hasClassName("add")) {
				
				clickAction(link, function(){new Ajax.Inline(link, "add");});
			}
			
			if (link.hasClassName("edit")) {
				
				clickAction(link, function(){ new Ajax.Inline(link, "edit");});
			}
			
			if (link.hasClassName("toggle")) {
				
				var target = $(link.rel);
				
				if (target.hasClassName("hidden")){
					
					target.removeClassName("hidden");
					target.hide();
				}
				
				Event.observe(link, "click", function(event){
					Event.stop(event);
					link.blur();
					$(link.rel).toggle();
				});
			}
		});		
	},
	
	initInputs: function(inputs){
		
		var i = 1;
		
		inputs.each(function(input){
			
			var container;
			
			if (input.hasClassName("autocomplete")){
				
				container = document.createElement("div");
				input.parentNode.appendChild(container);
				Element.extend(container);
				
				container.setStyle({width: (input.getWidth()-2) + "px"});
				
				console.log("container", container);
				
			    new YAHOO.widget.AutoComplete(
					input, container,
					new YAHOO.widget.DS_JSArray(tags), 
					{ delimChar: ",", minQueryLength: 0, typeAhead: true }
				);			

			} else if (input.hasClassName("calendar")){
			
/*				Event.observe(input.id, "focus", function(){
					container = document.createElement("div");
					container.id = "calendar_holder";
					$(container).addClassName('scal');
					input.parentNode.appendChild(container);
					Element.extend(container);

					var samplecal = new scal("calendar_holder",input.id,{year:2007,month:8,day:3,titleformat:'mmmm yyyy',closebutton:'X',dayheadlength:2,weekdaystart:1});
					samplecal.showCalendar();
					
				});
*/
/*				
				container = document.createElement("div");
				container.id = "cal"+i;
				input.parentNode.appendChild(container);
				Element.extend(container);
				
				container.hide();
				
				input.observe("focus", container.show.bind(container));
				//input.observe("blur", container.hide.bind(container));
				
				var cal = new YAHOO.widget.Calendar("cal_container", "cal"+i,{
					close: true,
					MONTHS_LONG:		["Januar", "Februar", "M\u00E4rz", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember"],
					WEEKDAYS_SHORT:		["So", "Mo", "Di", "Mi", "Do", "Fr", "Sa"],
					DATE_FIELD_DELIMITER: ".",
					MDY_DAY_POSITION: 	1,
					MDY_MONTH_POSITION: 2,
					MDY_YEAR_POSITION: 	3, 
					MD_DAY_POSITION: 	1, 
					MD_MONTH_POSITION: 	2 									
				});
				
				cal.select(input.value);
				cal.render();
				
				var handleSelect = function(type,args,obj) { 
				    var dates = args[0],date = dates[0]; 
				    var year = date[0], month = date[1], day = date[2]; 
				 
				    input.value = day + "." + month + "." + year;
					container.hide();
				};
				
				cal.selectEvent.subscribe(handleSelect, cal, true);
				
				console.log(cal._);
*/			
				datepickers[input.id] = new DatePicker({
					relative : input.id,
					language : 'de'
				});

			}
			
		});
	},
		
	init: function(){
		
		this.initLinks($$("a.ajax"));
		this.initInputs($$("input.ajax"));
	}
};

Element.observe(window, "load", function(){
	
	triplib.init();
});


// Social bookmarking script
var sb_url = encodeURIComponent(location.href);
var sb_title = encodeURIComponent(document.title);
					
function sb_status(element) {
	if (element=="clear") {
		document.getElementById('sb_status').innerHTML='';
	} else {
		document.getElementById('sb_status').innerHTML='Bookmark zu ' + element + ' hinzuf&uuml;gen';
	}
}

// Search script
function submit_search() {
	if ($('search-value').value.length < 3) {
		alert("Bitte gebe mindestens 3 Buchstaben ein!");
		return false;
	} else {
		return true;
	}
}