﻿var currentDoton = null;
var ids = new Array('buttona','buttonb','buttonc','buttond','buttone','buttonf','buttong','buttonh','buttoni'); //array of dots
function showMenu(evt) 
{
    var obj = document.getElementById('divMenuContainer');
    var menuDiv = document.getElementById('divMenu');

    if (menuDiv.style.display == 'none') {
        menuDiv.style.top = '-95px';
        menuDiv.style.left = '0px';
        menuDiv.style.display = 'block';
        var imgs = obj.getElementsByTagName('img');
        imgs[0].src = 'img/shareminus.bmp';
        if (obj.setCapture)
            obj.setCapture(false);
        else {
            if(obj.addEventListener)
            {
                document.addEventListener("click", showMenu, true);
                window.captureEvents(evt);
            }
            else
            {
                document.addEventListener("click", showMenu);
                window.captureEvents(evt);
            }
        }
    }
    else {
        menuDiv.style.display = 'none';
        var imgs = obj.getElementsByTagName('img');
        imgs[0].src = 'img/but_shareplus.gif'; 
        if (obj.setCapture) 
            obj.releaseCapture();
        else 
        {
            if(obj.removeEventListener)
            {
                document.removeEventListener("click", showMenu,true);
                window.releaseEvents(evt);
                if(evt.target.parentNode.id == "menuImgId" || evt.target.parentNode.id =="divMenuContainer")
                    evt.cancelBubble = true;
            }
            else
            {
                document.detachEvent('onclick', showMenu);
                window.releaseEvents(evt);  
                evt.cancelBubble = true;
            }
        }
    }
}     
        
function initRelatedLinks() {
    $(".relatedlinkDiv").each(function(i) {
        if (i > 0) { this.style.display = "none"; }
    })
}
function toggleRelatedLinks() {
    if ($(".relatedlinkDiv").length > 1) {
        var j;
        $(".relatedlinkDiv").each(function(i) {
            if (this.style.display != "none") {
                this.style.display = "none";
                j = i;
                return false;
            }
        })
        if (j < $(".relatedlinkDiv").length - 1) {
            $(".relatedlinkDiv").get(j + 1).style.display = "";
        }
        else {
            $(".relatedlinkDiv").get(0).style.display = "";
        }
    }

    setTimeout("toggleRelatedLinks()", 10 * 1000);
}
/*Email menu*/
function Email(evt) {
    currentDoton = $(".tnav .diodeon").get(0);
    if(null == currentDoton)
        currentDoton = $(".tnav .diodeoff").get(0);
    t1 = new Tween(document.getElementById('sq').style, 'left', Tween.strongEaseOut, 0, -2365, 0.7, 'px');
    t1.start();

    currentDoton.className = 'diodeoff';
    if (!document.all) {
        evt.cancelBubble = true;
    }
}
/*RSS menu*/
function RSS(evt) {
    window.open("http://feeds2.feedburner.com/morningstarvideofeed");
    if (!document.all) {
        evt.cancelBubble = true;
    }
}
/*Link menu*/
function GetLink(evt) {
    currentDoton = $(".tnav .diodeon").get(0);
    if(null == currentDoton)
        currentDoton = $(".tnav .diodeoff").get(0);
    t1 = new Tween(document.getElementById('sq').style, 'left', Tween.strongEaseOut, 0, -1892, 0.7, 'px');
    t1.start();

    currentDoton.className = 'diodeoff';
    if (!document.all) {
        evt.cancelBubble = true;
    }
}
function storeDoton() {
    currentDoton = $(".tnav .diodeon").get(0);
}
function restoreDoton() {
    if (currentDoton)
        doton(currentDoton);
    currentDoton = null;
}
function ShowVideo() {
    storeDoton();
    t1 = new Tween(document.getElementById('sq').style, 'left', Tween.strongEaseOut, -473, 0, 0.7, 'px');
    t1.start();
}
function turnoff(id) 
{ 
    document.getElementById(id).className='diodeoff';
} 
//changes dots to off mode
function offall() 
{ 
    for (var i = 0; i < ids.length; i++) 
    { 
        turnoff(ids[i]); 
    } 
} 
//for buttons to switch b/w 3 choices
function rotate(obj) {
    var getid=obj.id;
    switch(getid)
    {
        case ids[0]:
            t1 = new Tween(document.getElementById('sq').style,'left',Tween.strongEaseOut,-473,0,0.7,'px');
            t1.start();
            break;
        case ids[1]:
            t1 = new Tween(document.getElementById('sq').style,'left',Tween.strongEaseOut,0,-473,0.7,'px');
            t1.start();
            break;
        case ids[2]:
            t1 = new Tween(document.getElementById('sq').style,'left',Tween.strongEaseOut,0,-946,0.7,'px');
            t1.start();
            break;
        case ids[3]:
            t1 = new Tween(document.getElementById('sq').style,'left',Tween.strongEaseOut,0,-1419,0.7,'px');
            t1.start();
            break;
        case ids[4]:
            t1 = new Tween(document.getElementById('sq').style,'left',Tween.strongEaseOut,-473,0,0.7,'px');
            t1.start();
            break;
        case ids[5]:
            t1 = new Tween(document.getElementById('sq').style,'left',Tween.strongEaseOut,-473,0,0.7,'px');
            t1.start();
            break;
        case ids[6]:
            t1 = new Tween(document.getElementById('sq').style,'left',Tween.strongEaseOut,-473,0,0.7,'px');
            t1.start();
            break;
        case ids[7]:
            t1 = new Tween(document.getElementById('sq').style,'left',Tween.strongEaseOut,-473,0,0.7,'px');
            t1.start();
            break;
        case ids[8]:
            t1 = new Tween(document.getElementById('sq').style,'left',Tween.strongEaseOut,-473,0,0.7,'px');
            t1.start();
            break;
    }
}
function doton(obj) 
{
    if (obj.className == 'diodeoff' || currentDoton != null) 
    { 
        offall(obj); 
        obj.className = 'diodeon';
         rotate(obj); 
    } 
    currentDoton = null; 
}   
function sendEmail(pTo,pFrom,pNote,pVideoId,pVideoTitle)
{
    //var objWarning = document.getElementById("spnWarning");
    var videoTitle =  pVideoTitle+" | "+"Morningstar Video";
    var xmlHttp = createXMLHttpRequest();	
    var targetUrl = "SendEmail.ashx?toUser="+pTo+"&title="+videoTitle+"&fromUser="+pFrom+"&note="+pNote+"&videoId="+pVideoId;
    xmlHttp.onreadystatechange = function()
    {
         if (xmlHttp.readyState != 4) {
	         return false;
         }
         if (xmlHttp.status != 200) {
	         return false;
         }
         var text = xmlHttp.responseText;
         if(text =="true") 
         {
            document.getElementById("toMessage").innerHTML = "";
            document.getElementById("fromMessage").innerHTML="";
            document.getElementById("noteMessage").innerHTML="";
            alert("Send E-mail successfully");
            return false;
         }
         else
         {
            document.getElementById("toMessage").innerHTML = "";
            document.getElementById("fromMessage").innerHTML="";
            document.getElementById("noteMessage").innerHTML="";
            alert("Send E-mail error");
            return false;
         }
    }
    xmlHttp.open("Get",targetUrl,true);
    xmlHttp.send(null);
}
        
