//
//  Created by Chris Sessions on 2007-09-20.
//  Copyright (c) 2007 Highly Evolved, LLC. All rights reserved.
//

var ie6 = false /*@cc_on || @_jscript_version < 5.7 @*/
if(ie6) window.location = '/exploder'

Event.observe(window, 'load', function() {
  Nav.init()
  News.init()
  Theme.init()
  Team.init()
  Partners.init()
  Login.init()
  Features.init()
  Sfx.init()
  Excerpt.read()
})

var Excerpt = {
  
  id: Math.floor(99 * Math.random()),
  
  read: function() {
    $('ooc-excerpt').update('<p class="loading">Loading...<p>')
    new Ajax.Updater('ooc-excerpt', '/public_access/get_excerpt/' + this.id, {method: 'get'})
  },
  
  more: function() {
    new Ajax.Updater('popcontent', '/public_access/pop_excerpt/' + this.id, {method: 'get'})
    RedBox.showInline('popbox')
  },

  next: function() {
    this.id++
    this.read()
  },
  
  prev: function() {
    this.id--
    this.read()
  }
}

var Features = {
  
  init: function() {
    if($('hero-box') != undefined) {
      var so = new SWFObject('/swfs/features.swf', 'features', 707, 447, 9, '#333333')
      so.addParam('wmode', 'opaque')
      so.write('hero-box')
    }
  }
}

var Login = {
  
  init: function() {
    setTimeout(Login.make_handlers, 250)
  },
  
  make_handlers: function() {
    ['people_login', 'people_password'].each(function(e) {
      $(e).onfocus = function() {
        this.setStyle({backgroundImage: 'none'})
      }
      $(e).onblur = function() {
        this.setStyle({backgroundImage: Login.get_bg(this)})
      }
      $(e).setStyle({backgroundImage: Login.get_bg($(e))})
    })
  },
  
  get_bg: function(element) {
    return (element.value == '' || element.value == undefined) ?
           'url("/images/structure/back_input_' + element.name + '.gif")' : 'none'
  }
}

var Nav = {
  
  // NAV_TOOLTIPS: { 'nav-public-access':    {mask: 0, pos: 50},
  //                 'nav-this-is-a-heist':  {mask: -180, pos: 230},
  //                 'nav-out-in-the-wild':  {mask: -360, pos: 420},
  //                 'nav-shadow-ops':       {mask: -540, pos: 600}},
  
  active_nav: null,
  current_nav: null,
  // active_tip: null,
  
  init: function() {
    $$('a.nav').each(function(nav_btn) {
      nav_btn.onmouseover = Nav.rollover
      nav_btn.onmouseout = Nav.rollout
    })
    $$('a.active').each(function(current) {
      Nav.current_nav = $(current)
    })
  },
  
  rollover: function() {
    var div_id = this.id.split('-')
    div_id.shift()
    Nav.active_nav = $('nav-' + div_id.join('-'))
    // Nav.active_tip = $('nav-tooltip')
    // Nav.active_tip.setStyle({backgroundPosition: Nav.NAV_TOOLTIPS[this.id]['mask'] + 'px 0px', marginLeft: Nav.NAV_TOOLTIPS[this.id]['pos'] + 'px'})
    // Effect.Appear(Nav.active_tip, {duration: 0.1})
    if(Nav.active_nav != Nav.current_nav) {
      Nav.active_nav.setStyle({backgroundPosition: '0 -70px' })
    }
  },
  
  rollout: function(event) {
    // if(!Position.within(Nav.active_nav, Event.pointerX(event), Event.pointerY(event))) {
    //   Effect.Fade(Nav.active_tip, {duration: 0.5})
    // }
    if(Nav.active_nav != Nav.current_nav) {
      Nav.active_nav.setStyle({backgroundPosition: '0 0' })
    }
  }  
}

var News = {
  
  init: function() {
    $$('.news-item div').each(function(news_item) {
      var news = news_item.descendants()
      var so   = new SWFObject('/swfs/blog_head.swf', news_item.id + '_swf', 677, 31, 9)
      so.addVariable('h', news[0].innerHTML)
      so.addParam('wmode', 'transparent')
      so.write(news_item.id)
    })
  }
     
}

