var GLOBAL_WISHLIST = {};
var GLOBAL_SHELFLIST = {};
var GLOBAL_DETAILS = {};
var GLOBAL_SEARCH = {};
var GLOBAL_LOGIN= {};
var GLOBAL_OUT = {};
var GLOBALS={};

GLOBALS.current_page="index";


function setupNav() {
	$("link_index").addEvent("click", function(e){
		e.stop();
		if (GLOBALS.current_page=="index")
			return;
		GLOBALS.current_page="index";
		$("nav_wishlist").removeClass("selected");
		$("nav_shelf").removeClass("selected");
		GLOBALS.ajaxNav.direction = 'back';
		GLOBALS.ajaxNav.send({
			url:"/InternalAPI/html/anobii_iphone/index/getcontent"
		});
	});

	$("link_wishlist").addEvent("click", function(e){
		e.stop();
		if (GLOBALS.current_page=="wishlist")
			return;
		if (GLOBALS.current_page=="shelf")
			GLOBALS.ajaxNav.direction = 'back';
			
		GLOBALS.current_page="wishlist";
		$("nav_wishlist").addClass("selected");
		$("nav_shelf").removeClass("selected");
		GLOBALS.ajaxNav.send({
			url:"/InternalAPI/html/anobii_iphone/wishlist/getcontent"
		});
	});

	$("link_shelf").addEvent("click", function(e){
		e.stop();
		if (GLOBALS.current_page=="shelf")
			return;
		GLOBALS.current_page="shelf";
		$("nav_wishlist").removeClass("selected");
		$("nav_shelf").addClass("selected");
		GLOBALS.ajaxNav.send({
			url:"/InternalAPI/html/anobii_iphone/shelf/getcontent"
		});
	});
	
	
}

