(function ($) {


	//================== make tabs for the wild headings
	function homePageWildHeadings(){
		//first add some tabs + add ids
		var baseElement =$('div.view-id-home_page_wild_headings_widget', '#content');
		var wrapElements = $('div.wild-heading','#content');
		var whElements = $('div.view-id-home_page_wild_headings_widget .view-content .node-home-wild-headings', '#content');
		//add ids to wrapElements to which to link
		wrapElements.each(function(i){
			$(this).attr('id',"wh-"+i);
		});
		//loop through whElements and make a ul of links		
		if(whElements.length){
			baseElement.prepend('<ul class="home-wildheadings-tabs"></ul>');
		}
		whElements.each(function(i){
			var whName = $(this).children().filter('h2').text();
			//var whLink = $(this).attr('id');
			var whLink ="wh-"+i;
			var whListItem = '<li class="tab '+whName+'"><a href="#'+whLink+'">'+whName+'</a></li>';
			baseElement.children().first().append(whListItem);
		});
		
		//now call the tabs
		baseElement.tabs();
	}
	
	//================= make newsletter form anim
	function newsletterReveal(){
		$('form.webform-client-form').click(function(e){
			e.stopPropagation();
		});
			
		$('input#edit-submitted-email', '#content div.region-content form').focusin(function(){
			var tForm = $('form.webform-client-form', '#content div.region-content');
			tForm.focused = true;
			//move submit button
			tForm.addClass('active');
			if(this.value =='Enter email address'){
				this.value = '';
			}
			
			$(this).animate({'width':'193'},'fast');
			//show rest of form
			$("#webform-component-further-information").slideDown('fast');
		});
		
		$('#page','#page-wrapper').focusin(function(event){
		//	console.log($(event.target))
		//	console.log($(event.target).parents("form").length);
			if($(event.target).parents("form").length < 1  && $('form.webform-client-form').hasClass('active')){
				$("#webform-component-further-information").stop(true,true).delay(500).slideUp('fast',function(){
					$('input#edit-submitted-email', '#content div.region-content form').animate({'width':'137'},'fast');
					$('form.webform-client-form', '#content div.region-content').removeClass('active');
				});
			}
		});
		//and a click event
		$('body').click(function(event){
			if($(event.target).parents("form").length < 1  && $('form.webform-client-form').hasClass('active')){
				$("#webform-component-further-information").stop(true,true).delay(500).slideUp('fast',function(){
					$('input#edit-submitted-email', '#content div.region-content form').animate({'width':'137'},'fast');
					$('form.webform-client-form', '#content div.region-content').removeClass('active');
				});
			}
		
		});
		
		$('input#edit-submitted-email', '#content div.region-content form').focusout(function(){
			if(this.value ==''){this.value == 'Enter email address';}
		});
	}
	
	//======================= make itinerary rich functionality
	function tripListHover(elem){
		$(elem).hover(
			function(){
				$(this).addClass('hover');
			},
			function(){
				$(this).removeClass('hover');
			}
		);
	}
	
	//make links out of the entire list
	function tripListLinks(){
		var elem = $('div.block-rbb ul.events li div.node-trip-event', '#sidebar-second');
		//add hover behaviour as well
		tripListHover(elem);
		$(elem).click(function(e){
			var href = $(this).attr('rel');
			window.location = href;
			return false;
		}).css({'cursor':'pointer'});
	}
	
	function setupItinerary(){
		//hide all old tables
		var lis = $('ul.itinerary li.month', '#content');
	//	lis.children('h2').siblings('table.itinerary').css({'display':'none'});
		
		//add functionality to h2
		var h2s = $(lis).children('h2').click(function(){
			$(this).parent().toggleClass('open');
			//show hide table
			var tableIt = $(this).siblings('div.tablewrap');
			if(tableIt.is(":hidden") && $(this).parent().hasClass('open')){
				tableIt.stop(true, true).slideDown(600);
			}else{
				tableIt.stop(true, true).slideUp(150);
			}
		});
		
		//== set up behaviour on table rows
		$('ul.itinerary li.month tbody>tr', '#content').css({'cursor':'pointer'}).hover(function(){
			$(this).toggleClass('hover');
		},function(){
			$(this).toggleClass('hover');
		});
		
		var today = new Date();
		var thisYear = today.getFullYear();
		var thisDay = today.getDate();
		var thisMonth = today.getMonth();
		var madeupdate = '';//new Date('31 February '+thisYear);
		
		lis.each(function(){
			var theMonth = $(this).children('h2').first().text();
			//extract out only the date name
			var patt = /^[January|February|March|April|May|June|July|August|September|October|November|December]*/i;
			theMonth = patt.exec(theMonth);
			madeupdate = new Date(thisDay+' '+theMonth+' '+thisYear+' 23:59:59');
			if(madeupdate.getMonth() < (thisMonth-2)){madeupdate = new Date(thisDay+' '+theMonth+' '+(thisYear+1)+' 23:59:59');}
			if(today <= madeupdate){
				$(this).addClass('open');
				var slider = $(this).children('div.tablewrap');
				if(slider.is(":hidden")){
					slider.stop(true, true).slideDown('fast');
				}
				//== get href and bind click function
				var trs = $(this).children('div').children('table').children('tbody').children('tr');
				$(trs).each(function(){
					var href = $(this).children('td[class="itinerary"]').children('p').children('a').attr('href');
					if(href){
						$(this).click(function(){
							window.location = href;
						});
					}
				});
				
			}
		});
	}//setUpItinerary
	
	function formLableOverlay(){
		var textfields = $('form input[type="text"]');
		var textareas = $('form textarea');
		//== loop through and overlay label
		textfields.each(function(){
			var tf = $(this).siblings("label");
			var txt = tf.text();
			if(this.value == 'Your name' || this.value == 'Telephone...' || this.value == 'Enter your email' || this.value.indexOf ('Email') !=-1){
				this.value = txt;
			}
			tf.css({'display':'none'});
			$(this).focusin(function(){
				if(this.value == txt){
					this.value = '';
				}
			}).focusout(function(){
				if(this.value == ''){
					this.value = txt;
				}
			});
		});
		//== now the same for the textareas
		textareas.each(function(){
			var tf = $(this).parent().siblings("label");
			var txt = tf.text();
			txt = txt+'...';
			if(this.value == 'Your enquiry' || this.value == 'Your enquiry...'){
				this.value = txt;
			}
			tf.css({'display':'none'});
			$(this).focusin(function(){
				if(this.value == txt){
					this.value = '';
				}
			}).focusout(function(){
				if(this.value == ''){
					this.value = txt;
				}
			});
		});
	}//formLableOverlay
	
	//======================= init
	Drupal.behaviors.setUpTabs = {
    	attach: function(context, settings) {

			//js function calls
			if($('div.view-id-home_page_wild_headings_widget', '#content').length){
				homePageWildHeadings();
			}
			//== trip sidebar links
			if($('div.block-rbb', '#sidebar-second').length){
				tripListLinks();
			}
			
			//== call itinerary display
			if($('ul.itinerary', '#content').length){
				setupItinerary();
			}
			
			//== call form label overlay
			if($('form.webform-client-form', '#content').length){
				formLableOverlay();
			}
			
			//== Add os and browser classes for specific styles
			if(navigator.appVersion.indexOf("Mac")!=-1){
				$('body').addClass('Mac');
			}else if(navigator.platform.indexOf("Linux")!=-1){
				$('body').addClass('Linux');
			}else{
				$('body').addClass('PC');
			}
			
			var ua = $.browser;
			if (ua.safari) {
				$('body').addClass('safari-browser');
			}
			if (ua.webkit) {
				$('body').addClass('webkit-browser');
			}
			if (ua.msie) {
				$('body').addClass('msie-browser v'+ua.version.slice(0,1));
			}
			if (ua.opera) {
				$('body').addClass('opera-browser');
			}
			if (ua.mozilla) {
				$('body').addClass('mozilla-browser');
			}

			//$("[title]").tooltip();

			//call newsletter reveal
			if($('input#edit-submitted-email','.front div#webform-component-email').length){
				newsletterReveal();
				$('#edit-submitted-further-information-name','#content').focusin(function(){
					if(this.value == 'Your name'){this.value ='';}
				});
				$('#edit-submitted-further-information-name','#content').focusout(function(){
					if(this.value == ''){this.value ='Your name';}
				});
			}

		}
	};

})(jQuery);;
// $Id: jquery.cookie.js,v 1.1 2009/07/01 12:16:48 dries Exp $

/**
 * Cookie plugin 1.0
 *
 * Copyright (c) 2006 Klaus Hartl (stilbuero.de)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */
jQuery.cookie=function(b,j,m){if(typeof j!="undefined"){m=m||{};if(j===null){j="";m.expires=-1}var e="";if(m.expires&&(typeof m.expires=="number"||m.expires.toUTCString)){var f;if(typeof m.expires=="number"){f=new Date();f.setTime(f.getTime()+(m.expires*24*60*60*1000))}else{f=m.expires}e="; expires="+f.toUTCString()}var l=m.path?"; path="+(m.path):"";var g=m.domain?"; domain="+(m.domain):"";var a=m.secure?"; secure":"";document.cookie=[b,"=",encodeURIComponent(j),e,l,g,a].join("")}else{var d=null;if(document.cookie&&document.cookie!=""){var k=document.cookie.split(";");for(var h=0;h<k.length;h++){var c=jQuery.trim(k[h]);if(c.substring(0,b.length+1)==(b+"=")){d=decodeURIComponent(c.substring(b.length+1));break}}}return d}};
;

