(function($){
	var EDP = {

		init: function(){
            $('.readmore').click(function(e){
                EDP.toggleFamilyContent(this);
            });
            $('.close').click(function(e){
                e.preventDefault();
                EDP.closeAllFamilies();
            });
            this.loadCorrectFamily();
            $(window).bind( 'hashchange', function(){
                EDP.loadCorrectFamily();
            }).trigger( 'hashchange' );
			/* social media sharing */
			$('.facebook > a').click(function (e) {
				e.preventDefault();
				var network = this.hash.substr(1),
					func = social[network];
				if (!(network in social) || !$.isFunction(func)) {
					alert('Invalid social network.');
					return;
				}
				func();
			});
		},

        /**
         * Used for the read more links
         */
        toggleFamilyContent: function(el){
            $(el).hide().parent().addClass('larger').next('.family_content').show('slow', function(){
                // after the content has opened up - then scroll to the top of that content
                var target_offset = $(location.hash).offset();
                var target_top = target_offset.top;
                $('html, body').animate({scrollTop:target_top}, 500);
            });
        },

        /**
         * When clicked from the carousel
         */
        loadCorrectFamily: function(){
            EDP.closeAllFamilies();
//            setTimeout(EDP.setMinimumHeights,500);
            // show the full content of the family if there's a hash ID in the URL
            if(location.hash){
                var hash = location.hash;
                $(hash + ' .readmore').click();
            }
        },

        closeAllFamilies: function(){
            $('.family_content').hide();
            $('.readmore').show();
            $('.larger').each(function(){
                $(this).removeClass('larger');
            });
        },

        setMinimumHeights: function(){
            $('#the-coopers .first').height(161);
            $('#the-lindfields .family').height(146);
            $('#the-coys .family').height(146);
            $('#the-trussels .family').height(150);
            $('#the-bowens .family').height(148);
            $('#the-grieves .family').height(144);
            $('#the-rayners .family').height(144);
            $('#the-motterams .family').height(144);
            $('#the-drucs .last').height(144);
        },
        

        /**
         * Calculator
         */
		calc:{
			init: function(){
				/* only allow numerical input */
				$('input').keydown(function(event) {
					if ( event.keyCode == 46 || event.keyCode == 8 ) {
					} else {
						if (event.keyCode < 95) {
							if (event.keyCode < 48 || event.keyCode > 57 ) {
								event.preventDefault();
							}
						} else {
							if (event.keyCode < 96 || event.keyCode > 105 ) {
								event.preventDefault();
							}
						}
					}
				}); 
						
				/* hide/reset petrol field if groceries is less than $30 spend */
				$('#petrol').hide();	
				$('#input-supermarkets').blur(function(){
					if ($(this).val() > 30) {
						$('#petrol').show();
					} else {
						$('#petrol').hide();
						/* reset values to zero */
						$('#input-petrol').val('0');
						$('#select-petrol').val('');
						$('#output-petrol').text('0');
					}		
				});
				
				$("input").blur(function(){
					var id = $(this).attr("id").replace("input-", "");
//					var v = Number($(this).val().replace(",", ""));
					/* average spend per transaction */
					var v = Number($(this).val());
					v = Math.floor(v);
					/* frequency of spend */
					var s = Number($("#select-"+id).val());
					/* threshold for spend */
					var th = Number($("#threshold-"+id).html());
					/* output value */
					var r = 0;
					if(v > th && s > 0){
						r = (v - th) * s;
					}
					$("#output-"+id).html(r);
				});
				
				$("select").change(function(){
					var id = $(this).attr("id").replace("select-", "");
					var v = Number($("#input-"+id).val());
					v = Math.floor(v);
					var s = Number($(this).val());
					var th = Number($("#threshold-"+id).html());
					var r = 0;
					var cc = 0;
					if(v > th && s > 0){
						if (id == 'petrol') {
							r = v * 2 * s;
							cc = v * 1.3 * s;
						} else {
							r = (v - th) * s;
							cc = v * s;
						}
					}
					$("#output-"+id).html(r);
					$("#cc-"+id).html(cc);
					
					/* tally total points calculated annually */
					var ap = 0;
					$(".cc").each(function(){
						ap += Number($(this).html());
					});
					$("#annual-points").html(ap);						
				});
					
				/* if not using everyday rewards card */				
				$("#qff_no").click(function() {						
					$("#annual-points").html('0');	
				});		
				/* tally total points calculated annually */
				$("#qff_yes").click(function() {
					var ap = 0;
					$(".cc").each(function(){
						ap += Number($(this).html());
					});
					$("#annual-points").html(ap);					
				});		
				
				$(".calc_back_btn").click(function(e){
				    pageTracker._trackEvent("back to calculator", "click");
			  	});
			  	
				$(".see > a").click(function(e){
				    pageTracker._trackEvent("see other rewards", "click");
			  	});			  	
				
				/* on submit, calculate total QFF points earned */
				$("#action-submit").click(function(e){
				    pageTracker._trackEvent("submit", "click");
				  
				  $("#top_newcalculator").hide();
				  
					e.preventDefault();
					var r = 0;
	          		$(".annual-point").each(function(){
						r += Number($(this).html());
					});
					var cc = 0;
					$(".cc").each(function(){
						cc += Number($(this).html());
					});
					
					if(r < 0) r = 0;
					if(cc < 0) cc = 0;
					
					if ($("#qff_no:checked").length) {						
						$("#qff-point").html(r);	
					} else {
						$("#qff-point").html(r + cc);
					}
					
					/* gift card bands */
					var totalPts = parseInt($("#qff-point").text(), 10);
					
					if(totalPts<6750) {
				    $("#calculator_points1_count").text(totalPts);
					  $("#top_newcalculator_2").show();
					} else {					  
					  $("#calculator_points2_count").text(totalPts);
					  var pointsList = {
//	  						6750: ['bigw', 'dicksmith'],
//	  						7250: ['wish', 'woolworths', 'caltex', 'bws'],
//	  						13500: ['bigw'],
//	  						14500: ['bws','wish','woolworths','caltex','dicksmith'],
//	  						33750: ['bigw'],
//	  						36250: ['caltex','wish','woolworths','bws','dicksmith'],
//	  						67500: ['bigw'],
//	  						72500: ['woolworths','caltex','dicksmith','wish','bws']
							3750: '$25',
	  						6750: '$50',
	  						7250: '$50',
	  						13500: '$100',
	  						14500: '$100',
	  						33750: '$250',
	  						36250: '$250',
	  						67500: '$500',
	  						72500: '$500'
	  					}
	  					cards_html="";
	  					$.each(pointsList, function (pts, cardList) {
	  						if (totalPts > pts) {
	  							var cardIdx = Math.floor(Math.random() * cardList.length);	
	  							cards_html+='<div id="card_'+pts+'"><img src="images/card_'+pts+'.gif" width="70" height="45" /><br />'+cardList+'<br />'+pts+' points</div>';
	  						}
	  					});
	  					document.getElementById('points_card_list').innerHTML = cards_html;
	  					$("#top_newcalculator_3, .see").show();
					}					
					return false;
				});
				
//				$('#about-calc').hide();
				$('.read-more').click(function(e){
					$('#about-calc').slideToggle();
					$(this).hide();
					e.preventDefault();					
				});
				$('.close a').click(function(e){
					$('#about-calc').slideToggle();
					$('.read-more').fadeIn();
					e.preventDefault();
				});
			}
		}

	};
	window.EDP = EDP;
})(jQuery);
