$(document).ready(function(){

	pageController.init();
	breadcrumb.init();
	snippetLauncher.init();
	
}); // end of $(document).ready


/*
	page controller
--------------------------------------------------------------------- */
var pageController = {
	currentPage : "",
	firstVisit: false,
	
	init: function(){
		this.currentpage = $('#page').attr('class');
		
		home = ['home'];
		philosophy = ['philosophy'];
		locationIndex = ['locations'];
		locations = ['islington', 'notting-hill', 'kensington', 'belgravia'];
		catering = ['catering'];
		career = ['career'];
		blog = ['blog', 'blog-post', 'recipes'];
		
		// Home
		if( jQuery.inArray(this.currentpage, home) >= 0){
			// if the breadcrumb only has one item and it's value is Home, hide the breadcrumb and play into sequence
			if( $('#breadcrumb ol li a').length == 1 && $('#breadcrumb ol li a:first').text() == 'Home'){
				pageController.firstVisit = true;
			}
			homepageFeature.init();
		}
		// Philosophy
		if( jQuery.inArray(this.currentpage, philosophy) >= 0){
			$('#navigation-philosophy a').addClass('active');
		}
		// Location index
		if( jQuery.inArray(this.currentpage, locationIndex) >= 0){
			$('#navigation-locations a').addClass('active');
			$('#locations-feature img').removeClass('hide');
			$('#notting-hill-slidewhow').cycle({ fx:'fade', speed:2000, timeout:2000, delay:-4000});
			$('#islington-slidewhow').cycle({ fx:'fade', speed:2000, timeout:2000, delay:-3000 });
			$('#kensington-slidewhow').cycle({ fx:'fade', speed:2000, timeout:2000, delay:-2000 });
			$('#belgravia-slidewhow').cycle({ fx:'fade', speed:2000, timeout:2000, delay:-1000 });
		}
		// Locations
		if( jQuery.inArray(this.currentpage, locations) >= 0){
			$('#navigation-locations a').addClass('active');
			GoogleMaps.attachLocation(this.currentpage);
		}
		// Catering
		if( jQuery.inArray(this.currentpage, catering) >= 0){
			$('#navigation-catering a').addClass('active');
		}
		// Career
		if( jQuery.inArray(this.currentpage, career) >= 0){
			$('#navigation-career a').addClass('active');
		}
		// Blog
		if( jQuery.inArray(this.currentpage, blog) >= 0){
			$('#navigation-blog a').addClass('active');
			$('#comments li:last').addClass('last');
		}	
	}
}

/*
	breadcrumb controller
--------------------------------------------------------------------- */
var breadcrumb = {
	breadcrumbHeight : "",
	
	init: function(){
		if(pageController.firstVisit == false){
			$('#breadcrumb ol').show();
		}
		this.breadcrumbHeight = $('#breadcrumb ol').height();
		if(this.breadcrumbHeight > 20){
			breadcrumb.setUpBreadcrumb();
		}
	},
	setUpBreadcrumb: function(){
		$('#arrow').show(); // show arrow
		$('#breadcrumb ol').append('<li><a href="/breadcrumb/clear/" class="clear">Clear&nbsp;history</a></li>'); // append Clear link
		this.breadcrumbHeight = $('#breadcrumb ol').height(); // recalculate height of breadcrumb (inlcuding clear link)
		$('#breadcrumb ol').css({ overflow:"hidden", height:"16px"}); // truncate it
		breadcrumb.attachEventsBreadcrumb();
	},
	attachEventsBreadcrumb: function(){
		// on roll over/out
		$('#breadcrumb ol').hover(
			function(){ // over
				$('#breadcrumb').addClass('over');
				setTimeout(function(){
					breadcrumb.openBreadcrumb();
				}, 400);
			}, 
			function(){ // out
				$('#breadcrumb').removeClass('over');
				setTimeout(function(){
					breadcrumb.closeBreadcrumb();
				}, 400);
			}
		);
		// the clean/reset link
		$('#breadcrumb a.clear').click(function(){
			$("#breadcrumb ol").load( $(this).attr('href'), function(){
				breadcrumb.resetBreadcrumb();	
			});
			return false;
		})
	},
	openBreadcrumb: function(){
		if( $('#breadcrumb').is('.over') ){
			$('#arrow').hide();
			$("#breadcrumb ol").animate({height: this.breadcrumbHeight+"px"}, "fast" );
		}
	},
	closeBreadcrumb: function(){
		$('#arrow').show();
		$('#breadcrumb ol').animate({height: "16px"}, "fast", function(){
			// this callback function is to fix a dirty little bug in Safari.
			$('#breadcrumb ol').css({ height:"16px", overflow:"hidden" });
			$('#breadcrumb ol').removeClass('over');
		});
	},
	resetBreadcrumb: function(){
		$('#breadcrumb ol').removeClass('over');
		$('#breadcrumb').removeClass('over');
		$('#breadcrumb ol').unbind();
		$('#breadcrumb').unbind();
		$('#arrow').remove();
		breadcrumb.closeBreadcrumb();
	}
}

