    //    {left: 50, top: 140, 
    //        announce: {left: 700, top: 480}},


window.onload = function() {
    if (board.getUrl() != '') board.gPage = board.getUrl();
    board.page(board.gPage);
    board.announce('facebook', 'http://www.facebook.com/theGoOzer');
    //board.announce('GoOzerLoveGF', 'javascript:board.page("gfa11");');
};

var board = {
    
    _zindex: 100,
    _blackout: null,
    _announce: null,
    announceTimeout: 2000,
    goOzes: [],
    pos: {
        'goOzer1': {left: 173, top: 289	, 
            ilike: {left: 30, top: 590},
            twitter: {left: 800, top: 100},
            logo: {left: 20, top: 20},
            board: {left: 650},
            announce: {left: 570, top: 203}},
        'goOzer2': {left: 116, top: 140, 
            ilike: {left: 860, top: 140},
            twitter: {left: 800, top: 100},
            logo: {left: 20, top: 20},
            board: {left: 529},
            announce: {left: 40, top: 350}},
        'goOzer3': {left: 550, top: 190, 
            ilike: {left: 30, top: 490},
            twitter: {left: 800, top: 100},
            logo: {left: 880, top: 60},
            board: {left: 345},
            announce: {left: 100, top: 350}},
        'goOzer4': {left: 690, top: 360, 
            ilike: {left: 30, top: 135},
            twitter: {left: 800, top: 100},
            logo: {left: 323, top: 278},
            board: {left: 30},
			announce: {left: 580, top: 15}},
        'goOzer5': {left: 300, top: 150,
            ilike: {left: 747, top: 589},
            twitter: {left: 800, top: 100},
            logo: {left: 165, top: 533},
            board: {left: 735},
            announce: {left: 30, top: 0}},
    },
    ehUhPos: [[600, 300], [700, 200], [500, 400]],
    
    gVenue: 'goOzer1',
    gPage: 'gfa11',
    gWeather: 'clouds',
    
    ehUhSnd: 3,
    ehUhTxt: 1,
    routeSnd: 9,
    
    init: function() {
        $('#logo').css('position', 'absolute').css('top', '20px').css('left', this.getX($('#logo').width(), 20) + 'px').draggable({ containment: 'body', distance: 3 }).mousedown(function() { board.inFront($(this)); }).click(function() {board.playSound(11);});
        $('#ilike').css('position', 'absolute').css('top', this.goOzeYkey('ilike') + 'px').css('left', this.getX($('#ilike').width(), this.goOzeXkey('ilike')) + 'px').draggable({ containment: 'body', distance: 3 }).mousedown(function() { board.inFront($(this)); }).fadeIn();
        $('#goOzerbOard').css('left', this.getX($('#goOzerbOard').width(), this.goOzeXkey('board')) + 'px').css('display', 'block').draggable({ axis: 'x', containment: 'body', distance: 3 }).mousedown(function() { board.inFront($(this)); });
        board.inFront($('#goOzerbOard'));
        window.setTimeout(function() {board.playSound(12);}, 3000);

        $('.vid:first').css('display', 'block');
        $('.vidmenu a:first').addClass('active');
    },

    getUrl: function() {
        hrefSplit = window.location.href.split('#');
        if (hrefSplit.length > 1) {
            switch (hrefSplit[1].toLowerCase()) {
                case 'home': return 'hOme';
                case 'gfa11': return 'gfa11';
                case 'tour': return 'tOur';
                case 'about': return 'abOut';
                case 'video': return 'vIdeO';
                case 'contact': return 'cOntact';
            }
            return '';
        }
        return '';
    },
    
    setUrl: function(page) {
        if (window.location.href.split('temp').length < 2) window.location.href = 'http://www.thegoozer.com/#' + page;
    },
    
    playSound: function(frame) {
        var m = (document['goOzsOunds'] ? document['goOzsOunds'] : window['goOzsOunds']);
        if (m) { if (m.playSound) { m.playSound(0); m.playSound(frame); } }
    },
    
    playEhUh: function() {
        this.playSound(this.ehUhSnd);
        this.ehUhSnd++;
        if (this.ehUhSnd > 8) this.ehUhSnd = 3;
    },
    
    route: function() { this.playSound(this.routeSnd); this.routeSnd = (this.routeSnd == 9 ? 10 : 9); },

    weather: function(val) {
        if (0 == 1) {
            $('.weatherButton').removeClass('active');
            $('#' + val).addClass('active');
            this.gWeather = val;
        } else {
            this.playEhUh();
            this.goOze(this.ehUhPos[this.ehUhTxt - 1][0], this.ehUhPos[this.ehUhTxt - 1][1], 200, (this.ehUhTxt == 1 ? 'The weather is unGoOzable, but very GoOzy offline!<br />Stay GoOzed until next episode...' : (this.ehUhTxt == 2 ? 'Dude, wait till next episode... ' : '"Dude..."')), 3500);
            if (this.ehUhTxt < 3) this.ehUhTxt += 1;
        }
    },
    
    venue: function(val) {
        if (this.gVenue != val) {
            $('.venueButton').removeClass('active');
            $('#' + val).addClass('active');
            $('#goOzer').attr('class', val);
            $('#logo').css('display', (val == 'goOzer1' ? 'none' : 'block'));
            this.gVenue = val;
            this.reGoOze();
        }
    },
    
    page: function(val) {
        this.unGoOzeAll(function() {
            this.gPage = val;
            board.setUrl(val);
            var thisWidth = 400;
            $('#gfa11board').css('display', 'none');
            switch(val) {
                case 'hOme': this.venue('goOzer1'); break;
                case 'gfa11': this.venue('goOzer3'); $('#gfa11board').css('display', 'block'); break;
                case 'abOut': this.venue('goOzer2'); break;
                case 'vIdeO': thisWidth = 600; this.venue('goOzer5'); break;
                case 'cOntact': this.venue('goOzer4'); break;
            };
            if (val != 'gfa11') this.goOze(this.goOzeX(), this.goOzeY(), thisWidth, $('#' + val + 'GoOze').html());
            if (val == 'theXit') board.theXit(); else {
                $('.menuButton').removeClass('active');
                $('#menu' + val).addClass('active');
            }
        });
    },
    
    inFront: function($element) {
        board._zindex += 2;
        $element.css('z-index', board._zindex);
    },
    
    getX: function(width, x) {
        var xx = x + (($(window).width() - 1200) / 2);
        if (xx < 10) xx = 10;
        if (xx + width > $(window).width()) xx = $(window).width() - width - 10;
        return xx;
    },
    
    goOzeX: function() {
        return this.pos[this.gVenue].left;
    },
    
    goOzeY: function() {
        return this.pos[this.gVenue].top;
    },

    goOzeXkey: function(key) {
        return this.pos[this.gVenue][key].left;
    },
    
    goOzeYkey: function(key) {
        return this.pos[this.gVenue][key].top;
    },

    goOze: function(x,y,w,t,timeout) {
        var html = $('<div class="tXt"><div class="tXtTopCorner"></div><div class="tXtTop"></div><div class="tXtContent"></div><div class="tXtBottomCorner"></div><div class="tXtBottom"></div></div>');
        html.css('display', 'none').css('position', 'absolute').css('width', w + 'px').css('left', this.getX(html.width(), x) + 'px').css('top', y + 'px').mousedown(function() { board.inFront($(this)); });
        $('.tXtContent', html).html(t);
        var isDraggable = true;
        if (BrowserDetect.OS == 'Mac') {
            if ($('embed, object', html).length > 0) isDraggable = false;
        }
        if (isDraggable) html.draggable({ containment: 'body', distance: 3 });
        cms.init(html);
        html.appendTo($('#goOzer')).fadeIn(500);
        board.inFront(html);
        var self = this;
        if (timeout) window.setTimeout(function() {self.unGoOze(html);}, timeout + 500); else this.goOzes[this.goOzes.length] = html;
    },
    
    reGoOze: function() {
        var self = this;
        $('#ilike').animate({left: self.getX($('#ilike').width(), self.goOzeXkey('ilike')), top: self.goOzeYkey('ilike')}, 500, 'swing');
        $('#logo').animate({left: self.getX($('#logo').width(), self.goOzeXkey('logo')), top: self.goOzeYkey('logo')}, 500, 'swing');
        $('#goOzerbOard').animate({left: self.getX($('#goOzerbOard').width(), self.goOzeXkey('board')), top: 0}, 2000, 'swing');
        if (this.goOzes.length > 0) {
            window.setTimeout(function() {
                self.goOzes[0].animate({left: self.getX(self.goOzes[0].width(), self.goOzeX()), top: self.goOzeY()}, 500, 'swing');
            }, 300);
        }
        if (self._announce) {
            window.setTimeout(function() {
                self._announce.animate({left: self.getX(self._announce.width(), self.goOzeXkey('announce')), top: self.goOzeYkey('announce')}, 500, 'swing');
            }, 400);
        }
    },
    
    unGoOze: function(goOze) {
        goOze.fadeOut(1500, function() {goOze.remove();});
    },

    unGoOzeAll: function(finished) {
        if (this.goOzes.length > 0) {
            var self = this;
            $.each(this.goOzes, function(index, data) {
                self.unGoOze(data);
            });
            this.goOzes = [];
            window.setTimeout(function() { finished.call(self); }, 500);
        } else finished.call(this);
    },
    
    announce: function(key, link) {
        var html = $('<div id="announce" style="display: none; position: absolute;"><a><img /></a></div>');
        board._announce = html;
        board.inFront(html);
        $('img', html).attr('src', 'images/announce/' + key + '.png');
        $('a', html).attr('href', link).attr('target', '_blank');
        html.mousedown(function() { board.inFront($(this)); }).draggable({ containment: 'body', distance: 3 }).appendTo($('#goOzer'));
        window.setTimeout(function() {
            html.css('left', board.getX(html.width(), board.goOzeXkey('announce')) + 'px').css('top', board.goOzeYkey('announce') + 'px').show('scale',{},200);
            board.playSound(13);
        }, board.announceTimeout);
    },
    
    updateAnnounce: function(version) {
        if (version == (this._announceVersion + 1)) {
            this._announceVersion += 1;
            if (this._announceVersion < this.announces.length) {
                $('img', this._announce).attr('src', 'images/announce/' + this.announces[this._announceVersion].key + '.png');
                $('a', this._announce).attr('href', this.announces[this._announceVersion].link);
            }
        }
    },
    
    removeAnnounce: function() {
        board._announce.remove();
    },

    blackout: function() {
        if (!this._blackout) {
            this._blackout = $('<div>');
            this._blackout.css('position', 'absolute').css('left', '0').css('top', '0').css('right', '0').css('bottom', '0').css('background', '#000').css('opacity', 0).click(function() {
                board._blackout.fadeOut(300);
            });
            $('#goOzer').append(this._blackout);
        }
        this._blackout.css('opacity', 0).css('display', 'block').css('z-index', this._zindex - 1);
        this._blackout.fadeTo('slow', 0.75);
    },
    
    vid: function(num) {
        $('.vid').css('display', 'none');
        $('.vid' + num).css('display', 'block');
        $('.vidbtn').removeClass('active');
        $('.vidbtn' + num).addClass('active');
    },
    
    theXitTO: 2000,
    theXit: function() {
        this.unGoOzeAll();
        $('#goOzer').fadeOut(500);
        window.setTimeout(function() {
            $("#unGoOzer").dialog({
			    resizable: true,
			    height: 240,
			    modal: false,
			    buttons: {
				    'Yes': function() {
					    $(this).dialog('close').dialog("destroy");
					    $('#unGoOzer').attr('title', '...');
					    $('#unGoOzerAgain').html('&nbsp;again');
					    board.theXitTO = 500;
					    $('#goOzer').fadeIn(500);
				    },
				    'No': function() {
					    $(this).dialog('close').dialog("destroy");
					    window.setTimeout(function() {
                            $('#goOzer').css('background', 'blue').css('fontFamily', 'Courier').css('color', '#fff').html(
					            'A problem has been detected and GoOzer has been shut down to p<br />to your computer.<br />&nbsp;<br />The problem seems to be caused by the following file: MACBOOK.<br />&nbsp;<br />PAGE_FAULT_IN_USER_AREA<br />&nbsp;<br />** STOP: 0x000000050 (0xFD3094c2,0x00000001,0xFBFE7617,0x00000'
					        ).css('display', 'block');
                            $('#soundflash').html('');
					        alert('System error #876234a898. No valid GoOzer interface.');
					        window.setTimeout(function() {
					            alert('Webmaster says sorry.');
                                $("#reGoOzer").dialog({
                                    height: 140,
                                    modal: false,
                                    buttons: {
                                        'Yes': function() {
                                            $('#goOzer').css('background', '#000').html('');
                                            window.setTimeout(function() {window.location.reload();}, 1000);
                                        }
                                    }
                                });
					        }, 1000);
					    }, 5000);
				    }
			    }
		    });
        }, board.theXitTO);
    }
    
}

