$(document).ready(function(){
  iZindex = 500;
	$('div#header_nav ul li').each(function(){
	  $(this).css('z-index',iZindex);
	  iZindex--;
	});
	
	// Project - Navigation
	$('.large-image div[rel]').hide();
	$('.large-image div[rel=1]').show();
	
	$('.images .nav a').click(function(){
	  var projectID = $(this).attr('href');
	  var image = $(this).attr('rel');
	  $(projectID+' div').hide();
	  $(projectID+' div[rel='+image+']').show();
	  return false;
	});
	
  /* -- Cycle Version of Inner Project Image Nav
  // Image nav within Project
	$('.large-image').each(function(){
		strID = $(this).attr('rel');
		$(this).cycle({
			fx:		'fade',
			speed:'fast',
			timeout: 0,
			pager:'#cycle-pager-'+strID,
			containerResize: 0
		});
	});
	
	// Project - inner Image Navigation
  $('.images .nav a').click(function(){
    strID = $(this).attr('rel');
    strPagerID = $(this).attr('href');
    $(strPagerID+' a:nth-child('+strID+')').click();
    return false;
  });
	*/
  
	// Thumbail Panels - Click
  $('#thumbnail-panels a').click(function(){
  	strID = $(this).attr('rel');
  	if (strID) {
  		iCurrentProject = strID;
	  	changeProject();
  	}
    return false;
  });
  
  // Thumbail Panels - Paging
	$('#thumbnail-panels').cycle({
		fx:		'scrollHorz',
		speed:300,
		next: '#panel_next',
		prev: '#panel_prev',
		pager:'#panel_pager',
		timeout: 0,
		nowrap: 1,
		containerResize: 1
	});
  
  /* Long Description JS
  // Long Description - Toggle
  $('.read-more').click(function(){
    strBlurbID = $(this).attr('href');
    if ($(strBlurbID).is(':visible')) {
    	$(this).html('Read More');
    	$(strBlurbID).slideUp('fast');
    } else {
    	$(this).html('Read Less');
    	$(strBlurbID).slideDown('fast');
    }
    return false;
  });
  
  $('.display .nav a:not(.images .nav a)').click(function(){
    $('.blurb .blurb-more').hide();
  });
  */
  
	// Next project
	$('.project_next').click(function(){
	  iCurrentProject++;
	  if (iCurrentProject > iTotalProjects) iCurrentProject = 1;
	  changeProject();
	  return false;
	});
	
	// Prev project
	$('.project_prev').click(function(){
	  iCurrentProject--;
	  if (iCurrentProject < 1) iCurrentProject = iTotalProjects;
	  changeProject();
	  return false;
	});
	
	changeProject();
  // Project Arrow based nav
  /*
	$('#display-cycle').cycle({
		fx:		'fade',
		speed:'fast',
		next: '#project_next',
		prev: '#project_prev',
		prevNextClick: fnprevNextClick,
		timeout: 0,
		containerResize: 1,
		startingSlide: <?php echo $arProjects[$project_id] ?>
	});
	*/
});

function changeProject() {
	//Reset Read More links
	//$('.blurb-more').hide();
	//$('.read-more').html('Read More');
	
	$('#display-cycle .project').hide();
  $('#display-cycle .project[rel='+iCurrentProject+']').show();
  
  $('#thumbnail-panels a').removeClass('active');
  $('#portfolio-thumb-'+iCurrentProject).addClass('active');
  
  thisPanel = Math.ceil(iCurrentProject/iProjectsPerPage);
  stopMovies();
  $('#panel_pager a:nth-child('+thisPanel+')').click();//Re-enable this line!
}

function fnprevNextClick(isNext, slideIndex, slideElement) {//Not needed?
  //console.log('change project: '+slideIndex);
  slideIndex = slideIndex + 1;
	$('#thumbnail-panels a').removeClass('active');
  $('#portfolio-thumb-'+slideIndex).addClass('active');
}

function getHashValue(key, default_) {
  if (default_==null) default_="";
  key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regex = new RegExp("[\\#&]"+key+"=([^&#]*)");
  var qs = regex.exec(self.document.location.hash);
  if(qs == null)
    return default_;
  else
    return qs[1];
}

function stopMovies() {
	$('.qtmovie').each(function(){
		try {
			this.Stop();
		} catch(e) {}
	});
}
