var IMG_GRID_EXT = "_sq_thumb.jpg";
var IMG_DETAIL_LARGE_EXT = "_large_slide.jpg";
var IMG_FEATURED = "_largesq_thumb.jpg";
var GRID_SIZE = 35;
var PHOTOROW_SIZE = 7;
var IMAGE_URL = "/files/";

var images = new Array(); // currently used array of images
var imagesAll = new Array(); // all images go here
var imagesEditorsPick = new Array(); // only editor's picks go here
var imagesMostViewed = new Array(); // only most viewed go here

var cpt_detailview_photorow_offset = 0;
var cpt_page = 1;
var cpt_detailview_photo_count_ttl = 0;
var cpt_featured_img = false;

var cpt_view_container_height_grid = 700; // store globally so we can switch between view modes
var cpt_view_container_height_detail = 750; // store globally so we can switch between view modes

var cpt_menu_buttons = new Array("btnViewMostMode", "btnViewEditorsMode", "btnViewAllMode");

function cpt_switchMenuButton(current) {
  $("#cpt_menu_buttons div").each(function(i) {
    $(this).removeClass("hover");
  });
  $("#" + current).addClass("hover");
}

function cpt_loadGrid(page) {
  $('.cpt_gridview_editors_pick').hide(); // hide all editor's picks star images and rollover texts
  $('#cpt_gridview_main_image').hide(); // do not display editor's pick large version if not first page
  if (page == 1 && cpt_featured_img) {
    $('#cpt_gridview_main_image').show(); // display editor's pick large version if on first page
  }
  cpt_page = page - 1;
  cpt_detailview_photorow_offset = (cpt_page * GRID_SIZE);
  cpt_detailview_photorow_offset = (cpt_page == 0 ? 0 : cpt_detailview_photorow_offset - 9);
  var page_grid_size = (cpt_page == 0 && cpt_featured_img ? GRID_SIZE - 9 : GRID_SIZE); // do not overload images on first page
  _xCaptureNightlifeJS.loadGrid(cpt_page, cpt_detailview_photo_count_ttl, "img_grid_", cpt_detailview_photorow_offset, images, 'cpt_gridview_paging', "cpt_loadGrid", GRID_SIZE, page_grid_size);
}

function cpt_switchViewMode(mode) {
  if (mode == "grid") {
    $('#cpt_gridview').show('slow');
    $('#cpt_detailview').hide('slow');
    $('.pagination').show("slow");
    $('#cpt_menu_gridview_button').fadeOut(500);
    $('#cpt_detailview_photorow').hide("slow");
    //_xCaptureNightlifeJS.displayDiv('cpt_menu_gridview_button', "none");
    _xCaptureNightlifeJS.displayDiv('cpt_gridview_paging', "block");

    //_xCaptureNightlifeJS.displayDiv('cpt_gridview', "block");
    //_xCaptureNightlifeJS.displayDiv('cpt_detailview_photorow_container', "none");
    _xCaptureNightlifeJS.displayDiv('cpt_detailview', "none");
    // setup proper height based on the number of images
    $('#cpt_view_container').css('height', cpt_view_container_height_grid + "px");
  } else if (mode == "detail") {
    _xCaptureNightlifeJS.displayDiv('cpt_menu_gridview_button', "block");
    _xCaptureNightlifeJS.displayDiv('cpt_gridview_paging', "none");

    _xCaptureNightlifeJS.displayDiv('cpt_detailview', "block");
    _xCaptureNightlifeJS.displayDiv('cpt_gridview', "none");
    _xCaptureNightlifeJS.displayDiv('cpt_detailview_photorow_container', "block");
    $('#cpt_view_container').css('height', cpt_view_container_height_detail + "px");
  }
}

// TODO
function cpt_PhotorowCenter(photorow_start) {
  alert("Calculate Photorow Offset: Current: " + cpt_detailview_photorow_offset + " Photorow_start = " + photorow_start + "\nTotal: " + cpt_detailview_photo_count_ttl);
  if (cpt_detailview_photo_count_ttl <= PHOTOROW_SIZE) {
    return;
  }
  cpt_detailview_photorow_offset += photorow_start;
  if (cpt_detailview_photorow_offset - 3 - 1 >= 0) {
    cpt_detailview_photorow_offset = cpt_detailview_photorow_offset - 3 - 1; // center the thumbnail
  }
  if (cpt_detailview_photorow_offset >= cpt_detailview_photo_count_ttl) {
    cpt_detailview_photorow_offset = cpt_detailview_photo_count_ttl - PHOTOROW_SIZE; // push this thumbnail to right
  }
  alert("Requesting to start at: " + cpt_detailview_photorow_offset);
}

