/*!
 * jQuery StoneFlip Plugin
 * Examples and documentation at: http://www.steinwand-software.de/doc/stoneflip
 * Copyright (c) 2010 N. Steinwand
 * Version: 0.2 (13-07-2010)
 * Requires: jQuery v1.2.6 or later
 */

(function($) {

	$.stoneflip = {
		build : function(options) {

			var defaults = {
				animation_running: 0,
				request_queue: new Array(),
				zi: 1,
				last_rtl: 0,
				forward_only: 0
			};
				
			var settings = $.extend({}, defaults, options);

			return this.each(function(){
				var el = $(this);
				el.s = settings;
				var pages = el.find('.book_openpages');
				pages.each(function(){
					var page = $(this);
					var page_id = page.attr('id');
					$('#link_'+page_id+', .link_'+page_id+', #tab_'+page_id).bind('click', {page: page}, function(e){
						el.trigger('gotopage', [ e.data.page ]);
					});
				});
				$('.nextbut').bind('click', function(e){
					el.trigger('nextpage');
				});
				$('.prevbut').bind('click', function(e){
					el.trigger('prevpage');
				});
				
				var current_page;
				current_page = $(pages.get(0));

				// if (document.location.hash.substr(1)) {
					// var hash_page = $(document.location.hash);
					// if (hash_page.length && $.inArray(hash_page.get(0),pages)) {
						// current_page = hash_page;
					// }
				// }

				el.s.pages = pages;
				el.s.current_page = current_page;

				el.bind('gotopage', function(e,page,now) {
					// console.log('event_gotopage: '+$(page).attr('id'));
					$.stoneflip.gotopage(el,page,now);
				});
				el.bind('history', function(e,page,now) {
					// console.log('event_history: '+$(page).attr('id'));
					$.stoneflip.gotopage(el,page,now);
				});
				el.bind('nextpage', function(e) {
					var last;
					if ( el.s.request_queue.length ) {
						last = $(el.s.request_queue).get(el.s.request_queue.length - 1);
					} else {
						last = $(el.s.current_page).get(0);
					}
					var oldpos = $.inArray(last,el.s.pages);
					// console.log('event_nextpage: '+$(last).attr('id'));
					var max = el.s.pages.length - 1;
					if (oldpos >= max) {						
					
						// GO TO BUCHENDE						
						buchende();

					} else {
						$.stoneflip.gotopage(el,pages.get(oldpos+1));
					}
				});
				el.bind('prevpage', function(e) {
					var last;
					if ( el.s.request_queue.length ) {
						last = $(el.s.request_queue).get(el.s.request_queue.length - 1);
					} else {
						last = $(el.s.current_page).get(0);
					}
					var oldpos = $.inArray(last,el.s.pages);
					// console.log('event_prevpage: '+$(last).attr('id'));
					var max = el.s.pages.length - 1;
					if (oldpos <= 0) {
						// $.stoneflip.gotopage(el,pages.get(max));
						
						// BACKWARD TO BUCH
						buch();
						
					} else {
						$.stoneflip.gotopage(el,pages.get(oldpos-1));
					}
				});
				pages.not(current_page).css({
					"display": "none"
				});
				// if ($.history.callback) {
					// $.history.load(current_page.attr('id'));
				// }
				return el;
			});
		},

		gotopage : function(el,el_newpage,now) {
			
			var anitime = now ? 0 : 1200;
		
			var oldpage = $(el.s.current_page);
			var newpage = $(el_newpage);
			
			// console.log('gotopage: '+oldpage.attr('id')+' => '+newpage.attr('id'));

			var oldpos = $.inArray(oldpage.get(0),el.s.pages);
			var newpos = $.inArray(newpage.get(0),el.s.pages);
			
			if (newpos > -1 && newpos < 23) {
				$('#leftpageno').html( ( ( newpos + 1 ) * 2 ) - 1 );
				$('#rightpageno').html( ( ( newpos + 1 ) * 2 ) );
			} else {
				$('#leftpageno').html('');
				$('#rightpageno').html('');
			}
			
			if ( newpos < 8 || newpos > 22 ) { activate_tab('inhalt'); }
			if ( newpos >= 8 && newpos <= 17 ) { activate_tab('newsletter'); }
			if ( newpos == 19 ) { activate_tab('aktuelles'); }
			if ( newpos >= 20 && newpos <= 21 ) { activate_tab('anmeldung'); }
			if ( newpos == 22 ) { activate_tab('impressum'); }

			var rtl = 0;

			if (newpos > oldpos) {
				rtl = 1;
			}

			if (oldpos == newpos) {
				return;
			}

			if (el.s.animation_running) {
				if ( el.s.last_rtl != rtl ) {
					el.s.request_queue.push($(el_newpage));
					// console.log('gotopage: rtl change');
					return;
				}
				if ( el.s.request_queue.length ) {
					// console.log('gotopage: queue filled');
					if ( !$(el.s.request_queue).get(el.s.request_queue.length - 1) == $(el_newpage).get(0) ) {
						el.s.request_queue.push($(el_newpage));
					} else {
						// console.log('gotopage: last one identical');
					}
					return;
				}
			}

			el.s.last_rtl = rtl;
			
			if (el.s.forward_only) {
				rtl = 1;
			}

			el.s.current_page = newpage;

			el.s.animation_running = el.s.animation_running + 1;

			if ($.history.callback) {
				$.history.load(newpage.attr('id'));
			}
			
			var right = rtl ? 'right' : 'left';
			var left = rtl ? 'left' : 'right';
			
			oldpage.css({
				"zIndex": el.s.zi-1
			});
			
			if (!rtl || el.s.forward_only) {
				el.s.zi = el.s.zi+1;
			}

	 		newpage.find(".page_"+right).css({
				"width": rtl ? "420px" : "414px",
				"zIndex": rtl ? 1 : 0
			});

			if (rtl) {
				newpage.css({
					"right": "0",
					"left": null
				});
			} else {
				newpage.css({
					"left": "0",
					"right": null
				});
			}

			var counter = {
				val: 2
			};
			
			newpage.css({
				"zIndex": el.s.zi,
				"display": "block",
				"width": "0"
			}).animate({
				width: "834px"
			}, anitime, "easeOutQuad", function() {
				$.stoneflip.gotodone(el,counter,oldpage,newpage,left);
			});

	 		newpage.find(".page_"+left).css({
				"zIndex": rtl ? 2 : 1,
				"width": "0"
			}).animate({
				width: rtl ? "414px" : "420px"
			}, anitime, "easeOutQuad", function() {
				$.stoneflip.gotodone(el,counter,oldpage,newpage,left);
			});
			
		},
		
		gotodone : function(el,counter,oldpage,newpage,left) {
			counter.val = counter.val - 1;
			if (counter.val < 1) {
				el.s.animation_running = el.s.animation_running - 1;
				oldpage.css({
					"display": "none"
				});	
				oldpage.find(".page_"+left).css({
					"width": "0"
				});
				if (!el.s.animation_running && el.s.request_queue.length) {
					var queue = el.s.request_queue;
					el.s.request_queue = new Array();
					for ( var i in queue ) {
						$.stoneflip.gotopage(el,queue[i]);
					}
				}
			}
		}
		
	}
	
	$.fn.stoneflip = $.stoneflip.build

})(jQuery);