// JavaScript Document

// If set to 'true', all the popups will open in the same window.
// If set to 'false', the window name parameter will be considered 
var SAME_POPUP              = true;
var PROFILE_WINDOW_HEIGHT   = 500;
var PROFILE_WINDOW_WIDTH    = 520;
var LOGIN_WINDOW_HEIGHT     = 140;
var LOGIN_WINDOW_WIDTH      = 400;
var GALLERY_HEIGHT          = 600;
var GALLERY_WIDTH           = 625;

var currentWidget = null;
var widgetTimeout = null;

(function($) {
    $(document).ready( function () {
    
        new Menu( '#udm' );
        
        // builds the drop-down functionality for 
        // case studies and porfolio
        $('.openButton').each( function () {
            var li    = this;
            var $this = $(this);
            var $list = $this.find('+ ul');
            
            if ( $list.length ) {
                var timeout;
                var over    = function ( event ) { li.toggleList( true, event ); }
                var out     = function ( event ) { li.toggleList( false, event ); }
                
                var bodyh   = $(document.body).height();
                this.toggleList = function ( show, event ) {
                    if ( show ) {
                        if ( timeout ) clearTimeout( timeout );
                        $list.css( 'display', 'block' );
                    } else {
                        timeout = setTimeout( function ( ) { $list.css('display', 'none') }, 200 );
                    }
                }
                
                $list.hover( over, out );
                $this.hover( over, out );
                
            }
        })
        
        // tags all anchors linking to the slideshow with a special
        // popup style
        if ( typeof hs != 'undefined' ) {
            $('a[href*=our_work/slideshow]').click( function ( ) {
                var params = { objectType: 'iframe', contentId: 'hs-box', width: 660, height: 550 };
                if ( this.href.match( /category/ ) ) {
                    params.wrapperClassName = 'hs-portfolio';
                }
                return hs.htmlExpand( this, params );
            } );
        }
    } );

    /**
     * Menu
     */
    function Menu(list) {
        var _this = this;
        
        $(list).children('li').each(function (i, a) {
            // builds the sub-menusts
            var $this = $(this);
            
            var $children = $this.find('> ul');
            if ( $children.length > 0 ) {
                $this.addClass('with-child');
            }
            $children.each(function () {
                new Menu(this);
            });
            
            // adds the menu    
            this._menu = _this;
            
            // over and out even
            $this.hover(_this.onItemOver, _this.onItemOut);
            
            // adds the background switch
            var bg = $this.css('background-image');
            var item = this;
            this.showBackground = _this.getToggleFn(this);
        });
    }
    Menu.prototype = {
        /**
         * The current item
         */
        current: null,
        
        /**
         * Returns the background toggle function 
         * for a given menu item.
         */
        getToggleFn: function (menuitem) {
            var $img = $(menuitem).find('> a img');
            if ($img.length) {
                var off = $img.attr('src');
                var on  = $img.attr('src').replace('_off.', '_on.');
                return function (hover) {
                    $img.attr('src', hover ? on : off);
                }
            } else {
                return function (hover) { return off; }
            }
        },
        
        /**
         * Hides the item
         */
        hide: function (menuitem) {
            if (typeof menuitem._timeout != 'undefined') {
                clearTimeout(menuitem._timeout);
            }
            $(menuitem).children('ul').children('li').each(function () {
                if (this._menu.current == this) {
                    this._menu.hide(this);
                }
            });
            $(menuitem).children('ul').each(function () {
                 var $this = $(this);
                 // hide the children first for IE
                 $this.children('li').each(function () {
                     if (this._menu.current == this) {
                        this._menu.hide(this);
                     }
                 });
                 $this.css('display', 'none');
            });
            menuitem.showBackground(false);
        },
        
        /**
         * Shows the item
         */
        show: function (menuitem) {
            if (typeof menuitem._timeout != 'undefined') {
                clearTimeout(menuitem._timeout);
            }
            $(menuitem).children('ul').css('display', 'block');
            menuitem.showBackground(true);
        },
        
        /**
         * Function called when the menu item is hovered over.
         */
        onItemOver: function (event) {
            this._menu.show(this);
            if (this._menu.current != null) {
                if (this._menu.current == this) {
                    // over the same item
                    return;
                }
                this._menu.hide(this._menu.current);
            }
            this._menu.current = this;
            return true;
        },
        
        /**
         * Function called when the menu is left
         */
        onItemOut: function (event) {
            var _this = this;
            this._timeout = setTimeout(function () {
                _this._menu.hide(_this);
                if (_this._menu.current == _this) {
                    _this._menu.current = null;
                } 
            }, 400);
            return true;
        }
    };
})(jQuery);

