var IMG_GRID_EXT = "";//"_sq_thumb.jpg";
var IMG_DETAIL_LARGE_EXT = "_large_slide.jpg";
var IMG_DETAIL_PHOTOROW_EXT =  "_sm_square_thumb.jpg";//"_sq_thumb.jpg";
var GRID_SIZE = 12;
var PHOTOROW_SIZE = 3;
var images = Array();
var nl_detailview_photorow_offset = 0;
var nl_currentImageIdx = 0;
var nl_page = 1;
var nl_gridview_effects = Array("slow", "explode", "slide"); // , "fold", "puff", "scale"

function nl_photo(img, title, author, description) {
  this.img = img;
  this.title = title;
  this.author = author;
  this.description = description;
}

function nl_loadPhotoRow() {
  //alert("Start: " + nl_detailview_photorow_offset + ". Total: " + nl_detailview_photo_count_ttl);
  if (nl_detailview_photorow_offset >= nl_detailview_photo_count_ttl) {
    return false;
  } else if (nl_detailview_photorow_offset < 0) {
    nl_detailview_photorow_offset = 0;
  }

  for (var i = 0; i < PHOTOROW_SIZE; i++) {
    var obj = document.getElementById("img_photorow_" + i);
    if (!obj) { // there are less images than PHOTOROW_SIZE in this album
      continue;
    }
    if (images[i + nl_detailview_photorow_offset]) {
      nl_display_div("img_photorow_" + i, "inline");
      obj.src = images[i + nl_detailview_photorow_offset].img + IMG_DETAIL_PHOTOROW_EXT;
    } else {
      nl_display_div("img_photorow_" + i, "none");
      obj.src = "";
    }
  }
  return true;
}

function nl_loadGrid(page) {
  nl_page = page - 1;
  nl_detailview_photorow_offset = (nl_page * GRID_SIZE);
  for (var i = 0; i < GRID_SIZE; i++) {
    var obj_name = "img_grid_" + i;
    var obj = document.getElementById(obj_name);
    if (!obj) { // there are less images than GRID_SIZE in this album
      continue;
    }
    if (images[i + nl_detailview_photorow_offset]) {
      $("#" + obj_name).css("display", "inline");
      obj.src = images[i + nl_detailview_photorow_offset].img + IMG_GRID_EXT;
    } else {
      $("#" + obj_name).css("display", "none");
      obj.src = "";
    }
  }
  var page_grid_size = GRID_SIZE; //(nl_page == 0 && cpt_featured_img ? GRID_SIZE - 9 : GRID_SIZE); // do not overload images on first page
  _xCaptureNightlifeJS.loadGrid(nl_page, nl_detailview_photo_count_ttl, "img_grid_", nl_detailview_photorow_offset, images, 'nl_gridview_paging', "nl_loadGrid", GRID_SIZE, page_grid_size);
  $("#nl_gridview").show(); // make sure images are displayed
}

function nl_switchViewMode(mode) {
  if (mode == "grid") {
    //nl_display_div('nl_menu_gridview_button', "none");
    nl_display_div('nl_gridview_paging', "block");

    //nl_display_div('nl_gridview', "block");
    //nl_display_div('nl_detailview_photorow_container', "none");
    nl_display_div('nl_detailview', "none");
  } else if (mode == "detail") {
    nl_display_div('nl_menu_gridview_button', "block");
    nl_display_div('nl_gridview_paging', "none");
    nl_display_div('nl_detailview', "block");
    nl_display_div('nl_gridview', "none");
    nl_display_div('nl_detailview_photorow_container', "block");
    $('#nl_gridview').hide(nl_gridview_effects[Math.floor(Math.random() * nl_gridview_effects.length)]);
    $('#nl_detailview').show("slow");
    $('.pagination').hide("slow");
    $('#nl_menu_gridview_button').show("slow");
    $('#nl_detailview_photorow').show("slow");
  }
}

function nl_display_div(obj_id, display) {
  $("#" + obj_id).css("display", display);
  /*
  var obj = document.getElementById(obj_id);
  obj.style['display'] = display;
  */
}

// TODO
function nl_PhotorowCenter(photorow_start) {
  alert("Calculate Photorow Offset: Current: " + nl_detailview_photorow_offset + " Photorow_start = " + photorow_start + "\nTotal: " + nl_detailview_photo_count_ttl);
  if (nl_detailview_photo_count_ttl <= PHOTOROW_SIZE) {
    return;
  }
  nl_detailview_photorow_offset += photorow_start;
  if (nl_detailview_photorow_offset - 3 - 1 >= 0) {
    nl_detailview_photorow_offset = nl_detailview_photorow_offset - 3 - 1; // center the thumbnail
  }
  if (nl_detailview_photorow_offset >= nl_detailview_photo_count_ttl) {
    nl_detailview_photorow_offset = nl_detailview_photo_count_ttl - PHOTOROW_SIZE; // push this thumbnail to right
  }
  alert("Requesting to start at: " + nl_detailview_photorow_offset);
}

function nl_switchDetailViewPicture(photorow_offset) {
	nl_displayPictureByIndex(photorow_offset + nl_detailview_photorow_offset);
}

