/*global window: true, navigator: true, console: true, swfobject: true, google: true, jQuery: true, document: true, flashembed: true, GSmallMapControl: true, GMapTypeControl: true, GLatLng: true, GMarker: true, sIFR: true, api: true, Cufon: true, iScroll: true */

"use strict";

var currentElement, element, headerHeight, footerHeight, footerHiddenHeight, postsHeight, appleTouchDevice = false, myScroll;

google.load("jquery", "1.5");
//google.load("jqueryui", "1.8.9");
 
function showResponse(responseText, statusText, xhr, $form)
{
	$form.find('.message').removeClass('error success').addClass(statusText).show();
}

function echeck(str)
{
	var at="@", dot=".", lat=str.indexOf(at), lstr=str.length, ldot=str.indexOf(dot);

	if (str.indexOf(at)===-1)
	{
		//alert("Invalid E-mail ID");
		return false;
	}
	
	if (str.indexOf(at)===-1 || str.indexOf(at)===0 || str.indexOf(at)===lstr)
	{
		//alert("Invalid E-mail ID");
		return false;
	}
	
	if (str.indexOf(dot)===-1 || str.indexOf(dot)===0 || str.indexOf(dot)===lstr)
	{
		//alert("Invalid E-mail ID");
		return false;
	}
	
	if (str.indexOf(at,(lat+1))!==-1)
	{
		//alert("Invalid E-mail ID");
		return false;
	}
	
	if (str.substring(lat-1,lat)===dot || str.substring(lat+1,lat+2)===dot){
		//alert("Invalid E-mail ID");
		return false;
	}
	
	if (str.indexOf(dot,(lat+2))===-1)
	{
		//alert("Invalid E-mail ID");
		return false;
	}
	
	if (str.indexOf(" ")!==-1)
	{
		//alert("Invalid E-mail ID");
		return false;
	}
	return true;
}

if (navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPod/i) || navigator.userAgent.match(/iPad/i))
{
	appleTouchDevice = true;
}

function activatePage()
{
	headerHeight = jQuery("#header").outerHeight();
	footerHeight = jQuery("#footer").outerHeight();
	postsHeight = jQuery("#posts").outerHeight();
	footerHiddenHeight = "2em";

	if(appleTouchDevice === true)
	{
		jQuery.getScript
		(
			"/wp-content/themes/wednesday/js/iscroll.js",
			function ()
			{
				myScroll = new iScroll('header', {desktopCompatibility:true});
			}
		);
	}

	jQuery.getScript
	(
		"/wp-content/themes/wednesday/js/jquery.jscrollpane.min.js",
		function ()
		{
			var clientsScrollPane, clientsScrollPaneApi;

			clientsScrollPane = jQuery('#clients > div').jScrollPane
			(
				{
					hijackInternalLinks: true
				}
			);

			clientsScrollPaneApi = clientsScrollPane.data('jsp');

			jQuery('#clients > div').stop().fadeTo('fast', 0);

			if (appleTouchDevice !== true)
			{
				jQuery("#footer").css
				(
					{
						"position"		: "fixed",
						
					}
				);
				jQuery("ul.navigation li:first a").addClass('finite-scroll');
			}

			jQuery("#footer").css
			(
				{
					"height"		: 0
				}
			).animate
			(
				{
					"height"		: footerHiddenHeight	
				},
				500
			).hover
			(
				function ()
				{
					jQuery(this).stop().animate
					(
						{ 
							height: footerHeight
						},
						500
					);
		
					jQuery('#content').stop().fadeTo('slow', 0.2);
					jQuery('#clients > div').stop().fadeTo('fast', 1);
				},
				function ()
				{
					jQuery(this).stop().animate
					(
						{ 
							height: footerHiddenHeight
						},
						500
					);
		
					jQuery('#content').stop().fadeTo('slow', 1);
					jQuery('#clients > div').stop().fadeTo('fast', 0);
					clientsScrollPaneApi.scrollToY(0);
				}
			);
		}
	);
}

