$(function () {

	$("img.rollover, input.rollover").hover(function() {
		$(this).attr("src", $(this).attr("src").replace(/\.([^\.]+)$/, '-hl.$1'));
	}, function() {
		$(this).attr("src", $(this).attr("src").split("-hl.").join("."));
	});

	if ($.fn.prettyPhoto) {
		$('a[rel^=prettyPhoto]').prettyPhoto();
	}

});

// Apparently still called somewhere
function rollover_setup() { }

(function() {
	var auto_submit = true,
		submit_id = 0;

	function filter_check(e)
	{
		var count = 0,
			checked_count = 0,
			any = $("#filters :checkbox[name='"+this.name+"']").eq(0),
			other = any.end().not("[value=any]"),
			node,
			form;

		if (this.value == "any")
		{
			this.checked = true;
			// Clear other checkboxes
			other.attr("checked", false);
		}
		else
		{
			count = checked_count = 0;
			if (this.checked)
				any.attr("checked", false);

			// Cat boxes should change all boxes under them -- do this before we do the counts
			if (this.value.substr(0,4) == 'cat:')
			{
				node = $(this).parents("li").get(0);
				if (node) {
					$(":checkbox", node).attr("checked", this.checked);
				}
			}

			// Count total and checked total
			other.each(function() {
				count++;
				if (this.checked) {
					checked_count++;
				}
			});

			// Set "Any" if either no or all checkboxes are checked
			if (checked_count == 0) {
				any.attr("checked", true);
			}
			if (checked_count == count) {
				any.attr("checked", true);
				other.attr("checked", false);
			}
		}

		if (auto_submit) {
			if (submit_id > 0) {
				window.clearInterval(submit_id);
			}

			form = this.form;
			submit_id = window.setInterval(function() {form.submit();}, 1000 );
		}
	}

	function filter_sub_toggle(e)
	{
		var node,
			temp;

		node = $(this).siblings('ul').get(0);
		if (node)
		{
			temp = $(node).toggle();
			this.innerHTML = (temp.filter(":hidden").length > 0) ? '+' : '&ndash;';
		}

		return false;
	}

	filter_setup = function(auto)
	{
		if (typeof(auto) != 'undefined') {
			auto_submit = auto;
		}
		$("#filters :checkbox").click(filter_check);
		$("#filters a.filter_toggle").click(filter_sub_toggle);
	};

})();

