function search_keypress(e, control, decimals) {
	var key = (window.event) ? event.keyCode : e.which;
	if (window.event) key = event.keyCode
	else key = e.which
	if (key == 10 || key == 13) $('mini_search_form').submit();
}
		

function initMenu(){
	var nodes = document.getElementById("menu").getElementsByTagName("li");
	var _timerr;
	for (var i=0; i<nodes.length; i++)
	{
		if (nodes[i].parentNode.id=='menu') nodes[i].onmouseover = function()
		{
			var __this = this;
			_timerr = setTimeout(function(){
				if(__this.className.indexOf('hover')==-1) __this.className += " hover"
			}, 400);
		}
		nodes[i].onmouseout = function()
		{
			var __this = this;
			__this.className = __this.className.replace("hover", "");
			if(_timerr) clearTimeout(_timerr);
		}
	}
	for (var i = 0; i < nodes.length; i++)
	{
		var drops = [];
		var drop = nodes[i].getElementsByTagName("div");
		for (var j = 0; j < drop.length; j++)
		{
			if (drop[j].className.indexOf("drop-box-top") != -1)
			{
				drops.push(drop[j]);
				drop[j].drops = drops;

				drop[j].onmouseover = function ()
				{
					if(_timerr) clearTimeout(_timerr);
					this.parentNode.className += ' hover';
				}
			}
		}
	}
}

var transparentImage = "images/none.gif";
function fixTrans()
{
	if (typeof document.body.style.maxHeight == 'undefined') {

	var imgs = document.getElementsByTagName("img");
	
	for (i = 0; i < imgs.length; i++)
	{	
		if (imgs[i].src.indexOf(transparentImage) != -1)
		{
			return;
		}

		if (imgs[i].src.indexOf(".png") != -1)
			{
				var src = imgs[i].src;
				imgs[i].src = transparentImage;
				imgs[i].runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "',sizingMethod='scale')";
				imgs[i].style.display = "block";
			}
		}	
	}
}

	
function initSrc(){
	var _inputs = document.getElements('.search-box input');
	for(i=0;i<_inputs.length;i++){
		if(_inputs[i].className.indexOf('src-input')!=-1) var _btn1 = _inputs[i];
		if(_inputs[i].className.indexOf('inp-src')!=-1) var _btn2 = _inputs[i];
		_inputs[i].val = _inputs[i].value;
		_inputs[i].onfocus = function(){
			if(this.value == this.val) this.value = '';
		}
		_inputs[i].onblur = function(){
			if(this.value =='') this.value = this.val;
		}
	}
	if(_btn1 && _btn2){
		var _hold = _btn1.parentNode.parentNode.parentNode.parentNode.parentNode;
		_btn1.onfocus = function(){
			_hold.className += ' search-active-parent';
			_btn2.focus();
		}
		var btn_close = _hold.getElementsByTagName('a');
		for(var i = 0; i < btn_close.length; i++){
			if(btn_close[i].className.indexOf('btn-close') != -1){
				btn_close[i].onclick = function(){
					_hold.className = _hold.className.replace('search-active-parent','');
					return false;
				}
			}
		}
	}
}


var scrolling = false;

function shift_features_left() 
{
	if (scrolling == true) return false;
	else scrolling = true;
	var element = $('feature_glideinner').getFirst();
	var new_element = element.clone();
	new Fx.Morph( element, { 
		duration: 1000,
		onComplete: function( ) {
			element.destroy();
			new_element.injectInside($('feature_glideinner'));
			scrolling = false;
	}}).start( {'width' :0} );
}

function shift_features_right() 
{
	if (scrolling == true) return false;
	else scrolling = true;
	var element = $('feature_glideinner').getLast();
	var new_element = element.clone();
	var width = element.getFirst().getSize().x;
	element.destroy();	
	
	new_element.injectTop($('feature_glideinner'));
	new_element.setStyle('width','0px');

	new Fx.Morph( new_element, { 
		duration: 1000,
		onComplete: function( ) {
			scrolling = false;
	}}).start( {'width' :width} );
}

var shift_features = function(){ shift_features_left(); };

window.addEvent('domready', function( ) {
	
	initMenu();
	initSrc();
	fixTrans();
	
	$$( '#frequently_asked_questions a.toggle_link' ).each( function( el ) {
	    	
	        // Remove the hidden class 
	        var answer = el.getParent( ).getNext( );
	        answer.removeClass( 'faq_answer_hiden' );
	        answer.addClass( 'faq_answer_visible' );
	        
	        // Put the div in a toggle slide thing
	        var fx = new Fx.Slide( answer );
	        fx.hide( );
	        
	        // CLicking the link should toggle it
	        el.addEvent( 'click', function( e ) {
	        
	            e.stop( );
	            fx.toggle( );
	        
	        }.bindWithEvent( ) );
	    
	} );    
	
    // Get a list of categories
    var categories = document.getElements( '#subpage_content #classification_list div' );

    // Loop through them
    categories.each( function( el )
    {

        // Get the list of links to subcategories
        var links = el.getElement( '.subcategories' );

        // Check they are present
        if ( $defined( links ) )
        {

            // Create a slide effect and don't wait for previous ones to finish,
            // otherwise moving across multiple categories quickly could mean
            // the one you end up over won't start because the previous slides
            // haven't finished.
            var slide = new Fx.Slide( links, { wait: false } );
            var allowSlideIn = false;

            el.addEvent( 'hide', function( e ) {

                slide.slideOut( );

            } );

            var toggleLink = el.getElement( '.show_subcategories a' );

            if ( $defined( toggleLink ) )
            {

                toggleLink.addEvent( 'click', function( e ) {

                    new Event( e ).stop( );

                    slide.toggle( );

                    // Get all of the siblings of this element and make sure
                    // that the mouseleave event was fired.
                    var siblings = el.getParent( ).getChildren( );
                    siblings.each( function( elSib )
                    {

                        if ( elSib != el )
                        {

                            elSib.fireEvent( 'hide' );

                        }

                    } );

                    if ( toggleLink.get('text') == 'Click For More Options' )
                    {

                        toggleLink.set('text', 'Click To Hide Options' );

                    }
                    else
                    {

                        toggleLink.set('text', 'Click For More Options' );

                    }

                } );

                // Hide it to start with
                slide.hide( );
                links.setStyle( 'display', 'block' );

            }

        }

    } );	
	
	if ($defined($('feature_glideinner'))) {
		shift_features.periodical(10000); 
	}
} );

window.addEvent("unload", function(){
	$clear(shift_features);
});

