(function($) {
	$.fn.rotateEngine = function(options) {
	
		var defaults = {
			initialpage: 0,
			arrowmove: 44,
			scroller: true,
			menuhorizontal: false,
			arrow: "arrow.png",
			arrowrev: "arrow-rev.png"
		}
		var options = $.extend(defaults, options);		
		var currentpage = options.initialpage;		
		var timerId = 0;

		var obj = $(this);
			
		var totalpages = $('#viewscroller').children().length-1;

		return this.each(function() {				
			initEventListeners();
			autoScroll();
			$('#viewitem_0').addClass('currentopt');						
		});

		function initEventListeners() {
				
			$('.rotatemenu').find('a').bind('click', function(event) {
				event.preventDefault();
				options.scroller = false;
				clearTimeout(timerId);
				var temp = $(this).attr('id').split('_');
				fadeWindow(temp[1]);
			});
			
			$('.horizontalmenu').find('a').bind('click', function(event) {
				event.preventDefault();
				options.scroller = false;
				clearTimeout(timerId);
				var temp = $(this).attr('id').split('_');
				fadeWindow(temp[1]);
				displayTxt(temp[1]);
			});
			
			$('#garamap').bind('mouseenter', function(event) {
				$(this).fadeOut('fast', function() {
					var temp = $(this).find('img').attr('src');
					$(this).find('img').attr('src', $(this).attr('href'));
					$(this).attr('href', temp);
					$(this).fadeIn('fast');
				});
			});
			$('#garamap').bind('mouseleave', function(event) {
				$(this).fadeOut('fast', function() {
					var temp = $(this).find('img').attr('src');					
					$(this).find('img').attr('src', $(this).attr('href'));
					$(this).attr('href', temp);					
					$(this).fadeIn('fast');
				});
			});			
			$('#garamap').bind('click', function(event) {
				event.preventDefault();
			});
		}

		function displayTxt(propertyId) {
			$('.currenttxt').fadeOut('slow', function() {
				$('.currenttxt').removeClass('currenttxt');
				$('#txtitem_'+propertyId).addClass('currenttxt');
				$('#txtitem_'+propertyId).fadeIn('slow');
			});
			
		}
		function autoScroll() {
			timerId = setTimeout( function()
			{
				if(options.scroller == true) {
					if(currentpage < totalpages) {
						currentpage++;
					}
					else {
						currentpage = 0;
					}
					fadeWindow(currentpage);
					autoScroll();
				}
			}, 5000);			
		}

		function fadeWindow(option) {
			$('.currentitem').fadeOut('slow', function() {
				$(this).removeClass('currentitem');
				$('#vieweritem_'+option).fadeIn('slow', function() {
					$(this).addClass('currentitem');
					currentpage = option;
				});
			});
			//$('#vieweritem_'+option).fadeIn('slow', function() {
			//	$(this).addClass('currentitem');
			//	currentpage = option;
			//});
			
			highlightSection(option);											
		}

		function highlightSection(option) {
			if(option >= $('#viewermenu > ul').children().length) {
				if(option == $('#viewermenu > ul').children().length) {
					switchArrow("right", true);
				}
				else {
					switchArrow("right");
				}				
				var move_amount = ((option - $('#viewermenu > ul').children().length) * options.arrowmove)+25;				
			}
			else {
				if(option == 0) {
					switchArrow("left", true);
				}
				else {
					switchArrow("left");
				}
				var move_amount = (option * options.arrowmove)+25;
			}
			if(options.menuhorizontal == true) {
				$('#viewpointer').animate({
				    left: move_amount
				  }, {duration: 500, easing: 'easeOutBounce', complete: function() {
					}
				});				
			} 
			else {
				$('.viewitem').removeClass('currentopt');
				$('#viewitem_'+option).addClass('currentopt');
			}
		}
		
		function switchArrow(direction, top) {
			if(!options.menuhorizontal) {
				switch (direction) {
					case "left" :
						$('#viewpointer').html('<img src="assets/img/'+options.arrow+'" />');
						$('#viewpointer').css('left', '150px');
						if(top == true) {
							$('#viewpointer').css('top', '26px');				
						}
					break;
					case "right" :
						$('#viewpointer').html('<img src="assets/img/'+options.arrowrev+'" />');
						$('#viewpointer').css('left', '543px');
						if(top == true) {					
							$('#viewpointer').css('top', '26px');							 
						}
					break;				
				}
			}
		}

	}
			
})(jQuery);			
