﻿// Function to resize the Iframe to the size of the contained content.
var errorTimeout;
var timeoutTimeout;
function OnNavigate( url ) {
    var iframe = getObject( 'DetailFrame' );
    iframe.src = url;
    hideObject( 'DetailFrame' );
    
    // Show/Hide the Progress box if it exists.
    if ( getObject( 'divFailedLoad' ) ) {
        hideObject( 'divFailedLoad' );
    }
    if ( getObject( 'divProgress' ) ) {
        centerObject( 'divProgress' );
        showObject( 'divProgress' );

        // Ensure hiding of object after some amount of time.
        timeoutTimeout = setTimeout( 'OnNavigateError()', 20 * 1000 );       // 20 seconds
    }
}
function OnNavigateError() {
    // Show/Hide the Progress box if it exists.
    if ( getObject( 'divProgress' ) ) {
        // Show the Failure Area
        if ( getObject( 'divFailedLoad' ) ) {
            centerObject( 'divFailedLoad' );
            showObject( 'divFailedLoad' );

            // Ensure hiding of object after some amount of time.
            clearTimeout( timeoutTimeout );
            errorTimeout = setTimeout( 'hideObject( "divFailedLoad" )', 60 * 1000 );       // 60 seconds
        }
        
        // Hide the Progress Area
        hideObject( 'divProgress' );
    }
}
function OnContentLoad() {
    var height, width;
    var grid = document.getElementById( 'divGrid' );
    var iframe = document.getElementById( 'DetailFrame' );
    if ( isFirefox ) {   // this is for Firefox, can't get the value.
        height = documentHeight() - getHeight( 'divGrid' ) - getHeight( 'divFrameHeader' ) - getHeight( 'divHeader' ) - 60;
    }
    else {
        if ( parent.document.body.scrollHeight )
            height = parent.document.body.scrollHeight;
        else
            height = '300px';
        height = height - getHeight( 'divGrid' ) - getHeight( 'divFrameHeader' ) - getHeight( 'divHeader' ) - 60;
    }
    iframe.height = height;
    iframe.width = getWidth( 'divGrid' );
    
    //iframe.height = null;
    //height = documentHeight() - getHeight( 'divGrid' ) - getHeight( 'divMailHeader' ) - getHeight( 'divHeader' ) - 50;
    //height = document.body.clientHeight - getHeight( 'divBody' ) - getHeight( 'divMailHeader' ) - getHeight( 'divHeader' );
    //height = iframe.document.body.scrollHeight;
    //iframe.style.height = height;
    
    // Show/Hide the Progress box if it exists.
    if ( getObject( 'divProgress' ) ) {
        hideObject( 'divProgress' );
    }

    // Show the Navigated Frame
    showObject( 'DetailFrame' );
    clearTimeout( errorTimeout );
    clearTimeout( timeoutTimeout );
}

// Note Functionality
function ShowNote( el, id ) {
    var x, y;
    x = parseInt(getObjectAbsX( el )) + 'px'; //  + parseInt(getWidth(el.id)) - parseInt(getWidth( id )) + 'px'; 
    y = parseInt(getObjectAbsY( el )) - 100 + 'px'; // - parseInt(getHeight( el.id )) + 5 + 'px';
    //window.status = 'Location: ' +  x + ' ' + y;
	moveObject( id, x, y );
	showObject( id ); 

    // Show iframe behind note for ie.
    if ( isIE ) {
        // Use the Help IFrame
        var note = document.getElementById(id);
        var shim = document.getElementById('shim');
        note.style.zIndex = 100;
        shim.style.width = note.offsetWidth;
        shim.style.height = note.offsetHeight;
        shim.style.zIndex = 99;
        shim.style.Border = 'solid 10px red';
        moveObject( 'shim', getLeft(id), getTop(id) );
        showObject('shim');
    }
}
function HideNote( id ) {
    hideObject( id );
    if (isIE) {
        hideObject('shim');
    }
}

// Add an Event to the specified Object.
function addEvent(obj, evType, fn, useCapture){
        if (obj.addEventListener){
                obj.addEventListener(evType, fn, useCapture);
                return true;
        } else if (obj.attachEvent){
                var r = obj.attachEvent("on"+evType, fn);
                return r;
        }
        return false;
}

