var currentPage, //alias for current block structural parent and current content block
		currentNode, //alias for current content block
		currentSubNode; //for pages with subcontent, subnode is the alias for secondary/tertiary content (e.g. reviews for a vintage year)


//check if url contains node and page info, if not, set to homepage
if ($.address.path().replace('/', '') != '') {
	var tnode = $.address.path().split('/');
	if (tnode.length > 3 || /reviews/i.test(tnode[1])) {
		if (tnode.length < 3 && /reviews/i.test(tnode[1])) {
			setNodeAndPage(tnode[tnode.length-1]);
		} else {
			setNodeAndPage(tnode[tnode.length-2], tnode[tnode.length-1]);	
		}	
		//console.debug("first condition: "+tnode[tnode.length-2]+", "+tnode[tnode.length-1]);
	} else {
		setNodeAndPage(tnode[tnode.length-1]);
		//console.debug("second condition: "+tnode[tnode.length-1])
	}	
} else {
	setNodeAndPage("homepage");
}


function init() {
	$('#content-wrapper').addClass("page-"+currentNode);
	//console.debug("node: "+currentNode+", page: "+currentPage+", subnode: "+currentSubNode);
	if(currentPage === "homepage") {
		loadCurrentPageContent(currentPage);
	} else {
		goToPage(currentNode);
		//loadCurrentPageContent(currentPage);
	}
	
	$('.logo').bind('click', function(e){
		e.preventDefault();
			$.address.value("/homepage");
			setNodeAndPage("homepage");						
			goToPage("homepage");
	});
	buildMainNavigation();
	buildFooterNavigation();
}

function buildMainNavigation() {	
	var elems = $('.main-nav').find("a");
	var arr = jQuery.makeArray(elems);
	//console.debug(arr);
	
	$(arr).each(function(i){
		var anode;
		anode = $(this).attr('class');		
		$(this).click(function(e){
			e.preventDefault();
			$.address.value($(this).attr('href'));
			//console.debug(anode);
			setNodeAndPage(anode);						
			goToPage(anode);							
		});	
	});
}

function buildFooterNavigation() {		
	$('.trade').click(function(e){
		e.preventDefault();
		$.address.value('trade');
		//console.debug(anode);
		setNodeAndPage('trade');						
		goToPage('trade');							
	});	
}


function buildVintageMenu() {
	var subNode;
	var elems = $('.menu-vintage').find("a");
	var arr = jQuery.makeArray(elems);
	//console.debug(arr);
	
	$(arr).each(function(i){
		var anode;		
		$(this).click(function(e){
			e.preventDefault();
			var $menuItems = $('.menu-vintage a');
			$menuItems.removeClass('current');
			if(/reviews/i.test($.address.value())) {
				if($(this).attr('href').substr(0,4) == 'http') {
					var hrefArray = $(this).attr('href').split('/');
					var tempAddress = hrefArray[hrefArray.length-1];
					$.address.value(currentNode+"/"+tempAddress);	
				} else {
					$.address.value(currentNode+$(this).attr('href'));					
				}
				
				anode = $(this).attr("id");
				$('#review-details h1').html($(this).html());
			} else if(/our-wines_overview\/our-wines/i.test($.address.value())) {
				
				if($(this).attr('href').substr(0,4) == 'http') {
					var hrefArray = $(this).attr('href').split('/');
					anode = hrefArray[hrefArray.length-1];					
				} else {
					anode = $(this).attr('href').substr(1, $(this).attr('href').length);					
				}
				$.address.value(currentPage+"/"+anode);
			}	
			
			$(this).addClass('current');
			loadSubContent(anode);			
		});	
	});
	
	if(/reviews/i.test($.address.value())) {
		subNode = $('a[href="/'+currentSubNode+'"]').attr('id');
		$('#review-details h1').html(currentSubNode);
	}  else {
		subNode = currentSubNode;
	}
	
	if(currentSubNode && currentSubNode != '') {
		//console.debug(currentSubNode);	
		loadSubContent(subNode);
		$('.menu-vintage').find('a[href="/'+currentSubNode+'"]').addClass('current');
		
	} else {
		//load default content
		var defaultSubNode, subAddress;
		if($(arr[0]).attr('href').substr(0,4) == 'http') {
			var hrefArray = $(arr[0]).attr('href').split('/');
			subAddress = hrefArray[hrefArray.length-1];
		} else {
			subAddress = $(arr[0]).attr('href').substr(1, $(arr[0]).attr('href').length);
		}
			
		if(/reviews/i.test($.address.value())) {
			$.address.value(currentNode+"/"+subAddress);
			defaultSubNode = $(arr[0]).attr('id');
			$('#review-details h1').html(subAddress);
			
		} else if(/our-wines_overview\/our-wines/i.test($.address.value())) {
			$.address.value(currentPage+"/"+subAddress);
			defaultSubNode = subAddress;
			
		}	
		
		//console.debug(defaultSubNode);
		$(arr[0]).addClass('current');
		loadSubContent(defaultSubNode);
	}
	if(/zinfandel/i.test($.address.value())) {
		$('.menu-vintage-wrapper').remove();
	}
	
	if (!Modernizr.csscolumns) {
			$('.menu-vintage').columnize({columns: 2 });
		}
}

