/*************************************************************************************************
	
2010-07-02	TM	Added Showroom dropdown menu functionality

*************************************************************************************************/

/***********************************************/
// set global scope variables

var plow_group_array_interface = new Array( 6 ),
	plow_group_registry_interface = new Array( 6 ),
	file_count = 1;
	
/***********************************************/
// document ready, apply behaviors

$( document ).ready(
function()
{
	
	/***********************************************/
	// showroom dropdown
	$( '#nav_showroom').hover(
	function()
	{
		show_showroomdropdown();
	}, function()
	{
		hide_showroomdropdown();
	}
	);
	
	$( '#showroom_dropdown').hover(
	function()
	{
		show_showroomdropdown();
	}, function()
	{
		hide_showroomdropdown();
	}
	);
	
	/***********************************************/
	// set-up plow selector
	
	// content placement
	$( '#showroom_dropdown .selector_link' ).each(
	function ()
	{
		
		// set group number
		$( this ).data( 'group_number', ( $( this ).attr( 'alt' ) * 1 ) );
		
		// add object to registry
		plow_group_registry_interface[ $( this ).data( 'group_number' ) ] = $( this );
		
		// set array variable
		plow_group_array_interface[ $( this ).data( 'group_number' ) ] = 0;
		if ( $( this ).data( 'group_number' ) == 1 )
			plow_group_array_interface[ $( this ).data( 'group_number' ) ] = 1;
		
		// set click functionality
		$( this ).click(
		function ()
		{
			
			toggle_group_interface( $( this ) );
			
		}
		);
		
	}
	);
	
	updateProductDisplay_interface();
	
	// privacy statement modal window set-up
	$( '#modal-privacy' ).dialog(
	{
		width: 600,
		height: 350,
		modal: true,
		autoOpen: false,
		closeOnEscape: true,
		buttons: {
			"CLOSE": function() { $( this ).dialog( 'close' ); },
			"PRINT": function() { window.open( branding_path_js + '/privacy_print.php', 'privacy_policy', 'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width=600,height=350' ); }
		}
	}
	);
	
	// privacy statement modal open
	$( '#launch-privacy' ).click(
	function()
	{
		
		$( '#modal-privacy' ).dialog( 'open' );
		
	}
	);
	
	// legal modal window set-up
	$( '#modal-legal' ).dialog(
	{
		width: 600,
		height: 350,
		modal: true,
		autoOpen: false,
		closeOnEscape: true,
		buttons: {
			"CLOSE": function() { $( this ).dialog( 'close' ); },
			"PRINT": function() { window.open( branding_path_js + '/legal_print.php', 'legal_information', 'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width=600,height=350' ); }
		}
	}
	);
	
	// legal modal open
	$( '#launch-legal' ).click(
	function()
	{
		
		$( '#modal-legal' ).dialog( 'open' );
		
	}
	);
	
	// pricing modal window set-up
	$( '#modal-pricing' ).dialog(
	{
		width: 400,
		height: 300,
		modal: true,
		autoOpen: false,
		closeOnEscape: true,
		buttons: {
			"Return to Contact Information page": function() { $( this ).dialog( 'close' ); }
		}
	}
	);
	
	// pricing modal open
	$( '#launch-pricing' ).click(
	function()
	{
		$( '#modal-pricing' ).dialog( 'open' );
	}
	);
	
	// tech info modal window set-up
	$( '#modal-tech' ).dialog(
	{
		width: 600,
		height: 450,
		modal: true,
		autoOpen: false,
		closeOnEscape: true,
		buttons: {
			"Return to Contact Information page": function() { $( this ).dialog( 'close' ); }
		}
	}
	);
	
	// tech info modal open
	$( '#launch-tech' ).click(
	function()
	{
		$( '#modal-tech' ).dialog( 'open' );
	}
	);
	
	// tech info modal window set-up
	$( '#modal-msds' ).dialog(
	{
		width: 600,
		height: 400,
		modal: true,
		autoOpen: false,
		closeOnEscape: true,
		buttons: {
			"Return to Contact Information page": function() { $( this ).dialog( 'close' ); }
		}
	}
	);
	
	// tech info modal open
	$( '#launch-msds' ).click(
	function()
	{
		$( '#modal-msds' ).dialog( 'open' );
	}
	);
	
	// Distributor Request Form
	if ( document.getElementById('cur_snow_y') != null ) {
		toggle_me( 'cur_snow_y', $('#distributor_form_content')[0].cur_snow, $('#snow_toggle') );
		toggle_me( 'cur_salt_y', $('#distributor_form_content')[0].cur_salt, $('#salt_toggle') );
		$('#cur_snow_y').change(function()
		{
			toggle_me( 'cur_snow_y', $('#distributor_form_content')[0].cur_snow, $('#snow_toggle') );
		});
		$('#cur_snow_n').change(function()
		{
			toggle_me( 'cur_snow_y', $('#distributor_form_content')[0].cur_snow, $('#snow_toggle') );
		});
		$('#cur_salt_y').change(function()
		{
			toggle_me( 'cur_salt_y', $('#distributor_form_content')[0].cur_salt, $('#salt_toggle') );
		});
		$('#cur_salt_n').change(function()
		{
			toggle_me( 'cur_salt_y', $('#distributor_form_content')[0].cur_salt, $('#salt_toggle') );
		});
	}
	
	$('#add_files').click(
		function()
		{
			if ( $('#file_'+file_count).val() != '' && file_count < 20 )
			{
				ele = $('#file_'+file_count).clone();
				file_count++;
				ele.attr( 'id', 'file_'+file_count );
				ele.attr( 'name', 'file_'+file_count );
				$('#file_container').prepend( ele );
				if( file_count != 2 )
					$('#file_'+file_count).after( '<br/>' );
			}
		}
	);

	
}
);