function setupPage () {
	if ($("wishlist_sortid")) {
		GLOBAL_WISHLIST.ajaxLoadList = new Request ({
			url : "/InternalAPI/html/anobii_iphone/wishlist/getcontent",
			method : "get",
			onRequest: function() {
				GLOBAL_WISHLIST.loadingEffectorList.startLoading();	
			},
			onSuccess : function (responseText, responseXML) {
	//			$("content").set("html", responseText);
				GLOBAL_WISHLIST.loadingEffectorList.loadingCompleted();
				slideToNewPane('content', responseText, GLOBAL_WISHLIST.ajaxLoadList.direction, setupPage);
				GLOBAL_WISHLIST.ajaxLoadList.direction = 'forward';
			}
		});
		GLOBAL_WISHLIST.ajaxLoadList.direction = 'forward';

		GLOBAL_WISHLIST.loadingEffectorList = new LoadingEffect({
			loadArea: $($("item_list")),
			overlayColor: '#111111',
			wheelSize: 'large'
		});
		GLOBALS.current_page="wishlist";
		setupEvents("wishlist");
	}

	else if ($("shelf_sortid")) {
		GLOBAL_SHELFLIST.ajaxLoadList = new Request ({
			url : "/InternalAPI/html/anobii_iphone/shelf/getcontent",
			method : "get",
			onRequest : function () {
				GLOBAL_SHELFLIST.loadingEffectorList.startLoading();		
			},
			onSuccess : function (responseText, responseXML) {
	//			$("content").set("html", responseText);
				GLOBAL_SHELFLIST.loadingEffectorList.loadingCompleted();
				slideToNewPane('content', responseText, GLOBAL_SHELFLIST.ajaxLoadList.direction, setupPage);
				GLOBAL_SHELFLIST.ajaxLoadList.direction = 'forward';
			}
		});
		GLOBAL_SHELFLIST.ajaxLoadList.direction = 'forward';

		GLOBAL_SHELFLIST.loadingEffectorList = new LoadingEffect({
			loadArea: $("content"),
			overlayColor: '#111111',
			wheelSize: 'large'
		});

		GLOBALS.current_page="shelf";
		setupEvents("shelf");
	}

	else if ($("details")) {
		GLOBAL_DETAILS.loadingEffectorDetails = new LoadingEffect({
			loadArea: $("sections"),
			overlayColor: '#EAEAEA',
			wheelPos: 100
		});

		GLOBAL_DETAILS.ajaxLoadSubPages = new Request ({
			method : "get",
			onRequest : function () {
				GLOBAL_DETAILS.loadingEffectorDetails.loadArea = $('sections');
				GLOBAL_DETAILS.loadingEffectorDetails.startLoading();
			},
			onSuccess : function (responseText, responseXML) {
				GLOBAL_DETAILS.loadingEffectorDetails.loadingCompleted();
	//			$("sections").set("html", responseText);
				slideToNewPane('sections', responseText, GLOBAL_DETAILS.ajaxLoadSubPages.direction, setupSection);
				GLOBAL_DETAILS.ajaxLoadSubPages.direction = 'forward';
			}
		});
		GLOBAL_DETAILS.ajaxLoadSubPages.direction = 'forward';

		GLOBAL_DETAILS.ajaxLoadReviewsList = new Request ({
			url : "/InternalAPI/html/anobii_iphone/details/getreviewslist?view=reviews&more=1",
			method : "get",
			onRequest : function () {
				GLOBAL_DETAILS.loadingEffectorDetails.loadArea = $('more_reviews');
				GLOBAL_DETAILS.loadingEffectorDetails.startLoading();
			},
			onSuccess : function (responseText, responseXML) {
				GLOBAL_DETAILS.loadingEffectorDetails.loadingCompleted();
				if ($("button_more")){
					$("button_more").destroy();
				}
				$("reviews_list").set("html", $("reviews_list").get("html") + responseText);
				setupPage();
			}
		});

		GLOBALS.current_page="details";
		setupEvents("details");
	}

	else if ($("search_results")) {
		GLOBAL_SEARCH.loadingEffector = new LoadingEffect({
			loadArea: $("search_results"),
			overlayColor: '#111111',
			wheelSize: 'large'
		});

		GLOBAL_SEARCH.ajaxSearch = new Request ({
			method : "get",
			url : "/InternalAPI/html/anobii_iphone/search/getresults",
			onRequest : function () {
				GLOBAL_SEARCH.loadingEffector.startLoading();	
			},
			onSuccess : function (responseText, responseXML) {
//				$("search_results").set("html", responseText);
				GLOBAL_SEARCH.loadingEffector.loadingCompleted();
				slideToNewPane('search_results', responseText, GLOBAL_SEARCH.ajaxSearch.direction, setupPage);
				GLOBAL_SEARCH.ajaxSearch.direction = 'forward';
			}
		});
		GLOBAL_SEARCH.ajaxSearch.direction = 'forward';

		GLOBALS.current_page="search";
		setupEvents("search");
	}

	else if($("search_main")) {
		GLOBAL_SEARCH.loadingEffector = new LoadingEffect({
			loadArea: $("content"),
			overlayColor: '#111111',
			wheelSize: 'large'
		});
		GLOBAL_SEARCH.ajaxSearch = new Request ({
			method : "get",
			url : "/InternalAPI/html/anobii_iphone/search/getcontent",
			onRequest : function () {
				GLOBAL_SEARCH.loadingEffector.startLoading();			
			},
			onSuccess : function (responseText, responseXML) {
//				$("content").set("html", responseText);
				GLOBAL_SEARCH.loadingEffector.loadingCompleted();
				slideToNewPane('content', responseText, GLOBAL_SEARCH.ajaxSearch.direction, setupPage);
				GLOBAL_SEARCH.ajaxSearch.direction = 'forward';
			}
		});
		GLOBAL_SEARCH.ajaxSearch.direction = 'forward';

		GLOBALS.current_page="index";
		setupEvents("index");
	}

	else if($("login")) {
		
		GLOBAL_LOGIN.loadingEffector = new LoadingEffect({
			loadArea: $("content"),
			overlayColor: '#111111',
			wheelSize: 'large'
		});
		/*
		GLOBAL_LOGIN.ajaxLogin = new Request ({
			method : "post",
			url : "/InternalAPI/html/anobii_iphone/login/verify",
			onRequest : function () {
				GLOBAL_LOGIN.loadingEffector.startLoading();
			},
			onSuccess : function (responseText, responseXML) {
//				$("content").set("html", responseText);
				GLOBAL_LOGIN.loadingEffector.loadingCompleted();
				slideToNewPane('content', responseText, GLOBAL_LOGIN.ajaxLogin.direction, setupPage);
				GLOBAL_LOGIN.ajaxLogin.direction = 'forward';
			}
		});
		
		GLOBAL_LOGIN.ajaxLogin.direction = 'forward';
		*/
		GLOBALS.current_page="login";
		setupEvents("login");
	}
/*
	if($("logout")) {
		GLOBAL_OUT.loadingEffector = new LoadingEffect({
			loadArea: $("content"),
			overlayColor: '#111111',
			wheelSize: 'large'
		});
		GLOBAL_OUT.ajaxLogout = new Request ({
			method : "post",
			url : "/InternalAPI/html/anobii_iphone/login/logout",
			onRequest : function () {
				GLOBAL_OUT.loadingEffector.startLoading();				
			},
			onSuccess : function (responseText, responseXML) {
//				$("content").set("html", responseText);
				GLOBAL_OUT.loadingEffector.loadingCompleted();
				slideToNewPane('content', responseText, GLOBAL_OUT.ajaxLogout.direction, setupPage);
				GLOBAL_OUT.ajaxLogout.direction = 'forward';
			}
		});
		GLOBAL_OUT.ajaxLogout.direction = 'forward';

		setupEvents("logout");
	}
	*/
}


