
jQuery(document).ready(function() {

	/*
	 * Allow for manipulation of sidebar widgets
	 */
	jQuery('#sidebar-list').sortable({
				accept : 		'widget',
				axis:			'y',
				revert:			true,
				handle:			'h2',
				opacity:		0.5,
				zIndex:			10,
				cursor:			'move',
				cursorAt: 		{top:20}
			});
	
	
	/*
	 * Allow for manipulation of front page widgets
	 */
	jQuery('#home-rightWrap').sortable({
				accept : 		'draggable',
				axis:			'y',
				revert:			true,
				containment:	'document',
				handle:			'h2',
				opacity:		0.5,
				zIndex:			10,
				change:			function(e,ui) {
					if(jQuery('#home-rightWrap').sortable('toArray').length > 2) {
						if(jQuery(ui.item).attr('id') == jQuery('#home-rightWrap>div:first').attr('id') && 
							jQuery('#home-rightWrap>div:last').attr("id") != jQuery(ui.item).attr('id')) {

						   	jQuery('#home-leftWrap').prepend(jQuery('#home-rightWrap>div:last'));
						   							
						} else if(jQuery(ui.item).attr('id') != jQuery('#home-rightWrap>div:last').attr('id')) {
							
							jQuery('#home-leftWrap').prepend(jQuery('#home-rightWrap>div:last'));
							
						} else if(jQuery(ui.item).attr('id') == jQuery('#home-rightWrap>div:last').attr('id')) {
							
							jQuery('#home-leftWrap').append(jQuery('#home-rightWrap>div:first'));
							
						}
					}
				},
				cursor:			'move',
				connectWith: 	["#home-leftWrap"]
			});
			
	jQuery('#home-leftWrap').sortable({
				accept : 		'draggable',
				axis:			'y',
				revert:			true,
				handle:			'h2',
				opacity:		0.5,
				containment:	'document',
				zIndex:			10,
				change:			function(e,ui) {
					if(jQuery('#home-leftWrap').sortable('toArray').length > 2) {
						if(jQuery(ui.item).attr('id') == jQuery('#home-leftWrap>div:first').attr('id') && 
							jQuery('#home-leftWrap>div:last').attr("id") != jQuery(ui.item).attr('id')) {

						   	jQuery('#home-rightWrap').prepend(jQuery('#home-leftWrap>div:last'));
						   							
						} else if(jQuery(ui.item).attr('id') != jQuery('#home-leftWrap>div:last').attr('id')) {
							
							jQuery('#home-rightWrap').prepend(jQuery('#home-leftWrap>div:last'));
							
						} else if(jQuery(ui.item).attr('id') == jQuery('#home-leftWrap>div:last').attr('id')) {
							
							jQuery('#home-rightWrap').append(jQuery('#home-leftWrap>div:first'));
							
						}
					}
				},
				connectWith: 	["#home-rightWrap"], 
				cursor:			'move'
			});
	
	jQuery(".search-header").click(function() {
		jQuery(this).next().toggle();
		return false;
	});
	
	jQuery("#news-articles").change(function() {
		var articles = jQuery(this).val();
		var here = location.href;
		var joiner = "?";
		if(here.indexOf(joiner) > 0 && here.indexOf("articles=") > 0) {
			here = here.substring(0, here.indexOf(joiner));
		} else if(here.indexOf(joiner) > 0) {
			joiner = "&";
		}
		location.href = here + joiner + "articles=" + articles;
	});
	
	jQuery("select#results").change(function() {
		jQuery('form#filterform').submit();
	});
	
});


/*
 * Sub navigation menu slide from top
 */
/* disabled by JI because its annoying
jQuery('ul.sub-menu').ready(function() {
	
	if(jQuery('ul.sub-menu>li.current_page_item').length == 0) {	
		jQuery('ul.sub-menu').hide();
		jQuery('ul.sub-menu').slideDown(800);
	}
	
});
*/

jQuery(".search-section").ready(function() {
	jQuery(".search-section").hide();
});


/*
 * What's Hot interactivity
 */

jQuery("#whats-hot").ready(function() {
	initHot();
	
	jQuery("#whats-hot").hover(
		function () {
			
			jQuery("#whats-hot>img.title").attr('src', "/wp-content/themes/dircsa/images/whats-hot-expanded.gif");
			
			var hoverCSS = {
				'height' : 'auto',
				'background': '#fff'
			};
			
			jQuery("#whats-hot").css(hoverCSS);
			jQuery("#whats-hot>div.content").css('border', '3px dashed #cecece');
			jQuery("#whats-hot>div.content").slideDown(800);
		}, 
		function () {
			initHot();
		}
	);
		
	jQuery(window).resize(function(){
		initHot();
	});
	
});

function initHot() {
	
	var initCSS = {
		'position' : 'absolute',
		'width' : '176px',
		'height' : '31px',
		'top' : 158,
		'left' : jQuery('#container').width() -176,
		'display' : 'block',
		'background' : 'none',
		'text-align' : 'center',
		'overflow' : 'hidden'
	}
	
	jQuery("#whats-hot").css(initCSS);
	
	jQuery("#whats-hot>img.title").attr('src', "/wp-content/themes/dircsa/images/whats-hot.gif");
	jQuery("#whats-hot>div.content").hide();
	
}

function printSelected(table, field) {
	var values = new Array();
	var count = 0;
	
	jQuery('input[@name=directorySelect]:checked').each(function() {
		values[count] = jQuery(this).val();
		count++;
	});
	
	if(values.length == 0) return false;
	values = values.join(',');
	
	var url = '/wp-content/themes/dircsa/tbase_print.php?table=' + table + '&field=' + field + '&values=' + values;
	
	window.open(url, 'printSelected', "menubar=no,width=640,height=700,toolbar=no,scrollbars=yes");
	
	return false;
}

function selectAll() {
	jQuery('input[@name=directorySelect]').each(function() {
		this.checked = true;
	});
	
	return false;
}

function deselectAll() {
	jQuery('input[@name=directorySelect]').each(function() {
		this.checked = false;
	});
	
	return false;
}