var homepageFeature = {
	
	active: "",
	speed: 400,
	
	init: function(){
		if(pageController.firstVisit == false){
			homepageFeature.addEvents();
		}else{
			// Play sequence
			$('#top ul:first').css("display","none"); // IE 6 bug
			//$('#uno, #due, #tre, #qua, .wrapper, #selected-hightlights, #sidebar, #footer, #top ul:first').hide();
			$('#uno, #due, #tre, #qua, .wrapper, #selected-hightlights, #sidebar, #footer').hide();
			setTimeout(function(){
				$('#due').fadeIn(1000, function(){
					$('#uno').fadeIn(850, function(){
						$('#tre').fadeIn(600, function(){
							$('#qua').fadeIn(600, function(){
								homepageFeature.addEvents();
								$('.wrapper').slideDown(1000, function(){
									$('#selected-hightlights').slideDown(1000, function(){
										$('#top ul:first').show();
										$('#sidebar').fadeIn();
										$('#footer').fadeIn();
									});
								})
							})
						})
					})
				})
			}, 700);
		}// end else
	},
	addEvents : function(){
		$('.extendable').hover(
			function(){
				$(this).addClass('over');
				homepageFeature.active = $(this).attr('id');
				homepageFeature.expand();
			}, 
			function(){
				$(this).removeClass('over');				
				$(this).find("div:last").fadeOut("fast");
			}
		);		
		$('#homepage-feature').hover(
			function(){
				$(this).addClass('over');
			},
			function(){
				$(this).removeClass('over');
				homepageFeature.restore();
			}
		);
	},
	expand : function(){
		switch(homepageFeature.active){
			case 'uno':
				$('#uno').addClass('down');
				$('#uno').animate({left:"0px"}, homepageFeature.speed);
				$('#due').animate({left:"315px"}, homepageFeature.speed);
				$('#tre').animate({left:"450px"}, homepageFeature.speed);
				$('#qua').animate({left:"545px"}, homepageFeature.speed, function(){
					$('#qua').removeClass('down');
				});
			break;                 
			case 'due':            
				$('#uno').animate({left: "0px"}, homepageFeature.speed, function(){
					$('#uno').removeClass('down');
				});
				$('#due').animate({left:"135px"}, homepageFeature.speed);
				$('#tre').animate({left:"450px"}, homepageFeature.speed);
				$('#qua').animate({left:"545px"}, homepageFeature.speed, function(){
					$('#qua').removeClass('down');						
				});
			break;                 
			case 'tre':            
				$('#uno').animate({left: "0px"}, homepageFeature.speed, function(){
					$('#uno').removeClass('down');
				});
				$('#due').animate({left:"135px"}, homepageFeature.speed);
				$('#tre').animate({left:"450px"}, homepageFeature.speed);
				$('#qua').animate({left:"545px"}, homepageFeature.speed, function(){
					$('#qua').removeClass('down');
				});
			break;                 
			case 'qua':
				$('#qua').addClass('down');         
				$('#uno').animate({left: "0px"}, homepageFeature.speed, function(){
					$('#uno').removeClass('down');
				});
				$('#due').animate({left:"135px"}, homepageFeature.speed);
				$('#tre').animate({left:"230px"}, homepageFeature.speed);
				$('#qua').animate({left:"545px"}, homepageFeature.speed);
			break;
		}
		
		$('#'+homepageFeature.active+" .feature-content").fadeIn(homepageFeature.speed, function(){
			$(this).fadeTo("slow", 0.9);
		});
	},
	restore: function(){
		$('.extendable').stop();
		
		$('.extendable .feature-content').fadeOut(homepageFeature.speed);
		$('#uno').animate({left:'0px'}, homepageFeature.speed, function(){
			$('#uno').removeClass('down');	
		});
		$('#due').animate({left:'135px'}, homepageFeature.speed);
		$('#tre').animate({left:'450px'}, homepageFeature.speed);
		$('#qua').animate({left:'545px'}, homepageFeature.speed, function(){
			$('#qua').removeClass('down');	
		});
	}
}

