var iMac = {
    imgElement: 0,
    lastObj: 0,
    styles: new Array(
    	{
    		'background' :'#3E4245 url(/public/images/bg-zero.jpg) repeat fixed'
    	},
    	{
    		'background' :'#262a2d url(/public/images/bg-one.jpg) repeat-x fixed'
    	},
    	{
    		'background' :'#6D90A3 url(/public/images/bg-two.jpg) repeat fixed'
    	},
    	{
    		'background' :'#B76736 url(/public/images/bg-three.jpg) repeat fixed'
    	}
    ),

    start: function()
    {
    	if (Cookie.read('bg')) {
    		iMac.setCurrentStyle(document.getElement('li#' + Cookie.read('bg') + ' a'));
    		document.getElement('body').setStyles(iMac.styles[Cookie.read('bg')]);
    	}
    	if (Cookie.read('width')) {
    		iMac.setPageWidth(Cookie.read('width'));
    	}
	    iMac.registerEvents();
    },

    setCurrentStyle: function(p)
    {
    	$$('#styleselector li').removeClass('current');
    	if (p && p.getParent()) {
        	p.getParent().addClass('current');
    	}
    },

    registerEvents : function()
    {
        if ($('topheader-fscr-full')) {
            $('topheader-fscr-full').addEvent('click',function(e){
                iMac.setPageWidth('full');
        		return false;
            });
        }
        $('topheader-fscr-fixed').addEvent('click',function(e){
            iMac.setPageWidth('fixed');
    		return false;
        });
    	$$('#styleselector a').addEvent('click',function(e) {
    		iMac.setCurrentStyle(this);
    		document.getElement('body').setStyles(iMac.styles[this.getParent().get('id')]);
    		Cookie.dispose('bg');
    		Cookie.write('bg',this.getParent().get('id'),{path:'/', duration: 7});
    		return false;
    	});
        $('topheader-aapl').addEvent('mouseover',function(e) {
            iMac.showPopup(e, this, '', 212, 102);
        });
        $('topheader-aapl').addEvent('mouseout',function(e) {
            iMac.hidePopup();
        });
    },

    setPageWidth: function(type)
    {
        var screen = iMac.getScreen();
        if ('full' == type) {
            var wrapWidth     = 'auto';
            var wrapMargin    = '0 30px';
            $('topheader-fscr-full').set('class', 'topheader-fscr-current');
            $('topheader-fscr-fixed').set('class', 'topheader-fscr-mode');
            googleSearchFrameWidth = screen.width - 150;
        } else {
            var wrapWidth     = '961px';
            var wrapMargin    = '0 auto';
            $('topheader-fscr-fixed').set('class', 'topheader-fscr-current');
            $('topheader-fscr-full').set('class', 'topheader-fscr-mode');
            googleSearchFrameWidth = 890;
        }
        $('topheader').setStyle('width', wrapWidth);
        $('topheader').setStyle('margin', wrapMargin);
        $('parent').setStyle('width', wrapWidth);
        $('parent').setStyle('margin', wrapMargin);
        iMac.setSearchFrameWidth(googleSearchFrameWidth);
    	Cookie.write('width', type, {path:'/', duration: 7});
    },

    setSearchFrameWidth: function(width)
    {
        var iframe = $$('iframe');
        $each(iframe, function(one, index) {
            if ('googleSearchFrame' == one.name) {
                one.set('width', width);
            }
        })
    },

    getScreen: function()
    {
        var myHeight = 0;
        var myWidth  = 0;
        if( typeof( window.innerWidth ) == 'number' ) {
            //Non-IE
            myHeight = window.innerHeight;
            myWidth  = window.innerWidth;
        } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
            //IE 6+ in 'standards compliant mode'
            myHeight = document.documentElement.clientHeight;
            myWidth  = document.documentElement.clientWidth;
        } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
            //IE 4 compatible
            myHeight = document.body.clientHeight;
            myWidth  = document.body.clientWidth;
        }
        return {width : myWidth, height : myHeight};
    },

    cumulativeOffset: function(element)
    {
       var valueT = 0, valueL = 0;
       do {
         valueT += element.offsetTop  || 0;
         valueL += element.offsetLeft || 0;
         element = element.offsetParent;
       } while (element);
       return [valueL, valueT];
    },

    showPopup: function(e, obj, text, width, height)
    {
        if (!iMac.element) {
            iMac.element = document.createElement("div");
            iMac.element.id = 'popup';
            document.body.appendChild(iMac.element);
        }

        offs = iMac.cumulativeOffset(obj);
        x    = offs[0] - 50;
        y    = offs[1] + 22;

        iMac.element.innerHTML = '<img src="http://www.google.co.uk/finance/chart?q=NASDAQ:AAPL" id="topheader-aapl-chart" alt="" />';

        //IE fix
        iMac.element.style.cssText = 'z-index:100; border: 1px solid #000; position: absolute; display: block; top:'+y+'px;left:'+x+'px;width:'+width+'px;'+((height!=0)?'height:'+height+'px;':'');
        iMac.element.style.visibility = "visible";

        obj.blur();
        iMac.last_obj  = obj;
        e.cancelBubble = true;
        return true;
    },

    hidePopup: function()
    {
        if (iMac.last_obj != 0) {
            iMac.last_obj.blur();
        }
        iMac.element.innerHTML        = '';
        iMac.element.style.visibility = "hidden";
        iMac.last_obj                 = 0;
        return true;
    },

    doTextField: function(element, value, clear)
    {
    	var cssClass = element.className;
    	if (clear) {
    		if (element.value == value) {
    			element.value = '';
    		}
    	} else {
    		if (element.value == '') {
    			element.value = value;
    		}
    	}
    	if (element.value == value) {
    		element.className = cssClass.replace("active","inActive");
    	} else {
    		element.className = cssClass.replace("inActive","active");
    	}
    },

    doStyleTag: function()
    {
        var style = document.createElement('style');
        var def   = '';
        if (Cookie.read('width') == 'full') {
            def = '#parent, #topheader {width: auto; margin:0 30px;}';
    	}
        if (Cookie.read('bg')) {
            def = def + 'body {background:' + iMac.styles[Cookie.read('bg')].background + '}';
        }
        style.setAttribute("type", "text/css");
        if (style.styleSheet) {   // IE
            style.styleSheet.cssText = def;
        } else {
            style.appendChild(document.createTextNode(def));
        }
        document.getElementsByTagName('head')[0].appendChild(style);
    }
}
iMac.doStyleTag();
window.addEvent('domready',function() {
	iMac.start();
});