// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

// Preload navigation menu buttons
new Image().src = "/images/application/tabs/account_hover.gif";
new Image().src = "/images/application/tabs/account.gif";
new Image().src = "/images/application/tabs/community_hover.gif";
new Image().src = "/images/application/tabs/community.gif";
new Image().src = "/images/application/tabs/corporate_hover.gif";
new Image().src = "/images/application/tabs/corporate.gif";
new Image().src = "/images/application/tabs/developers_hover.gif";
new Image().src = "/images/application/tabs/developers.gif";
new Image().src = "/images/application/tabs/help_hover.gif";
new Image().src = "/images/application/tabs/help.gif";
new Image().src = "/images/application/tabs/login_hover.gif";
new Image().src = "/images/application/tabs/login.gif";
new Image().src = "/images/application/tabs/logout_hover.gif";
new Image().src = "/images/application/tabs/logout.gif";
new Image().src = "/images/application/tabs/mychumby_hover.gif";
new Image().src = "/images/application/tabs/mychumby.gif";
new Image().src = "/images/application/tabs/store_hover.gif";
new Image().src = "/images/application/tabs/store.gif";
new Image().src = "/images/application/tabs/widgets_hover.gif";
new Image().src = "/images/application/tabs/widgets.gif";


/* Navigation menu */

/* 
  PVII Menu CSS Express Drop-Down Menu
  www.projectseven.com
*/
function navigation_menu(){ //v1.1.0.2 by PVII-www.projectseven.com
 if(navigator.appVersion.indexOf("MSIE")==-1){return;}
 var i,k,g,lg,r=/\s*p7hvr/,nn='',c,cs='p7hvr',bv='navigation';
 for(i=0;i<10;i++){g=document.getElementById(bv+nn);if(g){
 lg=g.getElementsByTagName("LI");if(lg){for(k=0;k<lg.length;k++){
 lg[k].onmouseover=function(){body_selects =
document.getElementsByTagName("select");for (i = 0; i < body_selects.length;
i++) if(body_selects[i].className == 'menu_hide' && navigator.appVersion.indexOf("MSIE 6") > -1) {body_selects[i].style.visibility = 'hidden';};c=this.className;cl=(c)?c+''+cs:cs;this.className=cl;};
 lg[k].onmouseout=function(){body_selects =
document.getElementsByTagName("select");for (i = 0; i < body_selects.length;
i++)   if(body_selects[i].className == 'menu_hide' && navigator.appVersion.indexOf("MSIE 6") > -1) {body_selects[i].style.visibility =
  'visible';};c=this.className;this.className=(c)?c.replace(r,''):'';};}}}nn=i+1;}
}

Event.observe(window, 'load', loadAccordions);
Event.observe(window, 'load', navigation_menu);

function loadAccordions() {
	if($('vertical_container')) { //only if the accordion is present
		var bottomAccordion = new accordion('vertical_container');
		bottomAccordion.activate($$('#vertical_container .accordion_toggle')[0]);	
	}
}

var dismiss = function() {
	/*NOTE: please see guide/add if changes are made here*/
  var play_time = $('play_time');
  if(play_time) {
	play_time.blur();
  }
  var play_title = document.getElementById('title');
  if(play_title) {
	play_title.blur();
  }
  Lightbox.hideBox();
}

// Show selected control panel screen
function reveal(detail){
	var hideAll = document.getElementsByTagName("div") ;
		for(var i = 0; i < hideAll.length; i++) {
		  if(hideAll[i].className == "show") {
		      hideAll[i].className = "detail";
		  }
	}
	var section = document.getElementById(detail);
  section.className = "show";
}

if (typeof Effect == 'undefined') 
	throw("accordion.js requires including script.aculo.us' effects.js library!");

