(function($){
	$.fn.selectdd = function() {
	
		// init
		this.css({display: 'none'})
		var div, ul, option, zIndex = 100;
		for (var i=0; i < this.length; i++) {
			div = this.eq(i).wrap('<div class="select"></div>').parent().css({zIndex: --zIndex});
			ul = div.prepend('<ul></ul>').children().eq(0);
			option = $('option', this.eq(i));
			var selected = 0;
			
			for (var j=0; j < option.length; j++) {
				if ( $(option.eq(j)).attr('selected') ) { selected = $(option).eq(j).index(); };
				var url = option.eq(j).attr('value');
				url = ( url == '' )? '#' : url;
				ul.append('<li><a href="' + url + '">'+ option.eq(j).text() +'</a></li>')
			}
			div.prepend('<a href="#" class="btn">'+ option.eq(selected).text() +'</a>');
		}
		// events
		$('.select .btn').click(function () {
			$(this).parent().find('ul').slideToggle();
			return false;
		});
		$('.select li a').click(function () {
			var id = $(this).parent().index();
			var par_id = $(this).parents('.select').find('select').attr('id');
			var div = $(this).parents('.select');
			div.find('select option').attr('selected', '');
			document.getElementById(par_id).selectedIndex = id;
			div.find('.btn').text($(this).text());
			div.find('ul').slideUp();
			return false;
		});
		
		$(document).click(function (e) {
			$('.select ul').slideUp();
		});
		
		return this;
	};


	$(window).load(function () {
		var height = Math.max($('.widg-l').outerHeight(), $('.widg-r').outerHeight());
		$('.widg').each(function (index) {
			//$(this).height(height);
		});
	});

	$(document).ready(function() {
		
		/* Events Calendar Functions */
		
		$('table.libraryEventsCalendar a.eventTitle').tooltip({
			effect: 'slide',
			relative: true,
			offset: [5, 0]
		}).dynamic();
		
		$('#AllTypes, #all_locations').change(function(){
			if ( $(this).is(':checked') ) {
				$(this).parents('.event-types').find('input').attr('checked', true);
				$(this).parents('.locations').find('input').attr('checked', true);
			} else {
				$(this).parents('.event-types').find('input').attr('checked', false);
				$(this).parents('.locations').find('input').attr('checked', false);
			};
		});
		
		$('.event-filters .col div input').change(function(){
			if ( $(this).is(':checked') ) {
				var all_checked = false;
				$(this).parents('.event-types').find('input').not('#AllTypes').each(function(){
					if ( ! $(this).is(':checked') ) {
						all_checked = false;
						return false;
					};
					all_checked = true;
				});
				if ( all_checked ) {
					$(this).parents('.event-types').find('#AllTypes').attr('checked', true);
				};
				
				$(this).parents('.locations').find('input').not('#all_locations').each(function(){
					if ( ! $(this).is(':checked') ) {
						all_checked = false;
						return false;
					};
					all_checked = true;
				});
				if ( all_checked ) {
					$(this).parents('.locations').find('#all_locations').attr('checked', true);
				};
				
				//$(this).parents('.locations').find('input').attr('checked', true);
			} else {
				$(this).parents('.event-types').find('#AllTypes').attr('checked', false);
				$(this).parents('.locations').find('#all_locations').attr('checked', false);
			};
		})
		
		/* End Events Calendar Functions */
		
		$('#navigation > li:last').addClass('last');
		$('#navigation li .sub-menu').each(function(){
			$(this).find('li:last').addClass('last');
		});
		
		$('#content .post .entry .gform_wrapper .gform_body .disabled').find('textarea').attr('disabled', 'disabled');
		
		$('#navigation li').hover(
			function(){
				$(this).find('> .sub-menu').show();
			}, function(){
				$(this).find('> .sub-menu').hide();
			});
		
		$("a#customizeLinks").fancybox({
			'hideOnContentClick': false
		});
		
		$('#quickLinksForm').submit(function(){
			var data = $(this).serialize();
			var req_url = $(this).attr('action');
			
			$.ajax({
				url: req_url,
				type: 'GET',
				data: data,
				dataType: 'json',
				cache: false,
				success: function(d) {
					$('#quickLinksForm').parent().find('.message').remove();
					if ( d.error == undefined ) {
						$('#quickLinksForm').slideUp();
						$('#quickLinksForm').parent().append('<p class="success message">' + d.s + '</p>');
					} else {
						$('#quickLinksForm').parent().find('h2').after('<p class="error message">' + d.error + '</p>');
					}
				}
			})
			return false;
		})
		
		if ( $('#importantMessage').length != 0 ) {
			$('#importantMessage').marquee('importantMessage');
		};
		
		$('a.textResizer').click(function(){
			var size = $(this).attr('rel');
			if ( size != '' ) {
				$('body').removeClass('s1 s2 s3').addClass(size);
			};
			
			return false;
		})
		
		$('.blink')
			.focus(function(){
				if( $(this).val() == $(this).attr('title') ) {
					$(this).val('');
				}
			})
			.blur(function(){
				if( $(this).val() == '' ) {
					$(this).val( $(this).attr('title') );
				}
			});
			
		$('.searchf select, .how select').selectdd();
		
		
	});
})(jQuery)
