$(document).ready(function(){
	
	$('#poll_vote').click(function(){ vote(); return false; });
	
	$('#nav_link_home').mouseover( function(){ old = this.src;
		this.src = 'assets/nav/home_hover.gif'; }
		).mouseout(	function(){ this.src = old; }
	);

	$('#nav_link_about_the_d5').mouseover( function(){ old = this.src;
		this.src = 'assets/nav/about_the_d5_hover.gif'; }
		).mouseout( function(){ this.src = old; }
	);
	
	$('#nav_link_view_clinics').mouseover( function(){ old = this.src;
		this.src = 'assets/nav/view_clinics_hover.gif'; }
		).mouseout( function(){ this.src = old; }
	);

	$('#nav_link_who_we_are').mouseover( function(){ old = this.src;
		this.src = 'assets/nav/who_we_are_hover.gif'; }
		).mouseout( function(){ this.src = old; }
	);

	$('.sb_link').mouseover( function(){ 
		old = this.src.split('_');
		this.src = old[0]+'_hover.gif'; }
		).mouseout(	function(){ this.src = old[0]+'_'+old[1]; }
	);


	$('#d5_1_button').click( function(){ shakeSaltShaker(); return false; } );

	$('.d5_video_link').click( function(){ createVideo(this); return false; } );
	
	$('#calculator_link').click( function(){ calculate(); return false; } );
	
});


function shakeSaltShaker(){
	
	$('#d5_1_pic').animate({ marginTop: "5px" }, 200 );
	$('#d5_1_pic').animate({ marginTop: "-5px" }, 300 );
	$('#d5_1_pic').animate({ marginTop: "5px" }, 200 );
	$('#d5_1_pic').animate({ marginTop: "-5px" }, 300 );
	$('#d5_1_pic').animate({ marginTop: "5px" }, 200 );
	$('#d5_1_pic').animate({ marginTop: "-5px" }, 400 );
	$('#d5_1_pic').animate({ marginTop: "5px" }, 200 );
	$('#d5_1_pic').animate({ marginTop: "0px" }, 100 );
	
	var r=Math.floor(Math.random()*18);
	
	switch(r){
		case 0:
			$('#d5_1_results').html("<h2>Rosemary</h2> Good on: Meats, Fish, Poultry, Dressing, Potatoes, Peas, and Lamb");
			break;
		case 1:
			$('#d5_1_results').html("<h2>Tarragon</h2> Good on: Salad Dressings, Marinades, Sauces, Fish, Meats, and Poultry");
			break;			
		case 2:
			$('#d5_1_results').html("<h2>Cloves</h2> Good on: Fruits, Roasted Meats, Squash and Baked Fish");
			break;
		case 3:
			$('#d5_1_results').html("<h2>Mace</h2> Good on: Hot Breads, Fruit Salads, Fish, and Veal");
			break;
		case 4:
			$('#d5_1_results').html("<h2>Bay Leaves</h2> Good on: Meats, Poultry, Soups, Stews and Spaghetti Dishes");
			break;
		case 5:
			$('#d5_1_results').html("<h2>Garlic (not salt)</h2> Good on: Casseroles, Meats, Salads, Vegetables, and Tomato Dishes");
			break;			
		case 6:
			$('#d5_1_results').html("<h2>Ginger</h2> Good on: Meats, Fish, Poultry, Dressing, Potatoes, Peas, and Lamb");
			break;
		case 7:
			$('#d5_1_results').html("<h2>Marjoram</h2> Good on: Lamb, Salmon, Eggplant, and Green Salad");
			break;
		case 8:
			$('#d5_1_results').html("<h2>Thyme</h2> Good on: Fish, Meats, Poultry, Vegetables, and Rice");
			break;
		case 9:
			$('#d5_1_results').html("<h2>Parsley</h2> Good on: Salads, Broiled Meats, Baked and Stewed Meats, Poultry, Soups, Tomato, and Tomato Sauces");
			break;
		case 10:
			$('#d5_1_results').html("<h2>Oregano</h2> Good on: Soups, Stews, Italian Casserole, Steaks, Seafoods, and Tomatoes");
			break;
		case 11:
			$('#d5_1_results').html("<h2>Basil</h2> Good on: Beef, Lamb, Seafoods, Soups, Stews, Stewed on Fresh Tomatoes, Sauces, and Italian Dishes");
			break;			
		case 12:
			$('#d5_1_results').html("<h2>Curry Powder</h2> Good on: Lamb, Chicken, Fish, Beef, and Vegetables ");
			break;	
		case 13:
			$('#d5_1_results').html("<h2>Mustard(dry)</h2> Good on: Ground Meats, Fish, Salads, Sauces, Beans, and Chicken");
			break;	
		case 14:
			$('#d5_1_results').html("<h2>Lemon or Lime Juice</h2> Good on: Vegetables, Meats, Fish, Seafoods, Salads, and Fruit");
			break;	
		case 15:
			$('#d5_1_results').html("<h2>Allspice</h2> Good on: Stews, Soups, Ground Meats, BBQ Sauce, Tomatoes, Sweet Potatoes and Fish");
			break;	
		case 16:
			$('#d5_1_results').html("<h2>Sage</h2> Good on: Turkey , Dressing, Poultry, and Fish");
			break;	
		case 17:
			$('#d5_1_results').html("<h2>Nutmeg</h2> Good on: Cottage Cheese, Eggs, Fruits and Vegetables");
			break;	
		case 18:
			$('#d5_1_results').html("<h2>Paprika</h2> Good on: Chicken, Fish, Meats, Baked Potatoes, Coleslaw, and Wax Beans");
			break;				
			
		default:
			$('#d5_1_results').html("<h2>Ginger</h2> Good on: Meats, Fish, Poultry, Dressing, Potatoes, Peas, and Lamb");
			break;
	}
}


