/*global $, I18n, ajaxify_login, changelocale, closefb, console, currentpage, d, document, encodeURIComponent, handle_savedocument_response, handle_savesearch_response, handleresponse, location, open_in_window, reload_page, updatestatus, window */


var DEBUG = false;

function open_in_window(address, width, height) {
  var newWin;
  newWin = window.open(address, '', 'width=' + width + ',height=' + height + ',scrollbars=yes,resizable=yes,status=no,toolbar=no,location=no,menubar=no');
  newWin.focus();

}

function handleresponse(data) {
  if (data.rstatus == "ok") {
    closefb();
    reload_page();
  } else {
    var message = $("form.jformlogin p#msg");
    message.hide();
    message.html(data.errors[0][0] + data.errors[0][1]);
    message.show();
  }
}

function ajaxify_login() {
  $('form.jformlogin').submit(

  function () {
    var thislocale, frmcontents;
    thislocale = location.pathname.split('/')[1];

    frmcontents = $(this).serialize();
    $.post("/" + thislocale + "/user_sessions", frmcontents, function (data) {
      handleresponse(data);
    }, "json");
    return false;
  });
}

// end ready


function handle_savesearch_response(data) {

  d("saved search");
  if (data.rstatus == "ok") {

    // display search saved
    $('#dialog-loading').hide();
    reload_page();
    // display search saved
    $("#dialog-palette p#message").html(I18n.translate('favorites.your_search_has_been_saved'));
    $("#dialog-palette").show();

 } else {

    // display error msg
    $("form.jform p#msg").toggle();

  }
}

function handle_savedocument_response(data) {
  if (data.rstatus == "ok") {

    $('#dialog-loading').hide();

    // display search saved
    reload_page();
    $("#dialog-palette p#message").html(I18n.translate('favorites.your_text_has_been_saved'));
    $("#dialog-palette").show();
  } else {

    // display error msg
  }
}


function updatestatus() {
  var thislocale = location.pathname.split('/')[1];

  $.get("/" + thislocale + "/users/currentuser/status", function (data) {
    $("#user_controls").html(data);

  });
  // end ajax
}



function closefb() {
  $(document).trigger('close.facebox');
}

function changelocale(locale) {
  var newurl, thisurl;
  thisurl = location.href;
  if (locale == "en") {
    newurl = thisurl.replace('/lo/', '/en/');
  }
  else {
    newurl = thisurl.replace('/en/', '/lo/');
  }
  d(newurl);
  window.location.href = newurl;
}

function reload_page() {
  window.location.reload();
}



function show_large_image(image,caption,copyright,photocredit) {
    
    var data  = '<img src="/resources/fullsize/' + image + '.jpg" alt="">';
    data = data + caption ;
    data = data + "<p>" + photocredit + "</p>";
    data = data + "<p>" + copyright + "</p>";
    
    $.facebox(data);
}

//****************************************************//
// debug function
//****************************************************//

function d(m) {
  if (DEBUG) {
    console.info(m);
  }
}



