/******************************
    centerlist.js
    
    Javascript for the center list page on Taubman Corporate site
    
    Dependencies:
    jquery
    jquery.timers
    swfobject2

******************************/

function Centerlist( aConfig ) {
    var qstring = "?QString=";

    if( aConfig[0] == undefined ) {
        return null;
    }
    else {
        this.config = aConfig;
        this.centers = new Object();
        this.offices = new Object();
        for (i=0; i < config.length; i++) {
            config[i].curFrame = 0; // 0 to numFrames - 1
            config[i].numFrames = 2;
            config[i].frames = [];
            var container = $("#"+config[i].container_prefix)
            container.html("");
            container.css({
                'position' : 'relative'
            });
            
            this.centers[i] = new Object();
            this.offices[i] = new Object();

            for(j=0; j < config[i].numFrames; j++ ) {
                var new_id =  container.attr("id") + "_" + j;
                config[i].frames[j] = new_id;
                var html = '<div class="swf_frame" id="' + new_id + '"></div>';
                container.append(html);
            }
        }
    }
    

    this.addMap = function( swfURL, container_prefix, height, width ){
        this.map_info.push({
            swfURL:             swfURL,
            container_prefix:   container_prefix,
            height:             height,
            width:              width
        });
    };

    this.display = function() {
        for (i=0; i < config.length; i++) {
            var curFrame = config[i].curFrame;
            swfobject.embedSWF(
                config[i].swfURL,
                config[i].container_prefix + "_" + curFrame,
                config[i].width,
                config[i].height,
                config[i].flash_version,
                config[i].expressInstall,
                config[i].flashvars,
                config[i].params,
                config[i].attributes
            ); 
            $("#" + config[i].frames[curFrame]).css({
                'z-index' : '100'
            });
        }
    }

    this.update = function( map_index, newURL ) {
        if( map_index > config.length ) {
            return null;
        }
        var i = map_index;
        config[i].curFrame = ( (config[i].curFrame + 1) % config[i].numFrames );
        var url;
        if( newURL != undefined ) {
            url = newURL;
        }
        else {
            url = config[i].swfURL;
        }
        swfobject.embedSWF(
            url,
            config[i].container_prefix + "_" + config[i].curFrame,
            config[i].width,
            config[i].height,
            config[i].flash_version,
            config[i].expressInstall,
            config[i].flashvars,
            config[i].params,
            config[i].attributes
        );
        for(x=0; x < config[i].numFrames; x++){
            var switchtime = 500;
            if( x == config[i].curFrame ) {
                $("#" + config[i].frames[x]).css({
//                    'z-index'   : '100',
                    'position'  : 'absolute',
                    'top'       : '0',
                    'left'      : '0'
                }).oneTime( switchtime, "to_front",  function(){
                    $(this).css( { 'z-index' : '100' } ).show();
                });
            }
            else {
                $("#" + config[i].frames[x]).css({
//                    'z-index' : '0',
                    'position'  : 'absolute',
                    'top'       : '0',
                    'left'      : '0'
                }).oneTime( switchtime, "to_back", function() {
                    $(this).hide().css( { 'z-index' : '0' } );
                });
            }
        }
    }

    this.toggleCenter = function( map_index, center_id ) {
        if( this.centers[map_index][center_id] != undefined ) {
            this.centers[map_index][center_id] = undefined;
        }
        else {
            this.centers[map_index][center_id] = 1;
        }
    }

    this.getQueryString = function ( map_index ) {
        var c = new Array();
        var o = new Array();

        for (var prop in this.centers[map_index] ) {
            if( this.centers[map_index][prop] != undefined ){
                c.push( "centers=" + prop );
            }
        }

        for (var prop in this.offices[map_index] ) {
            if( this.offices[map_index][prop] != undefined ){
                o.push( "offices=" + prop );
            }
        }
        
        var qstr = c.join("&") 
        if( qstr.length > 0 ){
            qstr = qstr + "&" + o.join("&");
        }
        else {
            qstr = o.join("&");
        }
        return qstr;
    }

    this.toggleOffice  = function ( map_index, office_id ) {
        if( this.offices[map_index][office_id] != undefined ) {
            this.offices[map_index][office_id] = undefined;
        }
        else {
            this.offices[map_index][office_id] = 1;
        }
    }
    
    return this;
};
