MMSGHome = {
	banner: {
		currentDot: 0,
		gotoDot: function (dot) {
			if (dot != this.currentDot) {
				$("#BannersNav li.active").removeClass('active');
				$(this.selector+' li.active').fadeOut(400, function() {
					$(this).removeClass('active');
				});
				$('#nav_'+dot).addClass("active");
				$('#banner_'+dot).fadeIn(700, function() {
					$('#banner_'+dot).addClass("active");
					$('#nav_'+dot).addClass("active");

				});
				this.currentDot = dot;				
			}
			this.restartTimer();
		},
		goNext: function() {
			if (this.currentDot == (this.dotLength - 1)) {
				this.gotoDot(0);
			} else {
				this.gotoDot( parseInt(this.currentDot, 10)+1);
			}
			// console.log(this.currentDot);
		},
		init: function (selector) {
			this.selector = selector;
			var navDots = "";
			$(selector+' li').each(function(index) {
				$(this).attr("id","banner_"+index);
				navDots += '<li id="nav_'+index+'"></li>';
			});
			this.dotLength = $(selector+' li').size();
			this.currentDot = this.dotLength - 1;
			$(selector+'Nav').html(navDots);
			$(selector+'Nav li').click(function() {
				var thisId = $(this).attr("id");
				thisId = thisId.replace("nav_","");
				MMSGHome.banner.gotoDot(thisId);
			});
			this.timer = setTimeout( function () {
				MMSGHome.banner.goNext();
			},6000);
		},
		restartTimer: function () {
			clearTimeout(this.timer);
			this.timer = setTimeout( function () {
				MMSGHome.banner.goNext();
			},5000);
		},
		stopTimer: function () {
			clearTimeout(this.timer);
		}
		
	},
	contactus: {
		show: function() {
			$("#BannerContainer").hide();
			$("#ContactUs").show();			
			$('#FooterLinks li:eq(1) a').addClass('selected');
		},
		hide: function() {
			$("#BannerContainer").show();
			$("#ContactUs").hide();
			$('#FooterLinks li:eq(1) a').removeClass('selected');	
		},
		validate: function (input, validateBy) {
            var regx = {
                        contact : /(^[689]{1}[0-9]{7}$)/,
                        mobile  : /(^[89]{1}[0-9]{7}$)/,
                        email: /^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i            };
            var returnVal = null;
            switch(validateBy) {
            case 'mobile_no':
                returnVal = regx.mobile.test(input);
                break;
            case 'office_no':
            case 'home_no':
                returnVal = regx.contact.test(input);
                break;
            case 'email_address':
                returnVal = regx.email.test(input);
                break;
            default:
                returnVal = (input != "") ? true : false;
                break;
            }
            return returnVal;
        },
		send: function () {
			
			var returnValue = false;
			var contactExists = false;
			var sendable = true;
			var contact = {
				"mobile_no": $("#mobile_no").val(),
				"office_no": $("#office_no").val(),
				"home_no": $("#home_no").val()
			};
			var selectorArray = {
				"name": $("#name").val(),
				"email_address": $("#email_address").val(),
				// "car_model": $("#car_model").val(),
				// "car_registration": $("#car_registration").val(),
				"contact_group": $("#contact_group").val(),
				"subject": $("#subject").val(),
				"feedback": $("#feedback").val()
			};
			$.each(contact, function(index, val) {
				if (MMSGHome.contactus.validate(val, index)) {
					contactExists = true;
				}
			});
			$.each(selectorArray, function(index, val) {
				// if (val == "") {
				if (!(MMSGHome.contactus.validate(val, index))) {
					alertPop = (index == "contact_group") ? "Please select a category" : 'Please enter '+$('label[for="'+index+'"]').text();
					alert(alertPop);
					$('#'+index).focus();
					sendable = false;
					return false;
				}
				if (!(contactExists)) {
					alert('Please provide at least one valid contact number');
					sendable = false;					
					return false;
				}
			});
			if (sendable) {
				$.ajax({
				  url: 'contactus.asp',
				  type: 'POST',
				  dataType: 'json',
				  // data: $.param( $('Element or Expression') ),
					data: $('form#contactForm').serialize(),

	  				complete: function() {
				    //called when complete
					alert("message sent");
					$(':input','#contactForm')
					 .not(':button, :submit, :reset, :hidden')
					 .val('')
					 .removeAttr('checked')
					 .removeAttr('selected');
	  				},

	  				success: function() {
				    //called when successful
	 				},

	  				error: function() {
				    //called when there is an error
	  				}
					});
			}

			
			
			return returnValue;
		}
	}
};


jQuery(function($) {
	MMSGNav.init();
	MMSGHome.banner.init('#Banners');
	MMSGHome.banner.gotoDot(0);
	$('#BannersNav li').hover(function() {
		$(this).animate({opacity:0.6},0);
	}, function() {
		$(this).animate({opacity:1},0);
	});
	if (window.location.hash == "#/contactus") {
		MMSGHome.contactus.show();
	}
	$('#FooterLinks li:eq(1) a').click(function() {
		MMSGHome.contactus.show();
	});
});