person_date_setup = function(dates, custom_dates, button_img)
{
	var container = $("#date_container"),
		relationship = $("#relationship");

	function relationship_change(e)
	{
		show_dates(this.options[this.selectedIndex].text);
	}

	// Show or hide dates based on selected relationship
	function show_dates(relation)
	{
		var count = 0,
			list = $('li', container);

		$.each(dates, function(date_name) {
			var li = list.eq(count),
				show = (this.enabled ||
						$(':checked', li).length > 0 ||
						this.always ||
						$.inArray(relation, this.match) > -1);

			if (show) {
				li.show();
			}
			else {
				li.hide();
			}

			count++;
		});
	}

	function date_li(date_name, field_name, options)
	{
		var checked = (options.enabled) ? ' checked="checked"' : '',
			date = (options.date || ''),
			disabled = (options.individual) ? '' : ' disabled="disabled"';

		return '<li>' +
			'<label><input type="checkbox" name="enable_'+field_name+'" value="'+date_name+'"'+checked+' />' + date_name + '</label>' +
			' <input type="text" class="date" name="date_'+field_name+'" value="'+date+'" size="10"'+disabled+' />' +
			'</li>';
	}

	function date_custom_li(date_name, field_name, date)
	{
		date_name.replace(/"/, '&quot;');

		return '<li class="custom">' +
			'<a href="#" class="remove"></a>' + date_name +
			'<input type="hidden" name="enable_'+field_name+'" value="'+date_name+'" />' +
			' <input type="text" class="date" name="date_'+field_name+'" value="'+date+'" size="10" />'+
			'</li>';
	}

	function remove_custom(e)
	{
		var t = $(this);
		t.parent().hide().children(".date").val("_deleted_");
		return false;
	}

	// Build list of dates for later hiding
	function print_dates()
	{
		var html = '<ul id="alert_dates">',
			field_count = 0,
			group,
			custom,
			custom_initial_title = "Custom Title";

		$.each(dates, function(name) {
			html += date_li(name, field_count++, this);
		});
		$.each(custom_dates, function(name) {
			html += date_custom_li(name, field_count++, this)
		});

		html += '<li class="custom_add"><a href="#" class="add" title="Add custom date"></a><input type="text" name="custom" value="" size="15" /> <input type="text" class="date" name="date_custom" value="" size="10"></li>';

		html += "</ul>";
		container.html(html);

		// Set up actions
		group = $("input.date", container);
		group.datepicker({ showOn: "both", buttonImage: button_img, buttonImageOnly: true });
		group.filter(":disabled").datepicker("disable");
		group.change( function(e) {
			$(this).siblings("label").children(":checkbox").attr("checked", (this.value == '') ? '' : 'checked');
		});

		$("li.custom a").click(remove_custom);

		custom = $("li.custom_add input", container);
		custom.eq(0).focus(function(e) {
			var t = $(this);
			if (t.val() == custom_initial_title) {
				t.val("").css("color", "");
			}
		});
		custom.eq(0).blur(function(e) {
			var t = $(this);
			if (t.val() == '') {
				t.css("color", "#CCC").val(custom_initial_title);
			}
		});

		// Add custom date
		$("li.custom_add a", container).click(function(e) {
			var title = custom.eq(0).val(),
				date = custom.eq(1).val(),
				new_li;

			if (title == custom_initial_title || date == '') {
				alert("Assign a title and date to add for this custom date");
				return false;
			}
			new_li = $("li.custom_add").before(date_custom_li(title, field_count++, date)).prev();
			$("a", new_li).click(remove_custom);
			$("input.date", new_li).datepicker({ showOn: "both", buttonImage: button_img, buttonImageOnly: true });
			custom.val("");
			custom.eq(0).blur();
			return false;
		});
	};

	print_dates();
	relationship.change(relationship_change);
	relationship.change();
	$("li.custom_add input").eq(0).blur();
};

person_style_setup = function(styles)
{
	var list = $("#style_list"),
		select = $("select", list);


	function remove_style(el, opt)
	{
		$(el).parent("li").remove();
		opt.disabled = false;
		return false;
	}

	function add_style(opt)
	{
		if (!opt || opt.value <= 0) {
			return;
		}
		list.append('<li><a href="#" class="remove"></a><input type="hidden" name="styles[]" value="'+opt.value+'"/>'+opt.text+"</li>");
		$("li:last-child a.remove", list).click(function(e) { return remove_style(this, opt) });
		opt.disabled = true;
	}


	select.change(function(e) {
		add_style(this.options[this.selectedIndex]);
		this.selectedIndex = 0;
	});

	// Add styles passed in
	$.each(select.get(0).options, function() {
		if ($.inArray(parseInt(this.value, 10), styles) > -1) {
			add_style(this);
		}
	})
};

/*!
	Kwicks for jQuery (version 1.5.1)
	Copyright (c) 2008 Jeremy Martin
	http://www.jeremymartin.name/projects.php?project=kwicks

	Licensed under the MIT license:
		http://www.opensource.org/licenses/mit-license.php

	Any and all use of this script must be accompanied by this copyright/license notice in its present form.
*/
(function($){$.fn.kwicks=function(n){var p={isVertical:false,sticky:false,defaultKwick:0,event:'mouseover',spacing:0,duration:500};var o=$.extend(p,n);var q=(o.isVertical?'height':'width');var r=(o.isVertical?'top':'left');return this.each(function(){container=$(this);var k=container.children('li');var l=k.eq(0).css(q).replace(/px/,'');if(!o.max){o.max=(l*k.size())-(o.min*(k.size()-1))}else{o.min=((l*k.size())-o.max)/(k.size()-1)}if(o.isVertical){container.css({width:k.eq(0).css('width'),height:(l*k.size())+(o.spacing*(k.size()-1))+'px'})}else{container.css({width:(l*k.size())+(o.spacing*(k.size()-1))+'px',height:k.eq(0).css('height')})}var m=[];for(i=0;i<k.size();i++){m[i]=[];for(j=1;j<k.size()-1;j++){if(i==j){m[i][j]=o.isVertical?j*o.min+(j*o.spacing):j*o.min+(j*o.spacing)}else{m[i][j]=(j<=i?(j*o.min):(j-1)*o.min+o.max)+(j*o.spacing)}}}k.each(function(i){var h=$(this);if(i===0){h.css(r,'0px')}else if(i==k.size()-1){h.css(o.isVertical?'bottom':'right','0px')}else{if(o.sticky){h.css(r,m[o.defaultKwick][i])}else{h.css(r,(i*l)+(i*o.spacing))}}if(o.sticky){if(o.defaultKwick==i){h.css(q,o.max+'px');h.addClass('active')}else{h.css(q,o.min+'px')}}h.css({margin:0,position:'absolute'});h.bind(o.event,function(){var c=[];var d=[];k.stop().removeClass('active');for(j=0;j<k.size();j++){c[j]=k.eq(j).css(q).replace(/px/,'');d[j]=k.eq(j).css(r).replace(/px/,'')}var e={};e[q]=o.max;var f=o.max-c[i];var g=c[i]/f;h.addClass('active').animate(e,{step:function(a){var b=f!=0?a/f-g:1;k.each(function(j){if(j!=i){k.eq(j).css(q,c[j]-((c[j]-o.min)*b)+'px')}if(j>0&&j<k.size()-1){k.eq(j).css(r,d[j]-((d[j]-m[i][j])*b)+'px')}})},duration:o.duration,easing:o.easing})})});if(!o.sticky){container.bind("mouseleave",function(){var c=[];var d=[];k.removeClass('active').stop();for(i=0;i<k.size();i++){c[i]=k.eq(i).css(q).replace(/px/,'');d[i]=k.eq(i).css(r).replace(/px/,'')}var e={};e[q]=l;var f=l-c[0];k.eq(0).animate(e,{step:function(a){var b=f!=0?(a-c[0])/f:1;for(i=1;i<k.size();i++){k.eq(i).css(q,c[i]-((c[i]-l)*b)+'px');if(i<k.size()-1){k.eq(i).css(r,d[i]-((d[i]-((i*l)+(i*o.spacing)))*b)+'px')}}},duration:o.duration,easing:o.easing})})}})}})(jQuery);

/*!
 * jQuery Easing v1.3 - http://gsgd.co.uk/sandbox/jquery/easing/
 *
 * Open source under the BSD License.
 *
 * Copyright � 2008 George McGinley Smith
 * All rights reserved.
*/

/*
 * Uses the built in easing capabilities added In jQuery 1.1
 * to offer multiple easing options
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 *  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 *  GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 *
*/

// t: current time, b: begInnIng value, c: change In value, d: duration
jQuery.easing['jswing'] = jQuery.easing['swing'];

jQuery.extend( jQuery.easing,
{

	easeInOutQuad: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t + b;
		return -c/2 * ((--t)*(t-2) - 1) + b;
	}
});
