// jquery.cookie.js
jQuery.cookie=function(name,value,options){if(typeof value!='undefined'){options=options||{};if(value===null){value='';options.expires=-1}var expires='';if(options.expires&&(typeof options.expires=='number'||options.expires.toUTCString)){var date;if(typeof options.expires=='number'){date=new Date();date.setTime(date.getTime()+(options.expires*24*60*60*1000))}else{date=options.expires}expires='; expires='+date.toUTCString()}var path=options.path?'; path='+(options.path):'';var domain=options.domain?'; domain='+(options.domain):'';var secure=options.secure?'; secure':'';document.cookie=[name,'=',encodeURIComponent(value),expires,path,domain,secure].join('')}else{var cookieValue=null;if(document.cookie&&document.cookie!=''){var cookies=document.cookie.split(';');for(var i=0;i<cookies.length;i++){var cookie=jQuery.trim(cookies[i]);if(cookie.substring(0,name.length+1)==(name+'=')){cookieValue=decodeURIComponent(cookie.substring(name.length+1));break}}}return cookieValue}};


// jquery.ztabs.js
(function($) {
	$.fn.ztabs = function(options) {
		var defaults = {
			id: null,
			speed: 450,
			active: 'on',
			cookie: false,
			items: '.items',
			buttons: '.buttons'
		};
		var options = $.extend(defaults, options);
		var container = $(this);
		var i = $(options.items, container);
		var b = $(options.buttons, container);
		var c = options.cookie;
		var s = parseInt(options.speed);
		i.hide();
		if (c != false) var id = (!$.cookie(c)) ? options.id : $.cookie(c);
		if (id != null) {
			i.eq(id).show();
			b.eq(id).addClass(options.active);
		}
		b.click(function() {
			temp = b.index(this);
			// if (options.id == null && temp == id) alert('should hide');
			if (i.eq(temp).is(':visible')) return;
			b.eq(id).removeClass(options.active);
			$(this).addClass(options.active);
			i.eq(id).hide(s, function() {
				id = temp;
				i.eq(id).show(s/2);
				if (c != false) $.cookie(c, id, { expires: 1, path: '/' });
			});
		});
	};
})(jQuery);


// jquery.interfade
(function($){$.fn.innerfade=function(options){return this.each(function(){$.innerfade(this,options)})};$.innerfade=function(container,options){var settings={'animationtype':'fade','speed':'normal','type':'sequence','timeout':2000,'containerheight':'auto','runningclass':'innerfade','children':null};if(options)$.extend(settings,options);if(settings.children===null)var elements=$(container).children();else var elements=$(container).children(settings.children);if(elements.length>1){$(container).css('position','relative').css('height',settings.containerheight).addClass(settings.runningclass);for(var i=0;i<elements.length;i++){$(elements[i]).css('z-index',String(elements.length-i)).css('position','absolute').hide()};if(settings.type=="sequence"){setTimeout(function(){$.innerfade.next(elements,settings,1,0)},settings.timeout);$(elements[0]).show()}else if(settings.type=="random"){var last=Math.floor(Math.random()*(elements.length));setTimeout(function(){do{current=Math.floor(Math.random()*(elements.length))}while(last==current);$.innerfade.next(elements,settings,current,last)},settings.timeout);$(elements[last]).show()}else if(settings.type=='random_start'){settings.type='sequence';var current=Math.floor(Math.random()*(elements.length));setTimeout(function(){$.innerfade.next(elements,settings,(current+1)%elements.length,current)},settings.timeout);$(elements[current]).show()}else{alert('Innerfade-Type must either be \'sequence\', \'random\' or \'random_start\'')}}};$.innerfade.next=function(elements,settings,current,last){if(settings.animationtype=='slide'){$(elements[last]).slideUp(settings.speed);$(elements[current]).slideDown(settings.speed)}else if(settings.animationtype=='fade'){$(elements[last]).fadeOut(settings.speed);$(elements[current]).fadeIn(settings.speed,function(){removeFilter($(this)[0])})}else alert('Innerfade-animationtype must either be \'slide\' or \'fade\'');if(settings.type=="sequence"){if((current+1)<elements.length){current=current+1;last=current-1}else{current=0;last=elements.length-1}}else if(settings.type=="random"){last=current;while(current==last)current=Math.floor(Math.random()*elements.length)}else alert('Innerfade-Type must either be \'sequence\', \'random\' or \'random_start\'');setTimeout((function(){$.innerfade.next(elements,settings,current,last)}),settings.timeout)}})(jQuery);function removeFilter(element){if(element.style.removeAttribute){element.style.removeAttribute('filter')}}

$(document).ready(function() {

	$('#go').click(function() {
		$('#test').load('/home/test2', {size: 'small', limit: '6', 'switch': '||last'});
		//$('#test').load('/inc/tiles', {size: 'small', limit: '6'});
		return false;
	});

	// universal show hide
	$('.reveal').click(function() {
		content = $('.hidden', this);
		link = $('a', this);
		if (content.is(':hidden')) {
			content.show('300');
			link.text('hide');
		} else {
			content.hide('100').css('display', 'inline');
			link.text('show');
		}
		return false;
	});

	loadingHTML = '<p id="loading"><img src="/assets/i/i_loading.gif" width="16" height="16" alt="Loading..." /></p>';

	$("#entryform").validate({
		errorElement: "em",
		focusInvalid: false,
		rules: {
			title: { required: true },
			entry_date: { required: true },
			event_description: { required: true },
			category: { required: true }
		},
		messages: {
			title: { required: "You must provide an event title." },
			entry_date: { required: "You must provide a name." },
			event_description: { required: "Provide an email address." },
			category: { required: "You must choose a category."}
		},
		highlight: null,
		unhighlight: null
	});



	// open external links with "http..." in new window
	if ($('a[href^=http]').length > 0) {
		$('a[href^=http]').click(function() {
			var destination = $(this).attr('href');
			if (confirm("You are now leaving Historic Poole Forge's website. Close the new window to return. Click cancel to remain on our website. ")) window.open(destination);
			return false;
		});
	}

		
	// input content hide/restore and highlighting
	var inputs = $(".input");
	var inputDefaults = [];
	inputs.each(function() {
		if ($(this).val() != '') inputDefaults[inputs.index(this)] = $(this).val();
	});
	inputs.focus(function() {
		if ($(this).attr('readonly') == true) return false;
		$(this).addClass('on');
		tag = $(this).get(0).tagName;
		d = inputDefaults[inputs.index(this)];
		o = $(this).val();
		if (tag == 'SELECT') {return;} else if (tag == 'INPUT') {
			if (d == o) $(this).val('');
		} else if (tag == 'TEXTAREA') {
			if (d == o) $(this).text('');
		}
	});
	inputs.blur(function() {
		if ($(this).attr('readonly') == true) return false;
		$(this).removeClass('on');
		tag = $(this).get(0).tagName;
		d = inputDefaults[inputs.index(this)];
		o = $(this).val();
		if (tag == 'SELECT') {return;} else if (tag == 'INPUT') {
			if (o == '' || o == ' ') $(this).val(d);
		} else if (tag == 'TEXTAREA') {
			if (o == '' || o == ' ') $(this).text(d);
		}
	});
	
	$('#tools').ztabs({
		items: '.items > div',
		buttons: 'ul li > a',
		cookie: 'hpf_tool'
	});
	
	$('.panel').ztabs({
		id: 0,
		items: '.items > div',
		buttons: '.tabs a',
		cookie: 'hpf_tab'
	});
	

	
});