/* Author: Liberty Concepts
 * v.2.0
*/

$(document).ready(function(){

	// Main Navigation - depends on superfish-1.4.8-yui
	if ($("ul.sf-menu").length) {
		$("ul.sf-menu").supersubs({
				minWidth:15,
				maxWidth:30,
				extraWidth:1 
		}).superfish({delay:400,speed:200 }).find('ul').bgIframe({
				opacity:false
		});
	}

	// Text Resizer
	$('#font_select').click(function(){
		$('.content:not("h1,h2,h3,h4")').animate({'font-size':'100%','line-height':'150%'}, 500);
	});
	$('#font_select_mid').click(function(){
		$('.content:not("h1,h2,h3,h4")').animate({'font-size':'120%','line-height':'180%'}, 500);
	});
	$('#font_select_large').click(function(){
		$('.content:not("h1,h2,h3,h4")').animate({'font-size':'150%','line-height':'200%'}, 500);
	});

	//twitter feed - depends on Twitter blogger.js plugin
	$('#twitter_update_list').cycle({
		fx: 'scrollUp' ,
		timeout: 5000,		
		cleartypeNoBg: true		
	});

	// Flickr Feed - depends on jflickrfeed plugin
	$('#flickr').jflickrfeed({
		limit: 6,
		qstrings: {
			id: '37304598@N02'
		},
		itemTemplate:
		'<li>' +
			'<a rel="colorbox" href="{{image}}" title="{{title}}">' +
				'<img src="{{image_s}}" alt="{{title}}" />' +
			'</a>' +
		'</li>'
	}, function(data) {
		$('#flickr li a').colorbox();
	});
	
	// video colorbox not implemented anywhere yet
	//$('a.cb').colorbox({iframe:true, innerWidth:560, innerHeight:421});
		
	//fade siblings on hover
	$(".social a, #flickr li").live({
		mouseenter:
		 function() {
			$(this).siblings().stop().animate({opacity: 0.5}, "200"); 
		 },
		mouseleave:
		 function() {
			$(this).siblings().stop().animate({opacity: 1.0}, "200");   
		 }
		}
   );

	// slide show depends on cycle plugin
		$('.slides').cycle({
		fx: 'scrollHorz', // requires cycle.all for any fx besides fade
		timeout: 8000,
		cleartypeNoBg: true,
		
		// next and previous slide links
		next: '.next',
		prev: '.prev',

		pager:  '.controls',
		
		// put slide numbers in the controls
		// pagerAnchorBuilder:function(idx,slide){
		// return '<li><a href="#">'+ parseFloat(idx+1) +'</a></li>';
		// }
		
		// put bullets in the controls
		pagerAnchorBuilder: function(index, el) {
				return '<li><a href="#">&bull;</a></li>';     
		}
		
	});

	//tab widget
	//$('.tab').hide(); do this in css instead
	$('.activetab').show();
	$('#tabnav li').click(function (e) {
			var $myId = $(this).find('a').attr('href');
			var $myTab = $('#tabs').find('div#'+$myId);	
			$(this).addClass('activetab');
			$(this).siblings('li').removeClass('activetab');	
			$($myTab).siblings('div.tab').hide();	
			$($myTab).fadeIn();
			e.preventDefault();		
	});

	// multiple elements cycle
	$('#multiscroll').multipleElementsCycle({
		container: '#container',
		next: '.nextMulti',
		prev: '.prevMulti',
		start: 1, // defaults to 0 which will hide "prev" link
		show: 5, // how many elements to show at once
		speed: 400,
		scrollCount: 5 // how mwny elements to scroll when clicking next/prev
	});
	
	// show image captions above multi-cycle
	// did this on murphyforpa.com & thought it was handy
	$('#multi img').hover(function () {
		myDefault = $('#writeTitle').html();
		myTitle = $(this).prev('span').html();	
		$('#writeTitle').html('<em>'+myTitle+'</em>');
			}, function () {
			$('#writeTitle').html(myDefault);
		}
	);

	// validate forms - depends on validation plugin 1.8.1
	$("#theform").validate({
		rules: {
			/*fname: "required", // if input has class "required" you don't need these
			lname: "required",*/
			"contribute[amount]" : "required", // contribute[amount] is default CMS name for payment option radio buttons
			email: {
				/*required: true,*/
				email: true
			},
			zip: {
				digits: true, // don't allow letters in here
				minlength: 5
			}
		},
		messages: {
			fname: "Please enter your first name",
			lname: "Please enter your last name",
			email: "Please enter a valid email address",
			zip: 	 "Please enter your zip code",
			message: "C'mon, tell!",
			"contribute[amount]": "Please select an amount"
		},
		errorElement: "div",
        wrapper: "div",  // a wrapper around the error message
        errorPlacement: function(error, element) {
					offset = element.offset();
					error.insertBefore(element)
					error.addClass('message');  // add a class to the wrapper
					error.css('position', 'absolute');
					error.css('left', offset.left + element.outerWidth()*.04);
					error.css('top', offset.top - element.outerHeight());
					if (element.attr('type') == 'radio' || element.attr('type') == 'checkbox'){
							element = element.parent().parent(); // parent element is label; get parent of that
							error.css('left', offset.left + element.outerWidth()*.01);
							error.css('top', offset.top - element.outerHeight()*1.2); 
					}
					if (element.attr('type') == 'textarea'){
							error.css('left', offset.left + element.outerWidth()*.01);
							error.css('top', offset.top - element.outerHeight()/2); 
					}
        }		
	});

	// open external links in new window
	$('a').each(function() {
		$.expr[':'].external = function(obj){
			return !obj.href.match(/^mailto\:/)
			&& (obj.hostname != location.hostname)
			&& !obj.href.match(/^javascript\:/)
			&& !obj.href.match(/^http\:\/\/www.youtube.com\//)
		};
		$('a:external').addClass('external').attr('target','_blank');
	});

	
	// css for IE <9 - use first-shild instead of last to reduce need for extra js for css
	// $('').css({'':''});

});
