/*
  Universal film application.
  Used for creating professional films.
  Version 1.3 debug
  (c) 2001 Oleg Bugrimov, Danila Koltsov
  


*/

function Film(name){
    if( ! name) { 
        alert("name required"); 
        return false; 
    }
    // properties
    this.objname = name;
    this.images = new Array();
    this.order_type = new Array();
    this.order  = new Array();
    this.default_order_type = 'default';
    this.IE = (navigator.appName.charAt(0)=='M' && navigator.appVersion.charAt(0) > 3) ? true : false;  // IE ��� ��� ���?
    this.imagenames = new Array();
    this.transition = 12;           
    this.duration = 1.35;
    this.playing_line = 0;
    this.repeatFilmsPeriod = 0;
    this.debug = 0;
    this.prefix = '';
    this.sufix = '';
    this.timeout;
    this.numFilters = 1;
    this.filters = new Array();
    this.flag_for_filter = new Array();
    this.default_filter_type = "default";
    this.filters_order_type;
    this.period_before_line = new Array();
    this.global_period_between_images = 0;
    this.global_period_between_lines = 0;
    this.preload_level = 0; // 0-all images; 1-only one line

    // methods
    this.addLine = addLine;
    this.addCurrentLine = addCurrentLine;
    this.lineComplete = lineComplete;
    this.playFilm = playFilm;
    this.setImages = setImages;
    this.setFilter = setFilter;
    this.setPeriodBeforeLine = setPeriodBeforeLine;
    this.setGlobalPeriodBetweenImages = setGlobalPeriodBetweenImages;
    this.setGlobalPeriodBetweenLines = setGlobalPeriodBetweenLines;
    this.setOrderType = setOrderType;
    this.setDefaultOrderType = setDefaultOrderType;
    this.selectOrder = selectOrder;
    this.Stop = Film_Stop;
    this.SpoilLines = Film_SpoilLines;

    this.setFilterType = Film_SetFilterType;
    this.addToFiltersList = Film_AddToFiltersList;
    this.addFlagForFilter = Film_AddFlagForFilter;  
    this.addToFilters = Film_AddToFilters;
    this.clearFlagsFilter = Film_ClearFlagsFilter;
    
    this.playNextLine = Film_PlayNextLine;
    this.playPreviosLine = Film_PlayPreviosLine;

    // internal
    this.setFilterBeforePlay = Film_SetFilterBeforePlay;

    return this;
}


function Film_SpoilLines(){
  var i,rand;
  var temp = new Array();
  for(i=0; i<this.images.length; i++){
    do{
      rand = rnd(0,this.images.length-1)
    } while( rand == i );
    temp = this.images[i];
    this.images[i] = this.images[rand];
    this.images[rand] = temp;
  }
}


function Film_Stop(){
  clearTimeout(this.timeout);
  clearTimeout(this.linesTimeout);
}


function selectOrder(){
    var line = this.images.length;
    if( this.imagenames.length != selectOrder.arguments.length ){
        alert('selectOrder: number of arguments conflicts');
        return false;
    }
    this.order[line] = new Array();
    for(var i=0; i<selectOrder.arguments.length; i++){
        this.order[line][i] = selectOrder.arguments[i];
    }
    return true;
}


function setDefaultOrderType(type){
    this.default_order_type = type;
    return true;
}


function setOrderType(type){
    type = (!type) ? this.default_order_type : type;
    if( type != 'default' && type != 'random' && type != 'select' ){
        alert("setOrder: uknown type of order: "+type);
    }
    this.order_type[this.images.length] = type;
    return true;
}

function setPeriodBeforeLine(period){
    this.period_before_line[this.images.length] = period;
    return true;
}

function setGlobalPeriodBetweenImages(period){
    this.global_period_between_images = period;
    return true;
}

function setGlobalPeriodBetweenLines(period){
    this.global_period_between_lines = period;
    return true;
}

