/* Form Submission */

(function($){
	$.fn.formSubmit = function() {
		return this.each(function(){
					
			var form = $(this);

			form.find('button').click(function(e){
				
				var button = $(this);
				
				// Hide the button
				button.hide();
				
				$('.message').remove();
				
				var string = form.serialize();
				
				$.ajax({
				   type: "GET",
				   url: "/wp-content/themes/eab/eab_actions.php",
				   data: string,
				   success: function(msg){
					 if(msg == "success"){
						form.prepend('<div class="message">Message sent!</div>');
						$('.message').addClass('messageSuccess');
						$('.message').delay(3000).fadeOut();
						
						// Clear the form
						form.find('input').val('');
					 }else{
						
						form.prepend('<div class="message">Message was not sent. <strong>Please correct these errors:</strong><p>'+ msg.replace(/-/g, "<br/>") +'<p></div>');
						$('.message').addClass('messageError');
						 
					 }
					 button.show();
				   },
				   error: function(){
						form.prepend('<div class="message">Could not send your message. Refresh this page and try again</div>');
						$('.message').addClass('messageError');   
				   }
				 });
				
				e.preventDefault();
				
			});
			
		});		
	};	
})(jQuery);

/* Form Steps */

(function($){
	$.fn.Steps = function(options){
		var defaults = {
			submitFunction:function(){}
		}
		var options = $.extend(defaults, options);
		return this.each(function(){
			
			var steps = $(this);
			
			steps.children('.step').hide();
			steps.children('.step').eq(0).show();
			
			var step = 0;
			
			steps.children('.step').children('.next').click(function(){
				
				$('.s').val($('.s').val());
				
				if(step === steps.children('.step').length - 1){
					return false;
				}else{
					step++;
				}
				
				steps.children('.step').hide();
				steps.children('.step').eq(step).fadeIn();
				
			});
			
			steps.children('.step').children('.submit').click(options.submitFunction);
			
		});	
	};
})(jQuery);

/* Testimonials Popup */

(function($){
	$.fn.eabPopup = function(o){

		return this.each(function(){
			

			$(this).find('ul li').click(function(){
				
				url = $(this).find('a').attr('href');
				
				$('body').append('<div class="pu_bg">');
				$('.pu_bg').height($(document).height());
				$('.pu_bg').append('<div class="pu_container">');
				
				
			});
			
			$('.pu_bg').live('click', function(){
				$(this).fadeOut(function(){
					$(this).remove();
				});
			});
			
			
			$(this).find('ul li a').click(function(e){e.preventDefault()});
			$('.pu_container').live('click', function(e){e.stopPropagation()});
			
			
		});	
	};	
})(jQuery);





$(function(){
	
	$('form').formSubmit();
	$('.right').Steps({
		submitFunction:function(){
			string = $('.right *').serialize();
			window.location = "http://energyadvicebureau.com/tools/create_pdf.php?" + string;
		}
	});
	$('.left').Steps({
		submitFunction:function(){
			string = $('.left *').serialize();
			
			$('.result').remove();
			$(this).parent().append('<div class="result">');
			
			$.ajax({
			  type: 'POST',
			  url: "/wp-content/themes/eab/tool_renewal.php",
			  data: string,
			  success: function(data){
				
				parts = data.split('|');
				
				if(parts[1] == '01 Jan 1970' || parts[2] == '01 Jan 1970'){
					
					$('.result').html($('select:eq(1)').val() + ' doesn\'t currently provide ' + $('select:eq(2)').val()).addClass('red');
					
				}else{
				
					if(parts[0] == 'true'){
						$('.result').html('Great News, your renewal window is open, call our team now for the lowest rates available').addClass('green');	
					}else{
						$('.result').html('Unfortunately you can\'t switch at the moment. However, contact us today to see how we can help').addClass('red');
					}
				
				}
				
				  
			  },
			  error: function(){
				alert('bad');  
			  }
			});
			
		}
	});
	
});

// Clock

