(function() {
	// JSS selectors and rules that apply to the entire page
	var layout = {
		// center #page if window width is greater than #page width
		'#page@create': function() {
			var e = $(this);
			e.toggleClass('centered', $(window).width() > e.width());
		},
		// Make the menu have a pixel perfect width
		// We want it to stretch from its left side to within 100 pixels of the right
		// side of the #page element
		'#menu@create': function() {
			var e = $(this),
				d = Math.abs((e.position().left + e.width()) - $('#page').width() + 100),
				c = e.find('> ul > li > a'),
				p = parseInt(/\d+/.exec(c.first().css('paddingLeft'))) + (d / (c.length * 2));
			c.each(function() {
				$(this).css({
					paddingLeft: p,
					paddingRight: p
				});
			})
		},
		// enable drop down menus
		// Note: This is an inefficient means of doing this but... it works
		'#menu li@hover': function(evt) {
			var e = $(this).parents('li').andSelf(),
				n = (evt.type == 'mouseenter') ? 'add' : 'remove';
			e[n+'Class']('hover');
		},
		// adjusts height for floated elements so vertical borders appear as expected
		// used in the footer of the homepage and other places with floated columns
		'.verticalBorder@create': function() {
			var e = $(this);
			e.height(e.parent().height());
		},
		// email addresses are encrypted simply by reversing the email address
		// this code snippet reverses the letters so the address links work as expected
		/*'a[href^="mailto"]:not(.show)@create': function() {
			// email addresses are reversed by default
			var e = $(this),
				a = /^([^:]+:)([^@]+@[^$]+)/.exec(e.attr('href')),
				m = a[2].split('').reverse().join('');
			e.attr('href', a[1]+m).text(m);
		},*/
		// allow the layout to change dimensions while never being shorter than the browser window
		'#layoutBG@create': function() {
			var e = $(this),
				h = e.height(),
				w = e.width(),
				r = w / h,
				wh = $(window).height(),
				ww = $(window).width();
			if(
				h <= wh &&
				ww <= h * r
			) {
				e.height(wh).width(wh * r);
			}
			else {
				e.height('auto').width('100%');
			}
		}
	};

	var home = {
		'#featuredListings input:button@hover': function(evt) {
			$(this).toggleClass('hover');
		},
		
		
		'#featuredListings@create': function() {
			//var numPerRow = Math.floor($('#imageScroll').width() / $('#imageScroll').find('li').width());
			// e = the element object
			var e = $(this),
				flDivWidth = $('#imageScroll').width(),
				numPerRow = Math.floor( flDivWidth / e.find('li').width()),
				//currPage = ,
				//u = e.find('ul'),
				// m = current left margin of ul
				//m = /-?\d+/.exec(u.css('marginLeft'))[0];
				//currPage = Math.floor(Math.abs(m) / flDivWidth) + 1,
//alert("left margin: " + m + " fl width: " + flDivWidth + " currPage: " + currPage);
				// l = number's of feature plane (li)
				l = e.find('li').length;
			// pages = total pages
			e.data('pages', Math.ceil(l / numPerRow));
			// page = current page
			//e.data('currPage', currPage);
			//e.data('currPage', getCurrPage(e));
			if(l > numPerRow) {
				$('.btnRight').show();
			}
		},
		
		
		'.btnRight@click': function() {
			var e = $('#featuredListings'),
				// p = current page
				//p = e.data('currPage'),
				u = e.find('ul'),
				// m = current left margin of ul
				m = /-?\d+/.exec(u.css('marginLeft'))[0];

			u.css('marginLeft', m-610);
			//e.data('currPage', p++);
/*cp = getCurrPage(e);
alert("current Page: " + cp);*/
//alert("p: " + p + " -- e.data: " + e.data('pages') + " -- currPage: " + e.data('currPage'));
			//if(p == e.data('pages')) {
			if(getCurrPage(e) == e.data('pages')) {
				$(this).hide();
			}
			
			if(getCurrPage(e) > 1){
				$('.btnLeft').show();	
			}
		},
		
		
		'.btnLeft@click': function() {
			var e = $('#featuredListings'),
				//p = e.data('page'),
				u = e.find('ul'),
				m = parseInt(/-?\d+/.exec(u.css('marginLeft'))[0], 10);
			$('#featuredListings ul').css('marginLeft', m+610);
			//e.data('page', p--);
			//if(p == 0) {
			if(getCurrPage(e) == 1) {
				$(this).hide();
			}
			
			if(getCurrPage(e) < e.data('pages')){
				$('.btnRight').show();
			}
		}
	};

	var references = {
		// Vertically center the logos on the references page
		'#references .logo@create': function() {
			var e = $(this),
				p = $(this).parent();
			e.css('lineHeight', p.height()+'px');
		}
	};

	var sales = {
		// split the sales list for each aircraft type into three lists
		'#salesList ul.sales@create': function() {
			var e = $(this),
				c = e.children(),
				l = Math.ceil(c.length / 3),
				f = function(a, b) {
					c.slice(a, b).detach().insertAfter(e).wrapAll('<ul>');
				};
			f(l * 2, l * 3);
			f(l, l * 2);
		}
	};

	var listings = {
		// enable the photo gallery and preload images
		'#listing #photos a@create': function() {
			var e = $(this);
			e.fancybox();
			$('<img>').attr('src', e.attr('href'));
		},
		'#slideshow @click': function() {
			var e = $('#listing #photos a'),
				i = 1,
				interval = null;
			e.first().trigger('click');
			// loop until the slideshow is over
			interval = setInterval(function() {
				if(i < e.length) {
					$.fancybox.next();
					i++
				}
				else {
					$.fancybox.close();
					clearInterval(interval);
				}
			}, 3500);
		}
	}

	$(function(jQuery) {
		// Apply JSS events
		$('body').jss(layout)
			.jss(home)
			.jss(references)
			.jss(sales)
			.jss(listings);


		// Centering fix for #page element on small screens
		$(window).resize(function() {
			$('#page').trigger('create');
			$('#layoutBG').trigger('create');
		});
	});
})()

function getCurrPage(e){
	u = e.find('ul')
	m = /-?\d+/.exec(u.css('marginLeft'))[0];
	flDivWidth = $('#imageScroll').width();
	currPage = Math.floor(Math.abs(m) / flDivWidth) + 1;
	return currPage;
}
