var currentPage="home";
var currentProject=0;
var numberOfProjects = 0;
var numberOfProjectImages = 0;
var locationName = 'index.html';
var pagesArray = new Array('index.html', 'works.html', 'history.html', 'books.html', 'cv.html', 'contact.html');
var numberOfPages = 6;
var pageId = 0;
var urlParam = '';

$(document).ready(function() {
	fileName = window.location.href;
	var end = (fileName.indexOf("?") == -1) ? fileName.length : fileName.indexOf("?");
    locationName = fileName.substring(fileName.lastIndexOf("/")+1, end);
    
    
    
	if(locationName == 'works.html') {
		numberOfProjects = 9;
		currentPage = "works";
		pageId = 1;
		loadXMLNav(currentPage);
		getProject(currentPage, currentProject);
		
	} else if(locationName == 'history.html') {
		numberOfProjects = 7;
		currentPage = "history";
		pageId = 2;
		loadXMLNav(currentPage);
		getProject(currentPage, currentProject);
	} else if(locationName == 'index.html') {
		currentPage = "home";
		pageId = 0;
		
	} else if(locationName == 'cv.html'){
		currentPage = "cv";
		pageId = 4;
		showGallery();
	} else if(locationName == 'books.html'){
		currentPage = "books";
		pageId = 3;
		//loadXMLNav(currentPage);
		getProject(currentPage, currentProject);
	} else if(locationName == 'contact.html') {
		currentPage = "contact";
		pageId = 5;
		showGallery();
	} else {
		currentPage = "home";
		pageId = 0;
		
	}
	
	
	if(currentPage == 'home') {
		showHomeVideo();	
		$("#slideShow").css('z-index', '-1000');
		$("#slideShowVideo").css('z-index', '-1000');
		$("#slideShowVideo object").css('z-index', '-1000');
		$("#slideShowVideo embed").css('z-index', '-1000');
	}
	
	$("#nav01,#nav02,#nav03,#nav04,#nav05").hover(function() {
		$(this).animate({opacity: 0.5}, 500);
	}, function() {
		$(this).animate({opacity: 1}, 500);
	});
	
	
	
	$(document).bind('keyup', function (e) {
		writeKeyCookie();
		if(e.which == 87) {	
			if(pageId != numberOfPages-1) {
				window.location = pagesArray[pageId+1];
			} else {
				window.location = pagesArray[0];
			}
		}
		
		if(currentPage == 'home'){
			if(e.which == 37) {
					$("body").stop().clearQueue().scrollTo('-='+$(window).width()/2, 1000, {axis: 'x'});	
			} else if(e.which == 39) {
					$("body").stop().clearQueue().scrollTo('+='+$(window).width()/2, 1000, {axis: 'x'});
			}	
		}
		
		if(currentPage == 'works') {
			$('#notice').fadeOut();
			if(e.which == 37) {
				$("body").stop().clearQueue().scrollTo('-='+$(window).width()/2, 1000, {axis: 'x'});	
			} else if(e.which == 39) {
					$("body").stop().clearQueue().scrollTo('+='+$(window).width()/2, 1000, {axis: 'x'});
			} else if(e.which == 38) {
				if(currentProject != 0) {
					getProject(currentPage, currentProject-1);
				}
			} else if(e.which == 40) {			
				if(currentProject != numberOfProjects-1) {
					getProject(currentPage, currentProject+1);
				}
			}
		}
		
		if(currentPage == 'history') {
			$('#notice').fadeOut();
			if(e.which == 37) {
				$("body").stop().clearQueue().scrollTo('-='+$(window).width()/2, 1000, {axis: 'x'});	
			} else if(e.which == 39) {
					$("body").stop().clearQueue().scrollTo('+='+$(window).width()/2, 1000, {axis: 'x'});
			} else if(e.which == 38) {
				if(currentProject != 0) {
					getProject(currentPage, currentProject-1);	
				}
			} else if(e.which == 40) {			
				if(currentProject != numberOfProjects-1) {
					getProject(currentPage, currentProject+1);		
				}
			}
		}
		
		if(currentPage == 'books') {
			$('#notice').fadeOut();
			if(e.which == 37) {
				$("body").stop().clearQueue().scrollTo('-='+$(window).width()/2, 1000, {axis: 'x'});	
			} else if(e.which == 39) {
					$("body").stop().clearQueue().scrollTo('+='+$(window).width()/2, 1000, {axis: 'x'});
			} else if(e.which == 38) {
				if(currentProject != 0) {
					getProject(currentPage, currentProject-1);	
				}
			} else if(e.which == 40) {			
				if(currentProject != numberOfProjects-1) {
					getProject(currentPage, currentProject+1);		
				}
			}
		}
		
		if(currentPage == 'cv'){
			if(e.which == 38) {
				$('#notice').fadeOut();
				scrollCv('up');
				
			} else if(e.which == 40) {	
				$('#notice').fadeOut();
				scrollCv('down');
				
			}
		}
});
		
});

$(document).load(function() {
	getProjects(currentPage, currentProject);
	
});
		
			
$(window).resize(function() {
	rescaleContainer();
});

