/*
 *
 * Copyright (c) 2011 Paul Wand (http://paulwand.com)
 * Licensed under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 * 

 */


(function($){  
    $.fn.fbGenerator = function(options,callback) {  
        var defaults = {  
		albumid: 179008988793876,  
		maxheight: 800,  
		isAlbum:true,
		limit:0,
		size:113
        };  
        var options = $.extend(defaults, options);  
        
        var element=options.element=this;
		
		this.addClass("loading");
        //Get the album data Json
        
        $.getJSON('https://graph.facebook.com/' + options.albumid + '/photos?limit='+options.limit+'&callback=?', function(json){
            $(json.data).each(function (index, photo) {
                //If the photo is over maxheight pixels tall, use the secondary reduced size photo
                
                var mainphoto = 0;
                var thumbphoto = 1;
                var isovermax = (photo.images[mainphoto].height > options.maxheight);
                var isundermin = (photo.images[thumbphoto].height < options.size);
                mainphoto = isovermax ? 1 : 0;
                thumbphoto = isundermin ? 0 : 1 ;
                //Generate the A tag with the needed classes and nab the title from the facebook data
                $("<a title='"+photo.name+"' rel='fb-img' href=" +
                photo.images[mainphoto].source + "><img class='fb-thumb' src=" + 
                photo.images[thumbphoto].source + "  height=" + photo.images[thumbphoto].height +
                " width='" + photo.images[thumbphoto].width + "' title='" + photo.name + "'/></a>").appendTo(element);

            })
            element.find('img').batchImageLoad({
               						loadingCompleteCallback: myCallback(options),
               						imageLoadedCallback: imageLoaded(options)
               					});
              
        
                $("a[rel=fb-img]").fancybox({
                    'transitionIn'      : 'fade',
                    'transitionOut'     : 'fade',
                    'titlePosition'     : 'over',
                    'titleFormat'       : function(title, currentArray, currentIndex, currentOpts) {
                        return '<span id="fancybox-title-over">'+(title=='undefined'?'':title)+'</span>';}
                    
                });
            
        });


                   
           
               
            
        
        return this
    };  
})(jQuery);


  function myCallback(options) {

          $(".fb-thumb").each(function(id,el){

                 $(this).jclip($(this).width()/2-options.size/2,$(this).height()/2-options.size/2,options.size,options.size);

                     $(this).hover(function() {
                     $(this).css('border','solid white 1px');
                 },function() {
                     $(this).css('border','solid white 0px');                   
                 });
             });
             options.element.removeClass("loading");

  }

  function imageLoaded(options) {
      console.log(arguments);
  }


/**
 *  Plugin which is applied on a list of img objects and calls
 *  the specified callback function, only when all of them are loaded (or errored).
 *  @author:  H. Yankov (hristo.yankov at gmail dot com)
 *  @version: 1.0.0 (Feb/22/2010)
 *	http://yankov.us
 */

(function(a){a.fn.batchImageLoad=function(d){var b=a(this);var c=b.size();var e=c;var g=0;a.fn.batchImageLoad.defaults={loadingCompleteCallback:null,imageLoadedCallback:null};var f=a.extend({},a.fn.batchImageLoad.defaults,d);b.each(function(){if(a(this)[0].complete){e--;if(f.imageLoadedCallback){f.imageLoadedCallback(g,c)}}else{a(this).load(function(){g++;if(f.imageLoadedCallback){f.imageLoadedCallback(g,c)}if(g>=e){if(f.loadingCompleteCallback){f.loadingCompleteCallback()}}});a(this).error(function(){g++;if(f.imageLoadedCallback){f.imageLoadedCallback(g,c)}if(g>=e){if(f.loadingCompleteCallback){f.loadingCompleteCallback()}}})}});if(e<=0){if(f.loadingCompleteCallback){f.loadingCompleteCallback()}}}})(jQuery);