function cpt_switchDetailViewPicture(id) {
  _xCaptureNightlifeJS.switchDetailViewPicture(id, cpt_detailview_photorow_offset, "cpt_detailview_main_image", images, cpt_featured_img,
                                               "cpt_detailview_title", "cpt_detailview_author", "cpt_detailview_description", "/captures/")
}

function cpt_loadDetailViewPicture(photorow_start) {
  _xCaptureNightlifeJS.loadDetailViewPicture(photorow_start, cpt_detailview_photorow_offset, "cpt_detailview_main_image", cpt_detailview_photo_count_ttl, PHOTOROW_SIZE, "img_photorow_",
                                             images, cpt_featured_img, "cpt_detailview_title", "cpt_detailview_author", "cpt_detailview_description", "/captures/");

  $('#cpt_gridview').hide('slow');
  $('#cpt_detailview').show("slow");
  cpt_switchViewMode("detail");
  $('.pagination').hide("slow");
  $('#cpt_menu_gridview_button').show("slow");
  $('#cpt_detailview_photorow').show("slow");
  cpt_page = parseInt((photorow_start + cpt_detailview_photorow_offset) / GRID_SIZE, 10);
//$('#cpt_detailview').show("slow");
//cpt_switchViewMode("detail");
//$('.pagination').hide("slow");
//$('#cpt_menu_gridview_button').fadeIn(500);
//$('#cpt_detailview_photorow').show("slow");
}

function cpt_init() {
  cpt_switchViewMode("grid"); // in case user clicks on type buttons (most viewed, editor's picks, view all
  cpt_loadGrid(1);

  if (cpt_detailview_photo_count_ttl <= PHOTOROW_SIZE) {
    $('#cpt_detailview_photorow_container_left').hide();
    $('#cpt_detailview_photorow_container_right').hide();
  } else {
    $('#cpt_detailview_photorow_container_left').show();
    $('#cpt_detailview_photorow_container_right').show();
  }

  $('#cpt_menu_gridview_button').click(function(event){
    cpt_switchViewMode("grid");
  });

  $("div.cpt_gridview_editors_pick").hover(
    function(){$(this).find('div.cpt_gridview_editors_pick_rollover:hidden').fadeIn(500);},
    function(){$(this).find('div.cpt_gridview_editors_pick_rollover:visible').fadeOut(500);}
  );

  $("div.cpt_gridview_editors_pick_main_image").hover(
    function(){$(this).find('div.cpt_gridview_editors_pick_rollover_main_image:hidden').fadeIn(500);},
    function(){$(this).find('div.cpt_gridview_editors_pick_rollover_main_image:visible').fadeOut(500);}
  );

//$('#cpt_menu_event').click(function(){
//  $('#cpt_menu_event_dropdown').slideToggle(400);
//  $('#cpt_menu_event').toggleClass('arrow_flip');
//});

  $('#cpt_menu_gridview_button').click(function(event){
    $('#cpt_gridview').show('slow');
    $('#cpt_detailview').hide('slow');
    cpt_switchViewMode("grid");
    $('.pagination').show("slow");
    $('#cpt_menu_gridview_button').hide("slow");
    $('#cpt_detailview_photorow_container').hide("slow");
//  $("#cpt_view_container").css("height", "auto");
    cpt_loadGrid(cpt_page + 1);
  });

  $('#cpt_detailview_photorow_container_left').click(function() {
    //alert("cpt_detailview_photorow_offset = " + cpt_detailview_photorow_offset);
    if (cpt_detailview_photorow_offset >= 0) {
      if (cpt_detailview_photorow_offset > 0) {
        cpt_detailview_photorow_offset--;
        cpt_page = parseInt(cpt_detailview_photorow_offset / GRID_SIZE, 10);
      }
      _xCaptureNightlifeJS.loadPhotoRow(cpt_detailview_photorow_offset, cpt_detailview_photo_count_ttl, PHOTOROW_SIZE, "img_photorow_");
    }
  });
  $('#cpt_detailview_photorow_container_right').click(function() {
    //alert("cpt_detailview_photorow_offset = " + cpt_detailview_photorow_offset);
    if (cpt_detailview_photorow_offset + PHOTOROW_SIZE <= cpt_detailview_photo_count_ttl) {
      if (cpt_detailview_photorow_offset < cpt_detailview_photo_count_ttl) {
        cpt_detailview_photorow_offset++;
        cpt_page = parseInt(cpt_detailview_photorow_offset / GRID_SIZE, 10);
      }
      _xCaptureNightlifeJS.loadPhotoRow(cpt_detailview_photorow_offset, cpt_detailview_photo_count_ttl, PHOTOROW_SIZE, "img_photorow_");
    }
  });
  $('#btnViewAllMode').click(function() {
    cpt_switchMenuButton("btnViewAllMode");
    cpt_switchTypeMode("captures");
  });
  $('#btnViewEditorsMode').click(function() {
    cpt_switchMenuButton("btnViewEditorsMode");
    cpt_switchTypeMode("editors");
  });
  $('#btnViewMostMode').click(function() {
    cpt_switchMenuButton("btnViewMostMode");
    cpt_switchTypeMode("mostviewed");
  });
}

