// private functions
function debug(o) {
	if(window.console && window.console.log) {
		window.console.log(o);
	}
}

if(typeof jQuery != 'undefined') {
	jQuery.noConflict();
	(function($) {

		// app functions
		var App = {

			/**
			* Options
			* @desc General App related options
			*/
			options: {
				history:	true,
				className: {
					paused:		'paused',
					active:		'on',
					hover:		'hover'
				},
				cycle: {},
				fancybox: {
					centerOnScroll:		true,
					showCloseButton:	false,
					titleShow:			false,
					overlayOpacity:		1,
					overlayColor:		'#454545',
					onStart: function(){
						//$('body').addClass('fancybox-shown');
					},
					onClosed: function(){
						//$('body').removeClass('fancybox-shown');
					}
				},
				flashvars: {},
				flashparams: {
					loop:				'false',
					menu:				'false',
					play:				'true',
					salign:				'lt',
					scale:				'noscale',
					wmode:				'transparent',
					quality:			'high',
					allowScriptAccess:	'true',
					allowFullScreen:	'true'
				}
			},

			/**
			* Hover
			* @desc Fixing hover on navigation list items
			*/
			hover: function() {
				var sfEls, length, functions;

				sfEls		= document.getElementById('nav').getElementsByTagName('li');
				length		= sfEls.length;
				functions	= {
					mouseover: function() {
						this.className += ' ' + App.options.className.hover;
					},
					mouseout: function() {
						this.className = this.className.replace(App.options.className.hover, '');
					}
				};

				for(var i=0; i<length; i++) {
					sfEls[i].onmouseover	= functions.mouseover;
					sfEls[i].onmouseout		= functions.mouseout;
				}
			},

			/**
			* External
			* @desc Making links open in a new window / tab
			*/
			external: function(){
				$('a[rel=external], a.external').bind('click.external', function(event){
					var $$ = $(event.target);
					window.open($$.attr('href'));
					$$.blur();
					event.preventDefault();
				});
			},

			/**
			* flash
			* @desc
			* @uses flash()
			*/
			flash: function() {
				if($.fn.flash) {
					$('#flashArtists').flash($.extend({}, App.options.flashparams, {
						swf:	'/flash/listartist/list-artist.swf',
						height:	509,
						width:	362,
						flashvars: {
							sXMLFile: sXMLFile
						}
					}));
					
					$('#flashNewsGall').flash($.extend({}, App.options.flashparams, {
						swf:	'/flash/gallery/gallery.swf',
						height:	509,
						width:	604,
						flashvars: {
							sXMLFile: sXMLFile
						}
					}));
				}
			},

			/**
			* cycle
			* @desc 
			* @uses cycle()
			*/
			cycle: function(){
				if($.fn.cycle) {
					var $arrowNav,
						$splashPics,
						$workPhoto, $workPhotos,
						$newsEvent, $newsEventPages,
						$exhibitFeatured, $exhibitFeaturedImages;

					$arrowNav	= $('.arrowNav ul');

					// splash pics
					$splashPics = $('#splashPics');
					$splashPics.cycle({
						fx:				'fade',
						startingSlide:	0,
						timeout:		3500,
						speed:			500,
						pause:			2
					});

					// work photos
					$workPhoto	= $('#listOfWorkPhoto');
					$workPhotos	= $workPhoto.find('li');
					if($workPhotos.length > 0) {
						$('#listOfWorkPhoto ul').cycle({
							fx:				'fade',
							startingSlide:	0,
							timeout:		0,
							speed:			1000,
							next:			$arrowNav.find('.next'),
							prev:			$arrowNav.find('.prev')
						});
					}

					// news / events
					$newsEvent		= $('#newsEvents');
					$newsEventPages = $newsEvent.children('div.page');
					if($newsEventPages.length > 0) {
						$newsEvent.cycle({
							fx:				'scrollHorz',
							startingSlide:	0,
							timeout:		0,
							speed:			500,
							next:			$arrowNav.find('.next'),
							prev:			$arrowNav.find('.prev')
						});

					}

					// featured exhibit
					$exhibitFeatured		= $('#exhibitFeatured ul.images');
					$exhibitFeaturedImages	= $exhibitFeatured.children('li');
					if($exhibitFeaturedImages.length > 0) {
						$exhibitFeatured.cycle({
							fx:				'fade',
							startingSlide:	0,
							timeout:		3500,
							speed:			500,
							pause:			2
						});

					}

				}
			},

			/**
			* fancybox
			* @desc
			* @uses fancybox()
			* @see http://fancybox.net
			*/
			fancybox: function() {
				if($.fn.fancybox) {
					$('#listOfWorkPhoto a, #artistArtInfo a').fancybox($.extend({}, App.options.fancybox, {
						showCloseButton: $('body').is('.cunard')
					}));
				}
			},

			/**
			* scrollable
			* @desc
			* @uses jScrollPane()
			*/
			scrollable: function() {
				if($.fn.jScrollPane) {
					var $scrollable = $('#artistList, body.exhibition-listing #exhibLeft, #exhibText, #artistBio  .article');
					$scrollable.jScrollPane({animateTo: true, scrollbarWidth: '0'});
					$('a.scroll-arrows').bind('click', function(event) {
							$scrollable.get(0).scrollBy(parseInt($(event.target).attr('rel'), 10));
							event.preventDefault();
						}
					);
				}
			},

			/**
			* original
			* @desc
			* @uses jScrollHorizontalPane()
			*/
			original: function() {
				var $scroll = $('#horizScroll'),
					$info = $('#artistArtInfo'),
					nScrollW = 0;

				$scroll.find('li').each(function(index) {
					nScrollW += parseInt($(this).width(), 10);
				}).filter(':last').addClass('noPadd');

				nScrollW = nScrollW - parseInt($scroll.find('li a img').css('margin-right'), 10);

				$scroll.find('ul').css('width', nScrollW + 'px');

				if($.fn.jScrollHorizontalPane) {
					$scroll.jScrollHorizontalPane({
						scrollbarHeight:	9,
						scrollbarMargin:	0,
						resize:				true
					});
				}

				$scroll.bind('click', function(event){
					var $$, $li,
						href;

					$$	= $(event.target).closest('a');
					$li	= $$.closest('li');

					if($$.is('a')) {
						href = $$.attr('href');

						if(!$li.is('.' + App.options.className.active)) {
							$info.empty().load(href, function(){
								App.fancybox();
							});

							$li.addClass(App.options.className.active).siblings().removeClass(App.options.className.active);
						}
						$$.blur();
						event.preventDefault();
					}
				});
			},

			cunard: function() {
				var $body, $browser, $content,
					distance = 500;
				
				$body		= $('body');
				$browser	= $('#browser');
				$content	= $('#content');

				$browser.find('a[href^=#]').bind('click', function(event){
					var $$, $li,
						id;

					$$	= $(event.target).closest('a');
					$li	= $$.closest('li');
					id	= $li.attr('id');

					switch(id) {
						case 'browser-back':
							history.go(-1);
							return false;
							break;

						case 'browser-forward':
							history.go(+1);
							return false;
							break;

						case 'browser-up':
							//jQuery('body').animate({scrollTop: parseInt(jQuery('body').scrollTop(), 10) - distance}, 0);
							window.scrollBy(0,-900);
							break;

						case 'browser-down':
							//jQuery('body').animate({scrollTop: parseInt(jQuery('body').scrollTop(), 10) + distance}, 0);
							window.scrollBy(0,900);
							break;
					}

					$$.blur();
					event.preventDefault();
				});

				$content.find('div.info').css('min-height', parseInt($content.height(), 10) + 60);

				$('#medium form').change(function(){
					var $input = $(this);
					$input.closest('form').submit();
				}).closest('form').addClass('auto-submitting').find('input.submit').hide();
			}
		};


		/**
		* READY!
		* @desc Document ready, load all the app functions
		*/
		jQuery(document).ready(function($) {
			var $body,
				$artistList, $artistPics;

			$body = $('body').addClass('has-jquery');

			App.external();
			App.cycle();
			App.flash();
			App.fancybox();
			App.scrollable();

			if($body.is('.artistInfo')) {
				App.original();
			}

			if($body.is('.cunard')) {
				App.cunard();
			}

			// portfolio of artist list
			$artistList = $('#artistList li'),
			$artistPics = $('#artistPics li');
				
			$artistList.find('a').bind('mouseover mouseleave', function(event){
				var $$, $li, $img,
					index;
				
				$$		= $(event.target);
				$li		= $$.closest('li[id]');
				index	= $li.index();
				$img	= $artistPics.eq(index).find('img');

				switch(event.type) {
					case 'mouseover':
					case 'mouseenter':
						$img.stop().fadeTo(500,1);
						break;

					case 'mouseleave':
					case 'mouseout':
						$img.stop().fadeTo(500,0);
						break;
				}
			});

			// auto submit select boxes
			$('form.auto, select.auto-submit').bind('change', function(event){
				$(event.target).closest('form').get(0).submit();
			}).closest('form').find('div.submit, input:submit, button').addClass('access');
		});

		// IE 6 hover fix
		if(document.all && document.getElementById) {
			window.onload = App.hover;
		}

		// Flash External Interface
		$.fn.callFlash = function(callback, variables, prefix) {
			var $$, dom, data = false;
			$$ = $(this);
			if($$.length === 1) {
				dom = $$.get(0);
				if($$.attr('id').length > 0) {
					dom = document.getElementById($$.attr('id')); // safer
				}
				if(typeof prefix === 'string') {
					callback = prefix + callback.charAt(0).toUpperCase() + callback.substr(1);
				}
				if(typeof dom !== 'undefined' && typeof dom[callback] === 'function') {
					data = dom[callback](variables);
				}
			}
			return data;
		};

		// Preload images
		$.preloadImages = function() {
			$.each(arguments, function(i){
				jQuery('<img />').bind('load', function(){}).attr('src', this);
			});
		};

		$.fn.exists = function() {
			return (this.length > 0);
		};

		// http://james.padolsey.com/javascript/events-interface-for-jquery/
		$.fn.events = function(o){
			for(var i in o) {
				if(o.hasOwnProperty(i)) {
					this.bind(i, o[i]);
				}
			}
			return this;
		};

		/**
		 * jQuery mousehold plugin - fires an event while the mouse is clicked down.
		 * Additionally, the function, when executed, is passed a single
		 * argument representing the count of times the event has been fired during
		 * this session of the mouse hold.
		 *
		 * @author Remy Sharp (leftlogic.com)
		 * @date 2006-12-15
		 * @example $("img").mousehold(200, function(i){  })
		 * @desc Repeats firing the passed function while the mouse is clicked down
		 * @url http://remysharp.com/2006/12/15/jquery-mousehold-event/
		 *
		 * @name mousehold
		 * @type jQuery
		 * @param Number timeout The frequency to repeat the event in milliseconds
		 * @param Function fn A function to execute
		 * @cat Plugin
		 */

		(function ($) {
			$.fn.mousehold = function(timeout, f) {
				if (timeout && typeof timeout == 'function') {
					f = timeout;
					timeout = 100;
				}
				if (f && typeof f == 'function') {
					var timer = 0, fireStep = 0;

					return this.each(function() {
						var clearMousehold = function() {
							clearInterval(timer);
							if (fireStep == 1) f.call(this, 1);
							fireStep = 0;
						};

						$(this).mouseenter(function() {
							fireStep = 1;
							var ctr = 0, t = this;
							timer = setInterval(function() {
								ctr++;
								f.call(t, ctr);
								fireStep = 2;
							}, timeout);
						}).mouseout(clearMousehold).mouseup(clearMousehold);
					});
				}
			};
		})(jQuery);


	// end of jQuery closure
	}(jQuery));
}