function createVideo(d5_video_link){
	link = d5_video_link.href+"&hl=en&fs=1";
	//alert(link);
	$('#video_popup').css('display', 'block');
	$('#video_popup_inner').html(
		"<object width='425' height='344'><param name='movie' value='"+link+"&hl=en&fs=1'></param><param name='allowFullScreen' value='true'></param><param name='allowscriptaccess' value='always'></param><embed src='"+link+"&hl=en&fs=1' type='application/x-shockwave-flash' allowscriptaccess='always' allowfullscreen='true' width='425' height='344'></embed></object>"+
		"<div class='text_align_center'><a id='video_popup_close' href='#'>Close this Video</a></div>"
	);	
	$('#video_popup_close').click( function(){ $('#video_popup_inner').html(""); $('#video_popup').css('display', 'none'); });

}

function calculate(){
	val1 = $('#calculate_val_1').val();
	val2 = $('#calculate_val_2').val();
	
	r = (val2 / 20);
	r = r * 365 * val1;
	
	r = Math.round(r*Math.pow(10,2))/Math.pow(10,2);
	
	$('#calculated_result').html("<b>In one year, you could save <u>$"+r+"!</u></b>");
}

function vote(){
	
	poll_inputs = $('.poll_input');
	
	for(i=0; i<poll_inputs.length; i++){
		if(poll_inputs[i].checked){
			selected_vote = poll_inputs[i].value;
		}
	}
	
	$.ajax({
		type: 'POST', url: 'actions/poll.php',
		data: 'selected_vote='+selected_vote+'',
		success: 	function(r){ showVotes(r); },		
		error: 		function(){ $('#error_feedback').append(""); },
		complete: 	function(){ $('#data_table_loading').remove(); }
	});	
}

function showVotes(r){
	

	r = r.split(',');
	$('#poll').html('');
	
	options = new Array();
	
	options[0] = 'Controlling my Blood Pressure';
	options[1] = 'Lowering my Bad Cholesterol';
	options[2] = 'Maintaining my Blood Sugar';
	options[3] = 'Quitting Tobacco';
	options[4] = 'Remembering to Take a Daily Aspirin';
	
	
	$('#poll').append("<br /><h2>Poll Results:</h2>");
	
	
	
	total = 0;
	// Get Total Votes
	for(i=0; i<r.length-1; i++){
		rdata = r[i].split(':');
		total = total + parseInt(rdata[1]);
	}
	
	percent = new Array();
	
	// Calculate percentages
	for(i=0; i<r.length-1; i++){
		rdata = r[i].split(':');
		votes = parseInt(rdata[1]);
		percent[i] = Math.round( ((votes/total) * 100) );
	}
	
	// Display Results
	for(i=0; i<percent.length; i++){
		$('#poll').append("<br /><div style='width: 100px; background: #aaa;'><div style='height: 12px; width: "+percent[i]+"px; background: #fff;'></div></div><b>"+percent[i]+"%</b> - "+options[i]+"<br />");
	}

}