// Remove the specified Event from the Object.
function removeEvent(obj, evType, fn, useCapture){
        if (obj.removeEventListener){
                obj.removeEventListener(evType, fn, useCapture);
                return true;
        } else if (obj.detachEvent){
                var r = obj.detachEvent("on"+evType, fn);
                return r;
        }
        return false;
}

function createCookie(name,value,days) {
        if (days)
        {
                var date = new Date();
                date.setTime(date.getTime()+(days*24*60*60*1000));
                var expires = "; expires="+date.toGMTString();
        }
        else var expires = "";
        document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
        var nameEQ = name + "=";
        var ca = document.cookie.split(';');
        for(var i=0;i < ca.length;i++)
        {
                var c = ca[i];
                while (c.charAt(0)==' ') c = c.substring(1,c.length);
                if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
        }
        return null;
}

function eraseCookie(name) {
        createCookie(name,"",-1);
}

String.prototype.trim = function() {
        return this.replace(/^\s*/,'').replace(/\s*$/, '');
}
String.prototype.stripTags = function() {
        return this.replace(/<[^>]*>/g, "");
}

var stickyManager = { };

(function() {

function sticky(c,x,y,w,id) {
        this.elm = document.createElement("DIV");

        var o = this;
        o.style = o.elm.style;
        o.elm.className = 'yellow_sticky';

        document.body.appendChild(o.elm);
        o._id = o.elm.id = id||"sticky_" + new Date().getTime();

        o._mx = 0;
        o._my = 0;
        o._x = x||800;  // + Math.floor(Math.random()*501);
        o._y = y||180;  //+ Math.floor(Math.random()*201);
        o._w = w||200;

        // Child div for IE 5.5 & 6, an image for all others
        o.bgImg = document.createElement("img");
        
        // I'm using conditional compilation to ensure ie5.5, ie6 gets a div

        /*@cc_on
        @if (@_jscript_version >= 5.5 && @_jscript_version < 7 && @_win32)
                o.bgImg = document.createElement("div");
                o.elm.appendChild(o.bgImg);
                o.bgImg.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='//my.ciradar.com/images/sticky.png', sizingMethod='scale')"
        @end @*/
        
        o.elm.appendChild(o.bgImg);
        o.bgImg.src = '//my.ciradar.com/images/sticky.png';
        
        // Content div
        o.content = document.createElement("div");
        o.elm.appendChild(o.content);
        o.content.className = 'yellow_sticky_content';

        // Form
        o.form = document.createElement("form");
        o.elm.appendChild(o.form);
        o.form.className = 'yellow_sticky_form';
        o.form.action = '#';
        o.form.style.display = 'none';

        o.content.innerHTML = c;

        // Resize div
        //o.resize = document.createElement("div");
        //o.elm.appendChild(o.resize);
        //o.resize.className = 'yellow_sticky_resize';
        //o.resize.title = '';

        // Dragbar div
        o.dragBar = document.createElement("div");
        o.elm.appendChild(o.dragBar);
        o.dragBar.className = 'yellow_sticky_dragbar';
        o.dragBar.title = '';

        // Delete div
        o.remove = document.createElement("a");
        o.elm.appendChild(o.remove);
        o.remove.className = 'yellow_sticky_remove';
        
        // Should really be adding a DOM textNode here..
        o.remove.innerHTML = "&times;";
        o.remove.href = "#";
        o.remove.title = '';
        
        o.selectStart = document.onselectstart;
        
        // this function swiped from coudal slideshow?
        o.setOpacity = function(opacity) {
                // Fix for math error in some browsers
                opacity = (opacity == 100)?99.999:opacity;

                // Safari < 1.2, Konqueror
                o.style.KHTMLOpacity = opacity/100;
                // Older Mozilla and Firefox
                o.style.MozOpacity = opacity/100;
                // Safari 1.2, newer Firefox and Mozilla, CSS3
                o.style.opacity = opacity/100;
        }
        o.startDrag = function(e) {
                if (!e) var e = window.event;

                o._mx=(e.x)?e.x:e.clientX;
                o._my=(e.y)?e.y:e.clientY;

                o._x = parseInt(o.style.left);
                o._y = parseInt(o.style.top);

                o.setOpacity(92);
                o.bringToTop();
                addEvent(document,'mousemove',o.trackDrag, false);
                addEvent(document,'mouseup',o.stopDrag, false);
                document.onselectstart = function() { return false; };

                if (window.event && window.event.returnValue) {
                        window.event.returnValue = false;
                }
                if (e && e.preventDefault) {
                        e.preventDefault();
                }
        }
        o.trackDrag = function(e) {
                if(e) {
                        var diffx = e.clientX - o._mx;
                        var diffy = e.clientY - o._my;
                } else {
                        var diffx = window.event.x - o._mx;
                        var diffy = window.event.y - o._my;
                }
                o.style.left = Math.round(o._x + diffx) > 0 ? Math.round(o._x + diffx) + 'px' : "0px";
                o.style.top  = Math.round(o._y + diffy) > 0 ? Math.round(o._y + diffy) + 'px' : "0px";
        }
        o.stopDrag = function(e) {
                removeEvent(document,'mousemove',o.trackDrag, false);
                removeEvent(document,'mouseup',o.stopDrag, false);

                o.setOpacity(99);
                o._x = parseInt(o.style.left);
                o._y = parseInt(o.style.top);
                document.onselectstart = o.selectStart;
        }
        o.startResize = function(e) {
                o._mx=(!e)?window.event.x:e.clientX;
                o._w = parseInt(o.style.width);

                o.setOpacity(80);
                o.bringToTop();

                addEvent(document,'mousemove',o.trackResize, false);
                addEvent(document,'mouseup',o.stopResize, false);
                
                o.selectStart = document.onselectstart;
                document.onselectstart = function() { return false; };

                if (window.event && window.event.returnValue) {
                        window.event.returnValue = false;
                }
                if (e && e.preventDefault) {
                        e.preventDefault();
                }
        }
        o.trackResize = function(e) {
                var diffx = (!e)?window.event.x - o._mx:e.clientX - o._mx;
                var w = parseInt(o._w + diffx);
                if(w > 500) w = 500;
                else if(w < 140) w = 140;
                o.redraw(w);
        }
        o.stopResize = function(e) {
                var diffx = (!e)?window.event.x - o._mx:e.clientX - o._mx;
                o._w = parseInt(o._w + diffx);
                if(o._w < 140) o._w = 140;
                else if(o._w > 500) o._w = 500;
                o.setOpacity(99);
                removeEvent(document,'mousemove',o.trackResize, false);
                removeEvent(document,'mouseup',o.stopResize, false);
                document.onselectstart = o.selectStart;
        }
        o.redraw = function(wi) {
                var w = wi||parseInt(o._w);
                o.style.height = o.bgImg.style.height = o.style.width = o.bgImg.style.width = w + 'px';
                if(document.all && !window.opera) {
                        o.form.style.height = Math.round((w/100) * 88) + "px";
                        o.content.style.height = Math.round((w/100) * 90) + "px";
                        o.dragBar.style.height = Math.round((w/100) * 5) + "px";
                }

                //var fs = Math.round(w / 20);
                //o.content.style.fontSize = o.remove.style.fontSize = fs + 'px';
                o.content.style.fontsize = 12 + 'px';
        }
        o.removeSticky = function(e) {
                o.bringToTop();
                document.body.removeChild(o.elm);
                var stickytemp = new Array();
                for(var i=0; i<sticky.bag.length; i++) {
                        if(sticky.bag[i]._id != o._id) {
                                stickytemp.push(sticky.bag[i]);
                        }
                }

                sticky.bag = stickytemp;
        }

        o.bringToTop = function() {
                o.style.zIndex = ++sticky.z;
        }
        o.showAsSticky = function() {
                o.elm.onmouseover = o.mouseOver;
                o.elm.className = 'yellow_sticky';
                o.redraw();
                addEvent(o.content,'dblclick',o.startEdit,false);
        }
        
        // Initial event handlers
        addEvent(o.remove,'mousedown',o.removeSticky,false);
        addEvent(o.content,'mousedown',o.startDrag,false);
        addEvent(o.dragBar,'mousedown',o.startDrag,false);
        addEvent(o.content,'mousedown',o.bringToTop,false);
        //addEvent(o.resize,'mousedown',o.startResize,false);

        // Initial position
        o.style.left = o._x + 'px';
        o.style.top  = o._y + 'px';

        o.redraw();
        o.bringToTop();

        return o;
}

sticky.z = 0;
sticky.bag = new Array();

stickyManager = {
    newSticky:function(msg,width,id) {
            sticky.bag = new Array();
            var obj = new sticky(msg,750,200,width,id);
            sticky.bag.push(obj);
    }
}

})();


// Notify Atlas that we have been loaded
if(typeof(Sys) !== "undefined")
	Sys.Application.notifyScriptLoaded();