function cpt_loadJSon(dataFile) {
  // preload data
  $.getJSON(dataFile, function(data) {
    if (data.latestEditorsPickImg) {
      var obj_cpt_featured_img = document.getElementById("cpt_gridview_main_image_img");
    }
    cpt_featured_img = false;
    cpt_detailview_photo_count_ttl = 0;
    images = new Array(); // reset
    $.each(data.items, function(i,item) {
      if (obj_cpt_featured_img && i == data.latestEditorsPickImg) {
        cpt_featured_img = parseInt(data.latestEditorsPickImg, 10);
        cpt_featured_img = new _xCaptureNightlifeJS.setPhoto(item.id, item.uri, item.auth, item.cap, item.ep);
        obj_cpt_featured_img.src = IMAGE_URL + item.uri + IMG_FEATURED;
      } else { // do not show featured image in 2 places
        var index = (cpt_featured_img ? i - 1 : i); // compensate if featured item exists
        images[index] = new _xCaptureNightlifeJS.setPhoto(item.id, item.uri, item.auth, item.cap, item.ep);
        cpt_detailview_photo_count_ttl++;
      }
    });
    cpt_init();
  });
}

function cpt_switchTypeMode(mode) {
  $("#cpt_gridview_main_image").hide();
  if (mode == "captures") {
    images = imagesAll;
    $("#cpt_gridview_main_image").css("display", "inline");
  } else if (mode == "editors") {
    images = imagesEditorsPick;
  } else if (mode == "mostviewed") {
    images = imagesMostViewed;
  }
  if (images.length == 0) { // avoid loading file more than once
    dataFile = IMAGE_URL + "captures/" + mode + ".js";
    cpt_loadJSon(dataFile);
    if (mode == "captures") {
      imagesAll = images;
    } else if (mode == "editors") {
      imagesEditorsPick = images;
    } else if (mode == "mostviewed") {
      imagesMostViewed = images;
    }
  }
}

function cpt_initEvents() {
  $('#cpt_menu_upload_photo_button').click(function(event) {
    $('#capture_upload_cancel').unbind('click'); // get rid of any previous bindings from module_capture.js
    $('#capture_signin_cancel').unbind('click'); // get rid of any previous bindings from module_capture.js
    $('#capture_tyclose_button').unbind('click'); // get rid of any previous bindings from module_capture.js
    $('#cpt_menu_upload_photo_button_dropdown').slideToggle('slow');
    $('#capture_upload_cancel').click(function(event){
      $('#cpt_menu_upload_photo_button_dropdown').slideUp('slow');
    }); // overload to close
    $('#capture_signin_cancel').click(function(event){
      $('#cpt_menu_upload_photo_button_dropdown').slideUp('slow');
    }); // overload to close
    $('#cpt_menu_upload_photo_button_dropdown_close').click(function(event){
      $('#cpt_menu_upload_photo_button_dropdown').slideUp('slow');
    }); // overload to close
    $('#capture_tyclose_button').click(function(event){
      $('#cpt_menu_upload_photo_button_dropdown').slideUp('slow');
    }); // overload to close
  });
}

$(document).ready(function() {
  _xCaptureModuleJS.gotoUpload(); // do not display "just captured" portion
  cpt_switchTypeMode("captures");
  cpt_initEvents();
});
