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');
        this.flag=1;
        if(imgs.length == 1)
    	{
        	this.iterator = 0;
    	}
        
        if(animationDuration)
        {
            this.animationDuration = animationDuration;
        }
        
        if(interval != null)
        {
            this.interval = interval;
        }
    },
    
    start:function()
    {
        var _this = this;

        
        while( _this.flag == 0){}
        _this.flag=0;
        window.clearTimeout(_this.timerInstance);
        _this.timerInstance = window.setTimeout(function(){
            _this.timer();
        }, 1400);
        
    },
    
    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},900, null, function(){
                var currentId = '#'+_this.tempImageId+(_this.currentId-3);
                $(currentId).remove();

                $('#animhome h1').hide();
                if (item.title != ''){
                    $('#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 h1').show();
                }
                $('#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.flag=1;
                _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);
        im.attr('width','690');
        im.attr('height','370');
        parent.append(im);
        
        var item=this.getCurrentUrl();
        
        if(item)
    	{
        	//console.log(item);
        	if( item['urlImage'] != '' ) {
        		$("#animhome a").attr('href',item['urlImage']);
        		if( item['urlInBlank']=='1') {
            		$("#animhome a").attr('target','_blank');
            	} else {
            		$("#animhome a").attr('target','_self');
            	}
        	} else {
        		$("#animhome a").attr('href','javascript:void(0)');
        		$("#animhome a").attr('target','_self');
        	}
        	
        	im.attr('src', item['url']);
                this.iterator += 1;
                if (this.iterator>=imgs.length )this.iterator=0;
        	//this.iterator = (this.iterator+1) % imgs.length;

        	this.currentId++;
    	}
    },
    
    pause:function()
    {
        
    },
    
    getCurrentUrl : function()
    {
        return this.imgs[this.iterator];
    }
});
