// $Id: main.js 69 2008-04-29 11:51:52Z mstillwell $

var caroNumber = 0;
var currCaroNumber = 0;
var caroImageWidth = 300;
var caroSpeed = 5000;
var t = false;
var stopScroll = false;

// onLoad functions
function init() {
	externalLinks();
	doSifr();
	
	if( $$('div.home').length > 0 ){
		carousel();
		setSelectedCarouselElement();
		f=setTimeout("startCarousel()", caroSpeed);
	}
}

// opens links in new window with rel="external" to validate xhtml strict
function externalLinks() { 
	if (!document.getElementsByTagName) return; 
	var anchors = document.getElementsByTagName("a"); 
 	for (var i=0; i<anchors.length; i++) { 
   			var anchor = anchors[i]; 
 			if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external"){
 				anchor.target = "_blank"; 
       		} else if(anchor.getAttribute("href") && anchor.getAttribute("rel") == "thiswindow")anchor.target = "_top";
 	} 
}

function FlvPlayer (videourl) {
  this.videourl = 'videourl=' + videourl;
  this.width = 568;
  this.height = 377;
  this.dh; // drag handle
  
  this.build = function() {
    // Build html such as
    // <div id="modaldialog">
    //   <div id="flv_player">
    //     <div id="flv_player_horizon">
    //       <div id="flv_player_content">
    //         {flv player}
    //          <a id="flv_player_close" href="..."></a>
    //       </div>
    //     </div>
    //   </div>
    //  </div>
    var close = new Element('a', { 'id': 'flv_player_close', 'href': 'javascript:fp.hide()' });
    var content = new Element('div', { 'id': 'flv_player_content'});
    var horizon = new Element('div', { 'id': 'flv_player_horizon'});
    var player = new Element('div', { 'id': 'flv_player'});
    var dialog = new Element('div', { 'id': 'modaldialog', 'style':'display: none;'});
    
    body = $$('body');
    
    // This is a call to a modified version of AC_FL_RunContent - it returns the generated html
    // 
    content.innerHTML =  AC_FL_RunContent("codebase", "http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0",
                   "width", "488",
				   "height", "300",
				   "src", "/swf/synergy-videoplayer",
				   "quality", "high",
				   "pluginspage", "http://www.macromedia.com/go/getflashplayer",
				   "align", "middle",
				   "play", "true",
				   "loop", "true",
				   "scale", "showall",
				   "wmode", "window",
				   "devicefont", "false",
				   "id", "synergy-videoplayer",
				   "bgcolor", "#ffffff",
				   "name", "/swf/synergy-videoplayer",
				   "menu", "true",
				   "allowFullScreen", "false",
				   "allowScriptAccess","sameDomain",
				   "movie", "/swf/synergy-videoplayer",
				   "FlashVars", this.videourl,
				   "salign", ""
				   ); //end AC code
				       
    horizon.insert(content);
    player.insert(horizon);
    player.insert(close);
    dialog.insert(player);
    body[0].insert( { top: dialog });
  }
  
  this.show = function() {
    body = $$('body');
    offset = body[0].cumulativeScrollOffset();
    $('flv_player').style.top = (offset[1] + (document.viewport.getHeight() - this.height)/2) + "px";
    $('flv_player').style.left = (document.viewport.getWidth() - this.width)/2 + "px";
    $('modaldialog').show();
    this.dh = new Draggable('flv_player');
  }
  
  this.hide = function() {
    this.dh.destroy();
    $('modaldialog').hide();
  }
}