$(window).load( function() {
	rescaleContainer()
	
});

function showHomeVideo() {
	var flashvars = {};
	var params = { wmode: "opaque" };
	var attributes = {};
	swfobject.embedSWF("http://vimeo.com/moogaloop.swf?clip_id=26111842&server=vimeo.com&show_title=0&show_byline=0&show_portrait=0&color=00adef&fullscreen=1&autoplay=1&loop=1", "slideShowVideo", $(window).width(), $(window).height(), "9.0.0", "js/expressinstall.swf", flashvars, params, attributes);
}


function scrollCv(direction1) {
	if(direction1 == 'up') {
		
		if(($('#cvContainer').css('top') < '0px')) {
			$('#cvContainer').stop().clearQueue().animate({top:'+='+($(window).height()/2)+'px' },1000);	
		} 
	} else if(direction1 == 'down') {
		var offsetScreen = ($('#cvContainer').height() - $(window).height());
		var currentPos = $('#cvContainer').position(); 
		var currentScreen = currentPos.top*(-1);
		 
		if(  currentScreen < offsetScreen) {
			$('#cvContainer').stop().clearQueue().animate({top:'-='+($(window).height()/2)+'px' },1000);	
		}		
	}
}

function loadXMLNav() {
	
	var navContent = '';
	$.ajax({
		cache:false,
        type: "GET",
		url: currentPage+".xml",
		dataType: "xml",
		success: function(xml) {
 			$(xml).find('item').each(function(o){
 				var title = $(this).find('projecttitle').text() + '<br/>';
 				if(currentPage == 'works') {
 					var output1 = '<h1 onclick="getProject(\''+currentPage+'\','+o+')"><img src="assets/'+currentPage+'_0'+o+'.jpg" border="0" id="subNav0'+o+'"/></h1>';	
 				} else {
 					var output1 = '<h1 onclick="getProject(\''+currentPage+'\','+o+')"><img src="assets/'+currentPage+'_0'+o+'.png" border="0" id="subNav0'+o+'"/></h1>';
 				}
 				
 				navContent = navContent.concat(output1);
 							
 			});
 			$("#subNav").html(navContent);
			$("#subNav01,#subNav02, #subNav03, #subNav04, #subNav05, #subNav06, #subNav07, #subNav08, #subNav09, #subNav10, #subNav11").hover(function() {
					$(this).animate({opacity: 0.5}, 500);
				}, function() {
					$(this).animate({opacity: 1}, 500);
			});
			if(currentPage == 'works') {
				$("#subNav0"+currentProject).attr("src", "assets/"+currentPage+"_0"+currentProject+"_active.jpg");	
			} else {
				$("#subNav0"+currentProject).attr("src", "assets/"+currentPage+"_0"+currentProject+"_active.png");
			}
			
		
		}
	});
}

function showGallery() {
	$("#slideShow").html('<div style="padding-top:200px; color:#FFF; font-size: 12px;">LOADING</div>');	
	var imagesOutput = '';
	
	$.ajax({
		cache:false,
        type: "GET",
		url: "home.xml",
		dataType: "xml",
		success: function(xml) {
			var imagesOutput = '';
			var imageArr1 = new Array();
 			$(xml).find('item').each(function(o){
 				$(this).find('images').each(function(p){
 					var imageTemp = $(this).attr('src');
 					var imageTempWidth = $(this).attr('width');
 					var imageTempHeight = $(this).attr('height');
 					var imageOutput = '<img src="assets/works/'+imageTemp+'" width="'+imageTempWidth+'" height="'+imageTempHeight+'" border="0"/ class="slideImg" id="slideImg_'+p+'">';
 					imagesOutput = imagesOutput.concat(imageOutput);
 					imageArr1[p] = 'assets/works/'+imageTemp;
 					numberOfProjectImages = p;
 				});
			});			
			preloadImages(imageArr1, imagesOutput);
		}
	});
	
}

