var Bourne = Bourne || {};
$(document).ready(function() {
	Bourne.filter = {
		oldNameValue : '',
		oldDateValue : '',
		init : function () {
			this.initNameFilter();
			this.initDateFilter();
		},
		initNameFilter : function () {
			$(".filter-by-name input").bind('keyup', function(event) {
				var value = $(this).attr('value').toLowerCase();
				//do nothing if nothing has changed
			    if (value !== '' && value == Bourne.filter.oldNameValue) {
					return;
				}
				$(".listing-item").each(function(index) {
					var name = $(this).find("span.name").html();
					//avoid js errors if there are no names
					if (null === name) return;
					
					if (value !== '' && name.toString().toLowerCase().indexOf(value) == -1) {
						$(this).css('display','none').addClass('name-filtered');
					} else {
						if ( ! $(this).hasClass('date-filtered')) {
							$(this).css('display','block');
						}
						$(this).removeClass('name-filtered');
					}
				});
				Bourne.filter.oldValue = '' + value;
			});
		},
		initDateFilter : function () {
			$(".filter-by-date input").bind('keyup', function(event) {
				var value = $(this).attr('value').toLowerCase();
				//do nothing if nothing has changed
			    if (value !== '' && value == Bourne.filter.oldDateValue) {
					return;
				}
				$(".listing-item").each(function(index) {
					var date = $(this).find("span.date").html();
					//avoid js errors if no dates are there
					if (null === date) return;
					
					if (value !== '' && date.toString().toLowerCase().indexOf(value) == -1) {
						$(this).css('display','none').addClass('date-filtered');
					} else {
						if ( ! $(this).hasClass('name-filtered')) {
							$(this).css('display','block');
						}
						$(this).removeClass('date-filtered');
					}
				});
				Bourne.filter.oldDateValue = '' + value;
			});
		}
		
	};
	Bourne.filter.init();
	Bourne.slide = {
		pageWidth : 600,
		init : function() {
			var hash = window.location.hash;
			$(".title-page", ".page-left .left-nav").click(function(){
				var id = $(this).attr('id').split('-')[2];
				window.location.hash = $(this).find('a').attr('href');
				Bourne.slide.goTo(id);
				return false;
			});
			//on page load
	        if (hash) {
	            switch(hash) {
	                case '#biography' :
					case '#information' :
					case '#catalogues' :
					case '#pricelist' :
					case '#about_us' :
					case '#contemporary' :
	                    Bourne.slide.goTo(2);
	                break;
					case '#floor_plan' :
					case '#team' :
					case '#framing' :
	                    Bourne.slide.goTo(3);
	                break;
					case '#calendar' :
					case '#map' :
					case '#conservation' :
	                    Bourne.slide.goTo(4);
	                break;
					case '#terms_conditions' :
					case '#services' :
	                    Bourne.slide.goTo(5);
	                break;
					case '#glossary' :
	                    Bourne.slide.goTo(6);
	                break;
					case '#articles' :
	                    Bourne.slide.goTo(7);
	                break;
					case '#contact_frames' :
	                    Bourne.slide.goTo(8);
	                break;
					default:
						return false;
					break;
	            }
	        }
		},
		goTo : function(pageId) {
			var numberOfPages = $(".page-center .center-page").size();
			var positionLeft = (pageId - 1) * $(".page-center").width() / numberOfPages;

			//stupid fix for dark background pages
			switch (window.location.hash) {
				case '#pricelist':
					$('#body').css('background-color', '#e6e6e7');
				break;
				case '#contact_us':
					$('#body').css('background-color', '#bfbfc0');
				break;
				default:
					$('#body').css('background-color', '#f5f6f6');
				break
			}

			
			$(".page-center").animate({
				left : '-' + positionLeft + 'px'
			});
			//flash viewer fix and left content fadeout
			if (pageId == 1) {
				$("#flashContainerDiv").css('visibility', 'visible');
				$(".page-content").animate({
					left: '0'
				});
			} else {
				$("#flashContainerDiv").css('visibility', 'hidden');
				$(".page-content").animate({
					left: '240px'
				})
			}
			//title thing
			$('.title-page.selected', '.left-nav').removeClass('selected');
			$('#title-page-' + pageId).addClass('selected');
		}
	};
	
	Bourne.slide.init();
	
	Bourne.viewer = {
		imageReplaceInitiatedId : null,
		init : function() {
			$(".artwork-thumb img", ".page-right").each(function(){
				$(this).mousedown(function(){
					Bourne.viewer.replaceArtwork(this);
					Bourne.slide.goTo(1);
					return false;
				}).click(function(){return false});
			});
		},
		prev : function() {
			var $selectedImg = $(".artwork-thumb.selected img", ".page-right").eq(0);
			var $parent = $selectedImg.parent().parent().parent();
			if (0 !== $parent.prev().length) {
				var $newSelectedImg = $parent.prev().find('img');
				Bourne.viewer.replaceArtwork($newSelectedImg);
			} else {
				var $nextAll = $parent.nextAll();
				var $newSelectedImg = $nextAll.eq($nextAll.length - 1).find('img');
				Bourne.viewer.replaceArtwork($newSelectedImg);
			}
	    	$('.artwork-thumbs')[0].scrollTo('#artwork-thumb_' + $newSelectedImg.attr('id').split("_")[1]);
			
		},
		next : function() {
			var $selectedImg = $(".artwork-thumb.selected img", ".page-right").eq(0);
			var $parent = $selectedImg.parent().parent().parent();
			if (0 !== $parent.next().length) {
				var $newSelectedImg = $parent.next().find('img');
				Bourne.viewer.replaceArtwork($newSelectedImg);
			} else {
				var $prevAll = $parent.prevAll();
				var $newSelectedImg = $prevAll.eq($prevAll.length - 1).find('img');
				Bourne.viewer.replaceArtwork($newSelectedImg);
			}
	    	$('.artwork-thumbs')[0].scrollTo('#artwork-thumb_' + $newSelectedImg.attr('id').split("_")[1]);
		},
		replaceArtwork : function(imgElement) {

			var $img = $(imgElement);
			var $parent = $img.parent().parent().parent();
			if ($parent.hasClass('selected')) {
				return;
			}
			var id = $img.attr('id').split("_")[1];

			//reload flash
			var xmlUrl = '/Artwork/p/artwork/' + id + '/get/xml';
			var target = (navigator.appName.indexOf ("Microsoft") !=-1) ? window['ZoomViewer'] : document['ZoomViewer'];
			//tell flash callback we're replacing an image and which one
			Bourne.viewer.imageReplaceInitiatedId = id;
			//replace artwork in flash
			target.reloadXml(xmlUrl);
		},
		/**
		* Callback from flash when image finished loading
		* Replace text and finish image replacing process
		*/
		imageLoaded : function() {
			if (null !== Bourne.viewer.imageReplaceInitiatedId) {
				Bourne.viewer.replaceText(Bourne.viewer.imageReplaceInitiatedId);
				Bourne.viewer.imageReplaceInitiatedId = null;
			}
		},
		replaceText : function(id) {
			//replace text
			var replaceTo = Bourne.infos[id];
			var $placeHolder = $(".page-content", "#page .page-left");
			$placeHolder.html(replaceTo);
			
			//replace the page title / breadcrumb if we're in Artwork section
			Bourne.viewer.replaceTitle();

			//unhighlight other items
			$('.artwork-thumb', '.artwork-thumbs').removeClass('selected');

			//highlight item
			var $imgElement = $("#artwork-thumb_" + id);
			var $parent = $imgElement.parent().parent().parent();
			$parent.addClass('selected');
			
		},
		replaceTitle : function() {
			var $artworkTitle = $(".page-content .artwork-title h1", "#page .page-left").html();
			if (null !== $artworkTitle) {
				var $pageTitle = $('title', 'html head');
				var pageTitleArray = $pageTitle.html().split(" - ");
				pageTitleArray[2] = $artworkTitle;
				var newPageTitle = pageTitleArray.join(" - ");
				//$pageTitle.html(newPageTitle);
				document.title = newPageTitle;
				$("#header_bottom .breadcrumb.last").html($artworkTitle.toUpperCase())
			}
		}
	}
	Bourne.viewer.init();
})

