function ajaxAddFavorite() {
   $('#addfav').hide();
   $('#fav_progress').show();
   $.ajax({
      type: "POST",
      data: 'cmd=add_to_favorite',
      timeout: 5000,
      cache: false,
      success: function (obj) {
         if (obj == 0) {
            $('#view_favedby').hide();
            $('#cnt_faved').html('');
         } else {
            $('#view_favedby').show();
            $('#cnt_faved').html(obj).show();
         }
         if ($('#view_favedby').data('qtip')) {
           $('#view_favedby').qtip("destroy");
         }
         $('#fav_progress').hide();
         $('#remfav').show();
      },
      error: function (obj, err_str) {
         $('#fav_progress').hide();
         $('#addfav').show();
         var err = 'Network error';
         if (err_str) {
            err = err_str;
         }
         alert("Alas, something went wrong: "+err+"!");
      }
   });
}

function ajaxRemoveFavorite() {
   $('#fav_progress').show();
   $('#remfav').hide();
   $.ajax({
      type: "POST",
      data: 'cmd=remove_from_favorite',
      timeout: 5000,
      cache: false,
      success: function (obj) {
         if (obj == 0 || obj == '0') {
            $('#cnt_faved').html('');
            $('#view_favedby').hide();
         } else {
            $('#cnt_faved').html(obj);
            $('#view_favedby').show();
         }
         if ($('#view_favedby').data('qtip')) {
            $('#view_favedby').qtip("destroy");
         }
         $('#fav_progress').hide();
         $('#addfav').show();
      },
      error: function (obj, err_str) {
         $('#fav_progress').hide();
         $('#remfav').show();
         var err = 'Network error';
         if (err_str) {
            err = err_str;
         }
         alert("Alas, something went wrong: "+err+"!");
      }
   });
}

function ajaxAddRemoveNotify() {
   if (!$('.notify_select')) {
      return;
   }
   $('.notify_select').live("change", function () {
      var val_change = $(this).find(':selected').val();
      $('#select_notify_top option').each(function () {
         if ($(this).val() == val_change) {
            $(this).attr("selected", true);
         }
      });
   });
}
function ajaxAddNotify(pid) {
   $('#img_notif_disabled_top').hide();
   $('#notif_progress').show();
   $.ajax({
      type: "POST",
      data: {
         fav_cmd: 'add_notify',
         photo_id: pid
      },
      success: 
         function () {
            $('#notif_progress').hide();
            $('#img_notif_top').show();
            var arr = getElementsByIdPrefix("input", "notify_check");
            for (var i = 0; i < arr.length;i++) {
               arr[i].checked = true;
            }
         },
      error: 
         function () {
            alert('Alas, something went wrong');
            $('#notif_progress').hide();
            $('#img_notif_top').show();
         }
   });
 }
