//
// Opens up waveform viewer in new window, sets the screen size correctly.
//
// Parameters
//  Userid - id for user who waveforms we are accessing
//  Simserver - the simulation server containing the waveforms.
//  FileTag - the file tag for the waveform data being accessed.
//  GroupName - The groupname(tab) that should be displayed first (optional)
//  Site - the application server the viewer is running on.. right now just stage or production. (optional)
//  WindowName - the name of the window, useful for opening multiple waveform viewers, just make sure the window name is unique. (optional)
//  WfOptions - Options to be appended to the waveform url. These include:
//	&help=$CUSTOMER
//		Allows a customer specific help button to show up. The url for this help text is located in the AppSettings section of the web.config.
//		&help=renesas
//
//	&groupName[i]=$GROUP
//		Dislpays the specified groups. N is the index of the tab. 
//		&groupName1=OUTPUT
//
//	&groupName[i]_[j]=$GROUP
//		Displays the specified group in the ith tab, under the first waveform. j must be between 2 & 6. j controls the order of the tabs.
//		&groupName1=OUTPUT&groupName1_2=IC ... This would display the IC graph under the OUTPUT graph on a tab called OUTPUT
//
//	&DefTab=$GROUP_NAME
//		Sets the default tab that should be selected.
//	
//	&PM[i]=$PM_NAME
//		Adds a column to the checkbar that has a specified performance measure name in the header row. This is applied to all tabs..
//
//	&PM[i]_[j]=$PM_NAME
//		Adds a column to the lower table that has a specified performance measure name in the header row. This only works with when used with the groupName[N] param.
//		i represents the tab number (the first tab is 1)
//		j represents the column appended to the table.
//
//		&PM1_1=RMS ... This would add a column with RMS at the top, and RMS measurements for each waveform below.
//
//	&PM[i]_[j]_[k]=$PM_NAME
//		NOT SURE ???
//
//	&PMBoxPM[i]_[j]=$PERFORMANCE_MEASURE
//		NOT SURE ???
//
//	&PMBoxWF[i]_[j]=$WAVEFORM
//		NOT SURE ???
//
//	&PMBoxDisplay[i]_[j]=$Display
//		DEPRICATED ... No Longer Used!
//
//	&PMBoxCols=$NUMBER_OF_COLUMNS
//		Sets the number of performance measure columns. To hide performance meausers, simply set to 0.
//		&PMBoxCols=$NUMBER_OF_COLUMNS
//
//	&gHeight=$HEIGHT
//		Sets the height of the graph for all tabs.
//		&gHeight=400
//
//	&gHeight[N]=$HEIGHT
//		Sets the height of a specific graph. This must be used with &groupName
//		&groupName1=OUTPUT&gHeight1=100	... this will set the graph height of the first graph to 100.
//
//	&ShowLegend=0 or 1
//		Toggles the legend to the right of the graph on and off.
//		&ShowLengend=0 ... no legend will be rendered.
//
//	&ShowCheckbar=0 or 1
//		Toggles the checkbar on and off.
//		&ShowCheckbar=0 ... no checkbar will be displayed.
//		TODO: This causes and error with the window resizing.
//
//	&ShowCheckBarHeader= 0 or 1
//		Toggles the checkbars header on and off. 
//		&ShowCheckBarHeader=0 ... no header will be show on the checkbar.
//		TODO: This stops the waveforms from rendering.
//
//	&ShowMarkerDiff= 0 or 1
//		Stops the detla column, which is calculated from m1 & m2 from being displayed.
//
//	&ShowUnitCol=0
//		Stops the unit column from being rendered in checkbar.
//
//	&ShowUnitColLG=0
//		Stops the unit column from being rendered in Legend. Takes precedence over ShowUnitCol parameter.
//
//	&ShowUnitColCB=0
//		Stops the unit column from being rendered in checkbar.  Takes precedence over ShowUnitCol parameter.
//
//	&Top=$OFFSET
//		Controls the offset of the canvas that the graph is drawn on. Generally, this should not be changed.
//
//	&Left=$OFFSET
//		Controls the offset of the canvas that the graph is drawn on. Generally, this should not be changed.
//	
//	&YAxisWidth=$WIDTH
//		Sets the width of teh y-axis, this can be useful for long graph titles.
//
//	&LineWeigth=$Weight
//		Sets the thinkness of the lines on the graph.
//
//	&LegendTop=$OFFSET
//		Sets the offset of the legend from the top left corner. Generally, this should not be changed.
//
//	&LegendLeft=$OFFSET
//		Sets the offset of the legend from the top left corner. Generally, this should not be changed.
//
//	&CssOverride=$CSS_FILE_REF
//		Allows the user to add an additional css file to the page. This could be used to override default CSS settings.
//
//  WindowOptions - Options for opening the window. This controls whether or not the window is resizable, its dimensions, ect...
//
//

var Webscope = {
    OpenScopes: undefined,
    Version: 'Webscope_V2.11',
    open: function(UserId, Simserver, FileTag, DefaultGroup, Site, WindowName, WfOptions, WindowOptions) {
        if (Site === undefined) Site = 'stage2';
        if (DefaultGroup === undefined) DefaultGroup = '';
        if (WfOptions === undefined) WfOptions = '';
        if (WindowOptions === undefined) WindowOptions = '';
        if (WindowName === undefined) WindowName = 'viewer_1';

        WindowName = WindowName.replace(/ /g, '_');
        this.__checkOpenScopes();

        var WindowSize = this.__determineWindowSize();

        var StrArgs =
         "width=" + 600 +
         ",height=" + 300 +
         ",toolbar=no" +
         ",resizable=yes" +
         ",status=no" +
         ",location=yes" +
	 ",scrollbars=1" +
         WindowOptions;

        var WaveformUrl =
         "http://" + Site + ".transim.com/" + this.Version + "/MTabViewer.aspx" +
         "?userID=" + UserId +
         "&simserv=" + Simserver +
         "&fileTag=" + FileTag +
         "&DefTab=" + DefaultGroup +
         "&scrnW=" + WindowSize.w +
         "&scrnH=" + WindowSize.h +
         WfOptions;

        // closeByName(WindowName);
        //console.log(WaveformUrl);
        var wsWindow = window.open(WaveformUrl, '_blank', StrArgs);
        wsWindow.moveTo(0, 0);
        wsWindow.focus();
        this.OpenScopes.push({ name: WindowName, win: wsWindow });
    },
    closeAll: function() {
        this.__checkOpenScopes();
        for (var i = 0; i < this.OpenScopes.length; i++) {
            this.OpenScopes[i].win.close();
        }
        this.OpenScopes.splice(0);
    },
    closeByIndex: function(i) {
        this.__checkOpenScopes();
        if (i < this.OpenScopes.length) {
            this.OpenScopes[i].win.close();
            this.OpenScopes.splice(i, 1);
        }
    },
    closeByName: function(name) {
        this.__checkOpenScopes();
        for (var i = 0; i < this.OpenScopes.length; i++) {
            if (this.OpenScopes[i].name == name) {
                this.OpenScopes[i].win.close();
                this.OpenScopes.splice(i, 1);
            }
        }
    },
    __determineWindowSize: function() {
        var result = {
            w: screen.availWidth > 1280.0 ? 1280.0 : screen.availWidth,
            h: screen.availHeight
        }
        return result;
    },
    __checkOpenScopes: function() {
        if (this.OpenScopes === undefined) this.OpenScopes = new Array();
    }
};