function loadCurrentPageContent(page) {
	
	var loadContent = "/index.php/"+page;
	$('#content-wrapper').load(loadContent, function() {
	
		showPage();

		// console.debug("loadCurrentPageContent("+loadContent+") cw load post showpage");
		// console.debug("length: "+$('.scroll-pane').length)
		
		$('.scroll-pane').jScrollPane();
		if (!Modernizr.csscolumns) {
			$('.col-multi').columnize({columns: 2 });
		}
		
		if (/our-family_overview/i.test(currentNode) && Modernizr.csscolumns) {
			$('.col-multi').columnize({columns: 2 });
			
		}
		
	  	
	  if (/our-wines_overview\/our-wines/i.test(loadContent)) {
	  	buildVintageMenu();
	  	
	  } else if(/reviews/i.test(loadContent)) {
	  	var pageTitle;
	  	if(currentNode == 'reviews_special-selection-cabernet-sauvignon') {
	  		pageTitle = 'Special Selection Cabernet Sauvignon';
	  	} else if(currentNode == 'reviews_napa-valley-cabernet-sauvignon') {
	  		pageTitle = 'Napa Valley Cabernet Sauvignon';
	  	}
	  	$('.reviews-page-header').html(pageTitle);
	  	buildVintageMenu();
	  	
	  } else {
	  
	  	updatePageTitle();
	  }
	});
	
}

function loadSubContent(content) {
	var loadContent = "/index.php/"+currentPage+"/"+content;
	//console.debug(loadContent);
	if(/reviews/i.test(loadContent)) {
		$('.reviews-list').remove();
		$('#review-details').append('<div class="reviews-list"> </div>');
		if(!content || content == 'undefined') {
			location.href = '/index.php/404';
		} else {
			$('.reviews-list').load("/index.php/review_listing/"+content, function() {
			  $('.reviews-list').jScrollPane();	
			  updatePageTitle();
			});
		}
	} else if(/our-wines_overview\/our-wines/i.test(loadContent)) {
	
		$('#vintage-details').load(loadContent, function() {
		  $('.scroll-pane').jScrollPane();	
		  updatePageTitle();
		});
	}
	
}


function goToPage(location) {
	switch (location) {
		case 'homepage':
			$('#main-book').booklet(0); 
			break;
		case 'our-family_overview':
			$('#main-book').booklet(2); 
			break;
		case 'our-family_history':
			$('#main-book').booklet(4); 
			break;
		case 'our-family_vineyards':
			$('#main-book').booklet(6); 
			break;
		case 'our-wines_overview':
			$('#main-book').booklet(8); 
			break;
		case 'our-wines_special-selection-cabernet-sauvignon':
			$('#main-book').booklet(10); 
			break;
		case 'our-wines_napa-valley-cabernet-sauvignon':
			$('#main-book').booklet(12); 
			break;
		case 'our-wines_napa-valley-zinfandel':
			$('#main-book').booklet(14); 
			break;
		case 'reviews_special-selection-cabernet-sauvignon':
			$('#main-book').booklet(16); 
			break;
		case 'reviews_napa-valley-cabernet-sauvignon':
			$('#main-book').booklet(18); 
			break;
		case 'visit-us':
			$('#main-book').booklet(20); 
			break;
		case 'guestbook':
			$('#main-book').booklet(22); 
			break;
		case 'trade':
			$('#main-book').booklet(24); 
			break;
		
	}
	
}
function preTurn() {
	hidePage();
	
	//console.debug(currentPage);
}

function postTurn() {
	$('#content-wrapper').removeClass();
	$('#content-wrapper').addClass("page-"+currentNode);
	loadCurrentPageContent(currentPage);
	
		
	//console.debug(currentPage);
}

function hidePage() {
	$(".main-nav, .utility-nav, #content-wrapper").fadeOut();
	$('#loading-spinner').fadeIn();
	$('.content').remove();
}

function showPage() {
	$(".main-nav, .utility-nav, #content-wrapper").fadeIn();
	$('#loading-spinner').fadeOut();
}

