/*
-----------------------------------------------
FlowerPowerFundraising.com
Script: vdwService.js
Author: Ben Glassman
Organization: Vermont Design Works
Created: 3 Apr 2009
----------------------------------------------- */

vdwService = {
	init : function() {
		// Show the answer
		var showIndex = 0;
		if (window.location.hash.indexOf('#service-a') != -1) {
			var id = window.location.hash;
			if (id.indexOf('#service-a-spring-planting-guide-top') != -1) {
				id = '#service-a-spring-planting-guide-top';
				$jq('#nav-service-top').hide();
			}
			else if (id.indexOf('#service-a-guarantee') != -1) {
				id = '#service-a-guarantee';
			}
			else if (id.indexOf('#service-a-order-status') != -1) {
				id = '#service-a-order-status';
			}
			else if (id.indexOf('#service-a-privacy-policy?nlid=14034.1308959.0.0.3.-3&eid=howardh@gardeners.com') != 1) {
				id = '#service-a-privacy-policy';
			}
			else if (id.indexOf('#service-a-planting-guide-top') != -1) {
				id = '#service-a-planting-guide-top';
				$jq('#nav-service-top').hide();
			}
			else { $jq('#nav-service-top').show(); }
			showIndex = $jq('.service-a').index($jq(id));
			window.scrollTo(0,0);
		}
		$answers = $jq('.service-a').hide().eq(showIndex).show().addClass('current');
		$jq('.nav-service a').click(function(e) {
			if ($jq(this).attr('href').indexOf('#service-a') != -1) { e.preventDefault(); }
			vdwService.navCallback(this);
		});
		$jq('a.service-link').click(function(e) {
			e.preventDefault();
			vdwService.navCallback(this);
		});
		// Order status
		$jq('<ul id="error-container"></ul>').prependTo('#order-status-form').hide();
		$jq('#order-status-form').validate({
			errorLabelContainer : "#error-container",
			wrapper : "li"
		});

		$jq('#planting-guide-nav a').each(function(index) {
			$this = $jq(this);
			$this.attr('href', $this.attr('href').match(/#.+/)[0]);
		});
		$tabs = $jq('#planting-guide-nav').tabs();
		if (window.location.hash.indexOf('#service-a-planting-guide-top-sub') != -1) {
			var subtab = window.location.hash.match(/-sub-(.+?)$/)[1];
			$tabs.tabs('select', subtab);
		}
		$tabs.bind('tabsselect', function(event, ui) {
			var base = (window.location.hash.indexOf('-sub') == -1) ? window.location.hash.match(/(#service-a-.+?)$/)[1] : window.location.hash.match(/(#service-a-.+?)-sub/)[1];
			var selected = $jq(ui.panel).attr('id');
			$jq('#planting-guide-nav').attr('class', $jq('#planting-guide-nav').attr('class').replace(/\s?current[a-z-]+/, ''));	
			$jq('#planting-guide-nav').addClass('current-' + selected);
			window.location.hash = base+'-sub-'+selected;
		});
		if (window.location.hash) {
			$tabs.select(window.location.hash);	
		}

		$jq('#spring-planting-guide-nav a').each(function(index) {
			$this = $jq(this);
			$this.attr('href', $this.attr('href').match(/#.+/)[0]);
		});
		$tabs = $jq('#spring-planting-guide-nav').tabs();
		if (window.location.hash.indexOf('#service-a-spring-planting-guide-top-sub') != -1) {
			var subtab = window.location.hash.match(/-sub-(.+?)$/)[1];
			$tabs.tabs('select', subtab);
		}
		$tabs.bind('tabsselect', function(event, ui) {
			var base = (window.location.hash.indexOf('-sub') == -1) ? window.location.hash.match(/(#service-a-.+?)$/)[1] : window.location.hash.match(/(#service-a-.+?)-sub/)[1];
			var selected = $jq(ui.panel).attr('id');
			$jq('#spring-planting-guide-nav').attr('class', $jq('#spring-planting-guide-nav').attr('class').replace(/\s?current[a-z-]+/, ''));	
			$jq('#spring-planting-guide-nav').addClass('current-' + selected);
			window.location.hash = base+'-sub-'+selected;
		});
		if (window.location.hash) {
			$tabs.select(window.location.hash);	
		}


		// Hardiness zones
		$jq('<ul id="hardiness-error-container" class="error-container"></ul>').prependTo('#hardiness-zone-form').hide();
		$jq('#hardiness-zone-form').validate({
			errorLabelContainer : "#hardiness-error-container",
			wrapper : "li",
			submitHandler : function(form) {
				var hardiness_zone_zip = $jq('#hardiness_zone_zip').val();
				if ($jq('#hardiness-zone').length == 0) { $jq(form).append('<div id="hardiness-zone"></div>'); }
				$jq('#hardiness-zone').fadeOut('fast');
				$jq.ajax({
					type: "POST",
					url: "/assets/snippets/hardiness_zones/ajax_get_hardiness_zone.php",
					data: "hardiness_zone_zip=" + hardiness_zone_zip,
					success: vdwService.showHardinessZone,
					cache: false
				});	
			}
		});
		$jq('.map-hardiness-zone-detail').hide().click(function(e) {
			e.preventDefault();
			$jq(this).hide();
			var id = $jq(this).attr('href').substr($jq(this).attr('href').indexOf('#'));
			$jq(id).show();			
		});
		$jq('.map-hardiness-zone-zoom').click(function(e) {
			e.preventDefault();
			$jq('#map-hardiness-zone-full').hide();
			var id = $jq(this).attr('href').substr($jq(this).attr('href').indexOf('#'));
			$jq(id).show();
		});
		// Newsletter subscription
		$jq('<ul id="newsletter-error-container" class="error-container"></ul>').prependTo('#newsletter-subscription-form').hide();
		$jq('#newsletter-subscription-form').validate({
			errorLabelContainer : "#newsletter-error-container",
			wrapper : "li",
			submitHandler : function(form) {
				var data = $jq('#newsletter-subscription-form').serialize();
				if ($jq('#newsletter-subscription-message').length == 0) { $jq(form).prepend('<div id="newsletter-subscription-message"></div>'); }
				$jq('#newsletter-subscription-message').fadeOut('fast');
				$jq.ajax({
					type: "POST",
					url: "/assets/ajax/newsletter_signup.php",
					data: data,
					success: vdwService.newsletterMessage,
					cache: false,
					dataType : 'json'
				});	
			}
		});
	},
	showHardinessZone : function(data) {
		$jq('#hardiness-zone').html(data).fadeIn('fast');
	},
	newsletterMessage : function (data) {
		$jq('#newsletter-subscription-form').fadeOut('fast', function() {
			$jq('#newsletter-subscription-form').html(data.message_html).fadeIn('fast')
		});
	},
	navCallback : function(link) {
		var href = $jq(link).attr('href');
		if (href.indexOf('#service-a') != -1) {
			var id = href.substr(href.indexOf('#'));
			vdwService.showAnswer(id);
		}
	},
	showAnswer : function(id) {			
		// Clear existing current question
		$jq('.nav-service .current').removeClass('current');
		$jq('#content-service .current').removeClass('current');
		// Show new question
		$jq('#' + id.replace('-a-','-q-')).addClass('current');
		window.location.hash = id;
		window.scrollTo(0,0);
		$jq('#service-content .current').fadeOut('normal', function() {
			$jq(id).fadeIn('normal').addClass('current');
			if ( id == '#service-a-planting-guide-top' ||  id == '#service-a-spring-planting-guide-top') { 
				$jq('#nav-service-top').hide();
			} 
			else { $jq('#nav-service-top').show(); }
		}).removeClass('current');
	}
}

$jq(document).ready(vdwService.init);
