var Aq_ImageLoader = Class.create({
    imgs : null,
    iterator : 1,
    interval : 1400,
    timerInstance : null,
    dest : null,
    currentId : 0,
    animationDuration : 900,
    tempImageId : 'imageLoader',
    
    //Init the loader with the current image placeholder, array of urls, interval between transitions and animation duration
    init:function(dest, imgs, interval, animationDuration)
    {
        this.imgs = imgs;
        this.dest = dest;
        //$('#animhome').css('height','370px');
        $('#animhome').css('height','380px');
        $('#imageLoader').css('position', 'absolute');
        $('#imageLoader').css('top', '10px');
        $('#imageLoader').css('left', '10px');
        $('#imageLoader').css('z-index', '0');
        
        if(imgs.length == 1)
    	{
        	this.iterator = 0;
    	}
        
        if(animationDuration)
        {
            this.animationDuration = animationDuration;
        }
        
        if(interval != null)
        {
            this.interval = interval;
        }
    },
    
    start:function()
    {
        var _this = this;
        
        _this.timerInstance = setTimeout(function(){
            _this.timer();
        }, this.interval);
    },
    
    timer:function()
    {
    	var _this = this;
        //Adding the image
        var im = $(new Image());
        

        //$('#animhome h1,#animhome h2,#animhome h3').fadeOut();
        im.load(function(){
            $(this).animate({opacity:1}, _this.animationDuration, null, function(){
                var currentId = '#'+_this.tempImageId+(_this.currentId-3);
                $(currentId).remove();
                
                $('#animhome h1').html(item.title);
                $('#animhome h1').css('color','#'+item.titleColor);
                $('#animhome h1').css('background','#'+item.rectColor);
                $('#animhome h1').css('opacity',item.rectAlpha/100);
                
                $('#animhome h2').html('');
                $('#animhome h3').html('');
                if (item.subtitlePosition==1){
                	$('#animhome h3').html(item.subtitle);
                	$('#animhome h3').css('color','#'+item.subtitleColor);
                }else{
                	$('#animhome h2').html(item.subtitle);
                	$('#animhome h2').css('color','#'+item.subtitleColor);
                }
                Cufon.refresh('#animhome');
                //$('#animhome h1,#animhome h2,#animhome h3').fadeIn();

                _this.start();
            });
            
        });
        
        var parent = $(this.dest).parent();
        
        im.css('position', 'absolute');
        im.css('top', '10px');
        im.css('left', '10px');
        im.css('opacity', '0');
        im.attr('id', this.tempImageId+this.currentId);
        
        parent.append(im);
        
        var item=this.getCurrentUrl();
        
        if(item)
    	{
        	im.attr('src', item['url']);
        	this.iterator = (this.iterator+1) % imgs.length;
        	this.currentId++;
    	}
    },
    
    pause:function()
    {
        
    },
    
    getCurrentUrl : function()
    {
        return this.imgs[this.iterator];
    }
});