/**
 * Wear It With Pride common scripts
 * Copyright 2009 Mark. Sydney :: www.mark.com.au :: All Rights Reserved *

	 /'\_/`\                 /\ \
	/\      \    __      _ __\ \ \/'\
	\ \ \__\ \  /'__`\  /\`'__\ \ , <
	 \ \ \_/\ \/\ \_\.\_\ \ \/ \ \ \\`\   __
	  \ \_\\ \_\ \__/.\_\\ \_\  \ \_\ \_\/\_\
	   \/_/ \/_/\/__/\/_/ \/_/   \/_/\/_/\/_/

 * Author: Gilmore Davidson
--------------------------------------------------------------------------*/

(function ($, social) {
	var Wearit = {
		filePath: '',

		trackEvent: function (category, action, label, value) {
			try {
				pageTracker._trackEvent(category, action, label, value);
			} catch (e) {}
		},

		init: function (path) {
			Wearit.filePath = path;
			// External links
			$('a[href^="http://"]').attr({
				target: "_blank",
				title: "Opens in a new window"
			});

			// Social media
			social.setOptions({
				twitterMessage: 'Huge news! 85 laws that discriminated against same-sex couples have changed. See what fits you & WEAR IT WITH PRIDE. #WIWP ',
				proxyUrl: path + '/socialmedia/',
				gaTrackEvent: true
			});
			$('.social-tools a:not(.email 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();
			});

			// Facebox
			$.extend($.facebox.settings, {
				loadingImage: path + '/_images/facebox/loading.gif',
				closeImage: path + '/_images/facebox/closelabel.gif',
				opacity: 0.4
			});
			$('a[rel*=facebox]').facebox();
			$('a.close').trigger('close.facebox');

			// Email to friend
			$.ajaxSetup({cache:true});
			$('a[rel*=email]').click(function (e) {
				e.preventDefault();
				var url = $(this).attr('href') + '?url=' + encodeURIComponent(location.href);
				$.facebox({ajax: url});
				Wearit.trackEvent('SocialMedia', 'send to friend');
			});
		},

		facts: function (finder) {
			$('div.toggle-faq > div, dl.laws').hide();
			$('div.toggle-faq > h4, div.toggle-section > h2').click(function () {
				$(this).toggleClass('open').toggleClass('close').next().slideToggle('fast');
			});
			$('div.toggle-section > h2 a').click(function (e) {
				e.stopPropagation();
			});
			if (finder === true) {
				$('div.toggle-section > h2').click();
			}
		},

		sidebar: {
			finder: function () {
				$('#solutionFinder a[rel=continue]').click(function (e) {
					e.preventDefault();
					var $that = $(this);
					$('#solutionFinder fieldset.hide').slideDown('fast', function () {
						$('#solutionFinder .hide').show();
						$that.parent().hide();
					});
				});
			}
		},
		
		form: {
			submitHandler: function (form, hide, show, callback) {
				var $form = $(form);
				$form.find('input:submit').attr('disabled', 'disabled');
				$form.ajaxSubmit({
					url: $form.attr('action') + '.json',
					dataType: 'json',
					success: function (data) {
						if (data.success) {
							$(hide).hide();
							$(show).fadeIn('fast');
							if (callback !== undefined && $.isFunction(callback)) {
								callback();
							}
						} else {
							alert('Sorry, there was an error submitting the form. Please try again.');
							$form.find('input:submit').removeAttr('disabled');
						}
					}
				});
			},

			sendtofriend: function () {
				this.sendtofriend.validator = $("#form_stf").validate({
					rules: {
						"stfdata[fromName]":     "required",
						"stfdata[fromEmail]":    {required:true, email:true},
						"stfdata[friendName1]":  "required",
						"stfdata[friendEmail1]": {required:true, email:true},
						"stfdata[friendEmail2]": {email:true},
						"stfdata[friendEmail3]": {email:true}
					},
					messages: {
						"stfdata[fromName]":     {required:"Your name is required"},
						"stfdata[fromEmail]":    {required:"Your email address is required"},
						"stfdata[friendName1]":  {required:"Your friend's name is required"},
						"stfdata[friendEmail1]": {required:"Your friend's email address is required"}
					},
					submitHandler: function (form) {
						Wearit.form.submitHandler(form, '#stf_form', '#stf_thankyou', function () {
							setTimeout($.facebox.close, 2500);
						});
					}
				});
			},

			contact: function () {
				this.contact.validator = $("#form_contact").validate({
					rules: {
						"contact[name]":    "required",
						"contact[email]":   {required:true, email:true},
						"contact[subject]": "required",
						"contact[message]": "required"
					},
					messages: {
						"contact[name]":    {required:"Your name is required"},
						"contact[email]":   {required:"Your email address is required"},
						"contact[subject]": {required:"An email subject is required"},
						"contact[message]": {required:"Your message is required"}
					},
					submitHandler: function (form) {
						Wearit.form.submitHandler(form, '#form_contact', '#contact_thankyou');
					}
				});
			},

			preorder: function () {
				this.preorder.validator = $("#form_preorder").validate({
					rules: {
						"preorder[content_id]": "required",
						"preorder[size]":       "required",
						"preorder[name]":       "required",
						"preorder[email]":      {required:true, email:true}
					},
					messages: {
						"preorder[content_id]": {required:"Please select a t-shirt"},
						"preorder[size]":       {required:"Please select a shirt size"},
						"preorder[name]":       {required:"Your name is required"},
						"preorder[email]":      {required:"Your email address is required"}
					},
					submitHandler: function (form) {
						Wearit.form.submitHandler(form, '#form_preorder', '#preorder_thankyou');
					}
				});
				$.each(['NAME', 'EMAIL'], function (i, name) {
					$('#form_preorder_' + name.toLowerCase()).focus(function () {
						var $this = $(this);
						if ($this.val() === name) {
							$this.val('');
						}
					}).blur(function () {
						var $this = $(this);
						if ($this.val() === '') {
							$this.val(name);
						}
					});
				});
				$('#preorder_thankyou a[rel=more]').click(function (e) {
					e.preventDefault();
					$('#preorder_thankyou').hide();
					$('#form_preorder').show();
				});
			}
		}
	};

	window.Wearit = Wearit;
})(jQuery, social);