function setupEvents(which) {
	if (which == "wishlist") {
		$("wishlist_sortid").addEvent("change", function () {
			$("wishlist_sortid").getElements("option").each(function (el) {
				if (el.get("selected")) {
					var sortId = el.get("value");
					GLOBAL_WISHLIST.ajaxLoadList.send({data : {sortId : sortId}});
				}
			});
		});

		if ($("previous_page"))
		$("previous_page").addEvent("click", function (e) {
			e.stop();
			GLOBAL_WISHLIST.ajaxLoadList.direction = 'back';
			GLOBAL_WISHLIST.ajaxLoadList.send({data : {page : $("previous_page").get("page")}});
		});

		if ($("next_page"))
		$("next_page").addEvent("click", function (e) {
			e.stop();
			GLOBAL_WISHLIST.ajaxLoadList.send({data : {page : $("next_page").get("page")}});
		});
		/*
		$("item_list").getElements("ul").each(function (ul) {
			ul.getElements("a").each(function (a) {
				if (!a.get("id") || a.get("id") && (a.get("id").split("_")[0] != "move" && a.get("id").split("_")[0] != "remove"))
					return;
				a.addEvent ("click", function (e) {
					e.stop();
					GLOBAL_WISHLIST.loadingEffectorList.startLoading();
					new Request({
						url: a.get("href").replace("?", "ajax?"),
						method : "get",
						onSuccess: function (responseText, responseXml) {
							$("item_list").set("html", responseText);
							GLOBAL_WISHLIST.loadingEffectorList.loadingCompleted();
						}
					}).send();
					
				});
			});
		});
		*/
		setupBookDetailsAjax();
		$$('#item_list .item').each(function(item, i) {

			// Apply action events
			item.getElements('.actions a').each(function(action, i) {
				action.addEvent('click', function(e) {
					e.stop();
					if (action.get("href").test(/remove/) && !confirm((action.get("href").test(/wishlist/i)?remove_from_wishlist:remove_from_shelf+"?")))
						return;
					var loading = new LoadingEffect({
						loadArea: item,
						overlayColor: '#FFF'
					});
					new Request({
						url: action.get("href").replace("?", "ajax?"),
						method : "get",
						onRequest: function () {
							loading.startLoading();							
						},
						onSuccess: function (responseText, responseXml) {
							loading.loadingCompleted();
							new Fx.Morph(item).start({'height': 0, 'opacity': 0}).chain(function() {
								$("item_list").set("html", responseText);
								setupPage();
							});

						}
					}).send();

				});
			});

			// Setup actions_trigger
			item.getElement('.actions').addClass('hide');
			item.getElement('.actions_trigger').removeClass('active');

			item.getElement('.actions_trigger').addEvent("click", function(e) {
				e.stop();
				item.toggleClass('actions_active');
				if (item.hasClass('actions_active')) {
					this.getElement('a').set('text', 'Done');
					item.getElement('.actions').removeClass('hide');
	//				this.addClass('active');
				} else {
					this.getElement('a').set('text', 'Edit');
					item.getElement('.actions').addClass('hide');					
				}
			});

		});

	}

	else if (which == "shelf") {
		$("shelf_sortid").addEvent("change", function () {
			$("shelf_sortid").getElements("option").each(function (el) {
				if (el.get("selected")) {
					var sortId = el.get("value");
					GLOBAL_SHELFLIST.ajaxLoadList.send({data : {sortId : sortId}});
				}
			});
		});
		if ($("previous_page"))
		$("previous_page").addEvent("click", function (e) {
			e.stop();
			GLOBAL_SHELFLIST.ajaxLoadList.direction = 'back';
			GLOBAL_SHELFLIST.ajaxLoadList.send({data : {page : $("previous_page").get("page")}});
		});

		if ($("next_page"))
		$("next_page").addEvent("click", function (e) {
			e.stop();
			GLOBAL_SHELFLIST.ajaxLoadList.send({data : {page : $("next_page").get("page")}});
		});
		setupBookDetailsAjax ();
	}

	else if (which == "details") {
		document.getElement('.actions .addtoshelf').addEvent('click', function(e) {
			e.stop();
			if (document.getElement('.actions .addtoshelf').hasClass("remove")) {
				if (!confirm(remove_from_shelf+"?"))
					return;
				new Request({
					onRequest: function() {
						GLOBALS.actionLoadingEffector.loadArea = document.getElement('.actions .addtoshelf');
						GLOBALS.actionLoadingEffector.startLoading();
					},
					onSuccess: function(responseText, responseXML) {
						if (responseText=='true') {
							document.getElement('.actions .addtoshelf').removeClass('remove');
						}
						else if (responseText=='false');
						else
							$("content").set("html", responseText);

						GLOBALS.actionLoadingEffector.loadingCompleted();
					}
				}).send({
					url: '/InternalAPI/html/anobii_iphone/details/removeFromShelfAjax'
				});
			}
			else {
				new Request({
					onRequest: function() {
						GLOBALS.actionLoadingEffector.loadArea = document.getElement('.actions .addtoshelf');
						GLOBALS.actionLoadingEffector.startLoading();
					},
					onSuccess: function(responseText, responseXML) {
						
						if (responseText=='true') {
							document.getElement('.actions .addtoshelf').addClass('remove');
						}
						else if (responseText=='false');
						else
							$("content").set("html", responseText);
							
						GLOBALS.actionLoadingEffector.loadingCompleted();
					}
				}).send({
					url: '/InternalAPI/html/anobii_iphone/details/addToShelfAjax'
				});
			}
		});

		document.getElement('.actions .addtowishlist').addEvent('click', function(e) {
			e.stop();
			if (document.getElement('.actions .addtowishlist').hasClass("remove")) {
				if (!confirm(remove_from_wishlist+"?"))
					return;
				new Request({
					onRequest: function() {
						GLOBALS.actionLoadingEffector.loadArea = document.getElement('.actions .addtowishlist');
						GLOBALS.actionLoadingEffector.startLoading();
					},
					onSuccess: function(responseText, responseXML) {
						
						if (responseText=='true') {
							document.getElement('.actions .addtowishlist').removeClass('remove');
						}
						else if (responseText=='false');
						else
							$("content").set("html", responseText);
							
						GLOBALS.actionLoadingEffector.loadingCompleted();
					}
				}).send({
					url: '/InternalAPI/html/anobii_iphone/details/removeFromWishlistAjax'
				});
			}
			else {
				new Request({
					onRequest: function() {
						GLOBALS.actionLoadingEffector.loadArea = document.getElement('.actions .addtowishlist');
						GLOBALS.actionLoadingEffector.startLoading();
					},
					onSuccess: function(responseText, responseXML) {
						
						if (responseText=='true') {
							document.getElement('.actions .addtowishlist').addClass('remove');
						}
						else if (responseText=='false');
						else
							$("content").set("html", responseText);
						
						GLOBALS.actionLoadingEffector.loadingCompleted();
					}
				}).send({
					url: '/InternalAPI/html/anobii_iphone/details/addToWishlistAjax'
				});
			}
		});

		document.getElement('.back').addEvent('click', function(e){
			e.stop();
			new Request({
				onRequest: function() {
					GLOBALS.loadingEffector.loadArea = $('content');
					GLOBALS.loadingEffector.startLoading();
				},
				onSuccess: function(responseText, responseXML) {
					GLOBALS.loadingEffector.loadingCompleted();
					if (responseText){
						GLOBALS.ajaxNav.direction = 'back';
						slideToNewPane('content', responseText, GLOBALS.ajaxNav.direction, setupPage);
						GLOBALS.ajaxNav.direction = 'forward';						
					}
				}
			}).send({
				url: document.getElement('.back').get("href").replace("index","getcontent")
			});
		});

//		if ($("get_reviews") && !$("li_get_reviews").hasClass("selected")){
		if ($("get_reviews")){
			$("get_reviews").addEvent ("click", function (e) {
				e.stop();
				GLOBAL_DETAILS.ajaxLoadSubPages.direction = "left";
				GLOBAL_DETAILS.ajaxLoadSubPages.send({
					url: "/InternalAPI/html/anobii_iphone/details/getreviews"
				});
				$("li_get_reviews").addClass("selected");
				$("li_get_buy").removeClass("selected");
				$("li_get_info").removeClass("selected");
			});
		}
//		if ($("get_buy") && !$("li_get_buy").hasClass("selected")){
		if ($("get_buy")){
			$("get_buy").addEvent ("click", function (e) {
				e.stop();
				if ($("li_get_info").hasClass("selected"))
					GLOBAL_DETAILS.ajaxLoadSubPages.direction = "left";

				GLOBAL_DETAILS.ajaxLoadSubPages.send({
					url: "/InternalAPI/html/anobii_iphone/details/getbuy"
				});
				$("li_get_info").removeClass("selected");
				$("li_get_buy").addClass("selected");
				$("li_get_reviews").removeClass("selected");
			});
		}
//		if ($("get_info") && !$("li_get_info").hasClass("selected")){
		if ($("get_info")){
			$("get_info").addEvent ("click", function (e) {
				e.stop();
				GLOBAL_DETAILS.ajaxLoadSubPages.send({
					url: "/InternalAPI/html/anobii_iphone/details/getinfo"
				});
				$("li_get_info").addClass("selected");
				$("li_get_buy").removeClass("selected");
				$("li_get_reviews").removeClass("selected");
			});
		}
		
		setupSection();
	}

	else if (which == "search") {
		if ($("search_box")){
			$("search_box").addEvent("submit", function () {
				GLOBAL_SEARCH.ajaxSearch.send({
					data : {key : $("keywords").get("value")}
				});
				return false;
			});
		}

		if ($("previous_page"))
		$("previous_page").addEvent("click", function (e) {
			e.stop();
			GLOBAL_SEARCH.ajaxSearch.direction = 'back';
			GLOBAL_SEARCH.ajaxSearch.send({data : {page : $("previous_page").get("page")}});
		});
		if ($("next_page"))
		$("next_page").addEvent("click", function (e) {
			e.stop();
			GLOBAL_SEARCH.ajaxSearch.send({data : {page : $("next_page").get("page")}});
		});





		$$('#search_results .item').each(function(item, i) {

			// Setup actions_trigger
			item.getElement('.actions').addClass('hide');
			item.getElement('.actions_trigger').removeClass('active');

			item.getElement('.actions_trigger').addEvent("click", function(e) {
				e.stop();
				item.toggleClass('actions_active');
				if (item.hasClass('actions_active')) {
					this.getElement('a').set('text', 'Done');
					item.getElement('.actions').removeClass('hide');
					//	this.addClass('active');
				} else {
					this.getElement('a').set('text', 'Edit');
					item.getElement('.actions').addClass('hide');					
				}
			});


			// Apply action events
			item.getElement('.actions .addtoshelf').addEvent('click', function(e) {
				e.stop();
				if (item.getElement('.actions .addtoshelf').hasClass("remove")) {
					if (!confirm(remove_from_shelf+"?"))
						return;
					new Request({
						
						onRequest: function() {
							GLOBALS.actionLoadingEffector.loadArea = item.getElement('.actions .addtoshelf');
							GLOBALS.actionLoadingEffector.startLoading();
						},
						onSuccess: function(responseText, responseXML) {
							
							if (responseText == 'true' || responseText == true) {
								item.getElement('.actions .addtoshelf').removeClass('remove');
							}
							else if (responseText=='false');
							else
								$("content").set("html", responseText);
							
							GLOBALS.actionLoadingEffector.loadingCompleted();
						}
					}).send({
						url: '/InternalAPI/html/anobii_iphone/search/removeFromShelfAjax',
						data: {bookId: item.getElement("ul").get("id")}
					});
				}
				else {
					new Request({
						onRequest: function() {
							GLOBALS.actionLoadingEffector.loadArea = item.getElement('.actions .addtoshelf');
							GLOBALS.actionLoadingEffector.startLoading();
						},
						onSuccess: function(responseText, responseXML) {
							
							if (responseText=='true') {
								item.getElement('.actions .addtoshelf').addClass('remove');
							}
							else if (responseText=='false');
							else
								$("content").set("html", responseText);
							
							GLOBALS.actionLoadingEffector.loadingCompleted();
						}
					}).send({
						url: '/InternalAPI/html/anobii_iphone/search/addToShelfAjax',
						data: {bookId: item.getElement("ul").get("id")}
					});
				}
			});

			item.getElement('.actions .addtowishlist').addEvent('click', function(e) {
				e.stop();
				if (item.getElement('.actions .addtowishlist').hasClass("remove")) {
					if (!confirm(remove_from_wishlist+"?"))
							return;
					new Request({
						onRequest: function() {
							GLOBALS.actionLoadingEffector.loadArea = item.getElement('.actions .addtowishlist');
							GLOBALS.actionLoadingEffector.startLoading();
						},
						onSuccess: function(responseText, responseXML) {
							
							if (responseText=='true') {
								item.getElement('.actions .addtowishlist').removeClass('remove');
							}
							else if (responseText=='false');
							else
								$("content").set("html", responseText);
							GLOBALS.actionLoadingEffector.loadingCompleted();
						}
					}).send({
						url: '/InternalAPI/html/anobii_iphone/search/removeFromWishlistAjax',
						data: {bookId: item.getElement("ul").get("id")}
					});
				}
				else {
					new Request({
						onRequest: function() {
							GLOBALS.actionLoadingEffector.loadArea = item.getElement('.actions .addtowishlist');
							GLOBALS.actionLoadingEffector.startLoading();
						},
						onSuccess: function(responseText, responseXML) {
							
							if (responseText=='true') {
								item.getElement('.actions .addtowishlist').addClass('remove');
							}
							else if (responseText=='false');
							else
								$("content").set("html", responseText);
							
							GLOBALS.actionLoadingEffector.loadingCompleted();
						}
					}).send({
						url: '/InternalAPI/html/anobii_iphone/search/addToWishlistAjax',
						data: {bookId: item.getElement("ul").get("id")}
					});
				}
			});
		});
		setupBookDetailsAjax ();
	}

	else if (which == "index") {
		if ($("search_box")){
			$("search_box").addEvent("submit", function () {
				GLOBAL_SEARCH.ajaxSearch.send({
					data : {key : $("keywords").get("value")}
				});
				return false;
			});
			var searchInput = $('search_box').getElement('input');
			searchInput.addEvents({
				'focus': function() {
					if (searchInput.hasClass('default')) {
						searchInput.value = '';
						searchInput.removeClass('default');
					}
				},
				'blur': function() {
					if (searchInput.value == '') {
						searchInput.addClass('default');
						searchInput.value = "Enter ISBN or Keywords";
					}					
				}
			});
		}
	}

	if (!GLOBALS.translate){
		GLOBALS.translate = 1;
		$('language').addEvent('change', function (e) {
			new Request({
				url: '/InternalAPI/html/language/change',
				method: 'get',
				data: {lang: $(e.target.options[e.target.options.selectedIndex]).get('value')},
				onComplete: function(){
					location.reload();
				}
			}).send();
		});
	}
/*
	else if (which == "login") {
		$("login").addEvent("submit", function () {
			GLOBAL_LOGIN.ajaxLogin.send({
				data : {
					email : $("email").get("value"),
					password : $("password").get("value"),
					noHeader : true
				}
			});
			return false;
		});
		
	}
*/
/*	else if (which == "logout") {
		$("logout").addEvent("click", function (e) {
			e.stop();
			GLOBAL_OUT.ajaxLogout.send({
				data : {
					noHeader : true
				}
			});
		});
	}*/
}