function updateClock ( )
    {
// This array holds the "friendly" day names
var day_names = new Array(7)
day_names[0] = "Sunday"
day_names[1] = "Monday"
day_names[2] = "Tuesday"
day_names[3] = "Wednesday"
day_names[4] = "Thursday"
day_names[5] = "Friday"
day_names[6] = "Saturday"

// This array holds the "friendly" month names
var month_names = new Array(12)
month_names[0] = "January"
month_names[1] = "February"
month_names[2] = "March"
month_names[3] = "April"
month_names[4] = "May"
month_names[5] = "June"
month_names[6] = "July"
month_names[7] = "August"
month_names[8] = "September"
month_names[9] = "October"
month_names[10] = "November"
month_names[11] = "December"

// Get the current date
date_now = new Date()

// Figure out the friendly day name
day_value = date_now.getDay()
date_text = day_names[day_value]

// Figure out the friendly month name
month_value = date_now.getMonth()
date_text += " " + month_names[month_value]

// Add the day of the month
date_text += " " + date_now.getDate()

// Add the year
date_text += ", " + date_now.getFullYear()

// Get the minutes in the hour
minute_value = date_now.getMinutes()
if (minute_value < 10) {
    minute_value = "0" + minute_value
}

seconds = date_now.getSeconds();
if(seconds.toString().length < 2){
	seconds = '0' + seconds
}

// Get the hour value and use it to customize the greeting
hour_value = date_now.getHours()
if (hour_value == 0) {
   greeting = "Good morning, "
   time_text = " - " + (hour_value + 12) + ":" + minute_value + ":" + seconds + " AM"
}
else if (hour_value < 12) {
    greeting = "Good morning!"
    time_text = " - " + hour_value + ":" + minute_value + ":" + seconds + " AM"
}
else if (hour_value == 12) {
    greeting = "Good afternoon!"
    time_text = " - " + hour_value + ":" + minute_value + ":" + seconds + " PM"
}
else if (hour_value < 17) {
    greeting = "Good afternoon!"
    time_text = " - " + (hour_value - 12) + ":" + minute_value + ":" + seconds + " PM"
}
else {
    greeting = "Good evening!"
    time_text = " - " + (hour_value - 12) + ":" + minute_value + ":" + seconds + " PM"
} 
    $("#clock").html(date_text + time_text);
 
 }


/* Homepage slider JS */

(function($){$.fn.jCarouselLite=function(o){o=$.extend({btnPrev:null,btnNext:null,btnGo:null,mouseWheel:false,auto:null,speed:200,easing:null,vertical:false,circular:true,visible:3,start:0,scroll:1,beforeStart:null,afterEnd:null},o||{});return this.each(function(){var b=false,animCss=o.vertical?"top":"left",sizeCss=o.vertical?"height":"width";var c=$(this),ul=$("ul",c),tLi=$("li",ul),tl=tLi.size(),v=o.visible;if(o.circular){ul.prepend(tLi.slice(tl-v-1+1).clone()).append(tLi.slice(0,v).clone());o.start+=v}var f=$("li",ul),itemLength=f.size(),curr=o.start;c.css("visibility","visible");f.css({overflow:"hidden",float:o.vertical?"none":"left"});ul.css({margin:"0",padding:"0",position:"relative","list-style-type":"none","z-index":"1"});c.css({overflow:"hidden",position:"relative","z-index":"2",left:"0px"});var g=o.vertical?height(f):width(f);var h=g*itemLength;var j=g*v;f.css({width:f.width(),height:f.height()});ul.css(sizeCss,h+"px").css(animCss,-(curr*g));c.css(sizeCss,j+"px");if(o.btnPrev)$(o.btnPrev).click(function(){return go(curr-o.scroll)});if(o.btnNext)$(o.btnNext).click(function(){return go(curr+o.scroll)});if(o.btnGo)$.each(o.btnGo,function(i,a){$(a).click(function(){return go(o.circular?o.visible+i:i)})});if(o.mouseWheel&&c.mousewheel)c.mousewheel(function(e,d){return d>0?go(curr-o.scroll):go(curr+o.scroll)});if(o.auto)setInterval(function(){go(curr+o.scroll)},o.auto+o.speed);function vis(){return f.slice(curr).slice(0,v)};function go(a){if(!b){if(o.beforeStart)o.beforeStart.call(this,vis());if(o.circular){if(a<=o.start-v-1){ul.css(animCss,-((itemLength-(v*2))*g)+"px");curr=a==o.start-v-1?itemLength-(v*2)-1:itemLength-(v*2)-o.scroll}else if(a>=itemLength-v+1){ul.css(animCss,-((v)*g)+"px");curr=a==itemLength-v+1?v+1:v+o.scroll}else curr=a}else{if(a<0||a>itemLength-v)return;else curr=a}b=true;ul.animate(animCss=="left"?{left:-(curr*g)}:{top:-(curr*g)},o.speed,o.easing,function(){if(o.afterEnd)o.afterEnd.call(this,vis());b=false});if(!o.circular){$(o.btnPrev+","+o.btnNext).removeClass("disabled");$((curr-o.scroll<0&&o.btnPrev)||(curr+o.scroll>itemLength-v&&o.btnNext)||[]).addClass("disabled")}}return false}})};function css(a,b){return parseInt($.css(a[0],b))||0};function width(a){return a[0].offsetWidth+css(a,'marginLeft')+css(a,'marginRight')};function height(a){return a[0].offsetHeight+css(a,'marginTop')+css(a,'marginBottom')}})(jQuery);
