/*
  jquery.bkalpha.js
  
  $('#hoge').bkalpha({opacity:40});
*/
$.fn.bkalpha = function(param){
  var param = $.extend({opacity:0.4}, param); //default value.


  $('.bkalpha_mask.selected').css({
    'opacity':0,
    'filter':'alpha(opacity=0)',
    '-moz-opacity':0
  });
  
  var img_count = 0; // img counter
  
	var $return =  this.each(function(i){
	  var w = 89;
	  var h = 59;
	  img_count ++;

	  var photoURL = $(this).children('a').attr('href');
	  var photoCaption = $(this).children('a').children('img').attr('alt');
	  var photoCount = $(this).children('a').children('img').attr('title');


/*
	  //preload images.
		if(i > 0){
	    $('#images_wrapper').append('<img class="bg_images" style="display:none;" width="900" height="600" src="'+ photoURL +'" alt="" />');
		}
*/

	  $(this).append('<div class="bkalpha_mask" style="cursor:pointer;width:'+ w +'px;height:'+ h +'px;top:-'+ h +'px;"></div>');

	  $(this).children('.bkalpha_mask').hover(function(){//when mouse over li
	      $(this)
	        .stop()
          .css({
            'opacity':0,
            'filter':'alpha(opacity=0)',
            '-moz-opacity':0
          });
		  // imgを最前面に
////		  $(this).siblings("a").children("img").css({"position":"relative","z-index":"100"});
		  $(this).parent("li").css({"position":"relative","z-index":"1000"});
 	    },function(){//when mouse out of li
 	      if(!$(this).hasClass('selected')){ //when NOT selected.
          $(this)
            .css({
            'filter':'alpha(opacity='+ param.opacity*100 +')'
            })
            .animate({
              'opacity':param.opacity,
              '-moz-opacity':param.opacity
            });
		  // imgを最前面から戻す
////		  $(this).siblings("a").children("img").css({"position":"inline","z-index":"0"});
//		  $(this).parent("li").css({"position":"block","z-index":"0"});
		  $(this).parent("li").css({"z-index":"0"});
		  }
	    }).click(function(){
	      $('.bkalpha_mask').removeClass('selected')
	      $(this)
	        .addClass('selected');

		// caption表示
		if(photoCaption){
			$("#caption").fadeOut("fast",function(){$(this).html(">&nbsp;"+photoCaption).fadeIn();})
		}
		else{
			$("#caption").fadeOut("fast");
		}
		// img NO. 表示
		$("#no").html(photoCount);

		$(this).parent().siblings().children('.bkalpha_mask').css({
          'opacity':param.opacity,
          'filter':'alpha(opacity='+ param.opacity*100 +')',
          '-moz-opacity':param.opacity
        });

/*
				$.ajax({
  				type: "GET",
  				url: photoURL,
				beforeSend: function(){
					$("#loading").show();
				},
				 success: function(){
				 	$("#loading").hide();
		    		$('#images_wrapper').append('<img class="bg_images" style="display:none;" width="900" height="600" src="'+ photoURL +'" alt="" />');
			      	$('#images_wrapper').children('img').hide();

	  		    	$('#images_wrapper').children("img[src*='"+ photoURL +"']").fadeIn();
 				 },
				 error: function(arg1,arg2,arg3){
					alert(arg2);
			     }
				});
*/
			$("#loading").show();
			var $new_img$ = $('<img class="bg_images" style="display:none;" width="900" height="600" src="'+ photoURL +'" alt="" />');
			$new_img$.bind("load",function(){
				$('#images_wrapper').append($new_img$);
				$("#loading").hide();
			    $('#images_wrapper').children('img').hide();
	  			$('#images_wrapper').children("img[src*='"+ photoURL +"']").fadeIn();			
			});
/*
			$('#images_wrapper').append('<img class="bg_images" style="display:none;" width="900" height="600" src="'+ photoURL +'" alt="" />')
			.ready(function(){
//			.bind("load",function(){
				 	$("#loading").hide();
			      	$('#images_wrapper').children('img').hide();
	  		    	$('#images_wrapper').children("img[src*='"+ photoURL +"']").fadeIn();				
			});
*/			

	   
	    });
	});
	$('.bkalpha_mask').css({
	  'background':'#000',
  	'opacity':param.opacity,
    'filter':'alpha(opacity='+ param.opacity*100 +')',
    '-moz-opacity':param.opacity,
    'position':'relative'
 	});
	
  $('#images_wrapper img:first').fadeIn('slow');
	
	return $return;
};


