/*
  moopop: unobtrusive javascript popups via late binding using mootools 1.2
  
  copyright (c) 2007-2008 by gonchuki - http://blog.gonchuki.com
  
  version:	1.1
  released: June 23, 2008
  
  This work is licensed under a Creative Commons Attribution-Share Alike 3.0 License.
    http://creativecommons.org/licenses/by-sa/3.0/
*/

var moopop={width:0,height:0,captureByRel:function(attrVal,parent){this.capture((parent||document).getElements('a[rel*='+(attrVal||'popup')+']'));},capture:function(el,width,height){if($defined(width)&&$defined(height)){this.width=width;this.height=height;}
switch($type(el)){case'string':el=$$(el);case'element':case'array':$splat(el).each(this.add_pop_to,this);}
this.width=null;this.height=null;},add_pop_to:function(el){el.addEvent('click',function(e){e.stop();this.popup(el);}.bind(this));var size=el.get('rel').match(/\[(\d+),\s*(\d+)/)||['',this.width,this.height];var resizable=el.get('rel').match(/,(r)/)||[];if(size[1])el.store('popupprops','width='+size[1]+', height='+size[2]+(resizable[1]?', scrollbars=yes, resizable=yes':''));},popup:function(el){window.open(el.get('href'),el.get('name')||'',el.retrieve('popupprops')||'');}};window.addEvent('domready',function(){moopop.captureByRel('popup');});

/*
	@Class:		InputClearAndReplace
	@Author:		Brandon Gray for O3 World 2008
	@Brief:		Loops through all inputs/textareas with a class name of 'clear_replace' and clears the default text when focused
				when focus is lost it checks to see if the field is blank (no new data entered), if value is blank the default text is placed back in
	@Example: 	<input type="text" name="firstname" id="firstname" value="" class="clear_replace" />
	@Requirements:	Mootools 1.1			
*/

//var relative_path = "/fruitflowers/trunk/";
//var relative_path = "/trunk/";
var relative_path = "/";

var InputClearAndReplace = new Class ({
	
	options: {
		
		InputElement: '.clear_replace'
		
	},
	
	initialize: function()  {
		
		var inputElementList = $$( this.options.InputElement );
		var originalValue = new Array();
		
		inputElementList.each( function( element, i ) {
			
			originalValue.push( inputElementList[ i ].value );
			
			element.onfocus = function() {
				
				if( this.value == originalValue[ i ] ) this.value = '';
				
			},
			
			element.onblur = function() {
				
				if( this.value == '' ) this.value = originalValue[ i ];
				
			}
			
		});
		
	}

});

/*

	@Class:		CharCount
	@Author:		Brandon Gray for O3 World 2008
	@Brief:		Limits the amount of characters that can be entered into a textarea element.  Looks for the class 'char_limit' and assigns a limit to that textarea element
				based on the number included in the textarea's 'label' attribute.  The limit is counted down within a corresponding element with a class of 'char_count'
	@Requirements:	Mootools 1.2
				The textarea must have a 'label' attribute with a number included in it.  This will be the maximum amount of characters you wish to allow.
				The textarea must have a class of 'char_limit' and a unique 'ID'.
				There must be an element (div, span, p, etc.) to display the amount of remaining characters.  It should have a class of 'char_count'
	@Example:		<div class="input_wrap">
					<textarea id="comment" name="comment" rows="" cols="" class="char_limit" label="1000"></textarea>
					<p><div class="char_count"></div>. (HTML and URLs prohibited)</p>
				</div>
			
*/

var CharCount = new Class({

	initialize: function() {
		
		var inputElement = $( 'card_message' );
		var counterElement = $( 'count' );
		var max = 250;
			
		inputElement.addEvent( 'keydown', this.onKeyPress.bindWithEvent( this, [ inputElement, max, counterElement ] ) );
		inputElement.addEvent( 'keyup', this.onKeyPress.bindWithEvent( this, [ inputElement, max, counterElement ] ) );
			
		this.updateCount( inputElement, max, counterElement );
	
	},
	
	onKeyPress: function( event, target, max, counterElement ) {
		
		event = new Event( event );
		if ( !event.shift && !event.control && !event.alt && !event.meta ) this.updateCount( target, max, counterElement );
		
	},
	
	updateCount: function( target, max, counterElement ) {
	
		if ( target.value.length > max ) {
			
			target.value = target.value.substring( 0, max );
			
		}
		
		var count = max - target.value.length;
		
		if ( count == 1 ) {
		
			var string = '<strong>1</strong> character left';
		
		} else {
		
			var string = '<strong>' + count + '</strong> characters left';
		
		}
		
		counterElement.set( 'html', string );
		
	}
  
});

function dropEffect() {
	
	var menuItems  = $$( '#nav li.drop' );
	
	menuItems.each( function( menuItem, i ) {
		
		var ul = menuItem.getElement( 'ul' );
		
		ul.set({
			styles: { opacity: 0 },
			tween:  { 
				duration: 250,
				onComplete: function( event ) {
					
					if ( ul.getStyle( 'opacity' ) == 0 ) {
						ul.setStyle( 'display', 'none' );
					}
					
				},
				onStart: function( event ) {
					
					if ( ul.getStyle( 'opacity' ) == 0 ) {
						ul.setStyle( 'display', 'block' );
					}
					
				}
			}
		});
		
		menuItem.addEvents({
			
			'mouseenter' : function() {
				
				if ( Browser.Engine.trident4 ) {
					
					ul.setStyles({
						display: 'block',
						opacity: 1
					});
					
					if ( 'find' ) { $( 'find' ).getElement( 'select' ).setStyle( 'display', 'none' ); };
					
				} else {
					
					ul.tween( 'opacity', 1 );
					
				};
				
			},
			'mouseleave' : function() {
				
				if ( Browser.Engine.trident4 ) {
					
					ul.setStyles({
						display: 'none'
					});
					
					if ( 'find' ) { $( 'find' ).getElement( 'select' ).setStyle( 'display', 'block' ); };
				
				} else {
					
					ul.tween( 'opacity', 0 );
					
				};
				
			},
			'focus' : function() {
				menuItem.fireEvent( 'mouseenter' );
			},
			'blur' : function() {
				menuItem.fireEvent( 'mouseleave' );
			}
			
		});
		
	});
	
}

window.addEvent( 'domready', function() {

	// Drop down functionality
	dropEffect( );
	
	// Store locator functionality
	var isLocatorOpen = false;
	
	if( $( 'store_locator' ).getElement( 'a.find' ) ){
		
		$( 'store_locator' ).getElement( 'a.find' ).addEvent( 'click', function( event ) {
			
			var slideLocator = new Fx.Tween( $( 'store_locator' ) );
			
			if ( !isLocatorOpen ) {
				
				slideLocator.start( 'top', '0px' );
				isLocatorOpen = true;
				this.setStyle( 'background-image', 'url(images/icon_arrow_white_up.gif)' );
				
			} else {
				
				slideLocator.start( 'top', '-43px' );
				isLocatorOpen = false;
				this.setStyle( 'background-image', 'url(images/icon_arrow_white_down.gif)' );
				
			}
			
			event.stop();
			
		});
		
	};
	
	// Instantiate the InputClearAndReplace Class
	inputList = new InputClearAndReplace();
	
	// Flash cycle for homepage
	if ( $( 'flash_replace' ) ) {
		
		var flashvars = {};
		flashvars.XMLLocation = relative_path + 'data/home_cycle.cfm';
		var params    = {};
		params.wmode = "transparent";
		params.menu = "false";
		params.allowscriptaccess = "always";
		var attributes = {};
		swfobject.embedSWF("flash/home_cycle.swf", "flash_replace", "749", "330", "9.0.0", "", flashvars, params, attributes);
		
	};
	
	// Initiate character count on shipping info pages
	if ( $( 'card_message' ) && $( 'card_message' ).length != 0 ) {
	
		var LimitTextAreas = new CharCount();
		
	};
	
});

