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

function check_boxes(check, class_name) {
	
	// Get tags
	var tags = document.getElementsByTagName('input');
	
	// Filter by class
	var classed_tags = new Array();
	for (var i = 0; i < tags.length; i++)
	{
		if (tags[i].className.indexOf(class_name) != -1) {
			classed_tags[classed_tags.length] = tags[i];
		}
	}
	
	for (var i = 0; i < classed_tags.length; i++)
	{
		classed_tags[i].checked = check;
	}
}

function getCookie( name ) {
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
		return null;
	}
	if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ';', len );
	if ( end == -1 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
}

function setCookie( name, value, expires, path, domain, secure ) {
	var today = new Date();
	today.setTime( today.getTime() );
	if ( expires ) {
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	document.cookie = name+'='+escape( value ) +
		( ( expires ) ? ';expires='+expires_date.toGMTString() : '' ) + //expires.toGMTString()
		( ( path ) ? ';path=' + path : '' ) +
		( ( domain ) ? ';domain=' + domain : '' ) +
		( ( secure ) ? ';secure' : '' );
}

function deleteCookie( name, path, domain ) {
	if ( getCookie( name ) ) document.cookie = name + '=' +
			( ( path ) ? ';path=' + path : '') +
			( ( domain ) ? ';domain=' + domain : '' ) +
			';expires=Thu, 01-Jan-1970 00:00:01 GMT';
}

var wcc = {
	makeMenu: function() {
		
		/* Loop through animating elements and popuplate array */
		this.showHides = $$('.expandable');
		this.toggles = $$('h2.toggle a');
		
		this.movers = $$('.expandable .box-inner ul')
		this.downs = $$('.menu-category-down');
		this.ups = $$('.menu-category-up');
		
		// Determine starting category
		cat_index = getCookie('currentCategory');
		
		if (cat_index == null)
		{
			cat_index = this.showHides.length - 1;
		}
		
		// Close all but last category unless cookie set
		for (var i = 0; i < this.showHides.length; i++)
		{
			if (i != cat_index)
			{
				this.showHides[i].open = false;
				this.showHides[i].hide();
			}
			else
			{
				this.showHides[i].open = true;
				this.toggles[i].addClassName('on');
			}
		}
		
		this.toggles.each(function(el, index){
			/* Get associated showHide element */
			var target = this.showHides[index];
			
			/* Add event handler to toggle button and manipulate associated showHide element */
			el.observe('click', function(event) {
				for (var i = 0; i < this.showHides.length; i++)
				{
					if (this.toggles[i] != el)
					{
						// Swap button
						this.toggles[i].removeClassName('on');
						
						// Close category
						new Effect.BlindUp(this.showHides[i], { duration: 0.5 });
						Event.stop(event);
						this.showHides[i].open = false;
					}
					else if (!this.showHides[i].open)
					{
						// Swap button
						el.addClassName('on');
						el.blur();
						
						// Open category and set cookie
						new Effect.BlindDown(this.showHides[i], { duration: 0.5 });
						Event.stop(event);
						this.showHides[i].open = true;
						setCookie('currentCategory', i, false, '/');
					}
				}
			}.bind(this));
			
			var increment = 125;
			
			// Set starting position based on cookie
			// Determine starting category
			menu_pos = getCookie('menuPosition' + index);

			if (menu_pos == null)
			{
				menu_pos = 40;
			}
			
			wcc.movers[index].setStyle({
				top: menu_pos + 'px'
			});
			
			// Get offset position
			// Determined at time of authoring to be 0,41
			position = wcc.movers[index].positionedOffset(wcc.movers[index]);

			// Get Height
			height = wcc.movers[index].getHeight();
			
			// Set on/off state for down button
			if ((position.top + height - (increment * 3)) <= 41)
			{
				// Set button to off state
				this.ups[index].src = '/images/general/btn-menu-down-off.png';
				this.ups[index].setStyle({
					cursor: 'default'
				});
			}
			else
			{
				// Set button to on state
				this.ups[index].src = '/images/general/btn-menu-down.png';
				this.ups[index].setStyle({
					cursor: 'pointer'
				});
			}
			
			// Set on/off state for this
			if (position.top >= 41)
			{
				// Set button to off state
				this.downs[index].src = '/images/general/btn-menu-up-off.png';
				this.downs[index].setStyle({
					cursor: 'default'
				});
			}
			else
			{
				// Set button to on state
				this.downs[index].src = '/images/general/btn-menu-up.png';
				this.downs[index].setStyle({
					cursor: 'pointer'
				});
			}
			
			// Set up scrollers
			this.ups[index].observe('click', function(event){
				// Get offset position
				// Determined at time of authoring to be 0,41
				position = wcc.movers[index].positionedOffset(wcc.movers[index]);

				// Get Height
				height = wcc.movers[index].getHeight();
				
				// Determines rate of movement
				var step = ((position.top - 41).abs() / increment);
				
				// If bottom of inner box is allowing for 3 titles to be shown
				// Then it has reached it's highest allowed point
				if ((position.top + height - (increment * 3)) > '41')
				{
					// If menu is in the correct 'rest' position
					if (step == parseInt(step, 10))
					{
						// Set menu position cookie
						setCookie('menuPosition' + index, position.top-1-increment, false, '/');
						
						new Effect.Move(wcc.movers[index],{x: 0, y: (-1 * increment), duration: 0.5, mode:'relative'});
						// Reset opposing button
						wcc.downs[index].src = '/images/general/btn-menu-up.png';
						wcc.downs[index].setStyle({
							cursor: 'pointer'
						});
					}
				}
				
				// Set on/off state for this
				if ((position.top + height - (increment * 3)) <= 41 + increment)
				{
					// Set button to off state
					this.src = '/images/general/btn-menu-down-off.png';
					this.setStyle({
						cursor: 'default'
					});
				}
				
				Event.stop(event);
				this.blur();
			});

			this.downs[index].observe('click', function(event){
				// Get offset position
				// Determined at time of authoring to be 0,41
				position = wcc.movers[index].positionedOffset(wcc.movers[index]);
				
				// Determines rate of movement
				var step = ((position.top - 41).abs() / increment);
				
				if (position.top < '41')
				{
					// If menu is in the correct 'rest' position
					if (step == parseInt(step, 10))
					{
						// Set menu position cookie
						setCookie('menuPosition' + index, position.top-1+increment, false, '/');
						
						new Effect.Move(wcc.movers[index],{x: 0, y: increment, duration: 0.5, mode:'relative'});
						// Reset opposing button
						wcc.ups[index].src = '/images/general/btn-menu-down.png';
						wcc.ups[index].setStyle({
							cursor: 'pointer'
						});
					}
				}
				
				// Set on/off state for this
				if (position.top + increment >= 41)
				{
					// Set button to off state
					this.src = '/images/general/btn-menu-up-off.png';
					this.setStyle({
						cursor: 'default'
					});
				}
				
				Event.stop(event);
				this.blur();
			});
			
		}, this);
		
	},
	makeTopNav: function() {
		// Get all top nav button links
		this.topnavs = $$('#topnav ul a');
		this.topnavimages = $$('#topnav ul a img');
		this.onStates = {
			'home': '/',
			'howitworks': '/main/howitworks',
			'licensing': '/licensing',
			'faq': '/questions/faq',
			'contact': '/contact'
		};
		
		this.topnavimages.each(function(el, index){
			// Define regular expression literal for image swap
			// Tests for "buttons/$1.gif" and uses value to grab on state image
			// The main match matches all characters except forward slash ("/") and dot (".")
			// Therefore, the image name will be the set of characters following the last forward slash ("/")
			// and before the first dot (".")
			var pattern = /(.*)\/([^\/|\.]*)(.*)\.gif/;
			var imgName = el.src.replace(pattern, "$2");
			el.imageOn = new Image();
			el.imageOff = new Image();
			el.imageOn.src = el.src.replace(pattern, "$1") + '/' + imgName + '.mo.gif';
			el.imageOff.src = el.src.replace(pattern, "$1") + '/' + imgName + '.gif';
			
			// Determine current page and highlight
			var url = window.location.pathname;
			if (url == this.onStates[imgName])
			{
				this.topnavs[index].isSelected = true;
				this.topnavs[index].addClassName('on');
				this.topnavimages[index].src = el.imageOn.src;
			}
			else
			{
				this.topnavs[index].isSelected = false;
			}
		}, this);
		
		this.topnavs.each(function(el, index){
			// Add mouseover event
			el.observe('mouseover', function(event){
				for (var i = 0; i < this.topnavimages.length; i++)
				{
					this.topnavimages[i].src = this.topnavimages[i].imageOff.src;
				}
				this.topnavimages[index].src = this.topnavimages[index].imageOn.src;
			}.bind(this));
			el.observe('mouseout', function(event){
				for (var i = 0; i < this.topnavimages.length; i++)
				{
					if (this.topnavs[i].isSelected)
					{
						this.topnavimages[i].src = this.topnavimages[i].imageOn.src;
					}
					else
					{
						this.topnavimages[i].src = this.topnavimages[i].imageOff.src;	
					}
				}
			}.bind(this));
			el.observe('click', function(event){
				this.topnavs[index].blur();
			}.bind(this));
		}, this);
	},
	init: function() {
		this.makeTopNav();
		this.makeMenu();
	}
};