/*ajax  begin*/
function createXMLHttpRequest()
{
    if (window.XMLHttpRequest) {
        return new XMLHttpRequest();
    } else {
        if (window.Ajax_XMLHttpRequestProgID) {
	        return new ActiveXObject(window.Ajax_XMLHttpRequestProgID);
        } else {
	        var progIDs = ["Msxml2.XMLHTTP.5.0", "Msxml2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP", "Microsoft.XMLHTTP"];
	        for (var i = 0; i < progIDs.length; ++i) {
		        var progID = progIDs[i];
		        try {
			        var x = new ActiveXObject(progID);
			        window.Ajax_XMLHttpRequestProgID = progID;
			        return x;
		        } catch (e) {
		        }
	        }
        }
    }
    return null;
}
function checkEmailAddress(pAddress)
{
    var reg = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/i;
    var r = pAddress.match(reg);
    return (r!=null) 
}

/*
below are from designer.
*/
function Delegate() { }
        Delegate.create = function(o, f) {
            var a = new Array();
            var l = arguments.length;
            for (var i = 2; i < l; i++) a[i - 2] = arguments[i];
            return function() {
                var aP = [].concat(arguments, a);
                f.apply(o, aP);
            }
        }

        Tween = function(obj, prop, func, begin, finish, duration, suffixe) {
            this.init(obj, prop, func, begin, finish, duration, suffixe)
        }
        var t = Tween.prototype;

        t.obj = new Object();
        t.prop = '';
        t.func = function(t, b, c, d) { return c * t / d + b; };
        t.begin = 0;
        t.change = 0;
        t.prevTime = 0;
        t.prevPos = 0;
        t.looping = false;
        t._duration = 0;
        t._time = 0;
        t._pos = 0;
        t._position = 0;
        t._startTime = 0;
        t._finish = 0;
        t.name = '';
        t.suffixe = '';
        t._listeners = new Array();
        t.setTime = function(t) {
            this.prevTime = this._time;
            if (t > this.getDuration()) {
                if (this.looping) {
                    this.rewind(t - this._duration);
                    this.update();
                    this.broadcastMessage('onMotionLooped', { target: this, type: 'onMotionLooped' });
                } else {
                    this._time = this._duration;
                    this.update();
                    this.stop();
                    this.broadcastMessage('onMotionFinished', { target: this, type: 'onMotionFinished' });
                }
            } else if (t < 0) {
                this.rewind();
                this.update();
            } else {
                this._time = t;
                this.update();
            }
        }
        t.getTime = function() {
            return this._time;
        }
        t.setDuration = function(d) {
            this._duration = (d == null || d <= 0) ? 100000 : d;
        }
        t.getDuration = function() {
            return this._duration;
        }
        t.setPosition = function(p) {
            this.prevPos = this._pos;
            var a = this.suffixe != '' ? this.suffixe : '';
            this.obj[this.prop] = Math.round(p) + a;
            this._pos = p;
            this.broadcastMessage('onMotionChanged', { target: this, type: 'onMotionChanged' });
        }
        t.getPosition = function(t) {
            if (t == undefined) t = this._time;
            return this.func(t, this.begin, this.change, this._duration);
        };
        t.setFinish = function(f) {
            this.change = f - this.begin;
        };
        t.geFinish = function() {
            return this.begin + this.change;
        };
        t.init = function(obj, prop, func, begin, finish, duration, suffixe) {
            if (!arguments.length) return;
            this._listeners = new Array();
            this.addListener(this);
            if (suffixe) this.suffixe = suffixe;
            this.obj = obj;
            this.prop = prop;
            this.begin = begin;
            this._pos = begin;
            this.setDuration(duration);
            if (func != null && func != '') {
                this.func = func;
            }
            this.setFinish(finish);
        }
        t.start = function() {
            this.rewind();
            this.startEnterFrame();
            this.broadcastMessage('onMotionStarted', { target: this, type: 'onMotionStarted' });
            //alert('in');
        }
        t.rewind = function(t) {
            this.stop();
            this._time = (t == undefined) ? 0 : t;
            this.fixTime();
            this.update();
        }
        t.fforward = function() {
            this._time = this._duration;
            this.fixTime();
            this.update();
        }
        t.update = function() {
            this.setPosition(this.getPosition(this._time));
        }
        t.startEnterFrame = function() {
            this.stopEnterFrame();
            this.isPlaying = true;
            this.onEnterFrame();
        }
        t.onEnterFrame = function() {
            if (this.isPlaying) {
                this.nextFrame();
                setTimeout(Delegate.create(this, this.onEnterFrame), 0);
            }
        }
        t.nextFrame = function() {
            this.setTime((this.getTimer() - this._startTime) / 1000);
        }
        t.stop = function() {
            this.stopEnterFrame();
            this.broadcastMessage('onMotionStopped', { target: this, type: 'onMotionStopped' });
        }
        t.stopEnterFrame = function() {
            this.isPlaying = false;
        }

        t.continueTo = function(finish, duration) {
            this.begin = this._pos;
            this.setFinish(finish);
            if (this._duration != undefined)
                this.setDuration(duration);
            this.start();
        }
        t.resume = function() {
            this.fixTime();
            this.startEnterFrame();
            this.broadcastMessage('onMotionResumed', { target: this, type: 'onMotionResumed' });
        }
        t.yoyo = function() {
            this.continueTo(this.begin, this._time);
        }

        t.addListener = function(o) {
            this.removeListener(o);
            return this._listeners.push(o);
        }
        t.removeListener = function(o) {
            var a = this._listeners;
            var i = a.length;
            while (i--) {
                if (a[i] == o) {
                    a.splice(i, 1);
                    return true;
                }
            }
            return false;
        }
        t.broadcastMessage = function() {
            var arr = new Array();
            for (var i = 0; i < arguments.length; i++) {
                arr.push(arguments[i])
            }
            var e = arr.shift();
            var a = this._listeners;
            var l = a.length;
            for (var i = 0; i < l; i++) {
                if (a[i][e])
                    a[i][e].apply(a[i], arr);
            }
        }
        t.fixTime = function() {
            this._startTime = this.getTimer() - this._time * 1000;
        }
        t.getTimer = function() {
            return new Date().getTime() - this._time;
        }
        Tween.strongEaseOut = function(t, b, c, d) {
            return c * ((t = t / d - 1) * t * t * t * t + 1) + b;
        }