function activatePosts()
{

	jQuery(".more-content, .share-email-form, .send-to-a-friend, .add-to-mailing-list, .phplist, .copy").hide();

	jQuery("a[rel='external'], a[href='http://twitter.com']").attr("target", "_blank");

	jQuery.getScript
	(
		"/wp-content/themes/wednesday/js/jquery.scrollTo/jquery.scrollTo-min.js",
		function ()
		{
			jQuery('.return-to-top, .home h1').click
			(
				function ()
				{
					if (appleTouchDevice !== true)
					{
						jQuery.scrollTo('#main', 400);
						return false;
					}
				}
			);
			
			jQuery(".read-more, .overview h3").toggle
			(
				function ()
				{
					jQuery(this).closest('.overview').find('.read-more').hide().text('View Less...').show();
					jQuery(this).closest('.overview').find('.more-content').slideDown
					(
						400
					);
					if (jQuery('.single').length)
					{
						jQuery.scrollTo(this, 200);
					}
					return false;
				},
				function ()
				{
					jQuery(this).closest('.overview').find('.read-more').hide().text('View More...').show();
					jQuery(this).closest('.overview').find('.more-content').slideUp
					(
						100
					);
					if (jQuery('.single').length)
					{
						jQuery.scrollTo('#main', 200);
					}
					return false;
				}
			);
		}
	);

	jQuery.getScript
	(
		"/wp-content/themes/wednesday/js/jquery.copy/jquery.copy.js",
		function ()
		{
			jQuery('.copy').click
			(
				function ()
				{
					jQuery.copy(jQuery(this).text());
					return false;
				}
			);
		}
	);
	
	jQuery('.share.email').toggle
	(
		function ()
		{
			jQuery(this).closest('.post').find('.send-to-a-friend').show
			(
				"slide",
				{
					direction: "up"
				},
				150
			);
			return false;
		},
		function ()
		{
			jQuery(this).closest('.post').find('.send-to-a-friend').hide
			(
				"slide",
				{
					direction: "down"
				},
				150
			);
			return false;
		}
	);

	jQuery('.share.url').toggle
	(
		function ()
		{
			jQuery(this).closest('.post').find('.copy').show
			(
				"slide",
				{
					direction: "up"
				},
				150
			);
			return false;
		},
		function ()
		{
			jQuery(this).closest('.post').find('.copy').hide
			(
				"slide",
				{
					direction: "down"
				},
				150
			);
			return false;
		}
	);
	
	jQuery('.join.mailinglist').toggle
	(
		function ()
		{
			jQuery(this).closest('div').find('.add-to-mailing-list').show
			(
				"slide",
				{
					direction: "up"
				},
				150
			);
			return false;
		},
		function ()
		{
			jQuery(this).closest('div').find('.add-to-mailing-list').hide
			(
				"slide",
				{
					direction: "down"
				},
				150
			);
			return false;
		}
	);
	
	jQuery.getScript
	(
		"/wp-content/themes/wednesday/js/jquery.placeheld/jquery.placeheld.min.js",
		function ()
		{
			jQuery("input[placeholder]").placeHeld();
		}
	);
	
	jQuery.getScript
	(
		"/wp-content/themes/wednesday/js/jquery.form/jquery.form.js",
		function ()
		{
			jQuery(".send-to-a-friend").submit
			(
				function ()
				{
					var message = jQuery(this).find('.message'), fieldEmpty = false, emailInvalid = false, options;

					jQuery(this).children("input").each
					(
						function (index)
						{
							if (jQuery(this).val().length === 0)
							{
								fieldEmpty = true;
							}
						}
					);
					
					jQuery(this).children("input.email").each
					(
						function (index)
						{
							if (echeck(jQuery(this).val()) === false)
							{
								emailInvalid = true;
							}
						}
					);
					
					if (fieldEmpty === false && emailInvalid === false)
					{
						options = {
							target:        message, 
							//beforeSubmit:  showRequest, 
							success:       showResponse
						};

						jQuery(this).ajaxSubmit(options);
						return false;
					}
					
					message.text("Please fill in all fields correctly").removeClass('success').addClass('error').show();
					return false;
				}
			);
			
			jQuery(".add-to-mailing-list").submit
			(
				function ()
				{
					var message = jQuery(this).find('.message'), options;
					
					if ((jQuery(this).find('.full-name').val().length > 0) && (echeck(jQuery(this).find('.email').val()) === true))
					{
						options = {
							target:        message, 
							success:       showResponse
						};

						jQuery(this).ajaxSubmit(options);
						return false;
					}
					
					message.text("Please fill in all fields correctly").removeClass('success').addClass('error').show();
					return false;
				}
			);

			Cufon.refresh();
		}
	);
}

google.setOnLoadCallback
(
	function ()
	{
		jQuery(document).ready
		(
			function ()
			{
				activatePage();
				activatePosts();
			}
		);
	}
);