var cms = {
    init: function(element) {
        $('.ContactFormSend', element).each(function() {
            $(this).click(function() {
                var frm = $(this).closest('.ContactForm');
                var f = frm.find('.ContactFormMessage'); if (!f.val()) { f.focus(); return false; }
                f = frm.find('.ContactFormName'); if (!f.val()) { f.focus(); return false; }
                f = frm.find('.ContactFormEmail'); if (!f.val()) { f.focus(); return false; }
                if (!/^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@(([0-9a-zA-Z])+([-\w]*[0-9a-zA-Z])*\.)+[a-zA-Z]{2,9})$/.test(f.val())) { f.val(f.val().replace(' - unGoOzable!', '') + ' - unGoOzable!'); f.focus(); return false; }
                $.post('send.aspx', frm.find('input,textarea').serializeClassnames(), function(data) {
                    frm.closest('.ContactForm').html('iLike! Your message has been routed (whatever that means) and will be delivered to <strong>the GoOzer</strong>...'); 
                });
                return false;
            });
        });
        $('.TheaterFormSend', element).each(function() {
            $(this).click(function() {
                var frm = $(this).closest('.TheaterForm');
                var f = frm.find('.TheaterFormName'); if (!f.val()) { f.focus(); return false; }
                f = frm.find('.TheaterFormEmail'); if (!f.val()) { f.focus(); return false; }
                if (!/^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@(([0-9a-zA-Z])+([-\w]*[0-9a-zA-Z])*\.)+[a-zA-Z]{2,9})$/.test(f.val())) { f.val(f.val().replace(' - unGoOzable!', '') + ' - unGoOzable!'); f.focus(); return false; }
                f = frm.find('.TheaterFormPhone'); if (!f.val()) { f.focus(); return false; }
                f = frm.find('.TheaterFormHowMany'); if (!f.val()) { f.focus(); return false; }
                $.post('reservation.aspx', frm.find('input,select,textarea').serializeClassnames(), function(data) {
                    frm.closest('.TheaterForm').html('iLike! Your reservation has been made. Whenever you feel like changing your reservation, use the contact form on this website. See you next Sunday morning!<br /><br />The GoOzer'); 
                });
                return false;
            });
        });
        $('a.zoom', element).fancybox({
            padding: 5,
            margin: 5, 
            cyclic: true, 
            transitionIn: 'elastic', 
            transitionOut: 'elastic',
            easingIn: 'easeOutBack',
            overlayOpacity: 0.6,
            overlayColor: '#000'
        });
    },
    
}

