var DISBLED_SUBNAV_ALPHA = 0.3;
var DISBLED_NAV_ALPHA = 0.9;
var MOUSE_OFF_THUMB_ALPHA = 0.8;

var SECTION_PROJECTS = "projects";
var SECTION_HOME = "home";
var SECTION_RESUME = "resume";
var SECTION_CONTACT = "contact";

var PROJECTS_FLASH = "FLSH";
var PROJECTS_HTML = "HTML";
var PROJECTS_OTHER = "OTHR";

var PROJECT_VIEW_FILE_TYPE_IMG = "IMG";

var currentSubSection;
var currentSubNavCategory;

var currentProjectSection = "";
var currentProjectIndex = 0;
var currentProjectViewList = null;
var currentProject = null;

var projects = new Object();
var projectViews = new Object();

var alertFallback = false;
if (typeof console === "undefined" || typeof console.log === "undefined") {
  console = {};
  if (alertFallback) {
      console.log = function(msg) {
           alert(msg);
      };
  } else {
      console.log = function() {};
  }
}

$(document).ready(function(){
	var leftPos = 0;
	
	
	$(".navItem").hover(function(){$(this).fadeTo(100, DISBLED_NAV_ALPHA);},
						function(){$(this).fadeTo(100, 1);});
	
	switch(currentSection){
		case SECTION_HOME:
			$("#navHome").addClass('navItemSelected');
			leftPos = '455';
			break;
		case SECTION_PROJECTS:
			$("#navProjects").addClass('navItemSelected');
			leftPos = '560';
			
			setupSubNav();
			
			currentSubSection = PROJECTS_FLASH;
			getProjectSection(currentSubSection);
			
			$("#"+currentSubSection).show();
			
			break;
		case SECTION_RESUME:
			$("#navResume").addClass('navItemSelected');
			leftPos = '680';
			
			setupSubNav();
			
			currentSubSection = "subSectionOne";
			break;
		case SECTION_CONTACT:
			$("#navContact").addClass('navItemSelected');
			leftPos = '800';
			break;
		default:
			break;
	}
	
	$('#navDot').animate({left:leftPos}, 0, "swing");
	
	$(".projectViewArrow").click( function(){ 
		if ($(this).attr('id')=="prevProjectView"){ 
			
			if (currentProjectView > 0){
				console.log("before going prev " + currentProjectView);
				currentProjectView--;
				console.log("going prev " + currentProjectView);
				showProjectDetails();
			}
		}
		else{
			if (currentProjectView < currentProjectViewList.length-1){
				console.log("before going next " + currentProjectView);
				currentProjectView++;
				console.log("going next " + currentProjectView);
				showProjectDetails();
			}
		} 
			
  });
	
});

function setupSubNav(){
	$(".subNavItem:not(#subNavItemOne)").fadeTo(100, DISBLED_SUBNAV_ALPHA);
	
	$(".subSection:not(#subSectionOne)").hide();
	
	$(".subNavItem").hover(function(){$(this).fadeTo(100, 1);},
						   function(){
								if ($(this).attr("id") != currentSubNavCategory){
									$(this).fadeTo(100, DISBLED_SUBNAV_ALPHA);
								}
							});
	
	if (currentSection == SECTION_PROJECTS){
		$(".subNavItem").click(changePageSubSectionProjects);
	}
	else{
		$(".subNavItem").click(changePageSubSectionResume);
	}
	
	
}

function changePageSubSectionResume(){
	var oldSubSection = currentSubSection;
	
	currentSubNavCategory = $(this).attr("id");
	
	switch(currentSubNavCategory){
		case "subNavItemOne":
			currentSubSection = "subSectionOne";
			break;
		case "subNavItemTwo":
			currentSubSection = "subSectionTwo";
			break;
		case "subNavItemThree":
			currentSubSection = "subSectionThree";
			break;
		default:
			break;
	}
	
	$(".subNavItem[id!='"+currentSubNavCategory+"']").fadeTo(100, DISBLED_SUBNAV_ALPHA);
	
	console.log("hiding " + oldSubSection + " showing " + currentSubSection);
	$('#'+oldSubSection).fadeOut("fast", "linear", function(){
		$("#"+currentSubSection).fadeIn("fast", "linear");
		});
}

function changePageSubSectionProjects(){
	console.log("sub nav clicked " + $(this).attr("id"));
	var oldSubSection = currentSubSection;
	
	currentSubNavCategory = $(this).attr("id");
	
	switch(currentSubNavCategory){
		case "subNavItemOne":
			currentSubSection = PROJECTS_FLASH;
			break;
		case "subNavItemTwo":
			currentSubSection = PROJECTS_HTML;
			break;
		case "subNavItemThree":
			currentSubSection = PROJECTS_OTHER;
			break;
		default:
			break;
	}
	
	console.log("Current subsection: " + oldSubSection + " new: " + currentSubSection);
	
	$(".subNavItem[id!='"+currentSubNavCategory+"']").fadeTo(100, DISBLED_SUBNAV_ALPHA);
	
	$('#projectDetailViewer').fadeOut(200, 
		function(){
			$('#pageSubSections').fadeOut(200,
				function(){
				
					$("#"+oldSubSection).hide();
					$("#"+currentSubSection).show();
					
					getProjectSection(currentSubSection);
			
					$("#pageSubSections").fadeIn(200);
				});
				
		});
}