/***********************************************/
// showroom dropdown functions

function show_showroomdropdown ()
{
	
	$( '#showroom_dropdown').show( 0 );
	$( 'a#nav_showroom').css( 'background-position', '-55px -47px' );
	
}

function hide_showroomdropdown ()
{
	
	$( '#showroom_dropdown').hide( 0 );
	$( 'a#nav_showroom').css( 'background-position', '-55px 0px' );
	
}

function toggle_group_interface ( selector_object, force_state )
{
	
	// set all groups to off
	var array_length = plow_group_array_interface.length;
	for ( a=1; a<array_length; a++ )
	{
		
		plow_group_array_interface[ a ] = 0;
		
	}
	
	// set all display to passive
	$( '#showroom_dropdown li a.selector_link' ).removeClass( 'selector_link_active' );
	$( '#showroom_dropdown li a.selector_link' ).addClass( 'selector_link_passive' );
	
	// set current group to on
	plow_group_array_interface[ selector_object.data( 'group_number' ) ] = 1;
	
	// apply active class, remove passive
	selector_object.addClass( 'selector_link_active' );
	selector_object.removeClass( 'selector_link_passive' );

	updateProductDisplay_interface();
	
}

function updateProductDisplay_interface(){
	var activeSelectorLinks = $( '#showroom_dropdown #plow_selector .selector_link.selector_link_active' ),
		products = $('#showroom_dropdown div#plow_display a');

	// Set all item's 'can_show' flag to false
	products.data('can_show', false);

	activeSelectorLinks.each(function(){
		products
			.filter( '.group_' + $( this ).data( 'group_number' ) )
			.data( 'can_show', true );
	});

	// Loop over each product and show it if it's flagged
	products.each(function(){
		var $this = $(this);
		if($this.data('can_show'))
		{
			//$this.removeClass('plow_item_passive');
			$this.show( 0 );
		}
		else
		{
			//$this.addClass('plow_item_passive');
			$this.hide( 0 );
		}
	});
}

/***********************************************/
// Distributor form utility function

function toggle_me( true_ele_id, group_name, ele_to_toggle ){
	//Radio buttons are finicky ... for most forms, a simple checkbox would make this a lot simpler
	var gotcha = false;
	for ( var i=0; i<group_name.length; i++ ){
		if ( group_name[i].id == true_ele_id && group_name[i].checked == true ){
			gotcha = true;
		}
	}
	if ( gotcha ) 
		ele_to_toggle.show();
	else 
		ele_to_toggle.hide();
}