function getProject(categoryName, projectId) {
	
	$("body").scrollTo(0, 100, {axis: 'x'});	
	
	/* manage subNav */
	if(currentPage == 'works') {
		$("#subNav0"+currentProject).attr("src", "assets/"+currentPage+"_0"+currentProject+".jpg")
		$("#subNav0"+projectId).attr("src", "assets/"+currentPage+"_0"+projectId+"_active.jpg")	
	} else {
		$("#subNav0"+currentProject).attr("src", "assets/"+currentPage+"_0"+currentProject+".png")
		$("#subNav0"+projectId).attr("src", "assets/"+currentPage+"_0"+projectId+"_active.png")	
	}
	
	
	$("#slideShow").html('<div style="padding-top:200px; color:#FFF; font-size: 12px;">LOADING</div>');	
	var imagesOutput = '';
	
	$.ajax({
		cache:false,
        type: "GET",
		url: categoryName+".xml",
		dataType: "xml",
		success: function(xml) {
			var imagesOutput = '';
			var imageArr1 = new Array();
 			$(xml).find('item').each(function(o){
 				if(o == projectId) {
 					$(this).find('images').each(function(p){
 						var imageTemp = $(this).attr('src');
 						var imageTempWidth = $(this).attr('width');
 						var imageTempHeight = $(this).attr('height');
 						var infoTemp = $(this).attr('info');
						var titleTemp = $(this).attr('title');
 						if(currentPage == 'books') {
							if(infoTemp != '') {
								var imageOutput = '<a href="'+infoTemp+'" target="_blank"><img alt="-" alt2="'+titleTemp+'" src="assets/'+categoryName+'/'+imageTemp+'" width="'+imageTempWidth+'" height="'+imageTempHeight+'" border="0"/ class="slideImg" id="slideImg_'+p+'" border="0"></a>';	
							} else {
								var imageOutput = '<img alt="-" alt2="'+titleTemp+'" src="assets/'+categoryName+'/'+imageTemp+'" width="'+imageTempWidth+'" height="'+imageTempHeight+'" border="0"/ class="slideImg" id="slideImg_'+p+'" border="0">';	
							}
 						} else {
 							var imageOutput = '<img alt="'+infoTemp+'" src="assets/'+categoryName+'/'+imageTemp+'" width="'+imageTempWidth+'" height="'+imageTempHeight+'" border="0"/ class="slideImg" id="slideImg_'+p+'">';	
 						}			
 						
 						imagesOutput = imagesOutput.concat(imageOutput);
 						imageArr1[p] = 'assets/'+categoryName+'/'+imageTemp;
 						numberOfProjectImages = p;
 					});
 				}
			});			
			preloadImages(imageArr1, imagesOutput);
			currentProject = projectId;
			
			
		}
	});
}

function rescaleContainer() {
	//$("body").height($(window).height());
	//$(document).height($(window).height());
	
	$("#slideShow").height($(window).height());
	$("[id*='slideImg_']").each(function(){											 
		var oldHeight = $(this).height();
		var oldWidth = $(this).width();
		$(this).height($(window).height());
		var ratio = oldHeight / oldWidth
		if(oldHeight < $(window).height) {
			var newWidth = Math.round($(this).height() * ratio);		
		} else {
			var newWidth = Math.round($(this).height() / ratio);
		}
		$(this).width(newWidth);
	});
	getContainerWidth();
	positionNavigation();	
}

function getContainerWidth() {
	var returnWidth = 1;
	$(".slideImg").each(function(p) {
		returnWidth = returnWidth + $(this).width();
	});
	$("body").width(returnWidth);
	$("#slideShow").width(returnWidth);
}

function positionNavigation() {
	var subNavOffset = $(window).width() - $("#subNav").width();
	$("#subNav").css({"left": subNavOffset+"px", "float": "right", "position": "fixed"});
	var arrowOffsetTop = $(window).height() - 100;
	var arrowOffsetRight = $(window).width() - $("#scrollRight").width();
	$("#scrollLeft").css({"left": "0px", "top": arrowOffsetTop+"px", "float":"left", "position" : "fixed"});
	$("#scrollRight").css({"left": arrowOffsetRight+"px", "top": arrowOffsetTop+"px", "float":"left", "position" : "fixed"});
	$("#info").css({"left": "0px", "top":"200px", "float":"left", "position" : "absolute"});
	
	//position notice
	if(checkKeyCookie()) {
		$('#notice').css('display', 'none');	
	} else {
		var noticeTop = ($(window).height()) - $('#notice').height() - 90; 
		var noticeLeft = ($(window).width() / 2) - ($('#notice').width()/2);
		$('#notice').css({"left": noticeLeft+"px", "top":noticeTop+"px", "position": "fixed"});
	}
	
	
}

function writeKeyCookie() {
	$.cookie('narcisseScroll', 'true', { expires: 365, path: '/', domain: 'narcissetordoir.com' });
}

function checkKeyCookie() {
	var cookieValue = $.cookie('narcisseScroll');
	if(cookieValue == 'true') {
		return true
	} else {
		return false;
	}
}

function scrollImages(direction) {
	if(direction == 'left') {
		$("body").stop().clearQueue().scrollTo('-='+$(window).width()/2, 1000, {axis: 'x'});
	} else if (direction == 'right') {
		$("body").stop().clearQueue().scrollTo('+='+$(window).width()/2, 1000, {axis: 'x'});
	}	
}

function preloadImages(imageArr, htmlOutput) {
	$("#slideShow").html(htmlOutput);	
	rescaleContainer();
	$('div#slideShow img').each(function() {
		$(this).hover(function() {
			var infoInput = $(this).attr('alt');
			var titleInput = $(this).attr('alt2')
			if(infoInput != '') {	
				if(currentPage == 'books') {
					var infoInput = titleInput; 
				}
				$('.info').html(infoInput);
				var s = $(this).position();
				var pleft = s.left;
				var ptop = s.top;
				var offsetLeft = pleft;
				var offsetTop = ptop + $(this).height() - $('.info').height() - 200;
				$('.info').css({"left":offsetLeft+"px","top":offsetTop+"px", "display":"block"});
			}
		}, function() {
			$('.info').css({"display":"none"});
		});
	});	
}