function setupBookDetailsAjax () {
	$$('#item_list .item').each(function(item, i) {
		item.addEvent('click', function (e) {
			e.stop();
			new Request({
				onRequest: function() {
					GLOBALS.loadingEffector.loadArea = $('content');
					GLOBALS.loadingEffector.startLoading();
				},
				onSuccess: function(responseText, responseXML) {
					GLOBALS.loadingEffector.loadingCompleted();
					if (responseText){
						slideToNewPane('content', responseText, GLOBALS.ajaxNav.direction, function () {
							setupPage();
							var footer = $('footer').dispose();
							footer.inject('content', 'after');
						});
						GLOBALS.ajaxNav.direction = 'forward';
					}
				}
			}).send({
				url: item.getElement(".item_thumb a").get("href").replace("index","getcontent")
			});
		});
	});
}

function slideToNewPane(paneId, responseText, direction, callBackFn) {
	var tempDiv = new Element('div').set('html', responseText).inject($(paneId), 'after');
	slidePanes($(paneId), tempDiv, direction, callBackFn);
	tempDiv.setProperty('id', paneId);
}

function slidePanes(from, to, direction, callBackFn) {	
	
	var screenWidth = from.getSize().x;
	var startTop = from.getPosition().y;
	from.removeClass('hide').setStyles({
		'position': 'absolute',
		'top': startTop + 'px',
		'left': 0,
		'z-index': 300,
		'width': screenWidth + 'px',
		'height': from.getSize().y + 'px'
	});
	to.removeClass('hide').setStyles({
		'position': 'absolute',
		'top': startTop + 'px',
		'left': screenWidth + 'px',
		'z-index': 100,
		'width': screenWidth + 'px',
		'height': to.getSize().y + 'px'
	});
	if (direction == 'back' || direction == 'left') {
		new Fx.Tween(from).start('left', 0, screenWidth);
		new Fx.Tween(to).start('left', 0-screenWidth, 0).chain(function() {
			from.addClass('hide').setProperty('style', '').destroy();
			to.setProperty('style', '');
			callBackFn();
		});
	} else {
		new Fx.Tween(from).start('left', 0, 0-screenWidth);
		new Fx.Tween(to).start('left', screenWidth, 0).chain(function() {
			from.addClass('hide').setProperty('style', '').destroy();
			to.setProperty('style', '');
			callBackFn();
		});
	}
}