function ajaxRemoveNotify(pid) {

   $('#img_notif_top').hide();
   $('#notif_progress').show();
   $.ajax({
      type: "POST",
      data: {
         fav_cmd: 'remove_notify',
         photo_id: pid
      },
      success: 
         function () {
            $('#notif_progress').hide();
            $('#img_notif_disabled_top').show();
            var arr = getElementsByIdPrefix("input", "notify_check");
            for (var i = 0; i < arr.length;i++) {
               arr[i].checked = false;
            }
         },
      error: 
         function () {
            alert('Something went wrong');
            $('#notif_progress').hide();
            $('#img_notif_disabled_top').show();
         }
   });
}
function removeNotifiedUser() {
   if (!$('.remove_notified_user')) {
      return;
   }
   $('.remove_notifed_user').live('click', function() {      
      var user_id = $(this).attr('id').replace('remove_','');
      $.ajax({
         type: "POST",
         data: 
            { 
               fav_cmd: 'remove_notify', 
               entry_id: user_id
            },
         success: function (obj) {
            if (!isNaN(obj)) {
               if (obj == 0) {
                  $('#show_notified_users').qtip("destroy").hide();
               } else {
                  $('#notify_'+user_id).hide();
               }
            }
         }, 
         error: function (obj) {

         }
      });
   });
}
function showNotifiedUsers() {
   $('#show_notified_users').qtip({
      content: {
         title: {
            text: 'Following members have chosen to be notified of comments',
            button: 'x'
         },
         url: '?',
         data: {fcmd: 'show_notif'}
      },
      show: {
         solo: true,
         delay: 0,
         ready: true,
         when: {
            event: 'click'
         },
         effect: {
            length: 500,
            type: 'slide'
         }

      },
      position: {
          corner: {
            target: 'bottomMiddle',
            tooltip: 'topMiddle'
          },
          adjust: {
             y: 5,
            screen: true
          }
      },
      hide: false,
      style: {
         padding: 4,
         title: {
            padding: 3,
            'font-size': '12px',
            'white-space':'nowrap'
         },
         'font-size': '12px',
         border: {
            width: 2,
            radius: 5
         },
         tip: true,
         name: 'light',
         width: {
            max: 625,
            min: 625
         }
      }
   });
}
function showFavoriteUsers(ref,user_id, photo_id, page_num) {
   if (typeof(page_num) == 'undefined') {
      page_num = 1;
   }

   $('#'+ref).qtip({
      content: {
         title:  {
            text: 'Following members faved this',
            button: 'x'
         },
         url: '?',
         data: {cmd: 'viewusers', fav_user_id: user_id, fav_photo_id: photo_id, fav_page: page_num}
      },
      show: {
         solo: true,
         delay: 0,
         ready: true,
         when: {
            event: 'click'
         },
         effect: {
            length: 500,
            type: 'slide'
         }
      },
      position: {
          corner: {
            target: 'bottomMiddle',
            tooltip: 'topMiddle'
          },
          adjust: {
             screen: true,
             y: 5
          }
      },
      hide: false,
      style: {
         padding: 4,
         title: {
            padding: 3,
            'font-size': '12px',
            'white-space':'nowrap'
         },
         'font-size': '12px',
         border: {
            width: 2,
            radius: 5
         },
         tip: true,
         name: 'light',
         width: {
            max: 625,
            min: 625
         }
      },
      api: {

      }
   });
}
function next_page(page,user_id, photo_id) {
 var api=   $('#view_favedby').qtip("api");
 $.ajax({
    type: "POST",
    url: '?',
    data: {cmd: 'viewusers', fav_user_id: user_id, fav_photo_id: photo_id, fav_page: page},
    success: function(obj) {
       api.updateContent(obj);
    }
 });
}
function info_tooltips() {
   if (typeof ($.tools) == 'undefined') {
      return;
   }
   $('.show_tooltip, .license_tooltip').tooltip({ 
      tipClass: null,
      effect: 'fade',
      fadeOutSpeed: 100,
      fadeInSpeed: 0,
      predelay: 50,
      onBeforeShow : function () {
         var tip = this.getTip();
         var txt = tip.text();
         tip.html('<span class="tooltip-bubble"><span class="tooltip-bubble-arrow-border"></span><span class="tooltip-bubble-arrow"></span>'+txt+'</span>').css({'z-index': '999'});
         var offset = tip.width()/2-12;
         tip.find('.tooltip-bubble-arrow-border').css('left', offset);
         tip.find('.tooltip-bubble-arrow').css('left', offset);
         tip[0].style.filter = ''; 
      },
      onShow: function () {
         this.getTip()[0].style.filter = '';
      },
      offset: [17, 0],
      layout : '<span></span>',
      position: "bottom center"
   });
   if($.browser.msie && $.browser.version=="6.0") {
      $('.show_tooltip').hover(
         function() {
         $(this).css({'background': '#f2f2f2', 'cursor' : 'hand'});
         },function() {
         $(this).css('background', 'transparent');
         }
      );
   }
}
function init_share_button() {
   if (typeof ($.tools) == 'undefined') {
      return;
   }
   if (typeof(flash_url) == 'undefined') {
      flash_url = 'http://images.fotki.com/flash/clipboardCopy.swf'
   }
   if (typeof ($.tools) == 'undefined') {
      return;
   }

   $('#btn_share').tooltip({ 
      tipClass: null,
      effect: 'fade',
      delay: 1000,
      fadeOutSpeed: 100,
      fadeInSpeed: 0,
      events: { def: "'',''" },
      onShow : function () {
         var tip = this.getTip();
         var txt = $('#photo_share').html();
         tip.html('<div id="share_tooltip" class="tooltip-bubble" style="text-align: left !important"><div class="tooltip-bubble-arrow-border" style="left: 10px"></div><div style="left: 10px" class="tooltip-bubble-arrow"></div>'+txt+'</div>').css({'z-index': '998'});
         tip[0].style.filter = ''; 
      },
      offset: [3, -70],
      layout : '<span></span>',
      position: "bottom right"
   }).click(function () {
      var tip = $(this).data('tooltip');
      if (tip.isShown()) {
         tip.hide();
      } else {
         tip.show();
         tip.getTip().find('.share_link').each(function() {
            $(this).clipboard({
               flash_url: flash_url 
            });
         });
      }
   });
}
function init_edit_button() {
   var wd = $("#btn_edit").parent('li').width();
   if (typeof ($.tools) == 'undefined') {
      return;
   }
   
   $("#btn_edit").tooltip({
      events: { def: "'',''" },
      tipClass: null,
      tip: '#edit_menu',
      position: 'bottom right',
      opacity: 1,
      //effect: 'fade',
      delay: 1000,
      offset: [0, -wd],
      onShow : function () {
         this.getTip().css('z-index', 999);
      }
   }).click(function (e) {
      var tip = $(this).data('tooltip');
      e.preventDefault();
      if (tip.isShown()) {
         tip.hide();
      } else {
         tip.show();
      }
   });
   $('#edit_menu li a').width($('#edit_menu').width()-5);
}
function init_thumb_navig() {
  if (typeof ($.tools) == 'undefined') {
      return;
   }

   $('.th_scrollable').scrollable({
      speed: 200,
      onSeek: function () {
      var items = this.getItems()[this.getIndex()];
      $(items).find('img').each(function () {
         var src = $(this).attr('original');
         if (src) {
            $(this).hide();
            $(this).attr('src', src).attr('original','').fadeIn();
         }
      });
      }
   });
   var th_api = $('.th_scrollable').data('scrollable');
   var divs = th_api.getItems();
   var cnt = 0;
   if ($(divs).size() < 2) {
      $('div.browse').hide();
      $('#th_navig').css({'float': 'left'});      
   }
   var children = $(divs[0]).children();
   var elem_count = children.size();
   var elem_width = children.outerWidth(true);
   var ww = elem_count*elem_width;
 /*  if (ww < 500) {
      ww = 445;
   } */
   $('#th_navig').css('width',ww);
   $(divs).each(function() {
      var it = $(this).find('.th_current');
      if (it.length > 0) {
         th_api.seekTo(cnt,0);
         return;
      }
      cnt++;
   });
   $('#btn_thumbs').find('span:eq(0)').addClass('info_arrow_up_icon').removeClass('info_arrow_down_icon');
}