/*
	snippet controller
--------------------------------------------------------------------- */
var snippetLauncher = {
	
	url : "",
	position : {
		top: "",
		left: ""
	},
	theWindow : {
		width: "",
		height: ""
	},
	init: function(){
		if($('#page').is('.sitemap')){
			return;
		}else{
			//$("#content a[href*='/stories/'], #content a[href*='/recipes/']").after('  <img src="/images/snippet.gif" class="snippet-image" alt="" />');
			$("#content a[href*='/stories/'], #content a[href*='/recipes/']").addClass('snippet').click(function(){
				snippetLauncher.launch($(this));
				return false;
			});
			//snippetExceptions = ['#selected-hightlights li a', '#page.blog h2 a-hill', '#page.recipes h2 a'];
			$('#selected-hightlights li a.snippet').unbind();
			$('#selected-hightlights li a').removeClass('snippet');

			$('#page.blog h2 a.snippet').unbind();
			$('#page.blog h2 a').removeClass('snippet');

			$('#page.recipes h2 a.snippet').unbind();
			$('#page.recipes h2 a').removeClass('snippet');

			$('#page.recipes #selected-recipes a.snippet').unbind();
			$('#page.recipes #selected-recipes a').removeClass('snippet');
			
			$('#page.blog a.snippet').unbind();
			$('#page.blog a').removeClass('snippet');
			
			$('#homepage-feature a.snippet').unbind();
			$('#homepage-feature a').removeClass('snippet');
		}
	},
	launch:function(theObject){

		// gather some data
		this.theWindow.width = (( $(document).width() >= $(window).width() ) ? $(document).width() : $(document).width()) + 'px';
		this.theWindow.height = (( $(document).height() >= $(window).height() ) ? $(document).height() : $(window).height()) + 'px';
		
		this.url = $(theObject).attr("href");
		this.position = theObject.position("top");
		
		$("body").append("<div id='snippet'></div>");
		$("body").append("<div id='snippet-bg'>&nbsp;</div>");
		$("#snippet-bg").css({height:this.theWindow.height, width:this.theWindow.width});
		$("#snippet").css({left:-650, top: (this.position.top -100)});
		
		theUrl = ( this.url.charAt(this.url.length-1) != '/' ) ? this.url+'/' : this.url;
		$('#snippet').load(theUrl+'/snippet #snippet-wrapper', function(data){
			$("#snippet").animate({left: "-3"}, 500, function(){
				//alert('all done');
			});
		});

		$("#snippet-bg, #snippet").mouseup(function(){
			snippetLauncher.close();
		});
		
		$(window).resize(function(){
			$('#snippet-bg').css({
				width:	(( $(document).width() >= $(window).width() ) ? $(document).width() : $(window).width()) + 'px',
				height:	(( $(document).height() >= $(window).height() ) ? $(document).height() : $(window).height()) + 'px'
			});
		});
		
	},
	close: function(){
		$("#snippet").animate({left: "-650"}, 500, function(){
			$("#snippet-bg").remove();
			$("#snippet").remove();
		});
	}	
};


/*
	GoogleMaps
--------------------------------------------------------------------- */
var GoogleMaps = {
	init: function(){
		// intentionally left blank
	},
	attachLocation: function(location){
		// attach action to the #map-link
		var locationImage = "";
		$('#map-link').click(function(){
			
			if($(this).is('.map')){
				$('#location-map-or-image').html(locationImage);
				$(this).removeClass('map');
				$(this).text('Show Map');
			}else{
				locationImage = $('#location-map-or-image').html();
				GoogleMaps.attachMap('location-map-or-image', location);
				$(this).addClass('map');
				$(this).text('Hide Map');
			}
			return false;
		})
		
	},
	attachMap: function(id, location){
		// the map
		if (GBrowserIsCompatible()){
			var map = new GMap2( document.getElementById(id));
			var coordinates = this.getCoordinates(location)
			var latitudeLongitude = new GLatLng(coordinates.x,coordinates.y);
			
			map.setCenter(latitudeLongitude, 15);
			map.addControl(new GLargeMapControl());
			//map.addControl(new GMapTypeControl());
			
			var iconConf = new GIcon();
				iconConf.image = "/images/locationMapIcon.png";
				iconConf.shadow = "/images/logoGoogleMaps_bg.png";
				iconConf.iconSize = new GSize(143, 53);
				iconConf.shadowSize = new GSize(0, 0);
				iconConf.iconAnchor = new GPoint(63, 53);
				iconConf.infoWindowAnchor = new GPoint(0, 0);
				iconConf.infoShadowAnchor = new GPoint(0, 0);
			markerOptions = { icon:iconConf };
			
			map.addOverlay(new GMarker(latitudeLongitude, markerOptions));
		}
	},
	getCoordinates: function(location){
		// the cordinates for the different locations
		// http://maps.google.com/maps/geo?q=YOUR+SEARCH+QUERY+HERE&output=csv&key=YOUR+API+KEY+HERE
		switch(location){
			case 'notting-hill'	: return {x:51.514639,y:-0.199263}; break; 
			case 'islington'	: return {x:51.539551,y:-0.102468}; break;
			case 'kensington'	: return {x:51.502943,y:-0.193359}; break;
			case 'belgravia'	: return {x:51.499224,y:-0.156938}; break;
		}
	}
}