function getProjectSection(section){
	
	currentProjectSection = section;
	console.log("getting section " + section);
	if( projects[section] == undefined){
		
		$.getJSON(baseURL + "index.php/projects/get_projects/" + section, function(projectList) {
			console.log(projectList);
			projects[section] = projectList;
			
			$.each(projectList, 
					function(projectIndex, project){
				 		
				 		if (projectIndex%3 == 0){
				 			$("#"+section).append("<div class='projectThumbRow'>");
				 			
				 		}
				 		
				 		$(".projectThumbRow", "#"+section).last().append("<div class='projectThumb' rel='#projectDetailViewer'><div class='projectTitle'>"+project.title+"</div></div>");
				 		$(".projectThumb", $(".projectThumbRow", "#"+section)).last().css('background-image', 'url('+baseURL+'images/projects/'+project.thumb_file_name+'.jpg)');
				 		
				 		$(".projectThumb", $(".projectThumbRow", "#"+section)).last().data('projectIndex', projectIndex)
				 																			   .data('projectType', section)
				 																			   .click(projectSelected);
				 		
				 		
				 	}
			);
			
			
			$(".projectThumb").hover(function(){$(this).fadeTo(200, 1); $('.projectTitle', this).slideDown(200);}, 
									 function(){$(this).fadeTo(200, MOUSE_OFF_THUMB_ALPHA);$('.projectTitle', this).slideUp(200);});
		});
	}
	else{
		console.log("We already have the data for this.  just show it.");
		$('.projectThumb .projectTitle').hide();
		//console.log($("#"+section).html());
	}
	
	//console.log($("#"+section).html());
}

function projectSelected(){
	
	console.log("Getting project views for project index " + $(this).data('projectIndex') + " in section " + currentProjectSection);
	//$('#pageSubSections').fadeOut(200, function(){$('#projectDetailViewer').fadeIn(200);});
	//$('#pageSubSections').fadeOut(200);
	
	currentProject = projects[$(this).data('projectType')][$(this).data('projectIndex')];
	currentProjectIndex = $(this).data('projectIndex');
	currentProjectView = 0;
	
	// Don't have the views yet - need to ask for them.
	if (projectViews[currentProjectSection] == undefined || 
		projectViews[currentProjectSection][$(this).data('projectIndex')] == undefined){
		
		$.getJSON(baseURL + "index.php/projects/get_project_views/" + currentProject.id, 
				  function(projectViewList) {
						projectViews[currentProjectSection] = new Array();
						projectViews[currentProjectSection][$(this).data('projectIndex')] = projectViewList;
						
						currentProjectViewList = projectViewList;
						
						if (projectViewList.length > 0){
							showProjectDetails();
							$('#pageSubSections').fadeOut(200, function(){$('#projectDetailViewer').fadeIn(200);});
						}
					});
	}
	// Have the views - just use those.
	else{
		currentProjectViewList = projectViews[currentProjectSection][$(this).data('projectIndex')];
		if (currentProjectViewList.length > 0){
			showProjectDetails();
			$('#pageSubSections').fadeOut(200, function(){$('#projectDetailViewer').fadeIn(200);});
		}
	}
}

function showProjectDetails(){
	
	var projectView = currentProjectViewList[currentProjectView];
	
	$('#projectImg').fadeOut(100,
			function(){
//				$('img', '#projectImg').attr('src', baseURL + "images/placeholder.gif")
//										.attr('width', "300")
//										.attr('height', "300");
				
				if (projectView.file_type == PROJECT_VIEW_FILE_TYPE_IMG){
					$("#projectImg").html('');
					$("#projectImg").html("<img src='" + baseURL + "images/projects/" + projectView.file_name + ".jpg' width='" + projectView.file_width + "' height='" + projectView.file_height + "' />" );
//					$('img', '#projectImg').attr('src', baseURL + "images/projects/" + projectView.file_name + ".jpg")
//					.attr('width', projectView.file_width)
//					.attr('height', projectView.file_height);
					console.log("Image set to " + baseURL + "images/projects/" + projectView.file_name + ".jpg");
				}
				else{
					//must be a video...
				}
				
				$('#projectDetailTitle').html(currentProject.title);
				
				if (projectView.desc == "" || projectView.desc == null){
					$('#projectDesc').html(currentProject.desc);
				}
				else{
					$('#projectDesc').html(projectView .desc);
				}
				$('#projectToolList').html(currentProject.tools);
				$('#projectRoleList').html(currentProject.role);
				
				if (currentProject.client != null && currentProject.client != ""){
					$('#projectClient').html(currentProject.client);
				}
				else{
					$('#projectClient').html('');
				}
				
				//console.log("url " + currentProject.url);
				if (currentProject.url != null && currentProject.url != ""){
					
					$('#projectURLLink').html('View Project');
					$('#projectURLLink').attr('href', currentProject.url);
				}
				else{
					$('#projectURLLink').html('');
				}
				
				var currentProjectViewNumber = currentProjectView + 1;
				$('#pagerNumbers').text(currentProjectViewNumber + '/' + currentProjectViewList.length);
				
				$('.projectViewArrow').fadeTo(100, 1)
										  .css({'cursor' : 'pointer'});
					
				if (currentProjectView == currentProjectViewList.length-1){
					$('#nextProjectView').fadeTo(100, 0.5)
					  .css({'cursor' : ''});
				}
					
				if (currentProjectView == 0){
					$('#prevProjectView').fadeTo(100, 0.5)
					  .css({'cursor' : ''});
				}
					
				$('#projectImg').fadeIn(100);
				//$('#projectDetails').fadeIn(100);
//				$('#viewPager').fadeIn(100);
		});
}