function setupSection() {
	if ($("more_reviews")) {
		$("more_reviews").addEvent("click", function (e) {
			e.stop();
			GLOBAL_DETAILS.ajaxLoadReviewsList.send();
		});
	}
}


window.addEvent('domready', function() {
	GLOBALS.loadingEffector = new LoadingEffect({
		loadArea: $('content'),
		overlayColor: '#111',
		wheelSize: 'large'
	});
	
	GLOBALS.actionLoadingEffector = new LoadingEffect({
		loadArea: $('content'),
		overlayColor: '#FFF',
		wheelSize: 'small'
	});
	

	GLOBALS.ajaxNav = new Request ({
		method : "get",
		onRequest : function () {
			GLOBALS.loadingEffector.loadArea = $('content');
			GLOBALS.loadingEffector.startLoading();
		},
		onSuccess : function (responseText, responseXML) {
//			$("content").set("html", responseText);
			GLOBALS.loadingEffector.loadingCompleted();
			slideToNewPane('content', responseText, GLOBALS.ajaxNav.direction, setupPage);
			GLOBALS.ajaxNav.direction = 'forward';
		}
	});
	GLOBALS.ajaxNav.direction = 'forward';
	var root = "http://static.anobii.com/anobi/live/iphone/image/";
	new Asset.image(root+"button_icons.png");
	new Asset.image(root+"shelf_bg.png");
	new Asset.image(root+"shelf_frame.png");
	new Asset.image(root+"loading_large.gif");
	new Asset.image(root+"loading_small.gif");
	new Asset.image(root+"loading.gif");

	setupNav();
	setupPage();
});


window.scrollTo(0, 1);	