function th_scroll_next() {
   var th_api = $('.th_scrollable').data('scrollable');
   var divs = th_api.getItems();
   var size = th_api.getSize()-1;
   var last_div = $(divs[size]);
   var index = th_api.getIndex();
   var get_width = $('#photo_navig').width();
   if (index == size && last_div.attr('th_block') && last_div.attr('th_block') < last_div.attr('th_block_max')-1) {
      $.ajax({
      type: 'POST',
      data: { 
         cmd: 'get_thumb_navig',
         load_more: last_div.attr('th_block'),
         load_type: 'next',
         thumbrow_width: get_width
      },
      success: function( data ) {
         last_div.after(data);
         th_api.seekTo(index+1,200);
      },
      error: function() {
      }
   });

   } else {
      th_api.next();
   }
   return;
}
function th_scroll_prev() {
   var th_api = $('.th_scrollable').data('scrollable');
   var divs = th_api.getItems();
   var index = th_api.getIndex();
   var get_width = $('#photo_navig').width();
   if (index == 0 && $(divs[0]).attr('th_block') > 0) {
      $.ajax({
      type: 'POST',
      data: { 
         cmd: 'get_thumb_navig',
         load_more: $(divs[0]).attr('th_block'),
         load_type: 'prev',
         thumbrow_width: get_width
      },
      success: function( data ) {
         $(divs[0]).before(data);
         th_api.seekTo(1,0);
         th_api.seekTo(0,200);
      },
      error: function() {
      }
   });

   } else {
      th_api.prev();
   }
   return;

}
function ajax_get_thumbs() {
   var block =  $('#th_navig_block');
//	var expdate = new Date();
//	fixDate(expdate);
//	expdate.setTime(expdate.getTime() + (24 * 60 * 60 * 1000)*365);

   if (block.html().length > 0){
      if (block.is(":visible")) {
         $('#btn_thumbs').find('span:eq(0)').removeClass('info_arrow_up_icon').addClass('info_arrow_down_icon');
       //deleteCookie('th_show_thumb_navig','/', '.'+fotki_hostname);
         block.slideUp();
      } else {
         block.slideDown();
         $('#btn_thumbs').find('span:eq(0)').removeClass('info_arrow_down_icon').addClass('info_arrow_up_icon');
     //   setCookie('th_show_thumb_navig', 1, expdate, "/", "." + fotki_hostname);
      }
      return;
   }
   //setCookie('th_show_thumb_navig', 1, expdate, "/", "." + fotki_hostname);
   var get_width = $('#photo_navig').width();
   $.ajax({
      type: 'POST',
      data: { 
         cmd: 'get_thumb_navig',
         thumbrow_width: get_width
      },
      beforeSend : function () {
         block.html('<img src=http://images.fotki.com/ajax-loader.gif />');
      },
      success: function (data) {
         block.html(data);
         init_thumb_navig();
      },
      error: function () {
         alert('failed');
      }
   });
}
$(document).ready(function() {
/*   if (getCookie("th_show_thumb_navig")) {
      ajax_get_thumbs();
   } */
   init_edit_button();
   info_tooltips();
   init_share_button();
   ajaxAddRemoveNotify();
   removeNotifiedUser();
   if ($('.favorite_users')) {
   $('.favorite_users').live("mouseenter mouseleave", function(e) {
      if (e.type == 'mouseenter') {
      $(this).addClass('favorite_hover');
      }
      if (e.type == 'mouseleave') {
      $(this).removeClass('favorite_hover');
      }
      });
   }
   if ($('#addfav2')) {
   $('#addfav2').qtip({
      content: {
         text: ' ',
         title: {
            text: $('#fav_login_text').html(),
            button: 'x'
         }
      },
      show: {
         solo: true,
         when: 'click',
         delay: 0
      },
      hide: false,

      position: {
         corner: {
            target: 'bottomLeft',
            tooltip: 'topLeft'
         },  
         adjust: {
            resize: false, //have to disable it due to IE bug
            scroll: false, //have to disable it due to IE bug
            x: 12
         }   
      },
      style: {
         width: {
            min: 300,
            max: 800
         },  
         padding: 2,
         title: {
            padding: 4,
            'font-size': '12px',
            'white-space':'nowrap'
         },
         button: {
            'font-size': '10px',
            'padding-left': '5px',
            'padding-right': '5px'
         },
         'font-size': '0px',
         'white-space':'nowrap',
         padding: 0,
         margin: 0,
         border: {
            width: 2,
            radius: 4
         },
         tip: true,
         name: 'light'
      }

   });
   }
});