var livestream = {
	init: function() {
		this.startPolling();
	},
	
	pollerId: null,
	button: null,
	buttonBlink: null,
	buttonBlinkVisible: true,
	
	startPolling: function() {
		livestream.pollerId = window.setInterval(livestream.poll, 20000);
		//livestream.poll();
	},
	
	stopPolling: function() {
		window.clearInterval(livestream.pollerId);
	},
	
	poll: function() {
		$.get('http://www.theGoOzer.com/islive.htm?' + Math.random(), function(data) {
			if (data == '1') {
                livestream.stopPolling();
                livestream.button = $('<a class="livebutton" href="http://www.ustream.tv/channel/livegooze" target="_blank"><span class="onair">&nbsp;</span></a>');
				$('a', livestream.button).click(function() {
					livestream.button.remove();
					return false;
				});
				livestream.button.css('left', (($(window).width() / 2) - 193) + 'px').css('top', ($(window).height() - 250) + 'px');
				$('#goOzer').append(livestream.button);
				livestream.buttonBlink = window.setInterval(livestream.blink, 700);
			}
		});
	},
	
	blink: function() {
		livestream.buttonBlinkVisible = !livestream.buttonBlinkVisible;
		$('span', livestream.button).css('visibility', (livestream.buttonBlinkVisible ? 'visible' : 'hidden'));
	}
}

