/* -----------------------------------------------
BASE
----------------------------------------------- */

/* TradeGothic-BoldCondTwenty */
Cufon.replace('#Navigation a, #Home div.carousel h2, div.scrollable div.item-media h3, div.scrollable div.item-media p', { 
	fontFamily: 'TradeGothic-BoldCondTwenty', 
	hover: true 
});

/* TradeGothic-Bold */
Cufon.replace('div.title h2, div.carousel ul.tabs a, div#Main div.section div.content h3', { 
	fontFamily: 'TradeGothic-Bold', 
	hover: true 
});

function init() {
	document.body.className += "loading js";
}

var Body = null;
$(function() {
	Body = $(document.body);
	Setup.init();
	Navigation.init();
	Window.init();
	Lightbox.init();
	Icons.init();
	
	// Sections
	Home.init();
	AboutUs.init();
	Products.init();
	Technical.init();
	CaseStudies.init();
	Media.init();

	// Additional formatting / functions
	centerNavis(null);
	$("ul li:first-child").addClass("first");
	$("ul li:last-child").addClass("last");
	$("a[rel='external']").each(function() {
		var link = $(this) 	;
		link .click(function() {
			return !window.open(link.attr('href'));
		});
	});
	// hide empty scrollable next/prev
	$('div.scrollable').each(function(){
		var size = $(this).find('.item').size();
		if(size<2) {
			$(this).find('a.next, a.prev').addClass('disabled');
		}	
	});
	$('#EmailLink').html('<a href="mailto:info@walo.co.uk">info@walo.co.uk</a>');
});

/*	Setup
----------------------------------------------- */

var Setup = {	
	Body: null,
	init: function() {
		var cc = this;
		Body.addClass('jsReady');
		Body.removeClass('loading');	
	}
	
};

var Icons = {
	Triggers: null,
	init: function() {
		var cc = this
		cc.Triggers = $('a.icon');
		cc.Triggers.append('<span></span>');
	}
}

var Window = {
	Images: null,
	Sections: null,
	WinHeight: null,
	SectionCount: null,
	CurrentSection: null,
	init: function() {
		var cc = this;
		cc.Images = $('#Images div');
		cc.Sections = $('#Main div.section');
		cc.SectionCount = cc.Sections.length;
		cc.CurrentSection = 0;
		
		$(window).resize(function(){
			$.doTimeout( 'resize', 200, function( state ){
				cc.resize();
				$('#Navigation a:eq('+cc.CurrentSection+')').click();
				
			}, true);
			
		});
		
		$(window).scroll(function(){
			$.doTimeout( 'scroll', 200, function( state ){
			  cc.scroll();
			}, true);		
		});
		
		cc.resize();
		cc.scroll();
	},
	resize: function() {
		var cc = this;
		cc.WinHeight = $(window).height();
		
		if(cc.WinHeight<700) cc.WinHeight = 700;

		cc.Images.css({height: cc.WinHeight});
		cc.Sections.css({height: cc.WinHeight});
	},
	scroll: function() {
		var cc = this;
		var scrollTop = $(window).scrollTop();

		var i=0;
		while (i<=cc.SectionCount) {
			if(scrollTop>=cc.WinHeight*i) {
				if(scrollTop<cc.WinHeight*(i+1)) {
					cc.CurrentSection = i;
					Navigation.render(i);
				}
			}
		  i++;
		}
	}
};

var Navigation = {
	Triggers: null,
	init: function() {
		var cc = this;
		cc.Triggers = $('#Navigation a');
		
		cc.events();
	},
	events: function() {
		var cc = this;
		cc.Triggers.bind("click", function(e){
			var target = $(this).attr('href');
			$(window).scrollTo(target, 1000, { 
				queue: false, 
				easing: 'easeInOutQuart'	
			});
			return false;
	    });
	},
	render: function(number) {
		var cc = this
		cc.Triggers.removeClass('selected');
		$(cc.Triggers[number]).addClass('selected');
		Cufon.replace('#Navigation a');
	}
};

var Lightbox = {
	Ajax: null,
	init: function() {
		var cc = this;
		cc.Ajax = $('div#Lightbox div.ajax');
		$("a[rel=#Lightbox]").overlay({
			mask: '#000',
			onBeforeLoad: function() {
				cc.Ajax.html('<img src="'+this.getTrigger().attr("href")+'" width="900" height="560" />');
			}
		});
	}
};


/*	Sections
----------------------------------------------- */

var Home = {
	init: function() {
		$('div#Home div.panes div.item').click(function() {
			$('#Navigation a[href=#Products]').click();
			return false;
		});
		$("div#Home ul.tabs").tabs("div.panes > div.item", {
				effect: 'fade',
				fadeOutSpeed: "slow",
				rotate: true,
				current: 'selected'
			}).slideshow({
				next: '.next',
				prev: '.prev',
				autoplay: true
			});
	}
};

var AboutUs = {
	init: function() {
		$("div#AboutUs div.tabular ul.tabs").tabs("div#AboutUs div.tabular div.panes > div", {
			current: 'selected'
		});
	}
}

var Products = {
	init: function() {
		$("div#Products div.tabular ul.tabs").tabs("div#Products div.tabular div.panes > div", {
			current: 'selected'
		});
	}
}

var Technical = {
	init: function() {
		$("div#Technical div.tabular ul.tabs").tabs("div#Technical div.tabular div.panes > div", {
			current: 'selected'
		});
	}
}

var CaseStudies = {
	Ajax: null,
	init: function() {
		var cc = this;
		cc.Ajax = $('div#CaseStudyOverlay div.ajax');
		$("div#CaseStudies div.scrollable div.wpr").scrollable().navigator();
		$("div#CaseStudies div.item a").overlay({
			mask: '#000',
			onBeforeLoad: function() {
				cc.Ajax.load(this.getTrigger().attr("href"));
			},
			onLoad: function() {
				centerNavis('#CaseStudyOverlay div.navi');
			}
		});
	}
}

var Media = {
	init: function() {
		$("div#Media div.tabular ul.tabs").tabs("div#Media div.tabular div.panes > div", {
			current: 'selected'
		});
		$("div#Media div.scrollable div.wpr").scrollable().navigator();
	}
}

function centerNavis(selector) {
	if(selector==null) {
		var navis = $('div.navi');
	}
	else {
		
		var navis = $(selector);
	}
	
	navis.each(function() {
		var width = $(this).width()/2;
		$(this).css('marginLeft', -width);
	});
}
