// JavaScript Document
mvIE = 0;

(function() {
	var 
		fullScreenApi = { 
			supportsFullScreen: false,
			isFullScreen: function() { return false; }, 
			requestFullScreen: function() {}, 
			cancelFullScreen: function() {},
			fullScreenEventName: '',
			prefix: ''
		},
		browserPrefixes = 'webkit moz o ms khtml'.split(' ');
	
	// check for native support
	if (typeof document.cancelFullScreen != 'undefined') {
		fullScreenApi.supportsFullScreen = true;
	} else {	 
		// check for fullscreen support by vendor prefix
		for (var i = 0, il = browserPrefixes.length; i < il; i++ ) {
			fullScreenApi.prefix = browserPrefixes[i];
			
			if (typeof document[fullScreenApi.prefix + 'CancelFullScreen' ] != 'undefined' ) {
				fullScreenApi.supportsFullScreen = true;
				
				break;
			}
		}
	}
	
	// update methods to do something useful
	if (fullScreenApi.supportsFullScreen) {
		fullScreenApi.fullScreenEventName = fullScreenApi.prefix + 'fullscreenchange';
		
		fullScreenApi.isFullScreen = function() {
			switch (this.prefix) {	
				case '':
					return document.fullScreen;
				case 'webkit':
					return document.webkitIsFullScreen;
				default:
					return document[this.prefix + 'FullScreen'];
			}
		}
		fullScreenApi.requestFullScreen = function(el) {
			return el.webkitRequestFullScreen();
			return (this.prefix === '') ? el.requestFullScreen() : el[this.prefix + 'RequestFullScreen']();
		}
		fullScreenApi.cancelFullScreen = function(el) {
			return (this.prefix === '') ? document.cancelFullScreen() : document[this.prefix + 'CancelFullScreen']();
		}		
	}

	// jQuery plugin
	if (typeof jQuery != 'undefined') {
		jQuery.fn.requestFullScreen = function() {
	
			return this.each(function() {
				var el = jQuery(this);
				if (fullScreenApi.supportsFullScreen) {
					fullScreenApi.requestFullScreen(el);
				}
			});
		};
	}

	// export api
	window.fullScreenApi = fullScreenApi;	
})();


// do something interesting with fullscreen support





