var TCApplication = {wsrurl:"/WebObjects/textunes.woa/1/wsr",wosid:null,l:function(k){if (TCApplication.loc && TCApplication.loc[k]) return TCApplication.loc[k]; else return '/'+k+'/';}};
function $tc_wosid() { if (TCApplication.wosid) {return "?"+TCApplication.wosid;} else {return "";} }
/** Start:app **/
var TTQuickSearch = {

	init: function() {
		TTQuickSearch.form = $("quicksearch");
		TTQuickSearch.query = $(TTQuickSearch.form["query"]);
		TTQuickSearch.initValue = TTQuickSearch.query.getValue();
		if (TTQuickSearch.initValue.strip()==="") {
			TTQuickSearch.initValue = "Literatur auf dem iPhone";
			TTQuickSearch.query.value = TTQuickSearch.initValue;
		}
		Event.observe(TTQuickSearch.form, "submit", TTQuickSearch.submit);
		Event.observe(TTQuickSearch.query, "focus", TTQuickSearch.focus);
		Event.observe(TTQuickSearch.query, "blur", TTQuickSearch.blur);
	},
	
	focus: function() {
		if (TTQuickSearch.query.value === TTQuickSearch.initValue) {
		 	TTQuickSearch.query.value = "";
		 }
	},
	
	blur: function() {
		if (TTQuickSearch.query.value.strip() === "") {
			TTQuickSearch.query.value = TTQuickSearch.initValue;
		}
	},
	
	submit: function(e) {
		if (TTQuickSearch.query.value === TTQuickSearch.initValue || TTQuickSearch.query.value.strip() === "") {
			TTQuickSearch.query.value = "";
		}
	}
	
};

Event.observe(window, "load", TTQuickSearch.init);
/** End:app **/
/** Start:TTBookBrowser **/
function TTBookBrowser(elementId, batchcount, genres, publisher) {
	this.elementId = elementId;
	this.boxwidth = $(elementId).getWidth();
	this.batchcount = batchcount;
	this.allGenres = genres;
	this.genres = genres;
	this.publisher = publisher;
	this.batch = 0;
	this.backlink = $$("#"+elementId+"B a.backlink")[0];
	this.forelink = $$("#"+elementId+"B a.forelink")[0];
	this.batcher = $(elementId+"B");
	$(elementId+'_1').hide();
	this.allowToggle = true;
	this.currentBox = 0;
	this.backlink.observe("click", this.clickBack.bindAsEventListener(this));
	this.forelink.observe("click", this.clickNext.bindAsEventListener(this));
	var catselectlinks = $$("#"+elementId+"G a");
	catselectlinks.each(function(l) {
		l.observe("click", this.selectGenre.bindAsEventListener(this));
	}.bind(this));
}

TTBookBrowser.prototype = {

	clickBack: function(event) {
		if (this.allowToggle) {
			this.allowToggle = false;
			if (this.batch>0) { 
				this.batch = this.batch-1;
			}
			else {
				this.batch = this.batchcount-1;
			}
			this.direction = 1;
			this.reloadContent(); 
		}
		Event.stop(event);
	},

	clickNext: function(event) {
		if (this.allowToggle) {
			this.allowToggle = false;
			if (this.batch<this.batchcount-1) { 
				this.batch = this.batch+1;
			}
			else {
				this.batch = 0;
			}
			this.direction = -1;
			this.reloadContent();
		}
		Event.stop(event);
	},
	
	selectGenre: function(event) {
		$$("#"+this.elementId+"G a").each(function(l){l.removeClassName("selected");});
		var l = event.element();
		l.addClassName("selected");
		var genre = l.readAttribute("rel");
		if (genre=="ALL") {
			this.genres = this.allGenres;
		}
		else {
			this.genres = "(\""+genre+"\")";
		}
		this.batch = 0;
		this.direction = 1;
		this.reloadContent();
		Event.stop(event);
	},
	
	reloadContent: function() {
		this.batcher.setStyle({ backgroundImage: "url("+TCApplication.wsrurl+"/app/loadbooks.gif)"});
		new Ajax.Updater(this.elementId+'_'+((this.currentBox+1)%2), TTBookBrowser.contentURL,{
			parameters: { batch: this.batch, genres: this.genres, count: 9, publisher: this.publisher},
			onComplete: function(transport) {
				this.moveBoxes();
				if ("true"==transport.getHeader("x-tc-showbatcher")) {
					this.batcher.style.visibility = "visible";
				}
				else {
					this.batcher.style.visibility = "hidden";
				}
			}.bindAsEventListener(this)
		});
	},
	
	moveBoxes: function() {
		var box_out = $(this.elementId+'_'+this.currentBox);
		new Effect.Move(box_out, { x: this.direction*this.boxwidth, y: 0, mode: 'relative', duration:0.5, afterFinish: function() { box_out.hide(); } });
		this.currentBox = (this.currentBox+1)%2;
		var box_in = $(this.elementId+'_'+this.currentBox);
		box_in.style.left = (-this.direction*this.boxwidth)+"px";
		box_in.show();
		new Effect.Move(box_in, { x: 0, y: 0, mode: 'absolute', duration:0.5, afterFinish: function() { this.allowToggle = true; this.batcher.setStyle({ backgroundImage: "none"}); }.bind(this) });
	}

};
/** End:TTBookBrowser **/
if (!TTBookBrowser) TTBookBrowser={};
TTBookBrowser.contentURL="/WebObjects/textunes.woa/1/ajax/TTBookBrowser"+$tc_wosid();
TTBookBrowser.dataURL="/WebObjects/textunes.woa/1/ajax/TTBookBrowser/data"+$tc_wosid();

