
var motionslider=false;


var ms_loadingIndex = 0;
var ms_loadingImage = false;
var ms_currentImage = 0;
var ms_previousImage = -1;

function animationSwitch(){
    if(ms_previousImage!=-1){
        motionslider.childNodes[ms_previousImage].style.zIndex=0;
        var imgs=motionslider.childNodes[ms_previousImage].firstChild;
        var ww=900;
        var hh=Math.round((ww/imgs.width)*imgs.height);
        jQuery(imgs).animate({left:0,top:-100,width:900,height:hh},1800,'linear');
        var op=0;
        if(jQuery.browser.msie)
            op=1;
        jQuery(motionslider.childNodes[ms_previousImage]).animate({opacity:op},1000);
    }
    motionslider.childNodes[ms_currentImage].style.visibility='visible';
    motionslider.childNodes[ms_currentImage].style.zIndex=1;
    jQuery(motionslider.childNodes[ms_currentImage]).animate({opacity:1},800);
    
    var n=Math.round(Math.random()*400);
    var nn=Math.round(Math.random()*260)+50;
    var sz=(800+n)+Math.round(Math.random()*400);//1300
    
    if((sz-n)<860)
        n=sz-860;
    if(n<0){
        n=0;
    }
    
    var right=-n+sz;
    while(right<950){
        sz++;
        right=-n+sz;
    }
    
    var bottom=-nn+max_height;
    while(bottom<0){
        nn--;
        bottom=-nn+max_height;
    }
    
    var img=motionslider.childNodes[ms_currentImage].firstChild;
    
    var sh=0;
        sh=Math.round((sz/img.width)*img.height);
        jQuery(img).animate({left:-n,top:-nn,width:sz,height:sh},2300,'linear');
    
    ms_previousImage=ms_currentImage;
    if((ms_currentImage+1)>=motionslider_urls.length){
        ms_currentImage=0;
    }else
        ms_currentImage++;
    setTimeout(animationSwitch,2000);
}
var max_height=1000;

var imagesizes=new Array();


function ms_imageLoaded(){
    if(!ms_loadingImage){
        ms_loadingImage=true;
        var d=document.createElement('DIV');
        d.className='slide';
        var img=new Image();
        d.appendChild(img);
        motionslider.appendChild(d);
        d.style.visibility='visible';
        

        img.onload=function(){
            var w=this.width;
            var h=Math.round((860/w)*this.height);
            
            this.style.width='769px';
            this.style.height=Math.round((769.0/w)*h)+'px';
            max_height=h;
            jQuery(this.parentNode).animate({opacity:1},400);
            ms_imageLoaded();
            
        };
        img.src=motionslider_urls[ms_loadingIndex];
        return;
    }
    
    
    ms_loadingIndex++;
    
    if(ms_loadingIndex < motionslider_urls.length){
        var w=this.width;
        var h=Math.round((860/w)*this.height);
        
       // this.style.width='769px';
       // this.style.height=Math.round((769/w)*h)+'px';

        if(h<max_height)
            max_height=h;
        
        var d=document.createElement('DIV');
        d.className='slide';
        var img=new Image();
        d.appendChild(img);
        motionslider.appendChild(d);
        img.onload=ms_imageLoaded;
        img.src=motionslider_urls[ms_loadingIndex];
    }else{
        ms_currentImage++;
        animationSwitch();
    }
}

function motionslider_init(){
    motionslider=document.getElementById('motionslider');
    motionslider.onmousedown=function(e){
        if(e.preventDefault)
            e.preventDefault();
    };
    if(motionslider_urls.length==1){
        var d=document.createElement('DIV');
        d.className='slide';
        var img=new Image();
        d.appendChild(img);
        motionslider.appendChild(d);
        img.onload=function(){
            var w=this.width;
            var h=Math.round((860/w)*this.height);
            max_height=h;
            jQuery(this.parentNode).animate({opacity:1},400);
            this.parentNode.style.visibility='visible';
        };
        
        img.src=motionslider_urls[0];
        
    }else{
        ms_imageLoaded();
    }
    
}
if(window.addEventListener)
window.addEventListener('load',motionslider_init);
else if(window.attachEvent)
window.attachEvent("onload", motionslider_init);
else
document.getElementsByTagName('BODY')[0].onload=motionslider_init;