$(document).ready(function () {

  $('a#register[rel*=facebox]').live("click", function () {

    var thislocale = location.pathname.split('/')[1];
    $.get("/" + thislocale + "/users/currentuser/newpopup", function (data) {
      d(data);
      $.facebox(data);
    });
  });
  //end add click
  /* profile update */

  $('a#editprofile[rel*=facebox]').live("click", function () {

    var thislocale = location.pathname.split('/')[1];
    $.get("/" + thislocale + "/users/currentuser/editpopup", function (data) {
      d(data);
      $.facebox(data);
    });
  });
  //end add click
  /*
 *
  * */

  $('a#login[rel*=facebox]').live("click", function () {
    var thislocale = location.pathname.split('/')[1];

    $.get('/' + thislocale + '/user_sessions/new/newpopup', function (data) {
      d(data);
      $.facebox(data);
      //ajaxify_login();
    });

  });
  //end add click
  $('span#editsavedsearch').live("click", function () {
    var thislocale = location.pathname.split('/')[1];
    var address = '/' + thislocale + '/users/currentuser/savesearch?savedsearch%5Bid%5D=' + encodeURIComponent($(this).attr("aria-search-id"));

    $.get(address, function (data) {
      d(data);
      $.facebox(data);

    });
    return false;
  });
  //end add click
  $('a#savesearch').live("click", function () {
    var thislocale = location.pathname.split('/')[1];
    var address = $(this).attr('href');

    $.get(address, function (data) {
      d(data);
      $.facebox(data);

    });
    return false;
  });
  //end add click
  $('form#register.jform').live("submit", function () {
    var thislocale = location.pathname.split('/')[1];
    var frmcontents = $(this).serialize();
    $('#dialog-loading').toggle();
    $('#dialog-form').toggle();
    d("register...");
    d(frmcontents);
    $.post("/" + thislocale + "/users", frmcontents, function (data) {
      $.facebox(data);
    });
    return false;
  });



  /* update profile */

  $('form#updateprofile.jform').live("submit", function () {
    var thislocale, frmcontents, url;

    thislocale = location.pathname.split('/')[1];
    frmcontents = $(this).serialize();
    url = $(this).attr("action");

    $('#dialog-loading').toggle();
    $('#dialog-form').toggle();
    d("update ...");

    $.post(url, frmcontents, function (data) {
      $.facebox(data);
    });

    return false;
  });

  /**/
  $('form#savedocument.jform').live("submit", function () {
    var thislocale, frmcontents, url;
    thislocale = location.pathname.split('/')[1];
    frmcontents = $(this).serialize();
    url = $(this).attr('action');
    $('#dialog-loading').toggle();
    $('#dialog-form').toggle();
    d("register...");
    $.post(url, frmcontents, function (data) {
      handle_savedocument_response(data);
    }, "json");
    return false;
  });

  /** save search   **/
  $('form#savesearch.jform').live("submit", function () {
    var thislocale, frmcontents, url;
    thislocale = location.pathname.split('/')[1];
    frmcontents = $(this).serialize();
    url = $(this).attr('action');
    $('#dialog-loading').toggle();
    $('#dialog-form').toggle();
    d("saving search...");
    $.post(url, frmcontents, function (data) {
      handle_savesearch_response(data);
    }, "json");
    return false;
  });


  $('span#addfavorite').live("click", function () {
    var thislocale = location.pathname.split('/')[1];
    var url = '/' + thislocale + '/users/currentuser/savedocument?text=' + encodeURIComponent($(this).attr("aria-document-id"));
    $.get(url, function (data) {

      $.facebox(data);
    });
  });
  //end add click
  // add css class to current menu
  //  currentpage();

  $("#breadcrumb .popup").live("click", function () {
    var address;
    address = $(this).attr('href');
    open_in_window(address, 600, 350);
    return false;
  });

  $(".gallery .popup, #page_images .popup").live("click", function () {
    var address;
    address = $(this).attr('href');
    open_in_window(address, 950, 650);
    return false;
  });


  $(".infocontainer .popup").live("click", function () {
    var address;
    address = $(this).attr('href');
    open_in_window(address, 400, 300);
    return false;
  });


/*
  $("a.new_window").live("click", function () {
    var address;
    address = $(this).attr('href');
    open_in_window(address, 750, 550);
    return false;
  });

  $("a.new_window").live("click", function () {
    var address;
    address = $(this).attr("target", '_blank');
  });
  */

 $("div.pic").live("click", function (e) {
    var currentelem = $(this)
    captionelem = currentelem.find("div.caption");
    photoelm = currentelem;

  // if this is a caption grap the info 

    image = photoelm.find("img").attr("src");
    image = image.replace("/resources/","");
    image = image.replace("_s.jpg","");
    caption = captionelem.html();
    photocredit = photoelm.find("div.photocredit").html();
    copyright = photoelm.find("div.copyright").html();
    title = currentelem.find("p").html();
    
    show_large_image(image,"<h4>" + title + "</h4><br>" +caption,copyright,photocredit);

  });

  $("ul#page_images li").live("click", function (e) {
    var currentelem = $(this)


  if (currentelem.hasClass("caption")) { 
    captionelem = currentelem;
    photoelm = currentelem.prev("li");
  }
  else {
    photoelm = currentelem;
    captionelem = currentelem.next("li");

  }
  
  // if this is a caption grap the info 

    image = photoelm.find("img").attr("src");
    image = image.replace("/resources/","");
    image = image.replace("_s.jpg","");
    caption = captionelem.html();
    photocredit = photoelm.find("div.photocredit").html();
    copyright = photoelm.find("div.copyright").html();
    
    show_large_image(image,caption,copyright,photocredit);

  });


//show_large_image("image","title","caption","copyright");

});

$(document).bind('reveal.facebox', function () {
  ajaxify_login();
});