(function($) {  
		  	
	var fcFadeSpeed = 300, // Fade Speed
		fcTransitionSpeed = 500, // Transition Speed
		fcShowThumbnails = true, // Show Thumbnails
		fcShowZoom = true, // Show Zoom
		fcShowPopups = true,
		fcThumbOrientation = "horizontal", // Thumbnails Orientation
		fcThumbFit = true, // Fit Thumbnails
		fcThumbFitHeight = 100, // Thumbnails Fit Height
		fcThumbRows = 1,
		fcThumbColumns = 1,
		fcPlayButton = true,
		fcEnableFullscreen = false,
		fcFullscreenButton = true,
		fcThumbnailsButton = false,
		fcShowCounter = true,
		fcCommentButton = true,
		fcMailButton = true,
		fcPrintButton = true,
		fcMMSButton = true,
		fcFacebookButton = true,
		fcTweetButton = true,
		fcTumblrButton = true,
		fcGooglePlusBotton = true,
		fcFlickrButton = true,
		fcPlaySpeed = 7000,
		fcRepeat = false,
		fcAutoPlay = false,
		fcFullHeight = false,
		fcType = "Photo",
		fcCrossFade = true,
		fcTopID = "fc",
		fcPopupsID = "pu",
		fcZoomID = "zw",
		fcThumbnailsID = "ts",
		fcObjectsID = "os",
		fcObjectID = "ob",
		fcNotesID = "ns",
		fcViewsID = "vs",
		fcViewID = "vw",
		fcScrollBarWidth = 15

	$.widget("ui.fcGallery", {
		// default options
		options: {
			fadeSpeed: 300, 
			transitionSpeed: 500, 
			showThumbnails: true,
			showZoom: true,
			thumbOrientation: 'horizontal',
			thumbFit: true,
			thumbFitHeight: 100,
			thumbFitWidth: 100,
			thumbRows: 1,
			thumbColumns: 1,
			playButton: true,
			fullscreenButton: true,
			thumbnailsButton: false,
			commentButton: true,
			mailButton: true,
			printButton: true,
			mmsButton: true,
			facebookLikeButton: false,
			tweetButton: true,
			tumblrButton: true,
			googlePlusButton: true,
			flickrButton: true,
			playSpeed: 7000,
			repeat: true,
			autoPlay: false,
			fullHeight : true,
			type : 'Look',
			crossfade : true,
			showPopups: true,
			objectsID: "os",
			popupsID: "pu",
			zoomID: "zw",
			thumbnailsID: "ts",
			id: "fc"
			},
		_create: function(){
			var self = this ;  
			var o = self.options ;  
			var el = self.element ;	
			o.id = el.attr("id");
			var htmlStr;
			fcEnableFullscreen = self.checkFullscreen(o.fullscreenButton) ;
			o.scrollBarWidth = self._getScrollBarWidth();
			el.addClass("fcGallery");
			if (self.options.fullHeight){self._fullHeight()};

			o.objectsID = o.id + o.objectsID;
			el.find(">UL.fcObjects").attr("id",o.objectsID)

			o.popupsID = o.id + o.popupsID;
			fcPopupsID = o.popupsID;
			if (o.showPopups) { 
				htmlStr = "<span class=\"fcPopups\" id=\""+o.popupsID+"\"></span>";
				el.find(">#"+o.objectsID).append(htmlStr);
			};
			

			var viewWindow = el.find(">#"+o.objectsID).fcObjects({
				type: o.type,
				id: o.objectsID
			});

			if (o.showPopups) { 
				var popupWindows = el.find("#"+o.popupsID).fcWindows();
				el.on({
					'fcpopupclick': function(event,objectNumber) {
						if (!viewWindow.fcObjects("option").transitioning){
							viewWindow.fcObjects("show", objectNumber);
							el.fcTimer("pause");
						};
					}
				});
			};


			var timer = el.fcTimer({
					autoPlay:o.autoPlay, 
					playSpeed: o.playSpeed,
					tick: function(){
						el.find(">#"+o.objectsID).fcObjects("next");
					},					
					started: function(){
						el.find(">#"+o.objectsID).fcObjects("playing");
					},					
					stopped: function(){
						el.find(">#"+o.objectsID).fcObjects("paused");
					},					
				});


			if (o.showThumbnails) { 
				o.thumbnailsID = o.id + o.thumbnailsID;
				el.find(">DIV.fcThumbnails").attr("id",o.thumbnailsID)
				var thumbnails = el.find("#"+o.thumbnailsID).fcThumbnails({
					fadeSpeed :o.fadeSpeed, 
					rows : o.thumbRows,
					columns : o.thumbColumns,
					orientation : o.orientation,
					thumbFit : o.thumbFit,
					thumbFitHeight : o.thumbFitHeight,
					thumbFitWidth : o.thumbFitWidth,
					scrollBarWidth : o.scrollBarWidth,
					id : o.thumbnailsID
				});
				thumbnails.on({
					'fcthumbnailsclicked': function(event,objectNumber) {
						if (!viewWindow.fcObjects("option").transitioning){
							viewWindow.fcObjects("show", objectNumber);
							el.fcTimer("pause");
						};
					}
				});
				el.on({
					'fcobjectsupdated': function(event, objectNumber) {
						thumbnails.fcThumbnails("update", objectNumber);
					}
				});
			};


			if (o.showZoom) { 
				o.zoomID = o.id + o.zoomID;
				htmlStr = "<div class=\"fcZoomWindow\" id=\"" + o.zoomID + "\"></div>";
				el.append(htmlStr);
				var zoomWindow = el.find(">#"+o.zoomID).fcZoom({
					id : o.zoomID
				});
				el.on({
					'fcviewzoom': function(event,urls) {
						zoomWindow.fcZoom("show", event, urls);
						timer.fcTimer("stop");
					}
				});
			};

			el.on({
				'fcviewprevious': function(event,objectNumber) {
					el.find(">#"+o.objectsID).fcObjects("previous");
				},
				'fcviewnext': function(event,objectNumber) {
					el.find(">#"+o.objectsID).fcObjects("next");
				},
				'fcviewfirst': function(event,objectNumber) {
					el.find(">#"+o.objectsID).fcObjects("first");
				},
				'fcviewlast': function(event,objectNumber) {
					el.find(">#"+o.objectsID).fcObjects("last");
				},
				'fcviewfullscreen': function(event,objectNumber) {
					self._fullScreen();
				},
				'fcviewexitfullscreen': function(event,objectNumber) {
					self._exitFullScreen();
				},
				'fcviewpause': function(event,objectNumber) {
					el.fcTimer("pause");
				},
				'fcviewplay': function(event,objectNumber) {
					el.fcTimer("start");
				},
			});

			$(document).on({
				'webkitfullscreenchange': function(){
					viewWindow.fcObjects("resize");
					if ( !document.webkitIsFullScreen) {
						if (o.fullScreenElement == o.id) {
							thumbnails.fcThumbnails("changeOrient", "horizontal")
							if (o.showThumbnails) { 
								el.find("LI.fcView").fcView("windowed");
							};
							if (o.showZoom) { 
								zoomWindow.fcZoom("resize", event);
							};
						};
					} else {
						o.fullScreenElement = $(document.webkitCurrentFullScreenElement).attr('id');
						el.find("LI.fcView").fcView("fullscreen");
						if (o.showThumbnails) { 
							thumbnails.fcThumbnails("changeOrient", "vertical")
						};
						if (o.showZoom) { 
							zoomWindow.fcZoom("resize", event);
						}
					}
				}
			});


		},
		_init: function(){ 
			var self = this ;  
			var o = self.options ;  

			$(window).resize(function(event) { 
				self._resizeView(event);
			}); 
			
			var currentPhoto = self._getSearchNumber();

			var el = self.element.find(">#"+o.objectsID) ;	
			el.fcObjects("hideall");
			if (currentPhoto == 0){currentPhoto =1};
			el.fcObjects("show", currentPhoto);

		},
	    // Use the _setOption method to respond to changes to options
	    _setOption: function( key, value ) {
	      switch( key ) {
	        case "clear":
	          // handle changes to clear option
	          break;
	      }
		  // In jQuery UI 1.9 and above, you use the _super method instead
	      this._super( "_setOption", key, value );
	    },
		 // Use the _destroy method to clean up any modifications your widget has made to the DOM
		_destroy: function(){
		},
		_getScrollBarWidth: function() {
		    var div = $('<div style="width:50px;height:50px;overflow:hidden;position:absolute;top:-200px;left:-200px;"><div style="height:100px;"></div>'); 
   			 // Append our div, do our calculation and then remove it 
    		$('body').append(div); 
    		var w1 = $('div', div).innerWidth(); 
    		div.css('overflow-y', 'scroll'); 
    		var w2 = $('div', div).innerWidth(); 
    		$(div).remove(); 
		    return (w1 - w2); 
		},
		_fullScreen: function(){
			var self = this ;  
			var o = self.options ;  
			var el = self.element ;	
			var elem = document.getElementById(el.attr('id'));
			elem.webkitRequestFullScreen();
		},
		_exitFullScreen: function(){
			document.webkitCancelFullScreen();
		},
		checkFullscreen: function(enable){
			var self = this ;  
			var o = self.options ;  
			var el = self.element ;	
			var browserPrefixes = 'webkit moz o ms khtml'.split(' ');
			var prefix;
			
			console.info('checking fullscreen');

			if (enable){
			// check for native support
				if (typeof document.cancelFullScreen != 'undefined') {
					return true;
				} else {	 
				// check for fullscreen support by vendor prefix
					for (var i = 0, il = browserPrefixes.length; i < il; i++ ) {
						prefix = browserPrefixes[i];
						if (typeof document[fullScreenApi.prefix + 'CancelFullScreen' ] != 'undefined' ) {
							return true;
						}
						
					}
				}
	
			} else {
				return false;
			};
			
			return false;
		},
		_resizeView: function(event){
			var self = this ;  
			var o = self.options ;  
			var el = self.element ;	
			if (self.options.fullHeight){self._fullHeight()};
			el.find("#"+o.objectsID).fcObjects("resize");
		},
		_getSearchNumber: function(){
			var searchValue=window.location.search.substr(7);
			var searchNumber ;
			if (searchValue)
			{
				searchNumber = parseInt(searchValue);
				if (isNaN(searchNumber)) {
					window.location.search = "";
					searchNumber = 0;
				};
			} else { searchNumber = 0 };
			return (searchNumber);
		},
		_fullHeight: function(){
			this.element.height(window.innerHeight);
		},
		gofullscreen: function(){
		},
		cancelfullscreen: function(){
		}
	});		  

	$.widget("ui.fcObjects", {
		// default options
		options: {
			transitionSpeed : 300,
			fadeSpeed : 300,
			currentObject : 0,
			transitioning : false,
			autoPlay : false,
			playSpeed : 5000,
			repeat : false,
			size : 0,
			transitioning : false,
			type : 'Look',
			crossfade : true,
			objectID: "ob",
			id: "os"
			},
		_create: function(){
			var self = this ;  
			var o = self.options ;  
			var el = self.element ;	
			var i = 0;
			o.objectID = o.id + o.objectID;
			
			var object = el.find(">LI").each(function(index) {
				$(this).attr("id", o.objectID + index).fcObject({
					type: o.type,
					id: o.objectID + index
				});
			});
			i=0;
			o.size = object.length;
			//view.attr("id", o.objectID + i++ );

			el.on({
				'fcobjectstransitioned': function(event,oldObjectNumber) {
					o.transitioning = false;
					self._resethover(oldObjectNumber);
				},
				'mouseenter': function(event,objectNumber) {
					var currentObjectID = o.objectID + (o.currentObject-1)
					var currentObject = el.find(">#"+currentObjectID);
					var currentViewID = currentObjectID + fcViewsID + fcViewID + "0";
					var currentView = currentObject.find("#"+currentViewID);
					currentView.fcView("fadeInBarButton");
				},
				'mouseleave': function(event,objectNumber) {
					var currentObjectID = o.objectID + (o.currentObject-1)
					var currentObject = el.find(">#"+currentObjectID);
					var currentViewID = currentObjectID + fcViewsID + fcViewID + "0";
					var currentView = currentObject.find("#"+currentViewID);
					currentView.fcView("fadeOutBarButton");
				},
				'touchstart': function(event,objectNumber) {
					var currentObjectID = o.objectID + (o.currentObject-1)
					var currentObject = el.find(">#"+currentObjectID);
					var currentViewID = currentObjectID + fcViewsID + fcViewID + "0";
					var currentView = currentObject.find("#"+currentViewID);
					currentView.fcView("fadeInBarButton");
				},
				'touchend': function(event,objectNumber) {
					var currentObjectID = o.objectID + (o.currentObject-1)
					var currentObject = el.find(">#"+currentObjectID);
					var currentViewID = currentObjectID + fcViewsID + fcViewID + "0";
					var currentView = currentObject.find("#"+currentViewID);
					currentView.fcView("fadeOutBarButton");
				}
			})


		},
		_init: function(){ 
			var self = this ;  
			var o = self.options ;  
			var el = self.element ;	
			self.resize();
		},
	    _setOption: function( key, value ) {
	    },
		_destroy: function(){
		},
		resize: function(){
			var self = this ;  
			var o = self.options ;  
			var el = self.element ;	
			var width = el.width();
			var height = el.height();
			el.find(">LI").fcObject("resize", width, height);
		},
		playing: function() {  
			var self = this ;  
			var o = self.options ;  
			var el = self.element ;	
			el.find('DIV.fcPlayButton').hide();
			el.find('DIV.fcPauseButton').show();
		},
		paused: function() {  
			var self = this ;  
			var o = self.options ;  
			var el = self.element ;	
			photoData = o.photoData;
			el.find('DIV.fcPauseButton').hide();
			el.find('DIV.fcPlayButton').show();

		},
		next: function(){
			var self = this ;  
			var o = self.options ;  
			var el = self.element ;
			var objectNumber = o.currentObject + 1;
			if ( objectNumber <= o.size) {
				self.show(objectNumber);
			};
		},
		previous: function(){
			var self = this ;  
			var o = self.options ;  
			var el = self.element ;
			self.show(o.currentObject-1);
		},
		last: function(){
			var self = this ;  
			var o = self.options ;  
			var el = self.element ;
			self.show(o.size);
		},
		first: function(){
			var self = this ;  
			var o = self.options ;  
			var el = self.element ;
			self.show(1);
		},
		show: function(nextObjectNumber){
			var self = this ;  
			var o = self.options ;  
			var el = self.element ;
			var currentObjectNumber = o.currentObject;
			if (currentObjectNumber == nextObjectNumber) { return false }
			var currentObjectID = o.objectID + (currentObjectNumber-1)
			var currentObject = el.find("#"+currentObjectID);
			var nextObjectID = o.objectID + (nextObjectNumber-1)
			var nextObject = el.find("#"+nextObjectID);
			var currentView = currentObject.find("LI.fcView");
			var nextView = nextObject.find("LI.fcView");
			
			if (!(o.transitioning) 
				&& ( nextObjectNumber > 0 
						&& ( nextObjectNumber 
						<= o.size) )){
				o.transitioning = true;
				if(currentObject.fcObject("option").notesVisible==true ){
					nextObject.fcObject("showNotes");
				} else {
					nextObject.fcObject("hideNotes");
				};

				if(currentView.fcView("option").barButtonVisible==true){
					nextView.fcView("showBarButton");
				} else {
					nextView.fcView("hideBarButton");
				};

				if(currentView.fcView("option").leftVisible==true){
					nextView.fcView("showLeftBar");
				} else {
					nextView.fcView("hideLeftBar");
				};

				if(currentView.fcView("option").rightVisible==true){
					nextView.fcView("showRightBar");
				} else {
					nextView.fcView("hideRightBar");
				};
				
				oldObjectNumber = currentObjectNumber;
				nextObject.css('z-index',500).fadeIn(o.transitionSpeed,  function() {
					$(this).css('z-index','auto');
					self._trigger('transitioned', null , oldObjectNumber);  
				});
				if (currentObjectNumber>0 && o.crossfade) {
					currentObject.fadeOut(o.transitionSpeed);
				};
				
				o.currentObject = nextObjectNumber;
				self._trigger('updated', null ,nextObjectNumber);  
			};
		},
		_resethover: function(objectNumber){
			var self = this ;  
			var o = self.options ;  
			var el = self.element ;
			var currentObjectID = o.objectID + (objectNumber-1)
			var currentObject = el.find("#"+currentObjectID);
			var currentView = currentObject.find("LI.fcView");
			currentObject.fcObject("hideNotes");
			currentView.fcView("hideLeftBar");
			currentView.fcView("hideRightBar");
			currentView.fcView("hideBarButton");
			currentObject.hide();
		},
		hideall: function(){
			var self = this ;  
			var o = self.options ;  
			var el = self.element ;
			el.find(">LI").hide();
		}
	});		  

	$.widget("ui.fcObject", {
		// default options
		options: {
			transitionSpeed : 5000,
			fadeSpeed : 300,
			notesVisible : false,
			type : 'Photo',
			info : {},
			vsid : "vs",  // Views ID
			nsid : "ns",  // Notes ID
			id: "ob"
			},
		_create: function(){
			var self = this ;  
			var o = self.options ;  
			var el = self.element ;	

			o.vsid = o.id + o.vsid;
			o.nsid = o.id + o.nsid;
			el.find(">UL.fcViews").attr("id",o.vsid)
			el.find(">UL.fcNotes").addClass("fcFade").attr("id",o.nsid);

			self._getObjectInfo();

			var view = el.find(">#"+o.vsid).fcViews({
				fadeSpeed:o.fadeSpeed, 
				transitionSpeed:o.transitionSpeed, 
				objectIndex: o.info.index,
				objectTotal: o.info.total,
				type: o.type,
				notes: o.info,
				id: o.vsid
			});
			self.hideNotes();
			o.notesVisible = false;
			el.hover(function(event) {
					self.fadeInNotes(o.fadeSpeed);
				},
				function(event) {
					self.fadeOutNotes(o.fadeSpeed);
				}
			);

		},
		_init: function(){ 
			var self = this ;  
			var o = self.options ;  
			var el = self.element ;	
		},
		fadeInNotes: function(fadeSpeed){ 
			var self = this ;  
			var o = self.options ;  
			var el = self.element ;	
			el.find(">#"+o.nsid).fadeIn(fadeSpeed);
			el.find("DIV.fcObjectMenu").fadeIn(fadeSpeed);
			o.notesVisible = true;
		},
		fadeOutNotes: function(fadeSpeed){ 
			var self = this ;  
			var o = self.options ;  
			var el = self.element ;	
			el.find(">#"+o.nsid).fadeOut(fadeSpeed)
			el.find("DIV.fcObjectMenu").fadeOut(fadeSpeed)
			o.notesVisible = false;
		},
		showNotes: function(){ 
			var self = this ;  
			var o = self.options ;  
			var el = self.element ;	
			el.find(">#"+o.nsid).show();
			el.find("DIV.fcObjectMenu").show();
			o.notesVisible = true;
		},
		hideNotes: function(){ 
			var self = this ;  
			var o = self.options ;  
			var el = self.element ;	
			el.find(">#"+o.nsid).hide()
			el.find("DIV.fcObjectMenu").hide()
			o.notesVisible = false;
		},
	    _setOption: function( key, value ) {
	    },
		_destroy: function(){
		},
		_getObjectInfo: function(){
			var self = this ;  
			var o = self.options ;  
			var el = self.element ;	
			var a, i, j, img, obj, view, notes, views;
			obj = el.find(">#"+o.nsid);
			o.info = {
				'index' : parseInt(obj.attr('data-index')),
				'total' : parseInt(obj.attr('data-total')),
				'title' : obj.find(">LI.fcTitle").html(),
				'caption' : obj.find(">LI.fcCaption").html()
			}
		},
		resize: function(maxwidth, maxheight){
			var self = this ;  
			var o = self.options ;  
			var el = self.element ;	
			el.find(">#"+o.vsid).fcViews("resize", maxwidth, maxheight);
			self.center();
		},
		center: function(){
			var self = this ;  
			var o = self.options ;  
			var el = self.element ;
			var img = el.find("IMG");
			var marginleft = -(img.width() /2);
			var margintop = -(img.height() /2);
			el.css("margin-left", marginleft + "px");
			el.css("margin-top", margintop + "px");
		},

		move: function(){
		}
		
	});
	
	$.widget("ui.fcViews", {
		// default options
		options: {
			transitionSpeed : 300,
			fadeSpeed : 300,
			objectIndex : 0,
			objectTotal : 0,
			type : "Photo",
			notes : {},
			vwid : "vw",  //view ID
			id : "vs"
			},
		_create: function(){
			var self = this ;  
			var o = self.options ;  
			var el = self.element ;	

			o.vwid = o.id + o.vwid;
			
			var view = el.find(">LI").each(function(index) {
				$(this).attr("id", o.vwid + index).fcView({
					objectIndex: o.objectIndex,
					objectTotal: o.objectTotal,
					notes: o.notes,
					type: o.type,
					flickrButton: o.flickrButton,
					id: o.vwid + index
				});
			});
		},
		_init: function(){ 
		},
	    _setOption: function( key, value ) {
	    },
		_destroy: function(){
	    },
		resize: function(maxwidth, maxheight){
			var self = this ;  
			var o = self.options ;  
			var el = self.element ;	
			el.find(">LI").fcView("resize", maxwidth, maxheight);
		}
	});		  

	$.widget("ui.fcTouch", $.ui.mouse, {
		// default options
		options: {
			start: { x:0, y:0 },
			threshold: { x:30, y:20, d:50 }
			},
		_touchInit: function() { 
			var self = this ;  
			var o = self.options ;  
			var el = self.element ;	
			var clicked = false;
			start = { x: 0, y: 0 }
			end = { x: 0, y: 0 }
			el.on({
				"touchstart" : function (event) {
	//			document.title = "touchStart";
					start.x = event.originalEvent.targetTouches[0].pageX;
					start.y = event.originalEvent.targetTouches[0].pageY;
					o.start.x = start.x
					o.start.y = start.y
					end.x = start.x;
					end.y = start.y;
					trigger = false;
					self._touchStart(event);
					clicked = true;
//				    event.preventDefault();
				},
				"touchmove" : function(event) {
	//				document.title = "touchMove";
					clicked = false;
					end.x = event.originalEvent.targetTouches[0].pageX; // Updated X,Y coordinates
					end.y = event.originalEvent.targetTouches[0].pageY;
					var dX = start.x - end.x;
					var dY = start.y - end.y;
					var d = Math.sqrt(square(dX) + square(dY));
	//				var d = dX;
	//				document.title = d + " " + start.x + " " + end.x + " " + start.y + " " + end.y;
					if ((d>o.threshold.d) && (trigger == false)) { 
						var angle = Math.atan2(dY, dX);
						if ((angle < (Math.PI*.25) ) && (angle > -(Math.PI*.25 ) ) ) {
							trigger = true;
							self._swipeLeft(event);
							self._trigger('swipeleft',event,{});
							self._restart(event);
						} else if ((angle > (Math.PI*.75) ) || (angle < -( Math.PI*.75 ))) {
							trigger = true;
							self._swipeRight(event);
							self._trigger('swiperight',event,{});					
							self._restart(event);
						} else if ((angle > (Math.PI*.25) ) || (angle < ( Math.PI*.75 ))) {
							trigger = true;
							self._swipeUp(event);
							self._trigger('swipeup',event,{});					
							self._restart(event);
						} else if ((angle < -(Math.PI*.25) ) || (angle > -( Math.PI*.75 ))) {
							trigger = true;
							self._swipeDown(event);
							self._trigger('swipedown',event,{});
							self._restart(event);
						}
					};
					self._touchMove(event);
				},
				"touchend" : function(event) {
					trigger = false;
					if (clicked){
						self._trigger('click',event,{});	
						self._singleTap(event);					
					}
					self._touchEnd(event);
				},
				"touchcancel" : function(event) {
					self._touchCancel(event);
				},
				"gesturestart" : function(event) {
					self._gestureStart(event);
				},
				"gesturechange" : function(event) {
					self._gestureChange(event);
				},
				"gestureend" : function(event) {
					self._gestureEnd(event);
				}
				  });
			this._mouseInit()
		},
		_restart: function(event) {
			var self = this ;  
			var o = self.options ;  
			var el = self.element ;	
				start.x = event.originalEvent.targetTouches[0].pageX;
				start.y = event.originalEvent.targetTouches[0].pageY;
				o.start.x = start.x
				o.start.y = start.y
				end.x = start.x;
				end.y = start.y;
				trigger = false;
			},
		_touchStart: function(event) {},
		_touchMove: function(event) {},
		_touchEnd: function(event) {},
		_singleTap: function(event) {},
		_doubleTap: function(event) {},
		_touchCancel: function(event) {},
		_gestureStart: function(event) {},
		_gestureChange: function(event) {},
		_gestureEnd: function(event) {},
		_swipeLeft: function(event) {},
		_swipeUp: function(event) {},
		_swipeRight: function(event) {},
		_swipeDown: function(event) {},
		_tap: function(event) {},
		_doubleTap: function(event) {},
		_hold: function(event) {}
	 });		  

	$.widget("ui.fcView",  $.ui.fcTouch, {
		// default options
		options: {
			transitionSpeed : 300,
			fadeSpeed : 300,
			objectIndex : 0,
			objectTotal : 0,
			barButtonVisible : false,
			leftVisible : true,
			rightVisible : true,
			type : "Photo",
			twitterUser : "futureclaw",
			notes : {},
			info : {},
			id: "vw"
			},
		_create: function(){
			var self = this ;  
			var o = self.options ;  
			var el = self.element ;	
			self._getObjectData();
			
			var htmlStr
			if (o.info.originalURL){
				el.find("IMG").addClass("fcZoom").on({
				'click': function(event) {
					self._trigger('zoom',event, o.info);
					self._trigger('pause',event);
				}}) 
			}

			if (o.objectIndex>1){
				o.bbid = o.id + "bb";  //back bar
				o.bsid = o.id + "bs";  //back shade
				o.baid = o.id + "ba";  //back
				o.fiid = o.id + "fi";  //first 
				htmlStr = "<div class=\"fcBackBar\" id=\"" + o.bbid + "\">"
				htmlStr = htmlStr + "<div class=\"fcFill fcBarShade ui-corner-left\" id=\"" + o.bsid + "\"></div>"
				htmlStr = htmlStr + "<div class=\"fcBarButton fcBackButton fcBack\" id=\"" + o.baid + "\"></div>"
				htmlStr = htmlStr + "<div class=\"fcBarButton fcFirstButton fcFirst\" id=\"" + o.fiid + "\"></div></div>";
				el.append(htmlStr);
				o.leftVisible = false;
				el.find(">#"+o.bbid).on({
					'click': function(event) {
						self._trigger('previous',event);
						self._trigger('pause',event);
					},
					'mouseenter': function(event) {
						self.fadeInLeftBar(o.fadeSpeed);
					},
					'mouseleave': function(event) {
						self.fadeOutLeftBar(o.fadeSpeed);
					}
				}).find(">#"+o.fiid).on({
					'click': function(event) {
						self._trigger('first',event);
						self._trigger('pause',event);
					},
					'mouseenter': function(event) {
						$(this).removeClass('fcFirst').addClass('fcFirstHover');
						el.find("#"+o.baid).removeClass('fcBack').addClass('fcBackOff');
					},
					'mouseleave': function(event) {
						$(this).removeClass('fcFirstHover').addClass('fcFirst');
						el.find("#"+o.baid).removeClass('fcBackOff').addClass('fcBack');
					}
				});
			};
			
			if (o.objectIndex<o.objectTotal){
				o.nbid = o.id + "nb";  //next bar
				o.nsid = o.id + "ns";  //next shade
				o.neid = o.id + "ne";  //next
				o.laid = o.id + "la";  //last
				htmlStr = "<div class=\"fcNextBar\" id=\"" + o.nbid + "\">"
				htmlStr = htmlStr + "<div class=\"fcFill fcBarShade ui-corner-right\" id=\"" + o.nsid + "\"></div>"
				htmlStr = htmlStr + "<div class=\"fcBarButton fcNextButton fcNext\" id=\"" + o.neid + "\"></div>"
				htmlStr = htmlStr + "<div class=\"fcBarButton fcLastButton fcLast\" id=\"" + o.laid + "\"></div></div>";
				el.append(htmlStr);
				o.rightVisible = false;
				el.find(">#"+o.nbid).on({
					'click': function(event) {
						self._trigger('next',event);
						self._trigger('pause',event);
					},
					'mouseenter' : function(event) {
						self.fadeInRightBar(o.fadeSpeed);
					},
					'mouseleave' : function(event) {
						self.fadeOutRightBar(o.fadeSpeed);
					}
				}).find(">#"+o.laid).on({
					'click': function(event) {
						self._trigger('last',event);
						self._trigger('pause',event);
					},
					'mouseenter': function(event) {
						$(this).removeClass('fcLast').addClass('fcLastHover');
						el.find("#"+o.neid).removeClass('fcNext').addClass('fcNextOff');
					},
					'mouseleave': function(event) {
						$(this).removeClass('fcLastHover').addClass('fcLast');
						el.find("#"+o.neid).removeClass('fcNextOff').addClass('fcNext');
					}
				});
			};


			var permalink = o.info.permalink;
			var popupLink;
			htmlStr = "<div class=\"fcObjectMenu\">";
			if (fcPlayButton ) {
				o.plid = o.id + "pl";  //Play button ID
				o.paid = o.id + "pa";  //Pause button ID
				htmlStr = htmlStr + "<div class=\"fcPhotoButton fcPlayControl\">";
				htmlStr = htmlStr + "<div class=\"fcPhotoButton fcPlayButton\" id=\"" + o.plid + "\"></div>";
				htmlStr = htmlStr + "<div class=\"fcPhotoButton fcPauseButton\" id=\"" + o.paid + "\"></div>";
				htmlStr = htmlStr + "</div>";
			};

			if (fcFullscreenButton && fcEnableFullscreen) {
				o.fsid = o.id + "fs";  //Fullscreen button ID
				o.exid = o.id + "ef";  //Exit Fullscreen button ID
				htmlStr = htmlStr + "<div class=\"fcPhotoButton fcScreenControl\">";
				htmlStr = htmlStr + "<div class=\"fcPhotoButton fcFullscreenButton\" id=\"" + o.fsid + "\"></div>";
				htmlStr = htmlStr + "<div class=\"fcPhotoButton fcExitFullscreenButton\" id=\"" + o.exid + "\"></div>";
				htmlStr = htmlStr + "</div>";
			};
			if (fcThumbnailsButton ) {
				o.tbid = o.id + "tb";  //Thumbnails button ID
				htmlStr = htmlStr + "<div class=\"fcPhotoButton fcShowThumbgrid\" id=\"" + o.tbid + "\"></div>";
			};
			if (fcShowCounter ) {
				htmlStr = htmlStr + "<div class=\"fcCount\"><a href=\"" + permalink + "\">" + (o.info.index) + "</a> of " + o.info.total + "</div>";
			};
			if (fcFlickrButton) {
				o.flid = o.id + "fl";  //Flickr button ID
				popupLink = "http:\/\/www.flickr.com\/photos\/" + encodeURIComponent(o.info.flickrOwner) ;
				popupLink = popupLink + "/" + encodeURIComponent(o.info.flickrPID) ;
				htmlStr = htmlStr + "<a href=\"" + popupLink + "\" ";
				htmlStr = htmlStr + "onclick=\"window.open('" + popupLink ; 
				htmlStr = htmlStr + "','popup','width=800,height=900,scrollbars=yes,resizable=yes,toolbar=no,directories=no,location=no,menubar=no,status=no,left=0,top=0'); return false" + "\" >";
				htmlStr = htmlStr + "<div class=\"fcPhotoButton fcFlickrPhoto\" id=\"" + o.flid + "\"></div></a>";
			};
			if (fcTweetButton) {
				o.twid = o.id + "tw";  //Twitter popup trigger
				htmlStr = htmlStr + "<div class=\"fcPhotoButton fcTweetPhoto\" id=\"" + o.twid + "\"></div>";
			};
			if (fcTumblrButton) {
				o.tuid = o.id + "tu";  //Tumblr button ID
				popupLink = "http:\/\/www.tumblr.com\/share\/photo?source=" + encodeURIComponent(o.info.URL) ;
				popupLink = popupLink + "&caption=" + encodeURIComponent(document.title + " " + o.type + " " + o.notes.index) ;
				popupLink = popupLink + "&clickthru=" + encodeURIComponent(permalink) ;
				htmlStr = htmlStr + "<a href=\"" + popupLink + "\" ";
				htmlStr = htmlStr + "onclick=\"window.open('" + popupLink ;
				htmlStr = htmlStr + "','popup','width=500,height=500,scrollbars=yes,resizable=yes,toolbar=no,directories=no,location=no,menubar=no,status=no,left=0,top=0'); return false" + "\" >";
				htmlStr = htmlStr + "<div class=\"fcPhotoButton fcTumblPhoto\" id=\"" + o.tuid + "\"></div></a>";
			};
			if (fcFacebookButton) {
				o.fbid = o.id + "fb";  //Facebook popup trigger
				htmlStr = htmlStr + "<div class=\"fcPhotoButton fcFacebookPhoto\" id=\"" + o.fbid + "\"></div>";
			};
			htmlStr = htmlStr + "</div>";
			el.append(htmlStr);
			
			var om = el.find(">DIV.fcObjectMenu");

			var playButton = om.find("#"+o.plid).fcButton({
				title:"Play Slideshow", 
				click: function(event) {
					self._trigger('play',event);
				}
			});
			var pauseButton = om.find("#"+o.paid).fcButton({
				title:"Pause Slideshow", 
				click: function(event) {
					self._trigger('pause',event);
				}
			});


			var fullscreenButton = om.find("#"+o.fsid).fcButton({
				title:"Enter Full Screen Mode", 
				click: function(event) {
					self._trigger('fullscreen',event);
				}
			});
			
			var exitFullscreenButton = om.find("#"+o.exid).fcButton({
				title:"Exit Full Screen Mode", 
				click: function(event) {
					self._trigger('exitfullscreen',event);
				}
			});

			var thumbGridButton = om.find("#"+o.tbid).fcButton({
				title:"Show Thumbnails", 
				click: function(event) {
					self._trigger('thumbgrid',event);
				}
			});
			var facebookButton = om.find("#"+o.fbid).fcButton({
				title:"Like and Comment on Facebook", 
				click: function(event) {
					self._trigger('facebook',event);
				}
			});
			var tweetButton = om.find("#"+o.twid).fcButton({
				title:"Tweet this photo", 
				click: function(event) {
					self._trigger('tweet',event);
				}
			});
			var tumblrButton = om.find("#"+o.tuid).fcButton({
				title:"Post on Tumblr", 
				click: function(event) {
					self._trigger('tumblr',event);
				}
			});
			var flickrButton = om.find("#"+o.flid).fcButton({
				title:"Show Photo on Flickr", 
				click: function(event) {
					self._trigger('flickr',event);
				}
			});
			
			var objects = $("#"+fcPopupsID);

			var popup;
			var notes;
			notes = "<img class=\"fcPopupImage fcButton fcIndex\" src=\""+ o.info.URL +"\" data-index=\"" + o.info.index + "\">";
			notes = notes + "<span class=\"fcPopupCaption\"><h1 class=\"fcPopupTitleText\">" + o.notes.title + "</h1>";
			notes = notes + o.notes.caption;
			notes = notes + o.info.type + " " + o.notes.index + "<br><br>";

			if (fcFacebookButton) {
				htmlStr = "<div class=\"fcFacebookPhotoPopup\"></div>";
				objects.append(htmlStr);
				popup = objects.find('DIV.fcFacebookPhotoPopup:last');
		//	htmlStr = "<div class=\"fb-like\" data-href=\"" + permalink + "\" data-send=\"false\" data-layout=\"button_count\" data-width=\"90\" data-show-faces=\"false\" data-font=\"arial\"></div>";
				htmlStr = notes + "<fb:like href=\"" + permalink + "\" send=\"true\" layout=\"button_count\" width=\"200\" show_faces=\"true\" colorscheme=\"dark\" font=\"arial\"></fb:like></span>";
				htmlStr = htmlStr + "<fb:comments href=\"" + permalink + "\" num_posts=\"3\" width=\"320\" colorscheme=\"dark\"></fb:comments>";
				var facebookPopup = popup.fcPopup({
					title : "Like and comment in Facebook", 
					trigger: "#"+o.fbid,
					index: o.info.index,
					code : htmlStr
				});
			}
			
			if (fcTweetButton) {
				htmlStr = "<div class=\"fcTweetPhotoPopup\"></div>";
				objects.append(htmlStr);
				popup = objects.find('DIV.fcTweetPhotoPopup:last');
				htmlStr = notes + "<a href=\"https://twitter.com/share\" class=\"twitter-share-button\"";
				htmlStr = htmlStr + "data-url=\"" + permalink + "\" data-counturl=\"" + permalink + "\"";
				htmlStr = htmlStr + "data-count=\"horizontal\" data-via=\"" + o.twitterUser + "\"";
				htmlStr = htmlStr + "data-text=\"Check out " + document.title + " " + o.type + " " + o.notes.index +"\"";
				htmlStr = htmlStr + ">Tweet</a></span>";
				var tweetPopup = popup.fcPopup({
					title : "Share on Twitter", 
					trigger:  "#"+o.twid,
					index: o.info.index,
					code : htmlStr
				});
			}
						




			if( navigator.userAgent.match(/Android/i) ||
				navigator.userAgent.match(/webOS/i) ||
				navigator.userAgent.match(/iPad/i) ||
				navigator.userAgent.match(/iPhone/i) ||
				navigator.userAgent.match(/iPod/i) ){

				el.on({
					'fcviewswiperight' : function(event) {
						event.preventDefault();			
						self._trigger('previous',event);
						self._trigger('pause',event);
					},
					'fcviewswipeleft': function(event) {
						event.preventDefault();			
						self._trigger('next',event);
						self._trigger('pause',event);
					},
					'gesturestart' : function(event) {
						var offset = el.offset();
						event.pageX = offset.left + el.width()/2;
						event.pageY = offset.top + el.height()/2;
						event.preventDefault();			
						self._trigger('zoom',event, o.info);
						self._trigger('pause',event);
				}});  
	
			} else {
				el.on('mousewheel', function(event, delta) {
					event.preventDefault();
					if (delta<0){
						self._trigger('previous',event);
					}
					if (delta>0){
						self._trigger('next',event);
					}
					self._trigger('pause',event);
				});
			};

		},
		_init: function(){ 
			var self = this ;  
			var o = self.options ;  
			var el = self.element ;	
			self.hideLeftBar();
			self.hideRightBar();
			el.find("#"+o.paid).hide();
			el.find("#"+o.exid).hide();
			el.find("DIV.fcBarButton").hide();
			self._touchInit();
		},
	    _setOption: function( key, value ) {
	    },
		_destroy: function(){
	    },
		_resize: function(){
		},
		fullscreen: function(){ 
			var self = this ;  
			var o = self.options ;  
			var el = self.element ;	
			el.find("#"+o.fsid).hide();
			el.find("#"+o.exid).show();
		},
		windowed: function(){ 
			var self = this ;  
			var o = self.options ;  
			var el = self.element ;	
			el.find("#"+o.exid).hide();
			el.find("#"+o.fsid).show();
		},
		fadeInBarButton: function(fadeSpeed){ 
			var self = this ;  
			var o = self.options ;  
			var el = self.element ;	
			el.find("DIV.fcBarButton").fadeIn(fadeSpeed);
			o.barButtonVisible = true;
		},
		fadeOutBarButton: function(fadeSpeed){ 
			var self = this ;  
			var o = self.options ;  
			var el = self.element ;	
			el.find("DIV.fcBarButton").fadeOut(fadeSpeed);
			o.barButtonVisible = false;
		},
		showBarButton: function(){ 
			var self = this ;  
			var o = self.options ;  
			var el = self.element ;	
			el.find("DIV.fcBarButton").show();
			o.barButtonVisible = true;
		},
		hideBarButton: function(){ 
			var self = this ;  
			var o = self.options ;  
			var el = self.element ;	
			el.find("DIV.fcBarButton").hide()
			o.barButtonVisible = false;
		},
		fadeInLeftBar: function(fadeSpeed){ 
			var self = this ;  
			var o = self.options ;  
			var el = self.element ;	
			if (o.objectIndex>1){
				el.find("#"+o.bsid).fadeIn(fadeSpeed);
				o.leftVisible = true;
			};
		},
		fadeOutLeftBar: function(fadeSpeed){ 
			var self = this ;  
			var o = self.options ;  
			var el = self.element ;	
			el.find("#"+o.bsid).fadeOut(fadeSpeed);
			o.leftVisible = false;
		},
		showLeftBar: function(){ 
			var self = this ;  
			var o = self.options ;  
			var el = self.element ;	
			if (o.objectIndex>1){
				el.find("#"+o.bsid).show();
				o.leftVisible = true;
			};
		},
		hideLeftBar: function(){ 
			var self = this ;  
			var o = self.options ;  
			var el = self.element ;	
			el.find("#"+o.bsid).hide()
			o.leftVisible = false;
		},
		fadeInRightBar: function(fadeSpeed){ 
			var self = this ;  
			var o = self.options ;  
			var el = self.element ;	
			if (o.objectIndex<this.options.objectTotal){
				el.find("#"+o.nsid).fadeIn(fadeSpeed);
				o.rightVisible = true;
			};
		},
		fadeOutRightBar: function(fadeSpeed){ 
			var self = this ;  
			var o = self.options ;  
			var el = self.element ;	
			el.find("#"+o.nsid).fadeOut(fadeSpeed);
			o.rightVisible = false;
		},
		showRightBar: function(){ 
			var self = this ;  
			var o = self.options ;  
			var el = self.element ;	
			if (o.objectIndex<this.options.objectTotal){
				el.find("#"+o.nsid).show();
				o.rightVisible = true;
			};
		},
		hideRightBar: function(){ 
			var self = this ;  
			var o = self.options ;  
			var el = self.element ;	
			el.find("#"+o.nsid).hide()
			o.rightVisible = false;
		},
		_getObjectData: function(){
			var self = this ;  
			var o = self.options ;  
			var el = self.element ;	
			var a, i, j, img, obj, view, notes, views;
			img = el.find("IMG.fcImage");
			o.info = {
				'height' : img.height(),
				'width' : img.width(),
				'portrait' : true,
				'URL' : img.attr('src'),
				'originalHeight' : parseInt(img.attr('data-originalheight')),
				'originalWidth' : parseInt(img.attr('data-originalwidth')),
				'originalURL' :  img.attr('data-originalurl'),
				'smallHeight' : parseInt(img.attr('data-smallheight')),
				'smallWidth' : parseInt(img.attr('data-smallwidth')),
				'smallURL' :  img.attr('data-smallurl'),
				'flickrPID' : parseInt(img.attr('data-flickrpid')),
				'flickrOwner' : img.attr('data-flickrowner'),
				'permalink' : img.attr('data-permalink'),
				'type' : img.attr('data-type'),
				'index' : parseInt(img.attr('data-index')),
				'total' : parseInt(img.attr('data-total'))
			}
			if (o.info.height < o.info.width){
				o.info.portrait = false;
			} ;
		},
		resize: function(maxwidth, maxheight){
			var self = this ;  
			var o = self.options ;  
			var el = self.element ;	
			var boxRatio, naturalWidth, naturalHeight, naturalRatio, boxScale ;
			var height, width;

			boxRatio = maxheight/maxwidth;
			naturalWidth = o.info.width;
			naturalHeight = o.info.height;
			naturalRatio = naturalHeight/naturalWidth;
			if (naturalRatio > boxRatio){
				boxScale = naturalHeight/maxheight;
			} else {
				boxScale = naturalHeight/maxwidth/naturalRatio;
			};
			width = naturalWidth/boxScale;
			height = naturalHeight/boxScale;
			
			diff = (maxwidth - width)/2;
			if (diff<50){
				el.find("#"+o.bbid).css("margin-left",(50-diff));
				el.find("#"+o.nbid).css("margin-right",(50-diff));
			} else {
				el.find("#"+o.bbid).css("margin-left",0);
				el.find("#"+o.nbid).css("margin-right",0);
			}
			el.find("IMG.fcImage").width(width).height(height);
		}
	});		  

	$.widget("ui.fcControlBar", {
		// default options
		options: {
			},
		_create: function(){ 
			var self = this ;  
			var o = self.options ;  
			var el = self.element ;	
			
		},
		_init: function(){ 
			var self = this ;  
			var o = self.options ;  
			var el = self.element ;	
		}
	 });		  

	$.widget("ui.fcButton", {
		// default options
		options: {
			'href': "fcButtonLink",
			'title' : "",
			},
		_create: function(){ 
			var self = this ;  
			var o = self.options ;  
			var el = self.element ;	
			
			el.addClass('fcButton');

			var htmlStr;
			htmlStr = "<div class=\"fcFill fcButtonDefault\" title=\"" + o.title + "\"></div>"
			el.append(htmlStr);
			htmlStr = "<div class=\"fcFill fcButtonHover\" title=\"" + o.title + "\"></div>"
			el.append(htmlStr);
			el.on('click.fcButton', function(event) {
					event.preventDefault()
					self._trigger('click',event);
			});  
		},
		_init: function(){ 
			var self = this ;  
			var o = self.options ;  
			var el = self.element ;	
		}
	 });		  

	$.widget("ui.fcPopup", {
		// default options
		options: {
			'title' : "",
			'code' : "",
			'trigger' : "",
			'index' : 0,
			'fadeSpeed' : 300			
			},
		_create: function(){ 
			var self = this ;  
			var o = self.options ;  
			var el = self.element ;	
			
			var htmlStr;
			var index;
			el.addClass("fcPopup ui-corner-all");
			var that = $(o.trigger);
			that.one('mouseover', function(event) {
				htmlStr = "<div class=\"fcPopupTip\"></div><div class=\"fcPopupClose\"></div><div class=\"fcPopupTitle\">" + o.title + "</div><div class=\"fcPopupData\"></div>"
				el.append(htmlStr).draggable();
				var closeButton = el.find(">DIV.fcPopupClose").fcButton({
					title:"Close", 
					click: function(event) {
						event.preventDefault()
						el.fadeOut(o.fadeSpeed);
					}
				});
				el.find(">DIV.fcPopupData").append(o.code);
				index = parseInt(el.find(".fcIndex").attr('data-index'));
				if (index>0){
					el.find(".fcIndex").on('click', function(event) {
						event.preventDefault();
						self._trigger('click',event, index);
					});  
				}
				FB.XFBML.parse();
 				twttr.widgets.load()
 			});  
			that.find(">DIV.fcButtonHover, >DIV.fcButtonDefault").on({
				'click': function(event){
					event.preventDefault()
					el.fadeIn(o.fadeSpeed);
				},
			});
		},
		_init: function(){ 
			var self = this ;  
			var o = self.options ;  
			var el = self.element ;	
			el.hide();
		}
	 });		  

	$.widget("ui.fcTimer", {
		// default options
		options: {
			playSpeed: 1000,
			autoPlay: false,
			timerOn: false
			},
		_init: function() {
			var self = this ;  
			var o = self.options ;  
			if (o.autoPlay){
				this._timerStart() 
			}
		},
		_timerStart: function(){
			var self = this ;  
			var o = self.options ;  
			var el = self.element;	
			timerObject = this;
			timerID = window.setInterval("timerObject._trigger('tick');", o.playSpeed);
			o.timerOn = true;
			self._trigger('started'); 
		},
		_timerStop: function(){ 
			var self = this ;  
			var o = self.options ;  
			var el = self.element;
			if (o.timerOn){
				window.clearInterval(timerID); 
			};
			self._trigger('stopped'); 
		},
		start: function() { 
			this._timerStart() 
		},
		immediateStart: function() { 
			this._trigger('tick');
			this._timerStart() 
		},
		pause: function(){ 
			this._timerStop() 
		}
	 });		  

	$.widget("ui.fcThumbnail", {
		// default options
		options: {
			objectNumber : 0,
			fadeSpeed : 100,
			cuid : "cu", //cursor ID
			poid : "po"  //pointer ID
			},
		_create: function() {
			var self = this ;  
			var o = self.options ;  
			var el = self.element ;	
			el.addClass('fcThumbnail fcButton');
			
			self._getData();


			var htmlStr;
			htmlStr = "<div class=\"fcCursor\" id=\"" + o.cuid + "\"></div>"
			el.append(htmlStr);
			htmlStr = "<div class=\"fcPointer\" id=\"" + o.poid + "\"></div>"
			el.append(htmlStr);

			el.on('mouseover', function(event) {
					$(this).find("#"+o.poid).fadeIn(o.fadeSpeed);
				}).on('mouseleave', function(event) { 
					$(this).find("#"+o.poid).fadeOut(o.fadeSpeed);
			});  
			el.on('click', function(event) {
					self._trigger('click',event, o.info.index);
			});  

			},
		_init: function(){ 
			var self = this ;  
			var o = self.options ;  
			var el = self.element ;	
			el.find("#"+o.cuid).hide();
			el.find("#"+o.poid).hide();
			
		},
		_getData: function(){ 
			var self = this ;  
			var o = self.options ;  
			var el = self.element ;	


			var a, i, j, img, obj, view, notes, views;
			img = el.find("IMG");
			o.info = {
				'height' : img.height(),
				'width' : img.width(),
				'portrait' : true,
				'URL' : img.attr('src'),
				'index' : parseInt(img.attr('data-index')),
				'total' : parseInt(img.attr('data-total'))
			}
			if (o.info.height < o.info.width){
				o.info.portrait = false;
			} ;

		},
		active: function(){ 
			var self = this ;  
			var o = self.options ;  
			var el = self.element ;	
			el.find("#"+o.cuid).fadeIn(o.fadeSpeed);
		},	 
		inactive: function(){ 
			var self = this ;  
			var o = self.options ;  
			var el = self.element ;	
			el.find("#"+o.cuid).fadeOut(o.fadeSpeed);
		}	 
		
	});		  



	//
	//  Thumbnail Viewer
	//

	$.widget("ui.fcThumbnails", $.ui.mouse, {
		// default options
		options: {
			orientation: 'horizontal',
			scaleThumbs: true,

			rows: 1,
			columns: 1,
			thumbnailMaxHeight: 0,
			thumbnailMaxWidth: 0,
			thumbnailMinHeight: 100000,
			thumbnailMinWidth: 100000,
			thumbFit : true,
			thumbFitHeight : 100,
			thumbFitWidth : 100,
			vertical: false,
			gridded: false,
			setSize: 10,
			currentPhoto: 0,
			buttonSize: 30,
			first: false,
			last: false,
			isid: "is",  //iScroll ID
			webkitScrollBarWidth: 15,
			scrollBarWidth: 25,
			id: 'ts'
		},
		_create: function() {
			var self = this ;  
			var o = self.options ;  
			var el = self.element ;	
			
			o.isid = o.id+"is"; //iSCroll/ScrollWindow ID
			o.drid = o.id+"dr";
			o.tnid = o.id+"tn"; //Thumbnail ID

			el.find( "UL.fcDirectory").attr("id",o.drid);

			var thumb = el.find("LI.fcListing").each(function(index) {
				$(this).attr("id", o.tnid + index).fcThumbnail({
					id: o.tnid+index,
					click: function(event, value) {
						self._trigger('clicked',event, value);
					}
				});
			});
			
			var htmlStr;
			htmlStr = "<div class=\"fcScrollWindow\" id=\"" + o.isid + "\"></div>"
			el.wrapInner(htmlStr);

			o.ppid = o.id+"pp";  //next page ID
			o.npid = o.id+"np";  //next page ID
			htmlStr = "<div class=\"fcButton fcPreviousPage ui-corner-left\" id=\"" + o.ppid + "\"><div class=\"fcCenter fcPreviousPageButton\"></div></div>"
			el.append(htmlStr);
			el.find(">#"+o.ppid).on({
				'click': function(event){
					self._previousPage();
				}
			});

			htmlStr = "<div class=\"fcButton fcNextPage ui-corner-right\" id=\"" + o.npid + "\"><div class=\"fcCenter fcNextPageButton\"></div></div>"
			el.append(htmlStr);
			el.find(">#"+o.npid).on({
				'click': function(event){
					self._nextPage();
				}
			});
		
		},
		_init: function() {
			var self = this ;  
			var o = self.options ;  
			var el = self.element ;	
			var vertical;
			var scale;
			
			switch (o.orientation){
				case "horizontal":
					el.removeClass("fcVerticalThumbnails fcGriddedThumbnails").addClass("fcHorizontalThumbnails");
					vertical = false;
					o.thumbFitHeight = el.height();
					break;
				case "vertical":
					el.removeClass("fcHorizontalThumbnails fcGriddedThumbnails").addClass("fcVerticalThumbnails");
					vertical = true;
					o.thumbFitWidth = el.width();
					break;
				case "gridded":
					vertical = false;
					el.removeClass("fcVerticalThumbnails fcHorizontalThumbnails").addClass("fcGriddedThumbnails");
					break;
				default:
					vertical = false;
					el.removeClass("fcVerticalThumbnails fcHorizontalThumbnails fcGriddedThumbnails");
			};
			
			
			if (o.scaleThumbs){
				var k = 0;
				var info;
				el.find("LI.fcListing").each(function() {
					info = $(this).fcThumbnail("option",'info')
					thisHeight = info.height;
					thisWidth = info.width;
					if (o.orientation == 'vertical') {
						scale = o.thumbFitWidth / thisWidth ;
						thisHeight = thisHeight * scale;
						el.find("#" + o.tnid + k + ">IMG").width(o.thumbFitWidth).height(thisHeight);
					} else {
						scale = o.thumbFitHeight / thisHeight ;
						thisWidth = thisWidth * scale ;
						el.find("#" + o.tnid + k + ">IMG").width(thisWidth).height(o.thumbFitHeight);
					}
					k++;
				});
			};

			o.thumbnailMaxHeight = 0;
			o.thumbnailMaxWidth = 0;
			el.find("LI.fcListing").each(function() {
				thisHeight = $(this).outerHeight(true);
				thisWidth = $(this).outerWidth(true);
				if (thisHeight > o.thumbnailMaxHeight) {
					o.thumbnailMaxHeight = thisHeight;
				}
				if (thisWidth > o.thumbnailMaxWidth) {
					o.thumbnailMaxWidth = thisWidth;
				}
				if (thisHeight < o.thumbnailMinHeight) {
					o.thumbnailMinHeight = thisHeight;
				}
				if (thisWidth < o.thumbnailMinWidth) {
					o.thumbnailMinWidth = thisWidth;
				}

			});
			
			
			if (o.orientation == "gridded"){
				var i = 0, j = 0, k = 0;
					for (i = 0;( i < (o.rows)); i++) {
						for (j = 0;( j < (o.columns)); j++) {
							el.find("#"+o.tnid + k).css("left",j * o.thumbnailMaxWidth).css("top",i*o.thumbnailMaxHeight);
							k++;
						};
					};
					el.find("#"+o.drid).width(o.columns * o.thumbnailMaxWidth).height(o.rows * o.thumbnailMaxHeight);
			} else {
				var k = 0;
				var thumbnailsHeight = 0, thumbnailsWidth = 0;
				el.find("LI.fcListing").each(function() {
					thisHeight = $(this).outerHeight(true);
					thisWidth = $(this).outerWidth(true);
					if (vertical) {
						el.find("#"+o.tnid + k).css("left",0).css("top",thumbnailsHeight);
						thumbnailsHeight = thumbnailsHeight + thisHeight;
					} else {
						el.find("#"+o.tnid + k).css("left",thumbnailsWidth).css("top",0);
						thumbnailsWidth = thumbnailsWidth + thisWidth;
					}
					k++;
				});
				if (vertical) {
					el.find("#"+o.drid).width(o.thumbnailMaxWidth).height(thumbnailsHeight);
				} else {
					el.find("#"+o.drid).width(thumbnailsWidth).height(o.thumbnailMaxHeight);
				}
			};			

			var scrollWindow = el.find("#"+o.isid);

			if(vertical) {
				scrollWindow.removeClass('fcHScroll').addClass('fcVScroll');
			}else{
				scrollWindow.removeClass('fcVScroll').addClass('fcHScroll');
				};

			if( navigator.userAgent.match(/Android/i) ||
				navigator.userAgent.match(/webOS/i) ||
				navigator.userAgent.match(/iPhone/i) ||
				navigator.userAgent.match(/iPad/i) ||
				navigator.userAgent.match(/iPod/i) ){
//				el.find(".fcScrollWindow").addClass('mvScrollWindow');
				var myScroll = new iScroll(o.isid, 
					{ 
						snap: 'li', 
						vScroll: vertical ,  
						bounce: false , 
						onScrollMove: function() { self._move() }
					}
				);
			} else if ($.browser.webkit){
				scrollWindow.unbind("scroll").css('overflow','auto');
				if(vertical) {
					scrollWindow.width(o.thumbFitWidth+o.webkitScrollBarWidth).height('auto')
				}else{
					scrollWindow.height(o.thumbFitHeight+o.webkitScrollBarWidth).width('auto')
				};
			} else {
	   	 		var newScrollWindow = scrollWindow.fcScrollWindow({
					vertical: vertical, 
					scrollBarWidth:o.scrollBarWidth,
					drid: o.drid,
					id: o.isid
					}); 
			};
			scrollWindow.on("scroll",function(event) { self._move(); });
			this._move();
			this._mouseInit() ;

		},
		update: function(objectNumber) {
			var self = this ;  
			var o = self.options ;  
			var el = self.element ;	
			var location, start, size, thumbSize;
			var scrollWindow = el.find("#"+o.isid);
			var object = el.find("#"+o.tnid +(objectNumber-1));
			if (o.orientation == 'vertical'){
				location = object.position().top;
				start = scrollWindow.scrollTop();
				size = scrollWindow.height();
				thumbSize = object.height() 
				if ((location<0) || ((location+thumbSize)>=(size))) {
					scrollWindow.scrollTo(
						object,
						200, 
						{ 
							axis: 'y',
							offset: -(scrollWindow.height()/2)
						}
					)
				};
			} else {
				location = object.position().left;
				start = scrollWindow.scrollLeft();
				size = scrollWindow.width();
				thumbSize = object.width() 
				if ((location<0) || ((location+thumbSize)>=(size))) {
					scrollWindow.scrollTo(
						object,
						200, 
						{ 
							axis: 'x',
							offset: -(scrollWindow.width()/2)
						}
					)
				};
			}
			self._moveCursor(objectNumber);
		},
		changeOrient: function(orientation) {
			var self = this ;  
			var o = self.options ;  
			var el = self.element ;	
			if (orientation == "vertical") {
				o.orientation = 'vertical';
				this._init();
			} else {
				o.orientation = 'horizontal';
				this._init();
			}
		},
		_moveCursor: function(objectNumber) {
			var self = this ;  
			var o = self.options ;  
			var el = self.element ;	
			if (o.currentPhoto>0){el.find("#"+o.tnid+(o.currentPhoto-1)).fcThumbnail("inactive");}
			el.find("#"+o.tnid +(objectNumber-1)).fcThumbnail("active");
			o.currentPhoto = objectNumber;
		},
		_scrollTo: function(x) {
			var self = this ;  
			var o = self.options ;  
			var el = self.element ;	
			if (x<0) {x=0};
			if( navigator.userAgent.match(/Android/i) ||
				navigator.userAgent.match(/webOS/i) ||
				navigator.userAgent.match(/iPhone/i) ||
				navigator.userAgent.match(/iPad/i) ||
				navigator.userAgent.match(/iPod/i) ){
//				el.find(".fcScrollWindow").scrollTo(x,0,200);
			} else {
				if (o.orientation == 'vertical'){
					el.find("#"+o.isid).scrollTo(x,200,{axis:'y'});
				} else {
					el.find("#"+o.isid).scrollTo(x,200,{axis:'x'});
				}
			}
		},
		_move: function() { 
			var self = this ;  
			var o = self.options ;  
			var el = self.element ;
			var scrollWindow = el.find("#"+o.isid);
			if ((o.vertical ?  scrollWindow.scrollTop() : scrollWindow.scrollLeft())<=0){
				if ( o.first == false){
					o.first = true;
					el.find("#"+o.ppid).fadeOut(o.fadeSpeed);				
			};
			} else {
				if ( o.first == true){
					o.first = false;
					el.find("#"+o.ppid).fadeIn(o.fadeSpeed);				
				};
			};
			if ( (o.vertical ? 
				  (scrollWindow.scrollTop() + scrollWindow.height() ) : 
				  (scrollWindow.scrollLeft() + scrollWindow.width()) + 1) >= 
				((o.vertical ? scrollWindow.find("#"+o.drid).height() : 
				  scrollWindow.find("#"+o.drid).width() ) 
				 + o.scrollBarWidth )) {
				if ( o.last == false){
					o.last = true;
					el.find("#"+o.npid).fadeOut(o.fadeSpeed);				
				};
			} else {
				if ( o.last == true){
					o.last = false;
					el.find("#"+o.npid).fadeIn(o.fadeSpeed);				
				};
			};
		},
		_nextPage: function() {
			var self = this ;  
			var o = self.options ;  
			var el = self.element ;	
			var scrollWindow = el.find("#"+o.isid);
			var loc ;
			if (o.vertical){ 
				loc = scrollWindow.scrollTop()+scrollWindow.height();
			} else {
				loc = scrollWindow.scrollLeft()+scrollWindow.width();
			};
			self._scrollTo(loc);
		},
		_previousPage: function() {
			var self = this ;  
			var o = self.options ;  
			var el = self.element ;	
			var loc ;
			var scrollWindow = el.find("#"+o.isid);
			if (o.vertical){ 
				loc = scrollWindow.scrollTop()-scrollWindow.height();
			} else {
				loc = scrollWindow.scrollLeft()-scrollWindow.width();
			};
			self._scrollTo(loc);
		}
	 });		  

	//
	//  My custom scrollbar window
	//

	$.widget("ui.fcScrollWindow", $.ui.mouse, {
		// default options
		options: {
			vertical: false,
			scrollBarWidth: 25,
			drid: "dr", //directory ID
			id: "sw"
			},
		_create: function() { 
			var self = this ;  
			var o = self.options ;  
			var el = self.element ;	
			var htmlStr;
			
			o.vsid = o.id + "vs";  //vertical scrollbar
			o.hsid = o.id + "hs"; //horizontal scrollbar

			//
			//Horizontal Scrollbar
			//

			if (o.vertical){
				el.css("right",-(o.scrollBarWidth));
				htmlStr = "<div class=\"fcScrollbar fcVScrollbar\" id=\"" + o.vsid + "\"></div>";
				el.append(htmlStr);
				var contentSize = el.find("#"+o.drid).length();
				var vScrollBar = el.find("#"+o.vsid).fcScrollbar({
					windowSize: el.height(),
					contentSize: contentSize,
					vertical: true,
					id: o.vsid
				});  
	
    			vScrollBar.on('fcscrollbarscroll', function(event,ui) { 
					el.scrollTop(ui.scrollPosition);
				});      		
	
				el.on('scroll', function(event) { 
					vScrollBar.fcScrollbar("option","scrollPosition", $(this).scrollTop())
					.fcScrollbar("option","windowSize", $(this).height())
					.fcScrollbar("option","contentSize", ($(this).contents().height()+o.scrollBarWidth))
					.fcScrollbar("refresh");
					self._trigger('moved'); 
					});  
				$(window).on('resize', function(event) { 
					vScrollBar.fcScrollbar("option","scrollPosition", el.scrollTop())
					.fcScrollbar("option","windowSize", el.height())
					.fcScrollbar("option","contentSize", (el.contents().height())+o.scrollBarWidth)
					.fcScrollbar("refresh");
					self._trigger('moved'); 
				});  
			} else {
				el.css("bottom",-(o.scrollBarWidth));
				htmlStr = "<div class=\"fcScrollbar fcHScrollbar\" id=\"" + o.hsid + "\"></div>";
				el.after(htmlStr);
				var width = el.width();
				var contentSize = el.find("#"+o.drid).width();
				var hScrollBar = el.parent().find("#"+o.hsid).fcScrollbar({
					windowSize: width,
					contentSize: contentSize,
					vertical: false,
					id: o.hsid
				});  
	
    			hScrollBar.on('fcscrollbarscroll', function(event,ui) { 
					el.scrollLeft(ui.scrollPosition);
				});      		

				el.on('scroll', function(event) { 
					hScrollBar.fcScrollbar("option","scrollPosition", $(this).scrollLeft())
					.fcScrollbar("option","windowSize", $(this).width())
					.fcScrollbar("option","contentSize", ($(this).find("#"+o.drid).width()))
					.fcScrollbar("refresh");
					self._trigger('moved'); 
					});  
				$(window).on('resize', function(event) { 
					hScrollBar.fcScrollbar("option","scrollPosition", el.scrollLeft())
					.fcScrollbar("option","windowSize", el.width())
					.fcScrollbar("option","contentSize", (el.find("#"+o.drid).width()))
					.fcScrollbar("refresh");
					self._trigger('moved'); 
				});  
			};

			htmlStr = "<div class=\"fcScrollWindowWrapper fcFill\"></div>";
			el.wrap(htmlStr);

		},
		_init: function() {
			this._trigger('moved');
			this._mouseInit();
		},
		_resizeH: function() {
			this._trigger('moved');
			this._mouseInit();
		},
		_resizev: function() {
			this._trigger('moved');
			this._mouseInit();
		}

	});		  

	$.widget("ui.fcScrollbar", $.ui.mouse, {
		// default options
		options: {
			windowSize: 0,
			contentSize: 0,
			scrollPosition: 0,
			vertical: false,
			scrollScale: 0,
			id: "sb"
			},
		_create: function() {
			var self = this ;  
			var o = self.options ;  
			var el = self.element ;	
			
			o.tbid = o.id + "tb";
			
			// creation code for mywidget
			// can use this.options
			htmlStr = "<div class=\"fcScrollbarThumb fcFill\" id=\"" + o.tbid + "\"></div>";
			el.append(htmlStr);
    		var scrollThumb = el.find("#"+o.tbid).fcScrollbarThumb({
				vertical: o.vertical
			});  
			
			el.on({
				'mousedown': function(e,ui) {
					this.clickPoint = el.offset()['left'];
					var thumbSize = (self.options.vertical ? el.height() : el.width() ) * (o.windowSize/o.contentSize);
					var movement =( self.options.vertical ? e.pageY : e.pageX) -  this.clickPoint - (thumbSize/2)   ;
					var position = (o.scrollScale * movement);
					self._move(e, position);
				}
			});

    		el.find(".fcScrollbarThumb").on({
				'fcscrollbarthumbstart': function(e) {
					o.startingMouse = self.options.vertical ? e.originalEvent.pageY : e.originalEvent.pageX; 
					o.startingScroll = o.scrollPosition; 
				},
				'fcscrollbarthumbdrag': function(e) { 
					var movement = ( self.options.vertical ? e.originalEvent.pageY : e.originalEvent.pageX) - o.startingMouse;
					var position = o.startingScroll + (o.scrollScale * movement);
					self._move(e, position);
				}
			});  
			
			el.hide();
			
		},
		_init: function(){ this._mouseInit(); this.refresh(); },
		_mouseStart: function(e){ this.element.find("#"+o.tbid).fcScrollbarThumb("start",e) },
		_mouseDrag: function(e){ this.element.find("#"+o.tbid).fcScrollbarThumb("drag",e) },
		_mouseStop: function(e){ this.element.find("#"+o.tbid).fcScrollbarThumb("stop",e) },
		_move: function(e,position) { 
			var self = this ;  
			var o = self.options ;  
			o.scrollPosition = position;
			if (o.scrollPosition < 0) { o.scrollPosition = 0 }
			if (o.scrollPosition > (o.contentSize - o.windowSize)) { o.scrollPosition = (o.contentSize - o.windowSize) }
			self._trigger('scroll',e,{scrollPosition: o.scrollPosition});
			self.refresh();
		},
		refresh: function() {
			var self = this ;  
			var o = self.options ;  
			var el = self.element ;	
			// internal functions should be named with a leading underscore
			// manipulate the widget
			var thumbSize = o.windowSize/o.contentSize * 100 ;
			if (thumbSize >= 100){ el.fadeOut(150);
			} else { el.fadeIn(150)
			}
			var thumbOffset = o.scrollPosition/o.contentSize * 100 ;
			if (o.vertical){
				el.find("DIV.fcScrollbarThumb").height(thumbSize + "%").css("top", (thumbOffset + "%"));
				o.scrollScale = o.contentSize/el.height();
			} else {
				el.find("DIV.fcScrollbarThumb").width(thumbSize + "%").css("left", (thumbOffset + "%"));
				o.scrollScale = o.contentSize/el.width();
			}
		},
		destroy: function() {
			$.Widget.prototype.destroy.apply(this, arguments); // default destroy
			// now do other stuff particular to this widget
		}
	 });		  

	//
	//  Scrollbar thumb widget
	//
	
	$.widget("ui.fcScrollbarThumb", $.ui.mouse, {
		// default options
		options: {
			vertical: true
			},
		_init: function() { this._mouseInit() },
		_mouseStart: function(e){
			this._trigger('start',e,{});
			this.element.addClass("fcScrollbarThumbActive");
		},
		_mouseDrag: function(e){ this._trigger('drag',e,{}); },
		_mouseStop: function(e){ this.element.removeClass("fcScrollbarThumbActive") },
		start: function(e) { this._mouseStart(e) },
		drag: function(e) { this._mouseDrag(e) },
		stop: function(e){ this._mouseStop(e) }
	 });		  
	$.widget("ui.fcWindows", $.ui.mouse, {
		// default options
		options: {
			},
		_init: function() { this._mouseInit() },
	 });		  


	//
	//  Zoom Viewer
	//

	$.widget("ui.fcZoom", $.ui.fcTouch, {
		// default options
		options: {
			'lowResImage' : "",
			'highResImage' : "",
			'zoomHeight' : 0,
			'zoomWidth' : 0,
			'zoomScale' : 0,
			'zoomPadding' : 25,
			'fcLeft' : 0,
			'fcTop' : 0,
			'fcWidth' : 0,
			'fcHeight' : 0,
			'mouseWheelScale' : 1,
			'mouseX': 0,
			'mouseY' : 0,
			'fadeSpeed' : 200,
			'photoData' : {},
			id: "zw"
		},
		_create: function() { 
			var self = this ;  
			var o = self.options ;  
			var el = self.element ;	
			
			o.bgid = o.id + "bg"; //zoom background ID
			o.coid = o.id + "co"; //zoom controls ID
			o.hrid = o.id + "hr"; //high Res image ID
			o.lrid = o.id + "lr"; //low Res image ID
			o.slid = o.id + "sl"; //zoom slider ID
			o.loid = o.id + "lo"; //zoom loading ID
			o.inid = o.id + "in"; //zoom in ID
			o.ouid = o.id + "ou"; //zoom out ID

			htmlStr = "<div class=\"fcZoomBackground\" id=\"" + o.bgid + "\"><img class=\"fcZoomWindowImageLowRes\" id=\"" + o.lrid + "\"><img class=\"fcZoomWindowImage \" id=\"" + o.hrid + "\"></div>";
			el.append(htmlStr);

			htmlStr = "<span class=\"fcZoomControls  ui-corner-all\" id=\"" + o.coid + "\"><div class=\"fcZoomLoading\" id=\"" + o.loid + "\"></div>";
			htmlStr = htmlStr + "<div class=\"fcZoomOut\" id=\"" + o.ouid + "\"></div>";
			htmlStr = htmlStr + "<div class=\"fcZoomSlider\" id=\"" + o.slid + "\"></div>";
			htmlStr = htmlStr + "<div class=\"fcZoomIn\" id=\"" + o.inid + "\"></div></span>"
			el.append(htmlStr);

			el.hide();
  			if (( mvIE > 0 ) && ( mvIE < 9.0 )) {
				el.find("#"+o.coid).addClass("ieBackground");
				el.find("#"+o.loid).addClass("fcLoadingGIF");
				el.find("#"+o.inid).addClass("ui-icon ui-icon-circle-zoomin");
				el.find("#"+o.ouid).addClass("ui-icon ui-icon-circle-zoomout");
			};

			if( navigator.userAgent.match(/iPad/i) ||
				navigator.userAgent.match(/iPhone/i) ||
				navigator.userAgent.match(/iPod/i) ){
				htmlStr = "<div class=\"fcZoomForeground\"></div>"
				el.find("#"+o.bgid).append(htmlStr);
			} 

			//
			// Zoom slider
			//
	    	zoomSlider = el.find("#"+o.slid).slider({
				value:50,
				slide: function(event, ui) {
					self._zoomScale(event, ui.value);
				},
				change: function(event, ui) {
					event.pageX = o.mouseX;
					event.pageY = o.mouseY;
					self._zoomScale(event, ui.value);
				},
				start: function(event,ui){
					el.unbind('mousemove.fcZoom');
				},
				stop: function(event,ui){
					el.bind('mousemove.fcZoom', function(event) {
						self._zoomView(event);
					});
				}
			});  
    		el.find("IMG").on('click.fcZoom', function(event) { 
				el.unbind('mousemove.fcZoom');
				var photoNumber=(o.currentPhoto)-1;
				el.fadeOut(o.fadeSpeed);
				event.preventDefault();
			});  
			el.on('mousewheel.fcZoom', function(event, delta) {
				var change = zoomSlider.slider("value") + (delta * o.mouseWheelScale);
				zoomSlider.slider("value",change);
				event.preventDefault();
			});

    		$(window).bind('resize.fcZoom', function(event) { 
				self._resize(event);
			});  
			el.bind('mousemove.fcGallery', function(event) {
				self._zoomView(event);
			});
		},
		_init: function() { 
			var self = this ;  
			var o = self.options ;  
			var el = self.element ;		
			o.fcHeight = el.height();
			o.fcWidth = el.width();
			o.fcTop = el.offset()['top'];
			o.fcLeft = el.offset()['left'];
			this._touchInit(); 
		},
		_touchStart: function(event) { 
//			console.info("_touchStart");
			var self = this ;  
			var o = self.options ;  
			var allTouches = event.originalEvent.touches ;
			mouseStartX = (allTouches.length>1) ?((allTouches[0].pageX+allTouches[1].pageX)/2) :  allTouches[0].pageX ;
			mouseStartY = (allTouches.length>1) ?((allTouches[0].pageY+allTouches[1].pageY)/2) :  allTouches[0].pageY ;
			self._touchMove(event);
//			event.preventDefault();
		},
		_touchMove: function(event) { 
//			console.info("touchMove");
			var self = this ;  
			var o = self.options ;  
			var el = self.element ;		
			var allTouches;
			if (event.originalEvent.touches){ 
				allTouches = event.originalEvent.touches 
			} else {
				return false;
			};
			var pageX = (allTouches.length>1) ?((allTouches[0].pageX+allTouches[1].pageX)/2) :  allTouches[0].pageX ;
			var pageY = (allTouches.length>1) ?((allTouches[0].pageY+allTouches[1].pageY)/2) :  allTouches[0].pageY ;
			dX = pageX - mouseStartX ;
			dY = pageY - mouseStartY ;
			event.pageX = lastX - dX; 
			event.pageY = lastY - dY;
			var left = el.offset().left;
			var top = el.offset().top;
			if (event.pageX < left) {event.pageX = left; lastX = left };
			if (event.pageY < top) {event.pageY = top; lastY = top };
			if (event.pageX > (left+el.width())) {event.pageX = left+el.width(); lastX = left + el.width() };
			if (event.pageY > (top+el.height())) {event.pageY = top+el.height(); lastY = top + el.height() };
//			document.title = allTouches.length + ":" + lastX + " " + mouseStartX + " " + dX + " " + lastY + " " + mouseStartY + " " + dY;
			self._zoomView(event);
			event.preventDefault();
		},
		_touchEnd: function(event) { 
//			console.info("touchEnd");
			var self = this ;  
			var o = self.options ;  
			lastX = lastX - dX;
			lastY = lastY - dY;
			var allTouches = event.originalEvent.touches ;
			if (allTouches.length>0){
				mouseStartX = (allTouches.length>1) ?((allTouches[0].pageX+allTouches[1].pageX)/2) :  allTouches[0].pageX ;
				mouseStartY = (allTouches.length>1) ?((allTouches[0].pageY+allTouches[1].pageY)/2) :  allTouches[0].pageY ;
			}
//			event.preventDefault();
		},
		_gestureStart: function(event) { 
//			console.info("gestureStart");
			zoomStart = zoomSlider.slider("value") ;
			event.preventDefault();
		},
		_gestureChange: function(event) { 
			var self = this ;  
//			console.info("gestureChange");
			event.preventDefault();
			var change = (zoomStart + 1) * event.originalEvent.scale - 1;
			zoomSlider.slider("value",change);
			self._touchMove(event);
		},
		_gestureEnd: function(event) { 
			event.preventDefault();
		},
		_singleTap: function(event) { 
			var self = this ;  
			var o = self.options ;  
			var el = self.element ;	
			el.unbind('mousemove.fcZoom');
			var photoNumber=(o.currentPhoto)-1;
			el.fadeOut(o.fadeSpeed);
			event.preventDefault();
		},
		_zoomView: function(event) {
			var self = this ;  
			var o = self.options ;  
			var el = self.element ;	
			o.mouseX = event.pageX;
			o.mouseY = event.pageY;
			o.fcTop = el.offset()['top'];
			o.fcLeft = el.offset()['left'];
			
			var offsetX, offsetY;
			offsetX = event.pageX - o.fcLeft - o.fcWidth/2 ;
			offsetY = event.pageY - o.fcTop - o.fcHeight/2 ;
			var marginX = -o.zoomWidth/2 + o.zoomPadding - (offsetX*(o.zoomScale-1)); 
			var marginY = -o.zoomHeight/2 + o.zoomPadding - (offsetY*(o.zoomScale-1)) ;
			el.find("#"+o.bgid).css("left",o.fcWidth/2+marginX).css("top",o.fcHeight/2+marginY);
		},

		_zoomScale: function(event,scaleValue) {
			var self = this ;  
			var o = self.options ;  
			var el = self.element ;	
	
			o.zoomWidth = o.imageWidth + (scaleValue/100 * ((o.photoData.originalWidth) -  (o.imageWidth) )) + (2*o.zoomPadding);
			o.zoomHeight = o.imageHeight + (scaleValue/100 * ((o.photoData.originalHeight) - (o.imageHeight) )) + (2*o.zoomPadding);
			o.zoomScale = (o.zoomWidth-2*o.zoomPadding)/o.imageWidth;
			
			var xScale = (o.zoomWidth-2*o.zoomPadding)/(o.photoData.originalWidth);
			var yScale = (o.zoomHeight-2*o.zoomPadding)/(o.photoData.originalHeight);

			el.find("#"+o.bgid).width(o.zoomWidth-2*o.zoomPadding).height(o.zoomHeight-2*o.zoomPadding);
			el.find("#"+o.lrid).width(o.zoomWidth-2*o.zoomPadding).height(o.zoomHeight-2*o.zoomPadding);
			el.find("#"+o.hrid).width(o.zoomWidth-2*o.zoomPadding).height(o.zoomHeight-2*o.zoomPadding);
			self._zoomView(event);
		},
		_resize: function(event) { 
			var self = this ;  
			var o = self.options ;  
			var el = self.element ;	
			var oldHeight = o.fcHeight;
			var oldWidth = o.fcWidth;
			o.fcTop = el.offset()['top'];
			o.fcLeft = el.offset()['left'];
//			console.info(o.mvTop, o.mvLeft, el.width(), el.position(), el.offset());
			o.fcHeight = el.height();
			o.fcWidth = el.width();
			var	height = el.height() - 2*o.zoomPadding;
			var	width = el.width() - 2*o.zoomPadding;
			var	boxRatio = height/width;
			var boxScale;
			var	naturalWidth = o.photoData.originalWidth;
			var	naturalHeight = o.photoData.originalHeight;
			var	naturalRatio = naturalHeight/naturalWidth;
			if (naturalRatio > boxRatio){
				boxScale = naturalHeight/height;
				width= naturalWidth/boxScale;
			} else {
				boxScale = naturalHeight/width/naturalRatio;
				height= naturalHeight/boxScale;
			};
			o.imageWidth = width ;
			o.imageHeight = height ;
			event.pageX = o.mouseX + (o.fcWidth - oldWidth);
			event.pageY = o.mouseY + (o.fcHeight - oldHeight);
			
			self._zoomScale(event, el.find("#"+o.slid).slider("value"));
		},
		resize: function(event) { 
			this._resize(event);
		},
		getPhoto: function(info) {
			var self = this ;  
			var o = self.options ;  
			var el = self.element ;
			var em = el.find("#"+o.hrid);
			var pic = new Image();
			pic.src = info.originalURL;
			o.photoData = info;
			el.find("#"+o.lrid).attr('src',info.URL).width(info.originalWidth).height(info.originalHeight).show();
			el.find("#"+o.bgid).width(info.originalWidth).height(info.originalHeight).show();
			el.find("#"+o.loid).show();
			if( navigator.userAgent.match(/iPad/i) ||
				navigator.userAgent.match(/iPhone/i) ||
				navigator.userAgent.match(/iPod/i) ){
				el.find("#"+o.loid).hide();
//				el.find(".mvZoomForeground").width(photoData.originalWidth)
//					.height(photoData.originalHeight)
//					.css("background-image", 'url(' + photoData.originalURL + ')').show();
			} else {
				
					em.width(info.originalWidth).height(info.originalHeight).hide()
					.one('load', function() {
						em.fadeIn(o.fadeSpeed, function() {
							el.find("#"+o.lrid).hide();	
							el.find("#"+o.loid).hide();
							delete pic;
						});
					}).attr('src', info.originalURL).each(function() {
						if (pic.complete) $(this).trigger('load');
					});
				};

		},
		show: function(event, info) {
			var self = this ;  
			var o = self.options ;  
			var el = self.element ;		
			o.fcTop = el.offset()['top'];
			o.fcLeft = el.offset()['left'];
			self.getPhoto(info);
			o.mouseX = event.originalEvent.pageX;
			o.mouseY = event.originalEvent.pageY;
			lastX = event.originalEvent.pageX ;
			lastY = event.originalEvent.pageY ;
			self._resize(event);

			el.fadeIn(o.fadeSpeed);
		}
	 });		  

	//
	//  Move Elements into Layout Area
	//
	
	$.widget("ui.fcPageLayout", {
		// default options
		options: {
			},
		_create: function() {
			var self = this ;  
			var o = self.options ;  
			var el = self.element ;	
			o.bodyContent = el.find("DIV.BodyContent");
			self._replace("DIV.fcIntroduction", "DIV.Intro", "Introduction");
			self._replace("DIV.fcAuthors", "DIV.Byline", "Byline");
			self._replace("DIV.fcTitle", "DIV.Headline", "Headline");
		},
		_replace: function(sourceTag, targetTag, classIdentifier) {
			var self = this ;  
			var o = self.options ;  
			var el = self.element ;	
			var source = el.find(sourceTag).detach();
			var target = o.bodyContent.find(targetTag);
			if (target.length==0){
				source.addClass(classIdentifier).prependTo(o.bodyContent);
			} else {
				target.html(source.html());
				source.remove();
			}
		}
	 });		  

	$.widget("ui.fcListLayout", {
		// default options
		options: {
			fsid : "fsb", //fashion show block ID
			csid : "cat", //category block ID
			id : "ls"  //list
			},
		_create: function() {
			var self = this ;  
			var o = self.options ;  
			var el = self.element ;	
			
			el.find("DIV.listRow").each( function(){
				o.listRow = $(this);
				o.listSummary = o.listRow.find("DIV.listSummary");
				o.listSummary.wrapInner("<div class=\"Summary\"></div>");
				self._replace("UL.PostDetails", "UL.PostDetails", "PostDetails");
				self._replace("H3.listHeadline", "H3.Headline", "Headline");
				o.listSummary.wrapInner("<div class=\"Foreground\"></div>");
				var anchorImageBox = o.listSummary.find("DIV.AnchorImageBox").detach();
				anchorImageBox.prependTo(o.listSummary);
			});
			
			if (el.hasClass("FashionShows")) {
				var blockID = o.id + o.fsid;
				var fashionShowScrollList = el.find("DIV.BlockContent").attr("id",blockID).css("height","240px").fcScrollList({
					id : blockID  //Fashion Shows Block
				});
			}

			if (el.hasClass("fcCat")) {
				var blockID = o.id + o.csid;
				var categoriesScrollList = el.find("DIV.BlockContent").attr("id",blockID).css("height","480px").fcScrollList({
					id : blockID  //Fashion Shows Block
				});
			}

//			var scrollList = el.scrollList();
			
		},
		_replace: function(sourceTag, targetTag, classIdentifier) {
			var self = this ;  
			var o = self.options ;  
			var el = self.element ;	
			var source = o.listRow.find(sourceTag).detach();
			var target = o.listSummary.find(targetTag);
			if (target.length==0){
				source.addClass(classIdentifier).prependTo(o.listSummary);
			} else {
				target.html(source.html());
				source.remove();
			}
		}
	 });		  

	$.widget("ui.fcScrollList", {
		// default options
		options: {
			id: "sl",  //Scroll list
			orientation: "horizontal",


			rows: 1,
			columns: 1,
			thumbnailMaxHeight: 0,
			thumbnailMaxWidth: 0,
			thumbnailMinHeight: 100000,
			thumbnailMinWidth: 100000,
			thumbFit : true,
			thumbFitHeight : 100,
			thumbFitWidth : 100,
			gridded: false,
			currentPhoto: 0,
			buttonSize: 30,
			first: false,
			last: false,
			isid: "is",  //iScroll ID
			webkitScrollBarWidth: 15,
			scrollBarWidth: 25,


		},
		_create: function() {
			var self = this ;  
			var o = self.options ;  
			var el = self.element ;	
			
			o.isid = o.id+"is"; //iSCroll/ScrollWindow ID
			o.drid = o.id+"dr";
			o.tnid = o.id+"tn"; //Thumbnail ID
	
			var listParent = el.find("DIV.tplrepeat-parent").attr("id",o.drid);
			

			var row = listParent.find( ">DIV.listRow").each(function(index) {
				$(this).attr("id", o.tnid + index)
			});


			var htmlStr;
			htmlStr = "<div class=\"fcScrollWindow\" id=\"" + o.isid + "\"></div>"
			el.wrapInner(htmlStr);


			o.ppid = o.id+"pp";  //next page ID
			o.npid = o.id+"np";  //next page ID
			htmlStr = "<div class=\"fcButton fcPreviousPage ui-corner-left\" id=\"" + o.ppid + "\"><div class=\"fcCenter fcPreviousPageButton\"></div></div>"
			el.append(htmlStr);
			el.find(">#"+o.ppid).on({
				'click': function(event){
					self._previousPage();
				}
			});

			htmlStr = "<div class=\"fcButton fcNextPage ui-corner-right\" id=\"" + o.npid + "\"><div class=\"fcCenter fcNextPageButton\"></div></div>"
			el.append(htmlStr);
			el.find(">#"+o.npid).on({
				'click': function(event){
					self._nextPage();
				}
			});
		
		},
		_init: function() {
			var self = this ;  
			var o = self.options ;  
			var el = self.element ;	
			var vertical;
			var scale;
			
			switch (o.orientation){
				case "horizontal":
					el.removeClass("fcVerticalThumbnails fcGriddedThumbnails").addClass("fcHorizontalThumbnails");
					vertical = false;
					o.thumbFitHeight = el.height();
					break;
				case "vertical":
					el.removeClass("fcHorizontalThumbnails fcGriddedThumbnails").addClass("fcVerticalThumbnails");
					vertical = true;
					o.thumbFitWidth = el.width();
					break;
				case "gridded":
					vertical = false;
					el.removeClass("fcVerticalThumbnails fcHorizontalThumbnails").addClass("fcGriddedThumbnails");
					break;
				default:
					vertical = false;
					el.removeClass("fcVerticalThumbnails fcHorizontalThumbnails fcGriddedThumbnails");
			};
			
			o.vertical = vertical;
			

			o.thumbnailMaxHeight = 0;
			o.thumbnailMaxWidth = 0;
			el.find("DIV.listRow").each(function() {
				thisHeight = $(this).outerHeight(true);
				thisWidth = $(this).outerWidth(true);
				if (thisHeight > o.thumbnailMaxHeight) {
					o.thumbnailMaxHeight = thisHeight;
				}
				if (thisWidth > o.thumbnailMaxWidth) {
					o.thumbnailMaxWidth = thisWidth;
				}
				if (thisHeight < o.thumbnailMinHeight) {
					o.thumbnailMinHeight = thisHeight;
				}
				if (thisWidth < o.thumbnailMinWidth) {
					o.thumbnailMinWidth = thisWidth;
				}

			});
			
			
			if (o.orientation == "gridded"){
				var i = 0, j = 0, k = 0;
					for (i = 0;( i < (o.rows)); i++) {
						for (j = 0;( j < (o.columns)); j++) {
							el.find("#"+o.tnid + k).css("left",j * o.thumbnailMaxWidth).css("top",i*o.thumbnailMaxHeight);
							k++;
						};
					};
					el.find("#"+o.drid).width(o.columns * o.thumbnailMaxWidth).height(o.rows * o.thumbnailMaxHeight);
			} else {
				var k = 0;
				var thumbnailsHeight = 0, thumbnailsWidth = 0;
				el.find("DIV.listRow").each(function() {
					thisHeight = $(this).outerHeight(true);
					thisWidth = $(this).outerWidth(true);
					if (vertical) {
						el.find("#"+o.tnid + k).css("left",0).css("top",thumbnailsHeight);
						thumbnailsHeight = thumbnailsHeight + thisHeight;
					} else {
						el.find("#"+o.tnid + k).css("left",thumbnailsWidth).css("top",0);
						thumbnailsWidth = thumbnailsWidth + thisWidth;
					}
					k++;
				});
				if (vertical) {
					el.find("#"+o.drid).width(o.thumbnailMaxWidth).height(thumbnailsHeight);
				} else {
					el.find("#"+o.drid).width(thumbnailsWidth).height(o.thumbnailMaxHeight);
				}
			};			

			var scrollWindow = el.find("#"+o.isid);


			if(vertical) {
				scrollWindow.removeClass('fcHScroll').addClass('fcVScroll');
			}else{
				scrollWindow.removeClass('fcVScroll').addClass('fcHScroll');
				};


			if( navigator.userAgent.match(/Android/i) ||
				navigator.userAgent.match(/webOS/i) ||
				navigator.userAgent.match(/iPhone/i) ||
				navigator.userAgent.match(/iPad/i) ||
				navigator.userAgent.match(/iPod/i) ){
//				el.find(".fcScrollWindow").addClass('mvScrollWindow');
				var myScroll = new iScroll(o.isid, 
					{ 
						snap: 'DIV.listRow', 
						vScroll: vertical ,  
						bounce: false , 
						onScrollMove: function() { self._move() }
					}
				);
			} else if ($.browser.webkit){
				scrollWindow.unbind("scroll").css('overflow','auto');
				if(vertical) {
					scrollWindow.width(o.thumbFitWidth+o.webkitScrollBarWidth).height('auto')
				}else{
					scrollWindow.height(o.thumbFitHeight+o.webkitScrollBarWidth).width('auto')
				};
			} else {
	   	 		var newScrollWindow = scrollWindow.fcScrollWindow({
					vertical: vertical, 
					scrollBarWidth:o.scrollBarWidth,
					drid: o.drid,
					id: o.isid
					}); 
			};
			scrollWindow.on("scroll",function(event) { self._move(); });
			this._move();
	//		this._mouseInit() ;

		},

		_scrollTo: function(x) {
			var self = this ;  
			var o = self.options ;  
			var el = self.element ;	
			if (x<0) {x=0};
			if( navigator.userAgent.match(/Android/i) ||
				navigator.userAgent.match(/webOS/i) ||
				navigator.userAgent.match(/iPhone/i) ||
				navigator.userAgent.match(/iPad/i) ||
				navigator.userAgent.match(/iPod/i) ){
//				el.find(".fcScrollWindow").scrollTo(x,0,200);
			} else {
				if (o.orientation == 'vertical'){
					el.find("#"+o.isid).scrollTo(x,200,{axis:'y'});
				} else {
					el.find("#"+o.isid).scrollTo(x,200,{axis:'x'});
				}
			}
		},
		_move: function() { 
			var self = this ;  
			var o = self.options ;  
			var el = self.element ;
			var scrollWindow = el.find("#"+o.isid);
			if ((o.vertical ?  scrollWindow.scrollTop() : scrollWindow.scrollLeft())<=0){
				if ( o.first == false){
					o.first = true;
					el.find("#"+o.ppid).fadeOut(o.fadeSpeed);				
			};
			} else {
				if ( o.first == true){
					o.first = false;
					el.find("#"+o.ppid).fadeIn(o.fadeSpeed);				
				};
			};
			if ( (o.vertical ? 
				  (scrollWindow.scrollTop() + scrollWindow.height() ) : 
				  (scrollWindow.scrollLeft() + scrollWindow.width()) + 1) >= 
				((o.vertical ? scrollWindow.find("#"+o.drid).height() : 
				  scrollWindow.find("#"+o.drid).width() ) 
				 + o.scrollBarWidth )) {
				if ( o.last == false){
					o.last = true;
					el.find("#"+o.npid).fadeOut(o.fadeSpeed);				
				};
			} else {
				if ( o.last == true){
					o.last = false;
					el.find("#"+o.npid).fadeIn(o.fadeSpeed);				
				};
			};
		},
		_nextPage: function() {
			var self = this ;  
			var o = self.options ;  
			var el = self.element ;	
			var scrollWindow = el.find("#"+o.isid);
			var loc ;
			if (o.vertical){ 
				loc = scrollWindow.scrollTop()+scrollWindow.height();
			} else {
				loc = scrollWindow.scrollLeft()+scrollWindow.width();
			};
			self._scrollTo(loc);
		},
		_previousPage: function() {
			var self = this ;  
			var o = self.options ;  
			var el = self.element ;	
			var loc ;
			var scrollWindow = el.find("#"+o.isid);
			if (o.vertical){ 
				loc = scrollWindow.scrollTop()-scrollWindow.height();
			} else {
				loc = scrollWindow.scrollLeft()-scrollWindow.width();
			};
			self._scrollTo(loc);
		}


	});		  



})(jQuery);  
