/*	Global Configuration
----------------------------------------------- */
function init() {
	document.body.className += " loading js";
	
	Cufon.replace('h2, #Navigation li a', { fontFamily: 'Baskerville Cyrillic', hover: true });
	Cufon.replace('.date, .title p, h3, .credit p', { fontFamily: 'CourierSans-Light' });
}


$(function() {
	Setup.init();
	Branding.init();
	Navigation.init();
	Map.init();
});

var Setup = {	
	Body: null,
	init: function() {
		var cc = this;
		cc.Body = $(document.body);
		cc.Body.addClass('ready');

		// utility classes
		$("ul li:first-child").addClass("first");
		$("ul li:last-child").addClass("last");
	
		// external links
		$("a[rel='external']").each(function() {
			var link = $(this) 	;
			link.click(function() {
				return !window.open(link.attr('href'));
			});
		});
		
		// internal links
		$("a[rel='internal']").each(function() {
			var link = $(this) 	;
			link.click(function() {
				var hash = $(this).attr('href').substr(1);
				$(window).scrollTo($('.'+hash), 1000, { 
					queue: false, 
					easing: 'easeInOutQuart',
					offset: {left: 0, top: -20}
				});
				return false;
			});
		});
		
		$(window).scroll(function () {
			$.doTimeout( 'scroll', 400, function(state){
				Scroll.render();
			});
	    });
		
		$(window).resize(function () {
			$.doTimeout( 'scroll', 200, function(state){
				Resize.render();
			});
	    });
	
		$(window).load(function() {
			cc.Body.removeClass('loading');
			Resize.render();
			
			if (location.href.indexOf("#") != -1) {
				$(window).scrollTop(1);
				var hash = self.document.location.hash.substring(1);
				if(hash=="map") {
					$('#Navigation a.map').click();
				}
				else {
					var target = $('.'+hash);
					$(window).scrollTo(target, 1000, { 
						queue: false, 
						easing: 'easeInOutQuart',
						offset: {left: 0, top: -20}
					});
				}
				
			}
			else {
				var target = $('#Content div.post:last');
				$(window).scrollTo(target, 1000, { 
					queue: false, 
					easing: 'easeInOutQuart',
					offset: {left: 0, top: -20}
				});
			}	
		});
		
		$("a[rel='fancybox']").fancybox();
	}
};


var Navigation = { 
	Triggers: null,
	init: function() {
		var cc = this;
		cc.Triggers = $('#Navigation li a:not(.map)');
		cc.events();
	},
	events: function() {
		var cc = this;
		cc.Triggers.click(function(){
			var target = $('.'+$(this).attr('rel')+':first');
			if(target.length>0) {
				$(window).scrollTo(target, 1000, { 
					queue: false, 
					easing: 'easeInOutQuart',
					offset: {left: 0, top: -20}
				});
			}
			return false;
		});
	}
};

var Map = { 
	Trigger: null,
	Container: null,
	Wrapper: null,
	init: function() {
		var cc = this;
		cc.Container = $('#Map');
		cc.Trigger = $('#Navigation li a.map');
		cc.events();
		cc.resize();
	},
	events: function() {
		var cc = this;

		// it is assumed to be the onBeforeLoad event listener
		cc.Trigger.overlay({
			target: "#Map",
			top: 0,
			onLoad: function() {
				var latlng = new google.maps.LatLng(53.4823, -2.2342 );
				var myOptions = {
					zoom: 16,
					center: latlng,
					mapTypeId: google.maps.MapTypeId.ROADMAP,
					navigationControl: true,
				 	scaleControl: true,
					mapTypeControl: false,
					mrt: 'all'
				};
				var map = new google.maps.Map(document.getElementById("GoogleMap"), myOptions);
				var marker = new google.maps.Marker({
				    position: latlng,
					icon: 'http://24nq.com/wp-content/themes/24nq/images/marker.png',
				    map: map
				});
			},
			onClose: function() {
				$('#GoogleMap').html('');
			}
		});
		
		$(window).resize(function () {
			$.doTimeout( 'scroll', 200, function(state){
				cc.resize();
			});
	    });
		
	
	},
	resize: function() {
		var cc = this;
		var w = $(window).width();
		var h = $(window).height();
		cc.Container.css({height: h, width: w});
	}
};

var Branding = { 
	init: function() {
		var cc = this;
		$('div#Branding').cycle({ 
		    timeout:  15000,
			speed: 100
		});
	}
};

var Scroll = { 
	render: function() {
		var cc = this;
		var documentHeight = document.body.scrollHeight;
		var scrollPosition = $(window).scrollTop()
		var percentage = documentHeight/4;	
		// 
		// console.log(document.body.scrollHeight);
		// console.log('docHeight:'+documentHeight+' scollPos:' +scrollPosition+' percentage: '+percentage);

		if(scrollPosition<percentage) {
			$("body").colorBlend([{colorList:["current","#d8eaf3"], cycles: 1, strobe: false}]);
		}
		else if(scrollPosition<(percentage*2)) {
			$("body").colorBlend([{colorList:["current","#fac0bf"], cycles: 1, strobe: false}]);
		}
		else if(scrollPosition<(percentage*3)) {
			$("body").colorBlend([{colorList:["current","#fffcd2"], cycles: 1, strobe: false}]);
		}
		else {
			$("body").colorBlend([{colorList:["current","#e6f1d1"], cycles: 1, strobe: false}]);	
		}

	}
};

var Resize = {
	render: function() {
		var width = $('#Container').width();		
		if(width<980) {
			$('body').addClass('width-lowres');
		}
		else {
			$('body').removeClass('width-lowres');
		}	
	}
}

$.getDocHeight = function(){
    return Math.max(
        $(document).height(),
        $(window).height(),
        /* For opera: */
        document.documentElement.clientHeight
    );
};