function setFilter(transition, duration){
    this.transition = transition;
    this.duration = duration;
    return true;
}

function setImages(){
    for(var i=0; i<setImages.arguments.length; i++){
        this.imagenames[i] = setImages.arguments[i];
    }
    return true;
}

function addLine(){
    if( addLine.arguments.length != this.imagenames.length ){
        alert("addLine: number of adresses incorrect");
        return false;
    }
    var line = this.images.length;
    if( ! this.order_type[line] ){
        this.setOrderType();
    }

        if( !this.filters_order_type) {
        alert("Popal");
                this.setFilterType();
    }

    this.images[line] = new Array();
    for(var i=0; i < addLine.arguments.length ; i++){
        if( this.preload_level == 0 ){
            this.images[line][i] = new Image();
        } else {
            this.images[line][i] = new Object();
            this.images[line][i].not_image = true;
        }
        this.images[line][i].src = this.prefix+addLine.arguments[i]+this.sufix;
        this.images[line][i].width =300;
        this.images[line][i].height =200;


    }

        return true;
}


function addCurrentLine(){
    var line = this.images.length;
    if( ! this.order_type[line] ){
        this.setOrderType();
    }

        if( !this.filters_order_type) {
               this.setFilterType();
    }

    this.images[line] = new Array();
    for(var i=0; i < this.imagenames.length ; i++){
        if( this.preload_level == 0 ){
            this.images[line][i] = new Image();
        } else {
            this.images[line][i] = new Object();
            this.images[line][i].not_image = true;
        }
        this.images[line][i].src = eval("document."+this.imagenames[i]+".src");
    }

    return true;
}


function lineComplete(line){
    if( line >= this.images.length ){ alert("lineComplete: line with number "+line+" is not added."); return false }

    if( this.preload_level == 1 ){
        var src;
        for(var i=0; i < this.images[line].length; i++){
            if( this.images[line][i].not_image ){
                 src = this.images[line][i].src;
                 this.images[line][i] = new Image();
                 this.images[line][i].src = src;

            }
        }
    }

    for(var i=0; i < this.images[line].length; i++){
        if( ! this.images[line][i].complete ){
            if( this.debug ){
                window.status = "lineComplete: Image is not loading: "+this.images[line][i].src;
            }
            return false;
        }
    }
    return true;
}


function Film_SetFilterBeforePlay(){

    var maxFilter = this.filters.length;

    switch ( this.filters_order_type ) {
        case 'default':
                    this.setFilter(12,1.35);
                    break;
        case 'random': 
             do{
                num_filter = rnd(0,this.filters.length-1);
                if (this.numFilters > maxFilter) {
                                  this.clearFlagsFilter();
                                }

             } while (this.flag_for_filter[num_filter]!=0);
                    this.numFilters++;  
                    this.flag_for_filter[num_filter]=1;
                    this.setFilter(this.filters[num_filter][0],this.filters[num_filter][1]);
                    break;
        default:
            alert("playFilm: unknown filter type: "+this.filter_order_type[this.playing_line]);
            return false;
    }  
}


