
$(window).load(function() {

})

$(document).ready(function() {


  $('#albumDetail ul').jcarousel({
                                  itemLoadCallback: albumItemLoadCallback,
                                  vertical: false,
                                  animation: 'slow',
                                  easing: 'swing',
                                  scroll: 1,
                                  size: albumSize+1
                                });


  var lastLink = $('#submenu li:last').html();
  if(lastLink!=null) {
    var lastChar = lastLink.substring(lastLink.length-1,lastLink.length);
    
      if(lastChar == ',') {
        var newLastLink = lastLink.substring(0,lastLink.length-1);
        $('#submenu li:last').html(newLastLink);
      }
  }

  
  $('#albumDetail ul li').hover(function() {      
     $(this).children('div').css('color','black');

  }, function() {

      $(this).children('div').css('color','#b2b2b2');
  })
  
//  $('#albumDetail .jcarousel-prev-horizontal').hover(function() {
//  		if($(this).attr('disabled') == 'false') {
//  			$(this).css('backgroundImage', 'url("/images_new/prev_hover.png")'); 
//  		}
//  }, function() {
//  		$(this).css('backgroundImage', 'url("/images_new/prev.png")'); 
//  });
//  
//    $('#albumDetail .jcarousel-next-horizontal').hover(function() {
//  		if($(this).attr('disabled') == 'false') {
//  			$(this).css('backgroundImage', 'url("/images_new/next_hover.png")'); 
//  		}
//  }, function() {
//  		$(this).css('backgroundImage', 'url("/images_new/next.png")'); 
//  });
//  
})


function albumItemLoadCallback(carousel, state) {


     if (carousel.has(carousel.first, carousel.last)) {
        return;
    }

     $.ajax({
              url: '/index/ajax/albumItems/'+activeAlbumID+'/'+customlangid
              , type: "GET"
              , contentType: "json"
              , processData: false

              ,   success: function(data){
                  albumItemAddCallback(data, carousel);
                }

           });

    // Check if the requested items already exist

    
//
//    carousel.last = 1;
//    if (carousel.has(carousel.first, carousel.last)) {
//       return;
//    }
//
//    jQuery.get(
//        '/new/index/ajax/albumItems/'+activeAlbumID,
//        function(xml) {
//            albumItemAddCallback(carousel, carousel.first, carousel.last, xml);
//        },
//        'xml'
//    );
//
//
//    $('#albumDetail ul li').each(function() {
//      console.log($(this).children());
//      if($(this).children().length == 0) {
//
//
//      }
//    });
    //carousel.reload();
//
//
//    $('.jcarousel-next').bind('click', function(){
//      //albumItemActive++;
//      //setAlbumItemText(albumItemActive);
//    });
//
//    $('.jcarousel-prev').bind('click', function(){
//      //albumItemActive--;
//      //setAlbumItemText(albumItemActive);
//    });

}



function setAlbumItemText(count) {
    
    //var text = $("#albumDetail ul li:index("+count+") img").attr('alt');
    //var text = $("#albumDetail ul li").index(count);
    //$("#albumDetail #pictureText").html(text);
}

function albumItemAddCallback(data, carousel)
{

    //decode data
    var _data = JSON.parse(data);
 
    $(_data).each(function(i) {
       carousel.add(1+i,albumItemHTML(this.url, this.width, this.id, this.title));
    });
    
    $(".jcarousel-item a").fancybox({'overlayShow': true, 'padding':0, 'overlayOpacity':1,'overlayColor':'#fff', 'title':true, 'titlePosition':'outside'});
    carousel.add(albumSize+1, addSpacer(272));
   

    carousel.reload();
    //force alignement
    $('#albumDetail ul').css('margin-left','297px');
    $('#albumLoader').fadeOut();

};



/**
 * Item html creation helper.
 */
function albumItemHTML(url, width, id, title)
{
 
    var html = '<a rel="album" href="'+url.replace('/_medium/','/_enlarge/')+'"  title="'+title+'">';
    html += '<img id="albumItem_'+id+'"src="' + url + '" height="500" width="'+width+'" alt="'+title+'" />';
    html += '</a>';
    html += '<div>'+title+'</div>';
    return html;
};


function addSpacer(width) {
  return '<img src="/images/spacer.gif" height="500" width="'+width+'" alt="" />';
}