$(function() {
    board.init();
    livestream.init();
    
    $('#gfa11board a').fancybox().wiggle({ wiggles: 10000, travel: 2 });

    $.post('seconds.aspx', {}, function(counterValue) {
        $('#countDownNumber').text((Math.round((counterValue / 60) * 1000) / 1000).toFixed(3));
        window.setInterval(function() {
            counterValue -= 0.1;
            $('#countDownNumber').text((Math.round((counterValue / 60) * 1000) / 1000).toFixed(3));
        }, 100);
    });

    $.prototype.serializeClassnames = function () {
        var o = $;
        return o.param(this.map(function(){
            return this.elements ? o.makeArray(this.elements):this
        }).filter(function(){
            return !this.disabled&&(this.checked||/select|textarea/i.test(this.nodeName)||/text|hidden|password|search/i.test(this.type))
        }).map(function(E,F){
            var G=o(this).val();return G==null?null:o.isArray(G)?o.map(G,function(I,H){
                return {name:F.className,value:I}
            }):{name:F.className,value:G}
        }).get());
    }

});

var BrowserDetect = {
    init: function () {
        this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
        this.version = this.searchVersion(navigator.userAgent)
            || this.searchVersion(navigator.appVersion)
            || "an unknown version";
        this.OS = this.searchString(this.dataOS) || "an unknown OS";
    },
    searchString: function (data) {
        for (var i=0;i<data.length;i++) {
            var dataString = data[i].string;
            var dataProp = data[i].prop;
            this.versionSearchString = data[i].versionSearch || data[i].identity;
            if (dataString) {
                if (dataString.indexOf(data[i].subString) != -1)
                    return data[i].identity;
            }
            else if (dataProp)
                return data[i].identity;
        }
    },
    searchVersion: function (dataString) {
        var index = dataString.indexOf(this.versionSearchString);
        if (index == -1) return;
        return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
    },
    dataBrowser: [
        {
            string: navigator.userAgent,
            subString: "Chrome",
            identity: "Chrome"
        },
        {   string: navigator.userAgent,
            subString: "OmniWeb",
            versionSearch: "OmniWeb/",
            identity: "OmniWeb"
        },
        {
            string: navigator.vendor,
            subString: "Apple",
            identity: "Safari",
            versionSearch: "Version"
        },
        {
            prop: window.opera,
            identity: "Opera"
        },
        {
            string: navigator.vendor,
            subString: "iCab",
            identity: "iCab"
        },
        {
            string: navigator.vendor,
            subString: "KDE",
            identity: "Konqueror"
        },
        {
            string: navigator.userAgent,
            subString: "Firefox",
            identity: "Firefox"
        },
        {
            string: navigator.vendor,
            subString: "Camino",
            identity: "Camino"
        },
        {       // for newer Netscapes (6+)
            string: navigator.userAgent,
            subString: "Netscape",
            identity: "Netscape"
        },
        {
            string: navigator.userAgent,
            subString: "MSIE",
            identity: "Explorer",
            versionSearch: "MSIE"
        },
        {
            string: navigator.userAgent,
            subString: "Gecko",
            identity: "Mozilla",
            versionSearch: "rv"
        },
        {       // for older Netscapes (4-)
            string: navigator.userAgent,
            subString: "Mozilla",
            identity: "Netscape",
            versionSearch: "Mozilla"
        }
    ],
    dataOS : [
        {
            string: navigator.platform,
            subString: "Win",
            identity: "Windows"
        },
        {
            string: navigator.platform,
            subString: "Mac",
            identity: "Mac"
        },
        {
               string: navigator.userAgent,
               subString: "iPhone",
               identity: "iPhone/iPod"
        },
        {
            string: navigator.platform,
            subString: "Linux",
            identity: "Linux"
        }
    ]

};
BrowserDetect.init();


/*
jQuery Wiggle
Author: WonderGroup, Jordan Thomas
URL: http://labs.wondergroup.com/demos/mini-ui/index.html
License: MIT (http://en.wikipedia.org/wiki/MIT_License)
*/
jQuery.fn.wiggle=function(o){var d={speed:50,wiggles:3,travel:5,callback:null};var o=jQuery.extend(d,o);return this.each(function(){var cache=this;var wrap=jQuery(this).wrap('<div class="wiggle-wrap"></div>').css("position","relative");var calls=0;for(i=1;i<=o.wiggles;i++){jQuery(this).animate({left:"-="+o.travel},o.speed).animate({left:"+="+o.travel*2},o.speed*2).animate({left:"-="+o.travel},o.speed,function(){calls++;if(jQuery(cache).parent().hasClass('wiggle-wrap')){jQuery(cache).parent().replaceWith(cache);}
if(calls==o.wiggles&&jQuery.isFunction(o.callback)){o.callback();}});}});};
