var gb = {
  isHomePage: false,
  hideMenu: function() {
    // jQuery("#mainMenu ul").hide().find('li').css({opacity:0});
    jQuery("#mainMenu ul").hide().find('li');
    jQuery('#mainMenu ul').after('<a href="#">Rozwiń<br /><big>Menu</big></a>');
    jQuery('#mainMenu ul + a').click(function(e) {
      jQuery(this).animate({opacity:0},400,function() {
        jQuery(this).prev('ul').slideDown(600,function() {
          // jQuery(this).find('li').animate({opacity:1},1000);
          jQuery(this).find('li').animate(1000);
        });
        jQuery(this).slideUp(600,function() {
          jQuery(this).remove();
        });
      });

      e.preventDefault();
    });
  },
  loadImage: function(address) {
    var newImage= new Image();
    jQuery("#photoBig .photo").fadeOut('fast',function() {
      jQuery("#photoBig #ajaxLoader").show();
      jQuery(newImage).load(function() {
        jQuery("#photoBig #ajaxLoader").hide();
        jQuery("#photoBig .photo").attr("src",address).fadeIn('fast');
      }).attr('src',address);
    });
  },
  init: function() {
    if(this.isHomePage===true) {
      this.hideMenu();
    }
    jQuery("#photoBig .photo").hide();
    jQuery("#photosSmall a").click(function(e) {
      e.preventDefault();
      if(jQuery("#photoBig .photo").attr('src')!=jQuery(this).attr("href")) {
        gb.loadImage(jQuery(this).attr("href"));
      }
    });
    jQuery("#photosSmall a:first").hide();
    jQuery("#photoBig .frame").click(function() {
      if(jQuery("#photoBig .photo").attr('src')!=jQuery("#photosSmall a:first").attr("href")) {
        gb.loadImage(jQuery("#photosSmall a:first").attr('href'));
      }
    });
    this.loadImage(jQuery("#photosSmall a:first").attr('href'));
    jQuery(".adminPhoto a.delete").live('click',function(e){
      var clicked=jQuery(e.target);
      jQuery.get(clicked.attr('href'),function(d) {
        if(d=="OK") {
          clicked.parent().fadeTo(400,0,function() {
            jQuery(this).animate({width: 0},300,function() {
              jQuery(this).remove();
            });
          });
        }
      });
      e.preventDefault();
    });
    jQuery(".adminPhoto a").fadeTo(0,0);
    jQuery(".adminPhoto").live('mouseover',function() {
      jQuery(this).find('a').stop(true,false).fadeTo(300,1);
    }).live('mouseout',function() {
      jQuery(this).find('a').stop(true,false).fadeTo(300,0);
    });
    jQuery("#new_photo").parent('form').submit(function() {
      jQuery.ajaxFileUpload({
        url: jQuery('#new_photo').parent('form').attr('action'),
        secureuri: false,
        fileElementId: 'new_photo',
        dataType: 'json',
        success: function(d) {
          jQuery('.adminPhotos').append('<div class="adminPhoto"><img src="/static/images/photos/small/'+d.msg+'.jpg" alt="" /><a href="/ajax/photos/delete/'+d.msg+'" class="delete">X</a></div>');
          jQuery('.adminPhotos .adminPhoto:last a.delete').fadeTo(0,0);
          jQuery('.adminPhotos .adminPhoto:last').hide().fadeIn(300);
        }
      });
      return false;
    });
    jQuery('.moveup:first').hide();
    jQuery('.movedown:last').hide();
    jQuery('.moveup').click(function(e) {
      e.preventDefault();
      var clicked=jQuery(e.target);
      jQuery.get(clicked.attr('href'),function(d) {
        if(d=="OK") {
          clicked.parent('li').clone(true).insertBefore(clicked.parent('li').prev('li'));
          clicked.parent('li').remove();
          jQuery('.moveup, .movedown').show();
          jQuery('.moveup:first').hide();
          jQuery('.movedown:last').hide();
        }
      });
    });
    jQuery('.movedown').click(function(e) {
      e.preventDefault();
      var clicked=jQuery(e.target);
      jQuery.get(clicked.attr('href'),function(d) {
        if(d=="OK") {
          clicked.parent('li').clone(true).insertAfter(clicked.parent('li').next('li'));
          clicked.parent('li').remove();
          jQuery('.moveup, .movedown').removeClass('hidden');
          jQuery('.moveup:first').addClass('hidden');
          jQuery('.movedown:last').addClass('hidden');
        }
      });
    });
    jQuery('.photomoveup:first').addClass('hidden');
    jQuery('.photomovedown:last').addClass('hidden');
    jQuery('.photomoveup').click(function(e) {
      e.preventDefault();
      var clicked=jQuery(e.target);
      jQuery.get(clicked.attr('href'),function(d) {
        if(d=="OK") {
          clicked.parent('.adminPhoto').clone(true).insertBefore(clicked.parent('.adminPhoto').prev('.adminPhoto'));
          clicked.parent('.adminPhoto').remove();
          jQuery('.photomoveup, .photomovedown').removeClass('hidden');
          jQuery('.photomoveup:first').addClass('hidden');
          jQuery('.photomovedown:last').addClass('hidden');
        }
      });
    });
    jQuery('.photomovedown').click(function(e) {
      e.preventDefault();
      var clicked=jQuery(e.target);
      jQuery.get(clicked.attr('href'),function(d) {
        if(d=="OK") {
          clicked.parent('.adminPhoto').clone(true).insertAfter(clicked.parent('.adminPhoto').next('.adminPhoto'));
          clicked.parent('.adminPhoto').remove();
          jQuery('.photomoveup, .photomovedown').removeClass('hidden');
          jQuery('.photomoveup:first').addClass('hidden');
          jQuery('.photomovedown:last').addClass('hidden');
        }
      });
    });
  }
};
jQuery(document).ready(function() {
  gb.init();
});

