// Homepage Javascript Functions 
$(document).ready(function(){

	/* Disable Flash for IE7 */
	/*************************/
	var IE7 = navigator.appVersion.indexOf("MSIE 7.") != -1;
	if (IE7) {
		$('#flashContent').html('<div id="coopGrowthCon"><a href="coOpGrowth.html"><img src="http://www.afmcoop.com/images/noFlashPanels/coopGrowth.png" alt="Co-op Growth" /></a></div><div id="ourBrandsCon"><a href="ourBrands.html"><img src="http://www.afmcoop.com/images/noFlashPanels/ourBrands.png" alt="Our Brands" /></a></div><div id="productsAndServicesCon"><a href="productsAndServices.php"><img src="http://www.afmcoop.com/images/noFlashPanels/productsAndServices.png" alt="Products and Services" /></a></div><div id="aboutUsCon"><a href="aboutUs_missionStatement.html"><img src="http://www.afmcoop.com/images/noFlashPanels/aboutUs.png" alt="About Us" /></a></div><div id="retailerAlliancesCon"><a href="alliances.html"><img src="http://www.afmcoop.com/images/noFlashPanels/alliances.png" alt="Alliances" /></a></div>');
	}

	/* News box functions */
	/**********************/
	
		/* Display Javascript controls is Javascript is available */
		$("#newsControlsCon").css({"visibility":"visible"});

	/* News box functions */
	/**********************/
	
		$('.dynamicNewsArea').cycle({
			fx: 'scrollHorz',
			pause: true,
			timeout: 10000,
			next: '#newsNextButton',
			prev: '#newsPrevButton'
		});



	/* Privileges of Ownerhsip Panel */
	/*********************************/

		$('.slideshow').cycle({
			fx: 'fade',
			pause: true,
			next: '#privOfOwnershipNext',
			prev: '#privOfOwnershipPrev',
			after: privOwnershipAfter
		});


		// Slide number caption
		function privOwnershipAfter(curr,next,opts) {
			var caption = (opts.currSlide + 1) + ' / ' + opts.slideCount;
			$('#privOfOwnershipCaption').html(caption);
		}

		// Mouse over slide descriptions
		$('.privOfOwnershipSlide').hover(function(){
			$('.privOfOwnershipInfo', this).animate({'opacity':.85}, 250);
		},function(){
			$('.privOfOwnershipInfo', this).animate({'opacity':0}, 250);
		});

		function addCommas(nStr)
		{
			nStr += '';
			x = nStr.split('.');
			x1 = x[0];
			x2 = x.length > 1 ? '.' + x[1] : '';
			var rgx = /(\d+)(\d{3})/;
			while (rgx.test(x1)) {
				x1 = x1.replace(rgx, '$1' + ',' + '$2');
			}
			return x1 + x2;
		}

		function patronageSubmit(e) {
			var rebateRate = 3.5, input, result;
	
			input = parseInt($('input#weeklyPurchaseAmt').val().replace(",", ""), 10);
			
			if (isNaN(input)) {
				$('div#patronageRebateResult').html('Try Again.');
				$('input#weeklyPurchaseAmt').val('');
			} else {
				result = addCommas(parseInt(((input * rebateRate) * 52 * .01), 10));
				$('div#patronageRebateResult').html('$' + result);
				$('span#totalPurchaseAmt').html('$' + addCommas(parseInt((input * 52), 10)));
			}

		}

		$('#patronageForm').submit(function(e){
			patronageSubmit();
			$('#patronageFormControls').fadeOut("fast");
			$('#patronageResults').fadeIn("fast");
			e.preventDefault();
		});

		$('#patronageDefaultLayer').click(function(e) {
			$('#patronageDefaultLayer').fadeOut("fast");
			$('#patronageInteractiveLayer').fadeIn("fast");
			$('input#weeklyPurchaseAmt').focus();
		});
	
		$('#patronageCalcBtn').click(function(e) {
			patronageSubmit(e);
			$('#patronageFormControls').fadeOut("fast");
			$('#patronageResults').fadeIn("fast");
		});

		$('#patronageBackToStart').click(function() {
			$('#patronageInteractiveLayer').fadeOut("fast");
			$('#patronageDefaultLayer').fadeIn("fast");
		});

		$('#patronageBackToCalc').click(function() {
			$('#patronageResults').fadeOut("fast");
			$('#patronageFormControls').fadeIn("fast");
		});
	

});