var Partners = {
  
  PARTNER_DETAILS: [{img: 'blue_box.png', size: '68x20', alt: 'Blue Box', url: 'http://blueboxgrid.com/'},
                    {img: 'geyrhalter_design.png', size: '109x11', alt: 'Geyrhalter Design', url: 'http://www.geyrhalter.com/'},
                    {img: 'jay_david.gif', size: '50x47', alt: 'Jay David', url: 'http://www.explosion.nu/'},
                    {img: 'ruby.png', size: '40x40', alt: 'Ruby', url: 'http://www.ruby-lang.org/'},
                    {img: 'sensura.jpg', size: '48x48', alt: 'Sensura', url: 'http://www.sensurastudio.com/'},
                    {img: 'seriph.gif', size: '50x47', alt: 'Seriph', url: 'http://www.seriph.com/'},
                    {img: 'the_cosmonaut.gif', size: '136x16', alt: 'The Cosmonaut', url: 'http://thecosmonaut.com/'},
                    {img: 'practical.png', size: '75x31', alt: 'Practical', url: 'http://practical.cc/'},
                    {img: 'eric_larsen.gif', size: '66x48', alt: 'E. Tage Larsen Design', url: 'http://etagelarsen.com/'},
                    {img: 'two_thirty.png', size: '50x47', alt: 'TwoThirty', url: 'http://www.twothirty.com/'}],

  init: function() {
    this.shuffle(this.PARTNER_DETAILS)
    var width = 0, count = 0
    while(true) {
      var p    = this.PARTNER_DETAILS.pop()
      var size = p.size.split('x')
      if(width + parseInt(size[0]) > 270) break
      var tag  = '<a href="' + p.url + '"><img alt="' + p.alt + '" width="' + size[0] + '" height="' + size[1] + '" src="/content/partner_logos/' + p.img + '" /></a>'
      new Insertion.Bottom('partner-logos', tag)
      width += parseInt(size[0])
      count++
    }
    var margin = Math.floor((300 - width) / (count * 2))
    $('partner-logos').childElements().each(function(logo) {
      logo.setStyle({margin: '10px ' + margin + 'px 10px ' + margin + 'px'})
    })
  },
  
  shuffle: function(a) {
    for(var i = a.length - 1; i >= 0; i--) {
      var j = Math.floor( Math.random() * (i + 1)), tempi = a[i], tempj = a[j]
      a[i] = tempj, a[j] = tempi
    }
  }
}

var Sfx = {
  
  init: function() {
    var so = new SWFObject('/swfs/sfx.swf', 'sfx', 16, 16, 9)
    so.addParam("wmode", "transparent");
    so.write('flash-sounds')
    $$('a', 'button').each(function(b) {
      b.observe('mouseover', function() {
        $('sfx').over_sfx()
      })
      b.observe('click', function() {
        $('sfx').click_sfx()
      })
    })
  }
}

var Team = {
  
  active: 0,
  
  init: function() {
    this.active = Cookie.get('team') || 0
    var d = this.divs(this.active)
    d.invoke('setStyle', {display: 'block'})
    d[0].firstDescendant().addClassName('active')
  },
  
  show: function(id) {
    if(this.active != id) {
      this.hide_previous()
      this.active = id
      var d = this.divs(this.active)
      d[0].firstDescendant().addClassName('active')
      Effect.BlindDown(d[1], {duration: 0.3})
      Cookie.set('team', id, 1209600)

    }
  },
  
  hide_previous: function() {
    var d = this.divs(this.active)
    d[0].firstDescendant().removeClassName('active')
    Effect.BlindUp(d[1], {duration: 0.3})
  },
  
  divs: function(id) {
    return $$('#person_' + this.active + ' div')
  }
}

