/*jslint browser: true, white: true, plusplus: true, newcap: true,
    eqeqeq: true, evil: true, nomen: true,  regexp: true, undef: true,
    onevar: true */

/*global $,pageTracker,window,console,Cufon */

var WT = {
	// jQuery refs
	main : null, 
	sub : null, 
	subc : null, 
	subidx : null,
	subcount : null,
	bg_visible : null, 
	bg_hidden : null,
	
	// is the #sub panel visible?
	sub_showing : false,

	initHomepage : function () {
		window.setTimeout( 
			function () {
				$('#page').fadeIn(2000);
			},
			500
		);
	
	},

	initBackground : function () {
		WT.bg_visible = $('#bg');
		WT.bg_hidden  = $('#bg_alt');
	},
	
	preload : function () {
		var img = null, i;
		for (i = 0; i < arguments.length; i += 1) {
			img = new Image();
			$(img).attr('src', arguments[i]);  
		} 
	},
	
	showBackground : function (nav_id) {
		$('.bg').stop(true);
		WT.bg_visible.css({opacity: 1});

		window.setTimeout(function () {
			// set the background-image on the hidden layer and
			// swap depths
			WT.bg_hidden.attr('class', 'bg ' + nav_id);
			WT.bg_visible.fadeOut(1200, function () {
				var old_visible = WT.bg_visible;
				WT.bg_visible = WT.bg_hidden;
				WT.bg_hidden = old_visible;

				WT.bg_visible.css('z-index', 2);
				WT.bg_hidden.css({'z-index' : 1, 'opacity' : 1, 'display' : 'block'});

				//console.debug('bg_v', WT.bg_visible.attr('id'), WT.bg_visible.css('z-index'));
				//console.debug('bg_h', WT.bg_hidden.attr('id'), WT.bg_hidden.css('z-index'));
			});
			
		}, 500);
	},
	
	initNav : function () {
		// main nav clicks
		$('#logo a, #nav a').live('click.wt', function () {
			WT.showBackground($(this).parent().attr('id'));
			if (WT.sub_showing) {
				WT.sub.slideUp(400);
				WT.sub_showing = false;
			}
			
			WT.loadMain(this.href);
			return false;
		});
		
		WT.initSubNav();


	},
	
	loadMain : function (href) {
		$.get(href, function (d, s) {
			WT.main.html(d);
			Cufon.refresh();
			WT.initContactForm();
			WT.initSubNav();
		});	
	},
	
	initSubNav : function () {
		// inline subnav clicks
		$('#subnav a').bind('click.wt', function () {
			WT.loadSub(this.href);
			WT.subidx = $('#subnav li').index($(this).parent());
			return false;
		});
		
		WT.subidx = -1;
		WT.subcount = $('#subnav > li').size();

		$('#subnav').prepend('<div id="subnav_hittarget">&nbsp;</div');
		$('#subnav_hittarget').live('click.wt', function () {
			WT.subidx += 1;
			if ( WT.subidx >= WT.subcount) {
				WT.subidx = 0;
			}
			WT.loadSub($('#subnav li:eq(' + WT.subidx + ') a').attr('href'));

			return false;
		});
	},
	
	initGallery : function () {
		// inline gallery clicks
		$('#gallery a').bind('click.wt', function () {
			console.log(this.href);
			return false;
			WT.loadSub(this.href);
			//WT.subidx = $('#gallery li').index($(this).parent());
			return false;
		});
		
		WT.subidx = -1;
		WT.subcount = $('#gallery > li').size();

		/*$('#gallery').prepend('<div id="gallery_hittarget">&nbsp;</div');
		$('#gallery_hittarget').live('click.wt', function () {
			WT.subidx += 1;
			if ( WT.subidx >= WT.subcount) {
				WT.subidx = 0;
			}
			WT.loadSub($('#gallery li:eq(' + WT.subidx + ') a').attr('href'));

			return false;
		});*/
	},
	
	
	loadSub : function (href) {
		WT.sub.stop();
		WT.subc.stop();
		if (!WT.sub_showing) {
			WT.sub.css({display : 'block', 'height' : 1});
			WT.sub_showing = true;
			WT.subc.hide();
		}
		
		$.get(href, function (d, s) {
			// hide the content
			WT.subc.fadeOut(200, function () {
				var subh, distance, speed, oldh =  WT.subc.outerHeight();

				// set the content
				WT.subc.html(d);

				subh  = WT.subc.outerHeight();
				distance = subh - oldh;
				speed = distance * 2 * (distance < 0 ? -1 : 1); // consistent pixels per second
				    
				WT.sub.animate({'height' : subh}, speed, function () {
					WT.subc.fadeIn(200);
					Cufon.refresh('h1, h2, p.lede');
				});


			});
		});
	},
	
	initContactForm : function () {
		var f = $('#westtide_contact'), 
		        data = null;

		f.bind('submit.wt', function () {
			data = f.serialize();
			$.post( 
				f.attr('action'), 
				data, 
				function (response, status) {
					if (response.submitted) {
						$('p.lede').html(response.form_status);
						f.hide();
						$('#form_status').hide();
					} else {
						$('#form_status').html(response.form_status);
						WT.initContactForm();
					}
					return false;
				}, 'json');
			
			return false;
		});
	}
};

$(document).bind('ready', function () {
	$.easing.def = 'easeOutCirc';
	WT.sub = $('#sub');
	WT.subc = $('#sub div.content');
	WT.main = $('#main div.content');

	WT.initBackground();
	WT.initHomepage();
	WT.initNav();
	WT.initGallery();
	WT.initContactForm();
});