//function loadFlashObject(path, loadin, options){
//    
//}
function addLoadListener(fn){
    if (typeof window.addEventListener != 'undefined'){
        window.addEventListener('load', fn, false);
    }
    else if (typeof document.addEventListener != 'undefined'){
        document.addEvenListener('load', fn, false);
    }
    else if (typeof window.attachEvent != 'undefined'){
        window.attachEvent('onload', fn);
    }
    else{
        var oldFn = window.onload;
        if(typeof window.onload != 'function'){
            window.onload = fn;
        }
        else{
            window.onload = function()
            {
                oldFn();
                fn;
            };
        }
    }
}
function toggleWidgetVisibility(widgetID){
    if(widgetTimeout != null)
        clearTimeout(widgetTimeout);
    
    var elem = document.getElementById(widgetID);
    
    if(currentWidget != null && widgetID != currentWidget){
        hideWidget(currentWidget);
    }
        
    if(elem){
        elem.style.display = 'block';
        
        widgetTimeout = setTimeout("hideWidget('" + widgetID + "')",3000);
        currentWidget = widgetID;
    }
        
}
function hideWidget(widgetID){
    var elem = document.getElementById(widgetID);
    if(elem){
        elem.style.display = 'none';
    }
}
function displayPreview(widgetID){
    var elem = document.getElementById(widgetID);
    if(elem)
    {
        elem.style.display = 'block';
        clearTimeout(widgetTimeout);
    }
}
function hidePreview(widgetID){
    var elem = document.getElementById(widgetID);
    if(elem){
        elem.style.display = 'none';
        widgetTimeout = setTimeout("hideWidget('" + currentWidget + "')",100);
    }
}
function ClearForm (form){
    document.getElementById(form).reset();
    return false;
}
function MM_swapImgRestore() { //v3.0
	var i, x, a = document.MM_sr;
	for(i = 0; a && i < a.length && (x = a[i]) && x.oSrc; i++)
		x.src = x.oSrc;
}
function galleryPopup(URL) {
    if(SAME_POPUP)
        window_name = '';
    window.open(URL, window_name, 'toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no,width=' + GALLERY_WIDTH + ',height=' + GALLERY_HEIGHT);
}
function galleryClose()
{
    window.close();
}

function parentExists()
{
    return (parent.location == window.location)? false : true;
}


function MM_preloadImages() { //v3.0
	var d = document;
	if(d.images){
		if(!d.MM_p)
			d.MM_p = new Array();
	var i, j = d.MM_p.length, a = MM_preloadImages.arguments;
	for(i=0; i<a.length; i++)
		if (a[i].indexOf("#") != 0){
			d.MM_p[j] = new Image;
			d.MM_p[j++].src=a[i];
		}
	}
}

function MM_findObj(n, d) { //v4.01
	var p,i,x;
	if(!d)
		d=document;
	if((p = n.indexOf("?")) > 0 && parent.frames.length) {
		d=parent.frames[n.substring(p + 1)].document;
		n = n.substring(0, p);
	}
	if(!(x = d[n]) && d.all)
		x = d.all[n];
	for (i = 0; !x && i < d.forms.length; i++)
		x=d.forms[i][n];
	for(i = 0; !x && d.layers && i < d.layers.length; i++)
		x = MM_findObj(n,d.layers[i].document);
	if(!x && d.getElementById)
		x = d.getElementById(n);
	return x;
}

function MM_swapImage() { //v3.0
	var i, j = 0, x, a = MM_swapImage.arguments;
	document.MM_sr = new Array;
	for(i = 0; i < (a.length - 2); i += 3)
		if ((x = MM_findObj(a[i])) != null) {
			document.MM_sr[j++] = x;
			if(!x.oSrc)
				x.oSrc = x.src;
			x.src=a[i+2];
		}
}

function ProjectPopup(URL, window_name) {
    if(SAME_POPUP || !window_name)
        window_name = 'project';
    window.open(URL, window_name, 'toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no,width=520,height=' + PROFILE_WINDOW_HEIGHT);
}

function LoginPopup(URL) {
    window.open(URL, 'project', 'toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no,width=400,height=' + LOGIN_WINDOW_HEIGHT);
   
}

function Popup(URL) {
    window.open(URL, 'project');
}
