var timeout    = 500;
var closetimer = 0;
var ddmenuitem = 0;

jQuery().ready(function() {
    $('#header ul ul').css('display', 'none');
    
    $('#navigation > li').bind('mouseover', jsddm_open);
    $('#navigation > li').bind('mouseout',  jsddm_timer);

    $('#sidebar a').lightBox();

    $('#dealers .images a').lightBox();

    $('#cities').change(function() {
        window.location = _root + 'where_to_buy?city=' + $(this).val();
    });

    if ($('#logos').length) {
        $('#logos').jcarousel({
            auto: 2,
            wrap: 'last',
            initCallback: mycarousel_initCallback
        });
    };
});


function jsddm_open()
{
   jsddm_canceltimer();
   jsddm_close();
   ddmenuitem = $(this).find('ul').css('display', 'block');
}

function jsddm_close()
{  
    if(ddmenuitem) ddmenuitem.css('display', 'none');
}

function jsddm_timer()
{  
    closetimer = window.setTimeout(jsddm_close, timeout);
}

function jsddm_canceltimer()
{
    if(closetimer)
    {
        window.clearTimeout(closetimer);
        closetimer = null;
    }
}

document.onclick = jsddm_close;



// Load Flash function
function loadFlash( fname, w, h, params )
{
	var add_params = '';
	
	if ( params )
	{
		for( param in params ) {
			add_params += '<param name="'+param+'" value="'+params[param]+'" />';
		}
	}
	
	var Flash = ' \
		<object type="application/x-shockwave-flash" data="' + fname + '" width="' + w + '" height="' + h + '"> \
		<param name="quality" value="high" /> \
		<param name="scale" value="noscale" /> \
		' + add_params + ' \
		<param name="movie" value="' + fname + '" /> \
		</object> \
	';
	
	document.write( Flash );
}

jQuery.fn.fadeToggle = function(speed, easing, callback) {
   return this.animate({opacity: 'toggle'}, speed, easing, callback);
};

function order_it(speed)
{
    if (typeof(speed) == 'undefined') { speed = 'slow'; }
    
    $('#sum, #orders').hide(speed);
    $('#the_order').show(speed);
}

function order_back()
{
    $('#the_order').hide('slow');
    $('#sum, #orders').show('slow');
}

function mycarousel_initCallback(carousel)
{
    // Disable autoscrolling if the user clicks the prev or next button.
    carousel.buttonNext.bind('click', function() {
        carousel.startAuto(0);
    });

    carousel.buttonPrev.bind('click', function() {
        carousel.startAuto(0);
    });

    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });
};