function StaffBio() {
  this.width = 568;
  this.height = 377;
  this.dh; 
  
  // Build html like
  // <div id="modaldialog">
  //   <div id="staffBio">
  //     <a id="staffBioClose"></a>
  //     <div id="StaffBioContent">
  //      { content }
  //     </div>
  //   </div>
  // </div>
  this.build = function() {
    var close = new Element('a', { 'id': 'staffBioClose', 'href': 'javascript:staffbio.hide()' });
    var content = new Element('div', { 'id': 'staffBioContent' });
    var container = new Element('div', { 'id': 'staffBio' });
    var dialog = new Element('div', { 'id': 'modaldialog', 'style':'display: none;'});
    
    body = $$('body');

    container.insert(close);
    container.insert(content);
    dialog.insert(container);
    body[0].insert( { top: dialog });
  }
  
  this.show = function(html) {
    body = $$('body');
    offset = body[0].cumulativeScrollOffset();
    $('staffBio').style.top = (offset[1] + (document.viewport.getHeight() - this.height)/2) + "px";
    $('staffBio').style.left = (document.viewport.getWidth() - this.width)/2 + "px";
    $('staffBioContent').innerHTML = html;
    $('modaldialog').show();
    this.dh = new Draggable('staffBio');
  }
  
  this.hide = function() {
    this.dh.destroy();
    $('modaldialog').hide();
  }
}

function carousel() {
	$$('.home #rhColumn div#carousel-nav ul li a').each(function(element) {
		element.observe('click',function(e) {
			Event.stop(e);
			stopScroll = true;
						
			if( element.hasClassName('forward') ) {
				if( currCaroNumber < caroNumber){
					currCaroNumber++;
				}else{
					currCaroNumber = 0;
				}				

			}else if( element.hasClassName('back') ) {
				if( currCaroNumber > 0){
					currCaroNumber--;
				}else{
					currCaroNumber = caroNumber;
				}
			}else {
				var selection = element.className.split("_");
				currCaroNumber = selection[1] - 1;
			}
			
			scroller();
			
		});
	});
}

function startCarousel() {
	
	var carouselLi = $$('.caro_images');
	caroNumber = carouselLi.length-1;
	
	if(t!=false){
		clearTimeout(t);
	}	
	
	if( currCaroNumber < caroNumber){
		currCaroNumber++;
	}else{
		currCaroNumber = 0;
	}
	
	scroller();
	return false;
}

function scroller(){
		
	var position = -(caroImageWidth*currCaroNumber);		
	//slide image
	new Effect.Move($('carousel'), { x: position, y: 0, mode: 'absolute', transition: Effect.Transitions.sinoidal });
	//set links
	setSelectedCarouselElement();

	//loop
	if(stopScroll==true){
		clearTimeout(t);
	}else{
		t = setTimeout("startCarousel()",caroSpeed);
	}
}

function setSelectedCarouselElement() {
	var carolinks = $$('.home #rhColumn div#carousel-nav ul li');
	
	//remove selected class
	$$('.home #rhColumn div#carousel-nav ul li').each(function(element) {
		if(element.hasClassName('selected') == true) {
			element.removeClassName('selected');
		}
	});
	
	//add new classname
	carolinks[currCaroNumber+1].addClassName('selected'); 
	
}

function newsletterSignup() {
	  this.width = 568;
	  this.height = 377;
	  this.dh; 
	  
	  // Build html like
	  // <div id="modaldialog">
	  //   <div id="staffBio">
	  //     <a id="staffBioClose"></a>
	  //     <div id="StaffBioContent">
	  //      { content }
	  //     </div>
	  //   </div>
	  // </div>
	  this.build = function() {
	    var close = new Element('a', { 'id': 'staffBioClose', 'href': 'javascript:newsletter.hide()' });
	    var content = new Element('div', { 'id': 'signupContent' });
	    var container = new Element('div', { 'id': 'signup' });
	    var dialog = new Element('div', { 'id': 'modaldialog', 'style':'display: none;'});
	    
	    body = $$('body');

	    container.insert(close);
	    container.insert(content);
	    dialog.insert(container);
	    body[0].insert( { top: dialog });
	  }
	  
	  this.show = function(html) {
	    body = $$('body');
	    offset = body[0].cumulativeScrollOffset();
	    $('signup').style.top = (offset[1] + (document.viewport.getHeight() - this.height)/2) + "px";
	    $('signup').style.left = (document.viewport.getWidth() - this.width)/2 + "px";
	    $('signupContent').innerHTML = html;
	    $('modaldialog').show();
	    this.dh = new Draggable('signup');
	  }
	  
	  this.hide = function() {
	    this.dh.destroy();
	    $('modaldialog').hide();
	  }
}