var Theme = {

  THEME_DETAILS: {gray: {pos: 16, img: 'background_gray.gif'},
                  wood: {pos: 32, img: 'background_wood.jpg'},
                  fire: {pos: 48, img: 'background_fire.jpg'},
                  lava: {pos: 64, img: 'background_lava.jpg'},
                  rdnt: {pos: 80, img: 'background_hamster.gif'}},

  content_ht: 0,
  bg_count:   0,
  bg_img:     null,
  next_img:   null,
  ff_osx:     navigator.userAgent.indexOf('Macintosh') > -1 && navigator.userAgent.indexOf('Firefox') > -1,
  
  init: function() {
    this.bg_img = Cookie.get('theme') || 'gray'
    this.insert_bg_div()
    this.set_height()
    setInterval(this.set_height, 100)
    $$('#themes a').each(function(b) {
      b.observe('mouseover', function() {
        Theme.ponder(b.innerHTML.toLowerCase())
      })
      b.observe('mouseout', Theme.revert)
    })
  },
  
  set_height: function() {
    var h = $('content').getHeight()
    if(this.content_ht != h && $(Theme.bg_div()) != null) {
      $(Theme.bg_div()).setStyle({height: h + 'px'})
      Theme.content_ht = h
    }
  },
  
  ponder: function(bg) {
    $('theme-names').setStyle({backgroundPosition: '0px ' + -this.THEME_DETAILS[bg]['pos'] + 'px'})
    this.next_img = bg
  },
  
  revert: function() {
    $('theme-names').setStyle({backgroundPosition: '0px 0px'})
    this.next_img = null
  },

  change: function() {
    if(this.next_img != null && this.next_img != this.bg_img) {
      this.bg_img = this.next_img
      this.next_img = null
      this.fade_old_bg_div()
      var preload = new Image
      preload.onload = function() {
        var new_bg = Theme.insert_bg_div()
        if(!Theme.ff_osx) new Effect.Opacity(new_bg, {duration: 1.0, from: 0.0, to: 1.0})
        Cookie.set('theme', Theme.bg_img, 1209600)
      }
      preload.src = '/images/structure/' + this.THEME_DETAILS[this.bg_img]['img']
    }
  },
  
  insert_bg_div: function() {
    var new_bg = this.bg_div()
    new Insertion.Before('content', '<div id="' + new_bg + '" class="background"></div>')
    setTimeout(function() {
      $(new_bg).addClassName(Theme.bg_img)
    }, 100)
    return new_bg
  },
  
  fade_old_bg_div: function() {
    var div = this.bg_div()
    if($(div) != null) {
      if(!this.ff_osx) new Effect.Opacity(div, {duration: 1.0, from: 1.0, to: 0.0})
      setTimeout(function() {
        $(div).remove()
      }, 1000)
      this.bg_count++
    }
  },
  
  bg_div: function() {
    return 'background_' + this.bg_count
  }
}

var Cookie = {

	set: function(name, value, seconds) {
		if(seconds) {
			var d = new Date()
			d.setTime(d.getTime() + (seconds * 1000))
			var expires = '; expires=' + d.toGMTString()
		} else expires = ''
		document.cookie = name + '=' + value + expires + '; path=/'
	},
	
  get: function(name) {
  	assign = name + '='
  	ca = document.cookie.split(';')
  	for(i = 0; i < ca.length; i++) {
  		c = ca[i]
  		while(c.charAt(0) == ' ') c = c.substring(1, c.length)
  		if(c.indexOf(assign) == 0) return c.substring(assign.length, c.length)
  	}
  	return null
  },
  
	unset: function(name) {
		Cookie.set(name, '', -1)
	}
}


// TEMPORARY SCRIPTS

/* This script and many more are available free online at
The JavaScript Source :: http://javascript.internet.com
Created by: kojak :: http://commoncoder.com */

// fieldname, warningname, remainingname, maxchars
function CheckFieldLength(fn,wn,mc) {
  var len = fn.value.length;
  if (len > mc) {
    fn.value = fn.value.substring(0,mc);
    len = mc;
  }
  document.getElementById(wn).innerHTML = len;
  document.getElementById.innerHTML = mc - len;
}