﻿//open URL from drop down list
function OpenDropDownUrl(element, newWindow)
{
    //check selected element value is not empty
    if (element[element.selectedIndex].value != "")
    {
        //check if link should open in new window
        if (newWindow == true)
            window.open(element[element.selectedIndex].value, "ProfileGroup");
        else
            window.location = element[element.selectedIndex].value;
    }
}

/* BEGIN: opacity element fading */
function SetOpacity(elem, opacityAsInt)
{
    var opacityAsDecimal = opacityAsInt;

    if (opacityAsInt > 100)
        opacityAsInt = opacityAsDecimal = 100; 
    else if (opacityAsInt < 0)
        opacityAsInt = opacityAsDecimal = 0; 

    //check browser
    if (Sys.Browser.agent == Sys.Browser.InternetExplorer)
    {        
        if (opacityAsInt < 1)
            opacityAsInt = 1; // IE7 bug, text smoothing cuts out if 0
            
        elem.style.filter  = "alpha(opacity=" + opacityAsInt + ")";
    }
    else
    {       
        opacityAsDecimal /= 100;
        
        if (elem.style.MozOpacity != null) elem.style.MozOpacity = (opacityAsDecimal);
        if (elem.style.KhtmlOpacity != null) elem.style.KhtmlOpacity = (opacityAsDecimal);    
        if (elem.style.opacity != null) elem.style.opacity = (opacityAsDecimal);
    }
}

/* 
elemId - Element ID
fromOpacity - Starting opacity value
toOpacity - End opacity value
time - fade time in milliseconds
fps - frames per second, between 20-30 is usually enough

Example: FadeOpacity("SlidesPH", 1, 99, 500, 30);
*/
function FadeOpacity(elemId, fromOpacity, toOpacity, time, fps)
{
    var steps = Math.ceil(fps * (time / 1000));
    var delta = (toOpacity - fromOpacity) / steps;

    FadeOpacityStep(elemId, 0, steps, fromOpacity, 
                    delta, (time / steps));
}

function FadeOpacityStep(elemId, stepNum, steps, fromOpacity, 
                         delta, timePerStep)
{
    SetOpacity($get(elemId), 
               Math.round(parseInt(fromOpacity) + (delta * stepNum)));

    if (stepNum < steps)
        setTimeout("FadeOpacityStep('" + elemId + "', " + (stepNum+1) 
                 + ", " + steps + ", " + fromOpacity + ", "
                 + delta + ", " + timePerStep + ");", 
                   timePerStep);
}
/* END: opacity element fading */

// this function determines whether the event is the equivalent of the microsoft 
// mouseleave or mouseenter events. 
// http://www.dynamic-tools.net/toolbox/isMouseLeaveOrEnter/
function isMouseLeaveOrEnter(e, handler) 
{ 
    if (e.type != 'mouseout' && e.type != 'mouseover') 
        return false; 
        
    var reltg = e.relatedTarget ? e.relatedTarget : e.type == 'mouseout' ? e.toElement : e.fromElement; 
    
    while (reltg && reltg != handler) 
        reltg = reltg.parentNode; 
    
    return (reltg != handler); 
}

/* START rotate content */
RotateContent = function(menu, delay)
{
    this.menu = menu;
    this.timecount = delay <= 0 ? 1000 : delay;
    this.elementToShow = -1;
    this.lastElementIndex = this.menu.menuList.length - 1;
    this.timerId = null;
    this.timerOn = false;
    
    //start rotation
    this.Play = function()
    {
        this.timerOn = true;

        //create temporary variable as method cannot instantiate toolbar object in setTimeout
        var me = this;       
        
        //rotate first conent item
        me.Rotate(); 
        
        //set interval for rotation
        this.timerId = setInterval(function() {me.Rotate()}, this.timecount);
    }
    
    //rotate
    this.Rotate = function()
    {
        if (this.timerOn)
        {
            //select element to show
            this.elementToShow++;
            
            //check if element has not gone outside collection
            if (this.elementToShow > this.lastElementIndex)
                this.elementToShow = 0;
                
            //show content
            this.menu.showLayer(this.menu.menuList[this.elementToShow]);
        }
    }
    
    //pause rotation
    this.Pause = function()
    {
        if (this.timerOn)
        {
            clearInterval(this.timerId);
            this.timerId = null;
            this.timerOn = false;
        }
    }
    
    return this;
}
/* END rotate content */

/* START menu */
Menu = function(menus, delay, fade)
{
    //Define global variables
    this.timerID = null;
    this.timerOn = false;
    this.timecount = delay <= 0 ? 1000 : delay;
    this.menuList = menus.split(",");  
    this.fade = fade == true ? true : false;

    //Turns a layer on
    this.showLayer = function(layerName)
    {
        //hide all
        this.hideAll();
        
        var element = $get(layerName);
        
        //check if menu should be faded and not IE (bug where drop down does not appear after first view)
	    if (this.fade && Sys.Browser.agent != Sys.Browser.InternetExplorer)
	        FadeOpacity(element.id, 1, 99, 300, 30);

        element.style.display = "block";
        element.style.visibility = "visible";
	    
	    //clear timer
	    this.stopTime();
    };
    
    //Turns a layer off
    this.hideLayer = function(layerName)
    {
        var element = $get(layerName);
	        
	    element.style.display = "none";    
	    element.style.visibility = "hidden";
    };  
    
    //turns all layers off
    this.hideAll = function()
    {
        //loop through menu list
        for (var a = 0;a < this.menuList.length;a++)
        {
            this.hideLayer(this.menuList[a]);
        }
    }; 

    //start timer
    this.startTime = function() 
    {
        //create temporary variable as method cannot instantiate toolbar object in setTimeout
        var me = this;
        
        if (this.timerOn == false) 
        {
		    this.timerID=setTimeout(function() {me.hideAll()}, this.timecount);
		    this.timerOn = true;
        }
    };

    //stop timer
    this.stopTime = function() 
    {
        if (this.timerOn) 
        {
		    clearTimeout(this.timerID);
		    this.timerID = null;
		    this.timerOn = false;
        }
    }; 
    
    return this;
};
/* END drop down menu */