/*
Title:      jQuery.timothysaccenti
Description:New site for Timothy Saccenti by Build / Antenna
Developer:  Antenna Praxis (http://theantenna.net)
Released:   Feb 2010
Updated:    Mar 2010
Version:    0.0.2
License:    Not licensed for public or commercial use.
Usage:      
Notes:      			
To Do:      
Structure:  Nav
            Subnav
             - Thumbreel
             - List
               - sorting
            Viewer
             - Stills
             - Video
             - info
*/

(function($){
	
	$.timothysaccenti = $.extend(function(options){
		
		return $.timothysaccenti.init.call( this, options );
		
	},
	{
		init:function(options)
		{
			var that = this;
			
			$.timothysaccenti.fonts();
			$.timothysaccenti[!IS_IPHONE?'searchForm':'searchFormIphone']();
			
			// hide address bar
			if(IS_IPHONE) $(window).load(function(){
				setTimeout(function(){window.scrollTo(0, 1);},200);
			});
			
			if(IS_HOME ) $.timothysaccenti.intro();
			
			// nav fading
			if(IS_HOME || IS_SINGLE || IS_ERROR) return $.fadeGroup('.cat-item, .page_item','*');
			else // fade in/out non-active nav items
			{
				$('#header .cat-item:not(.current-cat), #header .page_item:not(.current_page_item)').addClass('inactive');
				$('.inactive').live('mouseenter',function(e){
					$(this).stop(true).animate({opacity:1});
				}).live('mouseleave',function(e){
					$(this).stop(true).animate({opacity:0.5});
				})
			}
			
			// build all modules
			$.each($.timothysaccenti.modules,function(i){
				var module = this.call(that,options);
			});
			
			$.timothysaccenti.rebootFromHash();
			$.timothysaccenti.catchBackButton();
		},
		
		modules:{}, // extended
		
		shareLinks: 
		'<a class="send-a capsule" target="_blank" title="Email" href="mailto:?subject=Timothy Saccenti- %TITLE%&body=%LINK%">Email</a>'
		+'<a class="send-a capsule" target="_blank" title="Twitter"  href="http://twitter.com/home?status=Timothy+Saccenti-+%TITLE%+%LINK%">Twitter</a>'
		+'<a class="send-b capsule" title="Facebook" target="_blank" href="http://www.facebook.com/sharer.php?u=%LINK%&t=Timothy+Saccenti-+%TITLE%">Facebook</a>'
		+'<a class="send-c capsule" title="Tumblr" target="_blank" href="http://tumblr.com/share?v=3&u=%LINK%&t=Timothy+Saccenti-+%TITLE%&s=">Tumblr</a>'
		+'<a class="send-d capsule" title="Delicious" target="_blank" href="http://delicious.com/save?url=%LINK%&title=Timothy+Saccenti-+%TITLE%">Delicious</a><div class="clear"></div>',

		fonts:function(selector)
		{
			//if(!IS_IPHONE)
			Cufon.replace(selector||'.capsule, .page_item, .cat-item, #subnav span, h1', { fontFamily: 'MonoFat', hover: true });
		},
		
		searchForm:function()
		{
			// Input field with Cufon

			var $search = $('li.search'),
				loop, // interval
				$inp = $('#search-text'),
				otxt = '', // old txt 
			 	$b = $('#search-form b'),
				label = $b.text(),
				refresh = function(){
					$.timothysaccenti.fonts('#search-form>b');
					//Cufon.replace('#search-form>b', { fontFamily: 'MonoFat' }); 
				},
				update = function(e){

					if(e && e.keyCode)
					{
						//log( e.keyCode );

						if(e.keyCode == 8) // delete
						{
							log('del');
							$b.html( $('cufontext',$b).text().slice(1,-1) + '_'  );
						}
						else if(e.keyCode == 13) // enter = submit
						{
							$inp.val( $('cufontext',$b).text().slice(0,-1).toLowerCase() );

							if( $inp.val() =="" ) e.preventDefault(); // no search string : dont submit

							else // submit
							{
								clearInterval(loop);
								return; //$('#search-form').submit();
							}
						}

						$inp.val('');

						refresh();
					}

					if( otxt == $inp.val() ) return; // input not changed

					else // update input
					{
						$b.html( 

							($inp.val().length>1? '' : $('cufontext',$b).text().slice(0,-1) ) + 
								$inp.val().toUpperCase() + '_'

						);

						$inp.val('');

						refresh();

						otxt = '';
					} 

				};


			//set up previous value
			if ( $inp.val() ) $b.html( $inp.val()+'_'  );
			
			// make empty width balance with SEARCH_
			$search.css({minWidth:$search.width()-10}); 

			$inp.val('').focus(function(){

				$inp.val('');
				$search.toggleClass('active');

				if( $('cufontext', $b).text() == label )
				{
					$b.html('_'); // clear label
					refresh();
				}

				loop = setInterval(update,50);

			}).blur(function(e){

				//log('finished');
				clearInterval(loop);

				$search.toggleClass('active');

				if( $('cufontext', $b).text() == "_" ){ // reset label
					$b.html(label);
					refresh();
				}

			}).keydown(update);
			
		},
		
		searchFormIphone:function() // 
		{
			var $search = $('li.search'),
				$inp = $('#search-text'),
			 	$b = $('#search-form b'),
				label = $b.text();
			
			$b.click(function(e){
				$inp.show().focus();
				$b.hide();
			});
			
			$inp.val(label).focus(function(){
				
				$search.toggleClass('active');
				
				$inp.val('').width(150);
				
			}).blur(function(e){

				$search.toggleClass('active');
				
				$inp.width(70).hide();
				$b.show();
			});
		},
		
		intro: function()
		{	
			
			if( $('#demo').length )
			{
				// Rapid slideshow
				var c = 0,
					ready = 0,
					isPaused = false,
					group = $('#demo img'),
					onload = function(e){

						$(this).height($(window).height()).css('opacity',0.2);

						if(++ready == group.length) start();

					},
					start = function()
					{
						//log('start');
						$('#header').css({position:'relative',zIndex:2})
						$('#demo').css({width:'100%',position:'absolute',zIndex:1,top:0})

						setInterval(function(){

							group.eq(c).hide();

							c++;
							if(c>group.length-1) c = 0;

							if(!isPaused) group.eq(c).show();

						},75)
					};

				group.each(function(e){

					var $t = $(this).hide(); 

					if( !$t.height() ) $t.load(onload);

					else onload.call(this);

				});

				$('#header').mouseenter(function(){
					isPaused = true;
				}).mouseleave(function(){
					isPaused = false;
				});
			}
			else if( $('#homepage').length )
			{
				// homepage message
				
				$('body'+$.browser.msie?', html':'').css({'overflow':'hidden'});
				
				$(window).resize(function(e){
					
					// .. take all img out, place in bg.
					var $img = $('#homepage img'),
						$win = $(window),
						winD = {w:$win.width(), h:$win.height()},
						imgD = {w:$img.width(), h:$img.height()},
						imgRatio = imgD.w / imgD.h,
						winRatio = winD.w / winD.h,
						newD = ( imgRatio < winRatio ) ?
							{w:winD.w, h:imgD.h * (winD.w/imgD.w) } // fit to winW
							: {w:imgD.w * (winD.h/imgD.h), h:winD.h }; // fit to winH
											
					// .. append centred
					$img.attr('width',newD.w).attr('height',newD.h).css({
						top: Math.round( ( winD.h - newD.h ) / 2 ),
						left: Math.round( ( winD.w - newD.w ) / 2 )
					}).addClass('bleed').remove().appendTo('#homepage').hide().fadeTo(1000,1); // .2

					Cufon.replace('#homepage', { fontFamily: 'MonoFat', hover: true });
					
				}).resize();
			
			}
			
		}
		
		
	}, net.theantenna.utils.hashManager);
	
	
	
	
	
	
	
	
	
	
	
	
	$.extend($.timothysaccenti.modules,{

		subnav:$.extend(function(options){

			return $.timothysaccenti.modules.subnav.init.call($('#subnav'),options); // all functions called from scope this= $(#subnav)

		},{
			init : function(options){

				var that = this;

				// page has no subnav - stop here
				if(IS_PAGE) return $.timothysaccenti.modules.viewer.view.info.call(this);

				// get view from cookie 
				var view = $.cookie('timothysaccenti.listMode') == 1? 'list' : 'thumbreel';
				$.timothysaccenti.modules.subnav.view[view].init.call(this);

				// toggling the views
				$('#listmodeon').click(function(e){
					e.preventDefault();
					if( $('#subnav').hasClass('list')) return;
					$.timothysaccenti.modules.subnav.view.list.init.call(that);
					$.cookie('timothysaccenti.listMode',1,{path:"/"});
				});
				$('#listmodeoff').click(function(e){
					e.preventDefault();
					if( $('#subnav').hasClass('thumbreel')) return;
					$.timothysaccenti.modules.subnav.view.thumbreel.init.call(that);
					$.cookie('timothysaccenti.listMode',0,{path:"/"});
				});

				// sort nav
				$('#sortTitle').click(function(e){
					e.preventDefault();

					var f = function(a,b){
							return $(a).attr('name') > $(b).attr('name') ? 1 : -1;
						},
						group = $('#subnav-wrapper a').removeClass('b').sort(f);
						group.filter(':even').addClass('b');
						group.appendTo( $('#subnav-wrapper') );

						$.timothysaccenti.modules.subnav.view.list.addContent.call( that, group );
				});
				$('#sortYear').click(function(e){
					e.preventDefault();

					var f = function(a,b){
							return $(a).attr('rev') < $(b).attr('rev') ? 1 : -1;
						},
						group = $('#subnav-wrapper a').removeClass('b').sort(f);
						group.filter(':even').addClass('b');
						group.appendTo( $('#subnav-wrapper') );

						$.timothysaccenti.modules.subnav.view.list.addContent.call( that, group );
				});

				// subnav link actions (click = launch, rollover = fade)
				$('#subnav-wrapper a')
					.live('click', function(e){

						if(IS_IPHONE) window.scrollTo(0, 1);

						var $t = $(this);
						net.theantenna.utils.hashManager.updateHash($t.attr('href'));

						$('#subnav-wrapper a.active').removeClass('active');
						$t.blur().addClass('active');

						$.timothysaccenti.modules.viewer.launch.call(this,e);

						if( that.hasClass('thumbreel') ) // stop reel
						{
							$('#subnav-wrapper').slidescroll("option", "speed", 0);
						}
					})
					.live('mouseenter',function(e){

						if(IS_IPHONE || $('#subnav').hasClass('list')) return;

						var $t = $(this);

						// show title
						$('#subnav-title').html($t.attr('name')).show();
						$.timothysaccenti.fonts('#subnav-title');

						// fade in this thumb, fade out all others
						$t.stop(true).animate({opacity:1});
						$('#subnav-wrapper a').not(this).stop(true).animate({opacity: 0.7
							//$('#viewer').hasClass('launched stills')? 0.7 : 0.2
							});

					}).live('mouseleave',function(e){

						if(IS_IPHONE || $('#subnav').hasClass('list')) return;

						// hide subnav title
						$('#subnav-title').hide();

						// fade out inactive thumbs, fade in active thumb
						$('#subnav-wrapper a:not(.active)').stop(true).animate({opacity:
							$('#viewer').hasClass('launched')? 0.2 : 1
						});
						$('#subnav-wrapper a.active').stop(true).animate({opacity:1});
					});

				$('#subnav-title').hide();

				// load more if present
				$.timothysaccenti.modules.subnav.load.call( this, $('#more a').attr('href') );

				return this.data('timothysaccenti.subnav',{view:view}) // the state
			},

			// loading more content inline
			load: function(url)
			{
				if(!url) return;

				var that = this;

				// ajax more data
				$.ajax({
					dataType:'HTML',
					type:'GET', data: url.indexOf('inline=true')<0? {inline:true}:{},
					url:url,
					success:function(data){

						var $data = $('<div>').html(data),
							$content = $('#subnav-wrapper a',$data),
							nxtPageUrl = $('#more a',$data).attr('href');

						//log($data,$content,nxtPageUrl);

						$content.appendTo('#subnav-wrapper');

						var view = that.hasClass('list') ? 'list' : 'thumbreel'; // that.data('timothysaccenti.subnav').view
						$.timothysaccenti.modules.subnav.view[view].addContent.call(that,$content);

						$.timothysaccenti.fonts('#subnav .capsule');

						// if permalink was in $content, need to call here - hacky
						$content.filter('a[href='+location.hash.toString()+']').click().mouseleave();

						$.timothysaccenti.modules.subnav.load.call( that, nxtPageUrl );
					}
				});
			},


			view:
			{
				thumbreel:
				{
					init:function()
					{
						this.addClass('thumbreel').removeClass('list');

						$('#listmodeon').addClass('inactive');
						$('#listmodeoff').removeClass('inactive');

						$('#info-marker').insertBefore('#subnav');

						$('#subnav img').show(); // make sure imgs are shown for calculating widths

						$.timothysaccenti.modules.subnav.view.thumbreel.addContent.call(this,$('#subnav a'));

						$('#sortNav').hide();
						//$('#subnav-title').show();

						// ---------------------------------------------------------------------------

						if(IS_IPHONE){ // no scrolling of reel
							$('#subnav-left, #subnav-right').addClass('deactivated').click(function(e){ e.preventDefault(); });
							return;
						}

						// start the thumbs slidescroll
						$('#subnav-wrapper').slidescroll('destroy').slidescroll({selector:'a',direction:-1}).slidescroll('start')

						$('#subnav-left, #subnav-right')[!$('#subnav-wrapper').slidescroll('hasContent')?'addClass':'removeClass']('deactivated');//.hide(); 

						// scrolling the thumb slides
						this.mouseenter(function(e){
								if( !$("#subnav-wrapper").slidescroll("isStarted") )
								{
									//$(this).addClass('stopped');
									$("#subnav-wrapper").slidescroll("start")
								}

							})
							.mouseleave(function(e){

								$("#subnav-wrapper")
									.slidescroll("option", "speed", $('#viewer').hasClass('launched video') ? 0 : 1)
									.slidescroll("option", "direction", -1)

							});

						$('#subnav-right')
							.click(function(e){ e.preventDefault(); })
							.mouseover(function(e){
								$('#subnav-wrapper a').stop(true).animate({opacity:1});
								$("#subnav-wrapper").slidescroll("option", "speed", 0);
							}).mouseleave(function(e){
								$('#subnav-wrapper a.active').mouseleave();
								if ( !$('#viewer').hasClass('launched video') ) $("#subnav-wrapper")
									.slidescroll("option", "speed", 1)
									.slidescroll("option", "direction", -1);
							})
							.mousedown(function(e){
								$("#subnav-wrapper")
									.slidescroll("option", "direction", 1)
									.slidescroll("option", "speed", 10);
							}).mouseup(function(e){
								$("#subnav-wrapper")
									.slidescroll("option", "speed", 0);
							});

						$('#subnav-left')
							.click(function(e){ e.preventDefault(); })
							.mouseover(function(e){
								$('#subnav-wrapper a').stop(true).animate({opacity:1});
								$("#subnav-wrapper")
									.slidescroll("option", "speed", 0);
							}).mouseleave(function(e){
								$('#subnav-wrapper a.active').mouseleave();
								if ( !$('#viewer').hasClass('launched video') ) $("#subnav-wrapper")
									.slidescroll("option", "speed", 1)
									.slidescroll("option", "direction", -1)
							})
							.mousedown(function(e){
								$("#subnav-wrapper")
									.slidescroll("option", "speed", 10)
									.slidescroll("option", "direction", -1)
							}).mouseup(function(e){
								$("#subnav-wrapper")
									.slidescroll("option", "speed", 0);
							});

						//darken thumbreel if switching from list to reel
						if( $('#viewer').hasClass('launched') )
							$('#subnav-wrapper a.active').mouseleave();

						this.stop().height(0).animate({height:125},{duration:1000});
					},

					addContent:function($content)
					{
						var w = this.width(),
							opacity = $('#viewer').hasClass('launched')? 0.2 : 1;

						$content.each(function(i)
						{
							var $t = $(this).removeClass('capsule').removeClass('b')
									.css('display','inline-block').fadeTo('slow',opacity),
								$img = $('img',$t);

							$t.css({width:$img.width(),height:$img.height()})	
							$('span',$t).show();
							$('b', $t).hide();

							// fade img in onload - not if imgs to left of screen
							// & no need if content launched due to mouseout event
							if($img.offset().left>w || opacity==.2) return true;

							$img.animate({opacity:0},{duration:0})
								.delay(i*200).animate({opacity:opacity},{duration:'slow'});							

						});

						this.stop().animate({height:125});
					}
				},

				list:
				{
					init: function(){
						$('#listmodeon').removeClass('inactive');
						$('#listmodeoff').addClass('inactive');

						$('#info-marker').insertBefore('#viewer');

						// teardown thumbreel
						$('#subnav-wrapper').slidescroll('destroy').css({left:0});
						$('#subnav').removeClass('thumbreel').addClass('list').add('#subnav-right,#subnav-left').unbind();

						$.timothysaccenti.modules.subnav.view.list.addContent.call( this, $('#subnav a') );

						$.timothysaccenti.fonts('#subnav .capsule');

						$('#sortNav').show();
						$('#subnav-title').hide();
						$('#subnav-left, #subnav-right').addClass('deactivated').click(function(e){ e.preventDefault(); });

					},

					addContent:function($content)
					{
						var ch = this.height(); // current height

						$content.each(function(i){
							var $t = $(this).addClass('capsule').css({width:'auto',height:'auto',opacity:1});
							$('img,span',$t).hide();
							$('b', $t).show();
						});

						$content.filter(':even').addClass('b');

						var nh = this.css('height','auto').height(); // new height

						//this.height(ch).stop().animate({height:nh}); // transition height

						// stagger fade in content
						$content.each(function(i){
							var $t = $(this);
							$t.hide().delay(i*25).fadeIn('def');
						});

					}
				}

			}

		})

	});
	
	
	
	
	
	
	
	
	
	
	$.extend($.timothysaccenti.modules,{

		viewer:$.extend(function(options){

			return $.timothysaccenti.modules.viewer.init.call(this,options);

		},{
			init : function(options)
			{

				$('#info').live('click', function(e){
					e.preventDefault();
					if($(this).hasClass('deactivated')) return;
					if(IS_IPHONE) window.scrollTo(0, 1);
					$('#info-overlay .bg').height( $('#info-overlay').height() ); // $('body').outerHeight(true)
					$('#info-overlay').fadeIn().find('h1').css('top',$('#info-marker').offset().top);
				});
				$('#overlay-close, #info-overlay .bg').live('click',function(e){
					e.preventDefault();

					// scroll to top
					$('html,body').stop(true).animate({scrollTop:0});

					$('#info-overlay').fadeOut();
				});

				// Embed fsviewer swf
				swfobject.embedSWF(
					TEMPLATE_DIR+"/swf/timothysaccenti.viewer.swf", 'fsviewer',
					"100%", "100%",
					"9.0.45", TEMPLATE_DIR+"/swf/expressInstall.swf",
					null,
					{bgcolor:"000000", allowFullScreen:"true", allowScriptAccess:"sameDomain", wmode:"transparent"}

				);

				$(net.theantenna.fsviewer.id).bind('mouseenter.fsviewer',function(e){
					$('.fullscreen').addClass('hover');
				}).bind('mouseleave.fsviewer',function(e){
					$('.fullscreen').removeClass('hover');
				})

				return this.data('viewer',{});
			},

			launch: function()
			{
				var $this = $(this),
					v = !$this.attr('data-vid') ? 'stills' : 'video';

				$('#loader').appendTo($('#viewer').removeClass().addClass('launched '+v)).fadeIn();


				// ajax a single page data, insert into #viewer
				$.ajax({
					// dataType:'JSON',
					type:'GET', data:{inline:true},
					url:$this.attr('rel'),
					success:function(data){
						//log(data);

						var title = $('h2 a',data).text(),
							$viewer = $('#viewer-wrapper');

						// full nav
						$('#viewer-nav').show();
						$('#loader').hide();
						$('#viewer-title').html(title);
						$.timothysaccenti.fonts('#viewer-nav h2');

						$.timothysaccenti.modules.viewer.view[v].call($this,data);

						// Share links
						$('#share-options').html(
							$.timothysaccenti.shareLinks
								.replace(/%TITLE%/g, escape(title) )
								.replace(/%LINK%/g, escape(
									//WWW+$this.attr('href')
									window.location.href
								))
						);
						$.timothysaccenti.fonts('#share-options .capsule');

						// lightbox info
						var info = $.trim( $('#info',data).html() ),
						 	hasInfo = $('<div>').html(info).children('br, .post-edit-link').remove().end().contents().length;

						if( hasInfo ) // only show if content present
						{
							// the info to show in overlay
							$('#info-overlay h1')
								.html( info )
								.find('a').attr('target','_blank').end()
								.append('<a id="overlay-close" href="#">(Close)</a>');

							$.timothysaccenti.fonts('#info-overlay h1');

							$('#info').removeClass('deactivated');
						}
						else $('#info').addClass('deactivated');
					}
				});
			},

			view: {

				info:function(){

					if(IS_IPHONE){
						$('#subnav-right, #subnav-left').addClass('deactivated').click(function(e){ e.preventDefault(); });
						return;
					}

					// start the thumbs slidescroll
					$('#subnav-wrapper').slidescroll({selector:'span',direction:1}).slidescroll('start')

					if( !$('#subnav-wrapper').slidescroll('hasContent') )
						$('#subnav-left, #subnav-right').addClass('deactivated');//.hide(); 

					// scrolling the thumb slides
					$('#subnav')
						.mouseleave(function(e){
							$("#subnav-wrapper")
								.slidescroll("option", "speed", 1)
								.slidescroll("option", "direction", 1)
						});


					$('#subnav-right')
						.click(function(e){ e.preventDefault(); })
						.mouseover(function(e){
							$("#subnav-wrapper").slidescroll("option", "speed", 0);
						}).mouseleave(function(e){
							$("#subnav-wrapper")
								.slidescroll("option", "speed", 1)
								.slidescroll("option", "direction", 1)
						})
						.mousedown(function(e){
							$("#subnav-wrapper")
								.slidescroll("option", "direction", 1)
								.slidescroll("option", "speed", 10)
						}).mouseup(function(e){
							$("#subnav-wrapper").slidescroll("option", "speed", 0);
						});

					$('#subnav-left')
						.click(function(e){ e.preventDefault(); })
						.mouseover(function(e){
							$("#subnav-wrapper")
								.slidescroll("option", "speed", 0);
						}).mouseleave(function(e){
							$("#subnav-wrapper")
								.slidescroll("option", "speed", 1)
								.slidescroll("option", "direction", 1);
						})
						.mousedown(function(e){
							$("#subnav-wrapper")
								.slidescroll("option", "speed", 10)
								.slidescroll("option", "direction", -1);
						}).mouseup(function(e){
							$("#subnav-wrapper")
								.slidescroll("start");
						});
				},

				stills:function(data){
					// images
					var imgs = $('img',data),
						$viewer = $('#viewer-wrapper');

					if(!IS_IPHONE && imgs.length) while(imgs.length<5) imgs = imgs.add(imgs.clone());

					//$('#viewer').removeClass('video').addClass('stills');
					$viewer.html(imgs);

					// get all files for fs view
					var files = [],
						fileKeys = [],
						w = $viewer.width(),
						$imgs = $('img',$viewer);

					$imgs.each(function(i){

						var $t = $(this),
							src = $t.attr('data-big');

						if(!fileKeys[src]){
							fileKeys[src] = 1;
							files.push( src );
						} 

						// fade imgs in onload
						if($t.offset().left>=w) return true; // only on visible imgs
						if($t.height()){
							$t.animate({opacity:0},{duration:0}).delay(i*200).animate({opacity:1},{duration:'slow'});
						} 
						// no height-as-onload hack possible as imgs have height defined
						// else $t.load(function(){
						// 	$t.hide().fadeIn('slow');
						// });

					});

					// reset after video content
					$('#fakevidcontrols, #viewer>a, #viewer-left, #viewer-right, #viewer-title').show(); 

					// teardown old slidescroll
					$viewer.slidescroll('destroy');
					$('#viewer, #viewer-right, #viewer-left').unbind();

					if(IS_IPHONE){
						$('.fullscreen').hide();
						$('#viewer-right, #viewer-left').addClass('deactivated').click(function(e){ e.preventDefault(); });
						return;
					} 

					var $fs = $('.fullscreen');
					if(imgs.length)
					{
						// Flash FS
						$('.fullscreen').show();
						// move flash onto thumb
						$('#fsviewer-wrapper').css( {
							//top:Math.round($fs.offset().top), left:Math.round($fs.offset().left),
							width:$fs.outerWidth(true)//, height:$fs.outerHeight(true)
						} );

						var fsviewer = net.theantenna.fsviewer;
						if ( fsviewer.flash instanceof jQuery ) $(fsviewer.id).one('ready.fsviewer',function(){
							fsviewer.flash.setFiles( files );
						})
						else fsviewer.flash.setFiles( files );

						// start slideshow
						$('#viewer-left,#viewer-right').removeClass('deactivated');
						$viewer.slidescroll({maxSpeed:20,hotspotW:350}).slidescroll('start');
					}
					else // No images - launch info
					{
						$fs.hide();
						$('#info').click();
						$('#viewer-left, #viewer-right').addClass('deactivated');
					}

					// scrolling the stills
					$('#viewer')
						.mouseleave(function(e){
							$("#viewer-wrapper")
								.slidescroll("option", "speed", 1)
								.slidescroll("option", "direction", 1);
						})

					$('#viewer-right')
						.click(function(e){ e.preventDefault(); })
						.mouseover(function(e){
							$("#viewer-wrapper")
								.slidescroll("option", "speed", 0);
						}).mouseleave(function(e){
							$("#viewer-wrapper")
								.slidescroll("option", "speed", 1)
								.slidescroll("option", "direction", 1);
						})
						.mousedown(function(e){
							$("#viewer-wrapper")
								.slidescroll("option", "speed", 25)
								.slidescroll("option", "direction", 1);
						}).mouseup(function(e){
							$("#viewer-wrapper")
								.slidescroll("option", "speed", 0);
						});

					$('#viewer-left')
						.click(function(e){ e.preventDefault(); })
						.mouseover(function(e){
							$("#viewer-wrapper")
								.slidescroll("option", "speed", 0);

						}).mouseleave(function(e){
							$("#viewer-wrapper")
								.slidescroll("option", "speed", 1)
								.slidescroll("option", "direction", 1);
						})
						.mousedown(function(e){
							$("#viewer-wrapper")
								.slidescroll("option", "speed", 25)
								.slidescroll("option", "direction", -1);
						}).mouseup(function(e){
							$("#viewer-wrapper")
								.slidescroll("option", "speed", 0);
						});

				},

				video:function(data)
				{
					var id = $.trim(this.attr('data-vid')),
						$viewer = $('#viewer-wrapper');

					$viewer.html( '<div id="player"><h1><a href="'+id+'" target="_blank">Click to<br/> Launch video</a></h1></div>' );

					if(!IS_IPHONE)
					{
						swfobject.embedSWF(
							TEMPLATE_DIR+"/swf/timothysaccenti.player.swf", "player",
							"100%", "540", "10.0.0", TEMPLATE_DIR+"/swf/expressInstall.swf",
							{src:id, isIE:$.browser.msie},
							{menu:"false", bgcolor:"#000000", allowScriptAccess:"sameDomain", allowFullScreen:"true"},
							{id:"player",name:"player"}
						);
						swfobject.createCSS("#player","outline:none"); // css outline fix
					}
					else{
						$.timothysaccenti.fonts('#player h1');
						$('#player h1').hide(0).delay(100).fadeIn();
					}

					$('#fakevidcontrols, #viewer>a, #viewer-left, #viewer-right, .fullscreen').hide(); // scroll bts

					// teardown old slidescroll
					$viewer.slidescroll('destroy');
					$('#viewer, #viewer-right, #viewer-left').unbind();

					// IE can't overlay HTML on Flash
					if($('html').hasClass('win')) $('#info-marker').insertBefore('#viewer-nav');

					// stop thumbs slidescroll
					//$("#thumbnails-wrapper").slidescroll("stop");
				}
			}

		})

	});
	
	
	
	
})(jQuery);


