(function($) {
	$.fn.viewerEngine = function(options) {
	
		var defaults = {
			initialpage: 0,
			arrowmove: 44,
			scroller: true
		}
		var options = $.extend(defaults, options);		
		var currentpage = options.initialpage;	
		var currentitem = false;	
		var timerId = 0;
		var currentMouseIn;

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

		return this.each(function() {				
			initEventListeners();
			$('#viewermenu > ul > li:first-child > a').addClass('currentopt');
			$('.item_menu > li:first-child > a').addClass('currentitemopt');
			
			//autoScroll();			
		});

		function initEventListeners() {
			$('.item_menu').find('a').bind('click', function(event) {
				event.preventDefault();
				var temp = $(this).attr('id').split('_');
				$('.propicon').css('background', 'url(/assets/img/display/icon-off.png) no-repeat');
				displaySection(temp[1]);				
			});
			$('.viewermenu').find('a').bind('click', function(event) {
				event.preventDefault();
				var temp = $(this).attr('id').split('_');
				displayProperty(temp[1]);
				displayTxt(temp[1]);
			});	
			
			$('.Apartments_icon').bind('click', function(event) {
				event.preventDefault();
				$('.Apartments_icon').css('background', 'url(/assets/img/display/icon-off.png) no-repeat');
				$(this).css('background', 'url(/assets/img/display/icon-on.png) no-repeat');
				var temp = $(this).attr('id').split('_');
				if($('.currentitem').find('.tab_prices').hasClass('currenttab')) {
					displaySection('plans');
				}								
				displayPlans('apartment', temp[1], $(this).attr('href'));
			});
			
			$('.Cottages_icon').bind('click', function(event) {
				event.preventDefault();
				$('.Cottages_icon').css('background', 'url(/assets/img/display/icon-off.png) no-repeat');
				$(this).css('background', 'url(/assets/img/display/icon-on.png) no-repeat');				
				var temp = $(this).attr('id').split('_');
				displayPlans('cottage', temp[1], $(this).attr('href'));
			});			
			
			$('.Rotundas_icon').bind('click', function(event) {
				event.preventDefault();
				$('.Rotundas_icon').css('background', 'url(/assets/img/display/icon-off.png) no-repeat');
				$(this).css('background', 'url(/assets/img/display/icon-on.png) no-repeat');				
				var temp = $(this).attr('id').split('_');
				displayPlans('rotunda', temp[1], $(this).attr('href'));
			});			
			
			$('.gallery_image').bind('click', function(event) {
				event.preventDefault();
				var temp = $(this).attr('id').split('_');
				displayImage($(this).attr('href'));
			});
			
			$('.develmap').bind('mouseenter', function(event) {
				event.preventDefault();
				var temp = $(this).attr('id').split('_');
				if(temp[0] != currentMouseIn) {				
					currentMouseIn = temp[0];
					displayRollover(temp[0]);
				}
			});
			$('.develmap').bind('mouseleave', function(event) {
				currentMouseIn = "";
			});			
			$('.develmap').bind('click', function(event) {
				event.preventDefault();
				var temp = $(this).attr('id').split('_');
				if(temp[1]) {
					displayProperty(temp[1]);
				}
			});			
			$('.develmap').bind('mouseleave', function(event) {
				event.preventDefault();
				resetMap();
			});	
			
			$('.priceover').bind('mouseenter', function(event) {
				event.preventDefault();
				var temp = $(this).attr('id').split('_');
				$('#'+temp[1]+'_'+temp[2]).css('background', 'url(/assets/img/display/icon-on.png) no-repeat');				
			});
			$('.priceover').bind('mouseleave', function(event) {
				event.preventDefault();
				var temp = $(this).attr('id').split('_');
				$('#'+temp[1]+'_'+temp[2]).css('background', 'url(/assets/img/display/icon-off.png) no-repeat');				
			});			
			$('.priceover').bind('click', function(event) {
				event.preventDefault();
				var temp = $(this).attr('id').split('_');
				switch(temp[1]) {
					case "Apartments" : var prop = "apartment"; break;
					case "Cottages" : var prop = "cottage"; break;
					case "Rotundas" : var prop = "rotunda"; break;										
				}				
				displaySection('plans');
				displayPlans(prop, temp[2], $('#' + temp[1] + '_' +temp[2]).attr('href'));
			});
		}
		
		function displayTxt(propertyId) {
			$('.currenttxt').fadeOut('slow', function() {
				$('.currenttxt').removeClass('currenttxt');
				$('#txtitem_'+propertyId).addClass('currenttxt');
				$('#txtitem_'+propertyId).fadeIn('slow');
			});
			
		}
		
		function displayRollover(imgId) {
			var tempbg = $('.currentitem').attr('style');
			$('.currentitem').attr('style', "background:url('/assets/img/display/development_previewer.jpg') no-repeat top left;");			
			$.ajax({
				type: "POST",
				url: "/display.php?imageid="+imgId,
				beforeSend: function() {
					$('#loader').css('display', 'block');
				},				
				success: function(msg){			
					$('#loader').css('display', 'none');
					$('.currentitem').find('.item_img').fadeOut('fast', function() {
						$('.currentitem').find('.item_img').html(msg);
						$('.currentitem').find('.item_img').fadeIn('fast', function() {
							$('.currentitem').attr('style', tempbg);	
						});					
					});
				}
			});			
		}
		
		function displayImage(img) {
			$('#loader').css('display', 'block');					
			$('.currentitem').find('.item_images').css('background', '#ccc');
			$('.currentitem').find('.item_images').css('width', '936');				
			$('.currentitem').find('.item_images').css('height', '350');
			if(!$('.currentitem').find('.galleryimgitem').attr('class')) {
					$('.currentitem').find('.item_images').html("<a href=\""+img+"\" rel=\"shadowbox\"><img src=\"/assets/img/zoomicon.gif\" width=\"28\" height=\"28\" class=\"piczoom\" /></a><img src=\""+img+"\" class=\"galleryimgitem\" />");											
					$('#loader').css('display', 'none');
			} else {	
				$('.currentitem').find('.galleryimgitem').fadeOut('fast', function() {
					$('.currentitem').find('.item_images').html("<a href=\""+img+"\" rel=\"shadowbox\"><img src=\"/assets/img/zoomicon.gif\" width=\"28\" height=\"28\" class=\"piczoom\" /></a><img src=\""+img+"\" class=\"galleryimgitem\" />");											
					$('#loader').css('display', 'none');		
					$('.currentitem').find('.galleryimgitem').fadeIn('fast');
					Shadowbox.setup();
				});				
			}				
																	
		}
		
		function displayPlans(type, planId, img) {		
			$.ajax({
				type: "POST",
				url: "/sales/"+type+"/"+type+"-"+planId,
				beforeSend: function() {
					$('#loader').css('display', 'block');
				},				
				success: function(msg){	
					$('#loader').css('display', 'none');
					var zoomimg = img.replace(".png", "-large.png");							
					$('.currentitem').find('.tab_plans').html(msg+"<a href=\"" + zoomimg + "\" rel=\"shadowbox\"><img src=\""+img+"\" /></a><div class=\"zoomicon\"><a href=\"" + zoomimg + "\" rel=\"shadowbox\"><img src=\"/assets/img/zoomicon.gif\" width=\"28\" height=\"28\" /></a></div>");
					Shadowbox.setup();
				}
			});			
		
		}

		function displayProperty(propertyId) {
		
			if($('.currentitem').attr('id') != "vieweritem_"+propertyId) {
				$('.currentopt').removeClass('currentopt');
				$('.currentitem').fadeOut('slow', function() {
					$(this).removeClass('currentitem');
					resetSections(propertyId);
					menuAdjust(propertyId);
					$('#viewitem_'+propertyId).addClass('currentopt');														
					$('#vieweritem_'+propertyId).fadeIn('slow', function() {
						$(this).addClass('currentitem');
					});
				});
			}				
		}

		function displaySection(sectionId) {

			//if($('.currentitem').find('.item_'+sectionId).css('display') != "block") {
				$('.currentitemopt').removeClass('currentitemopt');
				$('.currentitem').find('.currentcell').fadeOut('slow', function() {
					$(this).removeClass('currentcell');
					$('#option_'+sectionId).addClass('currentitemopt');
					displayInfo(sectionId);					
					$('.currentitem').find('.item_'+sectionId).fadeIn('slow', function() {
						$(this).addClass('currentcell');
					});
				});
			//}

		}

		function menuAdjust(propertyId) {
			if(propertyId == "0") {
				$('.item_menu li:nth-child(2)').addClass('hide');
				$('.item_menu li:nth-child(4)').addClass('hide');
				$('.item_menu li:nth-child(5)').removeClass('hide');									
			}	
			else {
				$('.item_menu li:nth-child(2)').removeClass('hide');
				$('.item_menu li:nth-child(4)').removeClass('hide');
				$('.item_menu li:nth-child(5)').addClass('hide');								
			}
			
		}

		function resetMap() {
			$('#vieweritem_0').find('.item_img').html("<img src=\"/assets/img/display/development_previewer.jpg\" width=\"936\" height=\"350\" usemap=\"#development\" />");			
		}

		function resetSections(propertyId) {
			$('.vieweritem').find('.item_cell').removeClass('currentcell');
			$('.vieweritem').find('.item_cell').css('display', 'none');
			
			if(propertyId == 0) {
				resetMap();
			}
			
			$('.vieweritem > div:first-child').css('display', 'block');
			$('.vieweritem > div:first-child').addClass('currentcell');
			
			$('.vieweritem').find('.item_tab').removeClass('currenttab');
			$('.vieweritem').find('.item_tab').css('display', 'none');			

			$('.item_info > .item_tab:first-child').addClass('currenttab');			
				
			$('.currentitemopt').removeClass('currentitemopt');	
			$('.item_menu > li:first-child > a').addClass('currentitemopt');							
		}

		function displayInfo(sectionId) {
			$('.currentitem').find('.currenttab').fadeOut('fast', function() {
				$(this).removeClass('currenttab');
				$('.currentitem').find('.tab_'+sectionId).fadeIn('fast', function() {
					$(this).addClass('currenttab');
					setScrollBar($(this));
				});
			});
			
		}

		function setScrollBar(tab) {
		
			if(tab.find('.scroller').height() > 280) {			
				tab.find('.slider-vertical').slider({
							orientation: "vertical",
							range: "min",
							min: 0,
							max: tab.find('.scroller').height()-280,
							value: tab.find('.scroller').height()-280,
							slide: function(event, ui) {
								tab.find('.scroller').css('top', (ui.value-(tab.find('.scroller').height()-280)));								
							}
				});
			}
		}

		function autoScroll() {
			timerId = setTimeout( function()
			{
				if(scroller == true) {
					if(currentpage < totalpages) {
						currentpage++;
					}
					else {
						currentpage = 0;
					}
					fadeWindow(currentpage);
					autoScroll();
				}
			}, 5000);			
		}
		

	}			
})(jQuery);			