function nl_displayPictureByIndex(p_nIndexToDisplay) {
  if (!images[p_nIndexToDisplay]) {
    return;
  }
  //nl_PhotorowCenter(photorow_offset);
  //nl_loadPhotoRow();  // NEED TO UPDATE: nl_detailview_photorow_offset
  var obj = document.getElementById("nl_detailview_main_image");
  obj.src = images[p_nIndexToDisplay].img + IMG_DETAIL_LARGE_EXT;
  $("#nl_detailview_title").html(images[p_nIndexToDisplay].title);
  $("#nl_detailview_author").html(images[p_nIndexToDisplay].author);
  $("#nl_detailview_description").html(images[p_nIndexToDisplay].description);
  $("#nl_view_container").css("height", "auto");

  nl_page = parseInt((p_nIndexToDisplay) / GRID_SIZE, 10);

  nl_currentImageIdx = p_nIndexToDisplay;

  nl_updateNextPrefImageButton();
}

function nl_go(slug) {
  if (slug != "") {
    slug += "/";
  }
  document.location="/the-scene/" + slug;
}

function setBackButtonState(p_bIsEnabled){
	if(p_bIsEnabled){
		$('#pagination_back').addClass('b_active');
	}else{
		$('#pagination_back').removeClass('b_active');
	}
}
function setForwardButtonState(p_bIsEnabled){
	if(p_bIsEnabled){
		$('#pagination_forward').addClass('f_active');
	}else{
		$('#pagination_forward').removeClass('f_active');
	}
}

function nl_updateRowPagingButtons(){
	var l_bNotOnFirstPage = !(0 == nl_detailview_photorow_offset);
	var l_bNotOnLastPage = !((nl_detailview_photorow_offset + PHOTOROW_SIZE) > nl_detailview_photo_count_ttl);
	//alert("Not on first page: " +l_bNotOnFirstPage + ", not on last page: " + l_bNotOnLastPage);
	setBackButtonState(l_bNotOnFirstPage);
	setForwardButtonState(l_bNotOnLastPage);
}

function nl_updateNextPrefImageButton(){
	if(nl_currentImageIdx == 0){
		$('#di_pagination .di_previous_page').removeClass('active');
		
	}else{
		$('#di_pagination .di_previous_page').addClass('active');
	}
	if(nl_currentImageIdx >= nl_detailview_photo_count_ttl-1){
		$('#di_pagination .di_next_page').removeClass('active');
	}else{
		$('#di_pagination .di_next_page').addClass('active');
	}
}

$(document).ready(function() {

  //nl_loadGrid(1);
  if (nl_detailview_photo_count_ttl <= PHOTOROW_SIZE) {
	setBackButtonState(false);
	setForwardButtonState(false);
  }

  $('#nl_menu_event').click(function(){
    $('#nl_menu_event_dropdown').slideToggle(400);
	$('#nl_menu_event').toggleClass('arrow_flip');
  });

  $('#nl_menu_buttons_view_all').click(function(event) {
    nl_go("all");
  });

  $('#nl_menu_gridview_button').click(function(event){
    $('#nl_gridview').show(nl_gridview_effects[Math.floor(Math.random() * nl_gridview_effects.length)]);
    $('#nl_detailview').hide('slow');
    nl_switchViewMode("grid", '');
    $('.pagination').show("slow");
    $('#nl_menu_gridview_button').hide("slow");
    $('#nl_detailview_photorow_container').hide("slow");
    $("#nl_view_container").css("height", "auto");
    nl_loadGrid(nl_page + 1);
  });

  $('#pagination_back').click(function() {
    // alert("nl_detailview_photorow_offset = " + nl_detailview_photorow_offset
    // 					+", slide count: " + nl_detailview_photo_count_ttl);
    if (nl_detailview_photorow_offset >= 0) {
      if (nl_detailview_photorow_offset > 0) {
        nl_detailview_photorow_offset -= PHOTOROW_SIZE;
        nl_page = parseInt(nl_detailview_photorow_offset / PHOTOROW_SIZE, 10);
      }
      nl_loadPhotoRow();
	  nl_updateRowPagingButtons();
    }
  });
  $('#pagination_forward').click(function() {
    // alert("nl_detailview_photorow_offset = " + nl_detailview_photorow_offset 
    // 					+", slide count: " + nl_detailview_photo_count_ttl);
    if (nl_detailview_photorow_offset + PHOTOROW_SIZE <= nl_detailview_photo_count_ttl) {
      if (nl_detailview_photorow_offset < nl_detailview_photo_count_ttl) {
        nl_detailview_photorow_offset += PHOTOROW_SIZE;
        nl_page = parseInt(nl_detailview_photorow_offset / PHOTOROW_SIZE, 10);
      }
      nl_loadPhotoRow();
	  nl_updateRowPagingButtons();
    }
  });

	$('#di_pagination .di_previous_page').click(function() {
		//alert("nl_currentImageIdx: " + nl_currentImageIdx);
		if(nl_currentImageIdx > 0){
			nl_currentImageIdx--;
		}
		nl_displayPictureByIndex(nl_currentImageIdx);
	});
	$('#di_pagination .di_next_page').click(function() {
		//alert("nl_currentImageIdx: " + nl_currentImageIdx);
		if(nl_currentImageIdx < nl_detailview_photo_count_ttl-1){
			// l_bNewPage = (nl_currentImageIdx % PHOTOROW_SIZE) ==  (PHOTOROW_SIZE - 1);
			// 			nl_loadPhotoRow();
			nl_currentImageIdx++;
		}
		nl_displayPictureByIndex(nl_currentImageIdx);
	});


});
