/**
 * 
 * 		title:		Fill Image Gallery
 *
 *		author: 	Ovidiu Stefancu
 *					http://www.wpworks.net
 *					info@wpworks.net
 * 
 *		info:		Loop Version
 * 
 */

var WPW = WPW || {}; 

WPW.bgLoop = function(){
    var main = this;
    var main_node = $('#wpw_fill_gallery');
    var items = [];
    var loaded_items = [];
    var bgIndex = 0;
    var Busy = false;
    var fadeSpeed = 2000;
    
    
    
    var thumbGallery;
    var random = WPW.piclist_random;
    
    $('#primary').prepend(main_node);
    
    var loopSpeed = WPW.piclist_loop_speed || 4000;
    loopSpeed = parseInt(loopSpeed);
    main.loopTimer = 0;
    var doLoop = WPW.piclist_loop;


    $('.fill_item_container', main_node).each(function(){
        var item_node = $(this);
        items.push(item_node);
        item_node.detach();
    });
    
    if(random){
    	bgIndex = parseInt(Math.random()*items.length);
    }
	
	var resizeImage = function(img){
		WPW.ResizeImage({
			img:img, 
			t_w:function(){return WPW.cW},
			t_h:function(){return WPW.cH},
			scale_mode:"fit_h",
			position_mode:"tc",
			force_fit: true,
			canvas_check: false
		}); 
	}	
	
    var loadImage = function(item){
        var img = new Image();
        img = $(img);
		
        img.load(function(){
        	img.data('loaded', true);
            item.append(img);
            item.data('img', img);
            item.data('loaded', true);
            loaded_items.push(item);
        
        	
            
			resizeImage(img);
			Busy = false;
			//show only if this is selected index
			if(item = items[bgIndex])fadeCurrent();
        }).error(function(){
        	Busy = false;
        	item.data('loaded', true);
        	alert('ERROR - Image not found:'+item.src);
        }).attr('src', item.attr('data-src'));
    }
	
    function setBgIndex(way){
        if(!way)way = 1;
        bgIndex = bgIndex + way;
        if(bgIndex < 0)bgIndex = items.length - 1;
        if(bgIndex > items.length - 1)bgIndex = 0;
        showCurrentBg();
	}	

    var container_position = 0;	
    function showCurrentBg(){
        if(!items[bgIndex].data('loaded')){
        	Busy = true;
            loadImage(items[bgIndex]);
        } else {
        	fadeCurrent();
        }
        
        main_node.data('bgIndex', bgIndex);
        main_node.trigger("IndexChanged");
    }
	
	function fadeCurrent(){
    	main_node.append(items[bgIndex]);
    	var img = items[bgIndex].data('img');
		Busy = true;
		img.hide().fadeIn(fadeSpeed, function(){
				Busy = false;
				main_node.children(".fill_item_container").not(".fill_item_container:last").detach();
				theLoop();
			});
		refreshSize();	
	}
	
	function fireTimer(){
		clearTimeout(main.loopTimer);
		setBgIndex();
	}
	
	function theLoop(){
		if(doLoop && items.length > 1){
			main.loopTimer = setTimeout(fireTimer, loopSpeed);
		}
// 		
		
		
	}
	
	if(items.length>0){
		
		showCurrentBg();
	}

	$('body').bind('LoopBg', function(){
		fireTimer();
	})

    var refreshSize = function(){
    	$(".fill_item_container img").each(function(){
    		if($(this)[0].complete)resizeImage($(this));
    	});
    	
        for (var i = 0; i<items.length; i++){
            items[i].css('width', WPW.cW + "px");
            items[i].css('height', WPW.cH + "px");
        }
        
        main_node.css('width', WPW.cW + "px");
        main_node.css('height', WPW.cH +  "px");      
     }  
	

	
    WPW.body.bind('RefreshSize', function(){
        refreshSize();
    });
    
    this.updateBgIndex = function(index){
    	if(!Busy){
    		bgIndex = index;
    		showCurrentBg();
    	}
    }
}

WPW.Core.register("bgLoop");

