/**
 * Practical Action Group JS (Not blogs/video/old practical answers)
 * 
 * @author jherring
 */
var twitterBoxes = new Array;
jQuery(document).ready(function($) {
	
	/**
	 * Feature carousel
	 */
	$('.feature_carousel').jcarousel({
		vertical : true,
		scroll : 1,
		visible : 3,
		wrap : 'circular',
		start : 1,
		// offset: 2,
		auto : 10,
		itemFirstInCallback : {
			onBeforeAnimation : carouselDeselect
		},
		itemLastInCallback : {
			onBeforeAnimation : carouselSelect
		},
		itemLastOutCallback : {
			onBeforeAnimation : carouselDeselect
		}, 
		initCallback: function(carousel) {
			carousel.clip.hover(function() {
				carousel.stopAuto();
			}, function() {
				carousel.startAuto();
			});
		},
		itemLoadCallback: function (carousel) {
			
			// Loading a twitter widget if we can.. this is so the carousel doesn't have to wait
			// for other scripts to kick in before finishing the load.
			twitterWidget('twitterBox');
		}
	});
	
	// Attempt to get the twitter box if theres no carousel 
	if ($('.feature_carousel').length == 0) {
		twitterWidget('twitterBox');
	}
	
	function carouselSelect(carousel, item, index, state) {
		$(item).addClass('selected');
		var image = $(item).children('.feature_image').css('backgroundImage');
		var width = $(item).children('.feature_image').css('width');
		$('.main_feature_image').hide();
		$('.main_feature_image').css('backgroundImage', image);
		$('.main_feature_image').fadeIn();
	}
	
	function carouselDeselect(carousel, item, index, state) {
		$(item).removeClass('selected');
	}
	
	/**
	 * Practical Answers Download/Login/Register Popups
	 */
	$("a.practical_answers_download").live('click', function(e) {
		e.preventDefault();
//		$('#dialog').html('<div class="loading">Loading..</div>');
		$('#dialog').html('<iframe id="dialog-iframe" marginheight="0" marginwidth="0" frameborder="0"></iframe>').dialog({
			position: 'center',
			title: $(this).attr('title'),
			height: 600,
			width: 880,
			modal: true,
			draggable: false,
			resizable: false,
			buttons: {
				"Close window": function() {
					$( this ).dialog( "close" );
				}
			}
		});
		$('#dialog-iframe').attr('src', $(this).attr('href')).css({width: $('#dialog').actualInnerWidth() - 10, height: $('#dialog').actualInnerHeight() - 10});
	});
	
	$("a.practical_answers_preview").live('click', function() {
		$('#dialog').html('<div class="loading">Loading..</div>');
		$("#dialog").load($(this).attr('href'), function() {
			$( "button, input:submit, a.button").button();
			if (typeof IN!='undefined') {
				IN.parse();
			}
			if (typeof addthis != 'undefined' && twttr != 'undefined') {
				addthis.toolbox(".social_links");
				twttr.widgets.load();
			}
		}).dialog({
			position: 'center',
			title: $(this).attr('title'),
			height: 700,
			width: 880,
			modal: true,
			draggable: false,
			resizable: false,
			buttons: {
				"Close window": function() {
					$( this ).dialog( "close" );
				}
			}
		});
		return false;
	});
	
	$("body").each(function() {
		/*$("#dialog #MemberPracticalAnswersLoginForm, #dialog #DownloadAddForm, #dialog .register_form FORM", this).live('submit', function(e) {
			e.preventDefault();
			var formdata = $(this).serialize();
			$('#dialog').html('<div class="loading">Loading..</div>');
			$.post($(this).attr('action'), formdata, function(returndata) {
				$('#dialog').html(returndata);
				$( "button, input:submit, a.button").button();
			});
		});*/
		/*$("#dialog .practical_answers_small_login_form a.button, .register_form a.button", this).live('click', function(e) {
			e.preventDefault();
//			$('#dialog').dialog({title: $(this).attr('title')});
			$('#dialog').html('<div class="loading">Loading..</div>');
			$("#dialog").load($(this).attr('href'), function() {
				$( "button, input:submit, a.button").button();
			});
		});*/
	});
	
	/**
	 * SSO Popups
	 */
	/*
	var ssoBoxSizes = new Array();
	ssoBoxSizes['sso_twitter'] = '500x250';
	ssoBoxSizes['sso_facebook'] = '400x200';
	ssoBoxSizes['sso_linkedin'] = '300x200';
	ssoBoxSizes['sso_openid'] = '300x200';
	$('#sso a').click(function(e) {
		e.preventDefault();
		var boxsize = ssoBoxSizes[$(this).attr('id')].split('x');
		window.open($(this).attr('href'), $(this).attr('title'), 'width=' + boxsize[0] + ', height=' + boxsize[1] + ', top=' + (e.pageX - 200) + ', left=' + (e.pageY - 200));
	});
	*/
	/**
	 * Search
	 */
	$('.search-box-advanced-label a').click(function(e) {
		e.preventDefault();
		if ($('.search-box-advanced').css('display') == 'none') {
			$('.search-box-advanced').show();
		} else {
			$('.search-box-advanced').hide();
		}
	});
	
	if ($('.search-box input#SearchQuery').val() == '') {
		$('.search-box input#SearchQuery').val('Start your search');
	}
	
	$('.search-box input#SearchQuery').focusin(function() {
		if ($(this).val() == 'Start your search') {
			$(this).val('');
		}
	}).focusout(function() {
		if ($(this).val() == '') {
			$(this).val('Start your search');
		}
	});
	$('.search-box FORM').submit(function() {
		if ($('.search-box input#SearchQuery').val() == '' || $('.search-box input#SearchQuery').val() == 'Start your search') {
			alert('Please provide a search query');
			return false;
		}
	});
});

/**
 * Jquery Functions
 */
$.fn.actualInnerWidth = function() {
	return (this.innerWidth() - parseInt(this.css('paddingLeft').replace('px', '')) - parseInt(this.css('paddingRight').replace('px', '')));
};
$.fn.actualInnerHeight = function() {
	return (this.innerHeight() - parseInt(this.css('paddingTop').replace('px', '')) - parseInt(this.css('paddingBottom').replace('px', '')));
};

/**
 * Other functions
 */
function twitterWidget(selectorId) {
	if ($('#' + selectorId).length > 0 && !(selectorId in twitterBoxes)) {
	    $.getScript(('https:' == document.location.protocol ? 'https://' : 'http://') + 'widgets.twimg.com/j/2/widget.js', function() {
			var twitter = new TWTR.Widget({
			  version: 2,
			  type: 'profile',
			  rpp: 4,
			  id: selectorId,
			  interval: 6000,
			  width: 'auto',
			  height: 300,
			  theme: {
			    shell: {
			      background: '#ffffff',
			      color: '#919191'
			    },
			    tweets: {
			      background: '#ffffff',
			      color: '#000000',
			      links: '#e2014c'
			    }
			  },
			  features: {
			    scrollbar: false,
			    loop: false,
			    live: false,
			    hashtags: false,
			    timestamp: false,
			    avatars: false,
			    behavior: 'all'
			  }
			}).render().setUser('practicalaction').start();
	    });
	    
	    // set the box to true so we know not to load it again
	    twitterBoxes[selectorId] = true;
	}
}