var accordion = Class.create();
accordion.prototype = {

	//
	//  Setup the Variables
	//
	showAccordion : null,
	currentAccordion : null,
	duration : null,
	effects : [],
	animating : false,
	
	//  
	//  Initialize the accordions
	//
	initialize: function(container, options) {
	  if (!$(container)) {
	    throw(container+" doesn't exist!");
	    return false;
	  }
	  
		this.options = Object.extend({
			resizeSpeed : 8,
			classNames : {
				toggle : 'accordion_toggle',
				toggleActive : 'accordion_toggle_active',
				content : 'accordion_content'
			},
			defaultSize : {
				height : null,
				width : null
			},
			direction : 'vertical',
			onEvent : 'click'
		}, options || {});
		
		this.duration = ((11-this.options.resizeSpeed)*0.15);

		var accordions = $$('#'+container+' .'+this.options.classNames.toggle);
		accordions.each(function(accordion) {
			Event.observe(accordion, this.options.onEvent, this.activate.bind(this, accordion), false);
			if (this.options.onEvent == 'click') {
			  accordion.onclick = function() {return false;};
			}
			
			if (this.options.direction == 'horizontal') {
				var options = $H({width: '0px'});
			} else {
				var options = $H({height: '0px'});			
			}
			options.merge({display: 'none'});			
			
			this.currentAccordion = $(accordion.next(0)).setStyle(options);			
		}.bind(this));
	},
	
	//
	//  Activate an accordion
	//
	activate : function(accordion) {
		if (this.animating) {
			return false;
		}
		
		this.effects = [];
	
		this.currentAccordion = $(accordion.next(0));
		this.currentAccordion.setStyle({
			display: 'block'
		});		
		
		this.currentAccordion.previous(0).addClassName(this.options.classNames.toggleActive);

		if (this.options.direction == 'horizontal') {
			this.scaling = $H({
				scaleX: true,
				scaleY: false
			});
		} else {
			this.scaling = $H({
				scaleX: false,
				scaleY: true
			});			
		}
			
		if (this.currentAccordion == this.showAccordion) {
		  this.deactivate();
		} else {
		  this._handleAccordion();
		}
	},
	// 
	// Deactivate an active accordion
	//
	deactivate : function() {
		var options = $H({
		  duration: this.duration,
			scaleContent: false,
			transition: Effect.Transitions.sinoidal,
			queue: {
				position: 'end', 
				scope: 'accordionAnimation'
			},
			scaleMode: { 
				originalHeight: this.options.defaultSize.height ? this.options.defaultSize.height : this.currentAccordion.scrollHeight,
				originalWidth: this.options.defaultSize.width ? this.options.defaultSize.width : this.currentAccordion.scrollWidth
			},
			afterFinish: function() {
				this.showAccordion.setStyle({
          height: 'auto',
					display: 'none'
				});				
				this.showAccordion = null;
				this.animating = false;
			}.bind(this)
		});    
    options.merge(this.scaling);

    this.showAccordion.previous(0).removeClassName(this.options.classNames.toggleActive);
    
		new Effect.Scale(this.showAccordion, 0, options);
	},

  //
  // Handle the open/close actions of the accordion
  //
	_handleAccordion : function() {
		var options = $H({
			sync: true,
			scaleFrom: 0,
			scaleContent: false,
			transition: Effect.Transitions.sinoidal,
			scaleMode: { 
				originalHeight: this.options.defaultSize.height ? this.options.defaultSize.height : this.currentAccordion.scrollHeight,
				originalWidth: this.options.defaultSize.width ? this.options.defaultSize.width : this.currentAccordion.scrollWidth
			}
		});
		options.merge(this.scaling);
		
		this.effects.push(
			new Effect.Scale(this.currentAccordion, 100, options)
		);

		if (this.showAccordion) {
			this.showAccordion.previous(0).removeClassName(this.options.classNames.toggleActive);
			
			options = $H({
				sync: true,
				scaleContent: false,
				transition: Effect.Transitions.sinoidal
			});
			options.merge(this.scaling);
			
			this.effects.push(
				new Effect.Scale(this.showAccordion, 0, options)
			);				
		}
		
    new Effect.Parallel(this.effects, {
			duration: this.duration, 
			queue: {
				position: 'end', 
				scope: 'accordionAnimation'
			},
			beforeStart: function() {
				this.animating = true;
			}.bind(this),
			afterFinish: function() {
				if (this.showAccordion) {
					this.showAccordion.setStyle({
						display: 'none'
					});				
				}
				$(this.currentAccordion).setStyle({
				  height: 'auto'
				});
				this.showAccordion = this.currentAccordion;
				this.animating = false;
			}.bind(this)
		});
	}
}