function setNodeAndPage(selector, subcontent){
	currentSubNode = '';
	switch (selector) {
		case 'homepage':
			currentNode = 'homepage';
			currentPage = 'homepage'; 
			break;
		case 'our-family_overview':
			currentNode = 'our-family_overview';
			currentPage = 'our-family_overview'; 
			break;
		case 'our-family_history':
			currentNode = 'our-family_history';
			currentPage = 'our-family_overview/our-family_history'; 
			break;
		case 'our-family_vineyards':
			currentNode = 'our-family_vineyards';
			currentPage = 'our-family_overview/our-family_vineyards'; 
			break;
		case 'our-wines_overview':
			currentNode = 'our-wines_overview';
			currentPage = 'our-wines_overview'; 
			break;
		case 'our-wines_special-selection-cabernet-sauvignon':
			currentNode = 'our-wines_special-selection-cabernet-sauvignon';
			currentPage = 'our-wines_overview/our-wines_special-selection-cabernet-sauvignon'; 
			break;
		case 'our-wines_napa-valley-cabernet-sauvignon':
			currentNode = 'our-wines_napa-valley-cabernet-sauvignon';
			currentPage = 'our-wines_overview/our-wines_napa-valley-cabernet-sauvignon'; 
			break;
		case 'our-wines_napa-valley-zinfandel':
			currentNode = 'our-wines_napa-valley-zinfandel';
			currentPage = 'our-wines_overview/our-wines_napa-valley-zinfandel'; 
			break;
		case 'reviews_special-selection-cabernet-sauvignon':
			currentNode = 'reviews_special-selection-cabernet-sauvignon';
			currentPage = 'reviews/12'; 
			break;
		case 'reviews_napa-valley-cabernet-sauvignon':
			currentNode = 'reviews_napa-valley-cabernet-sauvignon';
			currentPage = 'reviews/13';
			break;
		case 'visit-us':
			currentNode = 'visit-us';
			currentPage = 'visit-us'; 
			break;
		case 'guestbook':
			currentNode = 'guestbook';
			currentPage = 'guestbook'; 
			break;
		case 'trade':
			currentNode = 'trade';
			currentPage = 'trade'; 
			break;	
		default: 
			currentNode = '404';
			currentPage = '404'; 
			$.address.value('/404');
			location.href = '/index.php/404';
			break;
	}
	if(subcontent) {
		currentSubNode = subcontent;
	}

}

function updatePageTitle() {
	var pageTitle;
	if ($('h1.page-title-container').html() && $('h1.page-title-container').html() != '') {
		pageTitle = "Caymus Vineyards: ";
		
		if(/our-wines_overview\/our-wines/i.test(currentPage)) {
			pageTitle += "Our Wines: ";
			if ($('h2.page-title-container').html() && $('h2.page-title-container').html() != '') {
				pageTitle += $('h2.page-title-container').html();			
			}
			pageTitle += " "+$('h1.page-title-container').html();
		} else  {
			pageTitle = $('h1.page-title-container').html();		
			if ($('h2.page-title-container').html() && $('h2.page-title-container').html() != '') {
				pageTitle += " - " + $('h2.page-title-container').html();			
			}		
		}
	
	
			
	} else { 
		pageTitle = "Caymus Vineyards: "+currentNode.replace(/-/g, ' ').replace(/_/g, ": ");
		pageTitle = toTitleCase(pageTitle);
			
	}
	document.title = pageTitle;
	//console.debug('updatePageTitle: '+pageTitle);
}


function toTitleCase(str) {
    return str.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
}

$(document).ready(function() { 
	$('ul.main-nav').superfish({
		autoArrows:    false,
		dropShadows: 	false
	}); 
	$('#main-book').booklet({
		width:    882,
		height:   692,
		speed:    400,
		keyboard: false,
		pageNumbers:  false,
		hash: false,
		pagePadding: 0,
		before: preTurn,
		after: postTurn
	});
	
	/*Modernizr.load({
	  test: Modernizr.csscolumns,
	  yep: 'js/css3-multi-column.js',
	  nope: 'js/css3-multi-column.js'
	});*/
	
	if (!Modernizr.input.placeholder) {
		$("input[placeholder]").placeholder({ placeholderTextColour: "#999" });
	}
	
	
	$('#content-wrapper p > a').live('click', function(e) {
		e.preventDefault();
	 	if($(this).attr('href').substr(0,1) == '/' && $(this).attr('href').substr(1,4) != 'pdfs' && $(this).attr('href').substr(1,9) != 'downloads') {
	 		var anode;
	 		var ref = $(this).attr('href').substr(1, $(this).attr('href').length);
	 		if(/our-wines_overview/i.test(ref)) {
	 			if($(this).attr('id')) {
	 				anode = $(this).attr('id');
	 			} else {
	 				anode = $(this).attr('class');
	 			}
	 		} else {
	 			anode = ref;
	 		} 		
			$.address.value($(this).attr('href'));
			//console.debug('.internal-link click: '+anode);
			setNodeAndPage(anode);						
			goToPage(anode);
	 	} else if ($(this).attr('href').substr(1,4) == 'pdfs' || $(this).attr('href').substr(1,9) == 'downloads') {
	 		window.location.href = $(this).attr('href');
	 		//console.debug($(this).attr('href').substr(1,9));
	 	}			
	 		
	});
	
});
$(window).load(function() {
     init();
});