function Film_PlayNextLine(){

    if( ! this.lineComplete(this.playing_line) ){
        setTimeout(this.objname+".playNextLine()", 500);
        return;
    }

    this.Stop();

    // clearing previos line, because it's already played
    if( this.preload_level == 1 ){
        var line = this.playing_line;
        var src;
        if(this.playing_line > 0){
            line--;
        } else {
            line = this.images.length-1;
        }
        for(var i=0; i < this.images[line].length; i++){
            src = this.images[line][i].src;
            this.images[line][i] = new Object();
            this.images[line][i].src = src;
            this.images[line][i].width = 300;
            this.images[line][i].height = 200;

            this.images[line][i].not_image = true;
        }
    }

    var num;
    var tmp_arr = new Array(this.imagenames.length); 

    this.setFilterBeforePlay();

    for(var i=0; i < this.imagenames.length; i++){
        switch ( this.order_type[this.playing_line] ) {
            case 'default':
                num = i;
                break;
            case 'random': 
                do{
                  num = rnd(0,this.imagenames.length-1);
                } while( tmp_arr[num] );
                tmp_arr[num] = true;
                break;
            case 'select':
                num = this.order[this.playing_line][i];
                break;
            default:
                alert("playFilm: unknown order of images: "+this.order_type[this.playing_line]);
            return false;
        }

        //this.timeout = setTimeout('playFilter('+this.IE+',document.'+this.imagenames[i]+', "'+this.images[this.playing_line][i].src+'", '+this.duration+', '+this.transition+')', num*this.global_period_between_images);
        this.timeout = setTimeout('playFilter('+this.IE+',document.'+this.imagenames[i]+', '+this.objname+'.images['+this.playing_line+']['+i+'], '+this.duration+', '+this.transition+','+this.preload_level+')', num*this.global_period_between_images);
    }

    if(this.playing_line >= this.images.length-1){
        this.playing_line = 0;
        this.line_period = this.repeatFilmsPeriod;
    } else {
        this.playing_line++;    
        this.line_period = 0;
    }

    this.line_period += (this.period_before_line[this.playing_line] > 0) ? this.period_before_line[this.playing_line] : this.global_period_between_lines;
    this.line_period += this.imagenames.length*this.global_period_between_images;
}


function Film_PlayPreviosLine(){
    if(this.playing_line > 0 ){
        this.playing_line--;
    } else {
        this.playing_line = this.images.length-1;    
    }
    if(this.playing_line > 0 ){
        this.playing_line--;
    } else {
        this.playing_line = this.images.length-1;    
    }
    this.playNextLine();
}


function playFilm(){
    if( playFilm.arguments.length >0 ) { alert("palyFilm: don't need parameters here?"); return false }
    var theperiod = 500;

    if( this.lineComplete(this.playing_line) ){
        this.playNextLine();
        theperiod += this.line_period;
    }

    this.linesTimeout = setTimeout(this.objname+".playFilm()",  theperiod);
    //return false;
}


function playFilter(IE, img, toimg, duration, transition, preload_level){
    if(  IE ){
        if( img.filters[0].status == 2 ){
            return false;
        }
        img.filters[0].Duration = duration;
        img.filters[0].Transition = transition;
        img.filters[0].Apply();
    }
    img.src = toimg.src;
    if( img.height != toimg.height ){
         img.height = toimg.height;
    }
    if( img.width != toimg.width ){
         img.width = toimg.width;
    }
    if( IE ){ 
        img.filters[0].Play();
    }
    return true;
}


function rnd(c, x){var therange = x-c+1;    return (Math.floor(Math.random() * Math.pow(10,("" + therange).length)) % therange) + parseInt(c);}


function Film_SetFilterType (type){
        type = (!type) ? this.default_filter_type : type;
    if( type != 'default' && type != 'random'){
        alert("unknown filter type: "+type);
        } 

        this.filters_order_type = type;
    return type;
}


function Film_AddToFilters (){
        var args = Film_AddToFilters.arguments;
        for (var i=0; i < (args.length); i+=2){
        this.addToFiltersList (args[i],args[i+1]);
        }
        this.setFilterType ("random");
        return true;      
}
                            

function Film_AddToFiltersList (dr,tr){
    var filter = this.filters.length;
    this.filters[filter] = new Array();
    this.filters[filter][0] = dr;
    this.filters[filter][1] = tr; 
    this.addFlagForFilter();
    return true; 
}

function Film_AddFlagForFilter (){
    var flag = this.flag_for_filter.length;
    this.flag_for_filter[flag]=0;
    return true;
}

function Film_ClearFlagsFilter(){
        var flag = this.flag_for_filter.length-1;
        for (i=0; i<=flag; i++){
           this.flag_for_filter[i]=0;
        } 
        this.numFilters = 1;
    return true;
}


