DD_roundies.addRule('#top-nav, #login, #main, #tagline, #nav, #nav .section, #nav .section ul li a.top, .callout, .callout div, #article-tools, #article-tags, #written-request, #signs, #content .book-info', '4px'); //4 round corners
DD_roundies.addRule('#tabs li a', '4px 4px 0 0'); //2 top round corners
DD_roundies.addRule('#tab-contents', '0 0 4px 4px'); //2 bottom round corners

jQuery.fn.exists = function(){return jQuery(this).length>0;}

$(function(){
	//left nav functionality
	$('#nav .section li li a').each(function(){
		if($(this).siblings().size() > 0){
			$(this).toggle(
				function(){
					$('#nav .section li ul:visible').parent().children('a').trigger('click');
					$(this).parent().addClass('open').end().next('ul').slideDown();
				},
				function(event){
					$(this).parent().removeClass('open').end().next('ul').slideUp();
				}
			);
		}
	});
	$('#nav .section li li li a.current').parent().parent().parent().children('a').trigger('click'); //trigger the toggle to open the subnav if it contains a link with .current
	
	//
	$('#tabs').tabs({ fx: { opacity: 'toggle' } }).tabs('rotate', 10000, false);
	
	
	
	
	//calendar datepicker functionality
	if ($('month-header h2').exists()){
		$('.month-header h2').click(function() {
			var dateID = $(this).next('.calendar-month').attr('id');
			var $dateInput = $('#' + dateID);
			var dateData = $dateInput.val();
			var offSet = $dateInput.prev('h2').offset();
			var startDate = $.datepicker.parseDate('mm/dd/yy', dateData);
			$dateInput.datepicker({dateformat: 'mm/dd/yy'});
			$dateInput.datepicker('setDate', startDate);
			$dateInput.datepicker('show');
			$("#ui-datepicker-div").css( { 'left': offSet.left + 390 + 'px', 'top': offSet.top + 25 + 'px' } );
		});
	}
	
	//text resizer functionality
	if ($('#article-tools .larger').exists()){
		$('#article-tools .larger, #article-tools .smaller').click(function(){ //when the smaller/larger actions are clicked
			var currentFontSize = parseFloat($('#article-content p, #food-article-content p').css('font-size'), 10); //get the current font size as a decimal value
			var currentLineHeight = parseFloat($('#article-content p, #food-article-content p').css('line-height'), 10); //get the current line height as a decimial value
			
			if($(this).attr('class') == 'larger'){  //check to see which one was clicked, if larger
				newFontSize = currentFontSize + 2;  //add 2 to font size
				newLineHeight = currentLineHeight + 2 + 'px'; //add 2 and the 'px' extension to line height
			}
			else { //if smaller
				newFontSize = currentFontSize - 2; //subtract 2 from font size
				newLineHeight = currentLineHeight - 2 + 'px'; //subtract 2 from line height and add the 'px' extension
			}
	
			$('#article-content p, #food-article-content p').css('font-size', newFontSize); //set the new font size
			$('#article-content p, #food-article-content p').css('line-height', newLineHeight); //set the new line height
			return false;
		});
	}
	

	//clear search box
	$('input#keywords.search-text').focus(function(){
		$(this).val('');
	});
	$('input#keywords.text').focus(function(){
		$(this).val('');
	});
	$('input.newsletter-email').focus(function(){
		$(this).val('');
	});
	
	//show and hide login box
	$('#login-button').click(function(){
		$('#login').show();
		return false;	
	});
	$('#close').click(function(){
		$('#login').hide();	
	});
	
	if($('.zoom').exists()){
		$('.zoom').fancybox();
	}
	
	//popup functionality for print 
	if($('.print').exists()){
		$('.print').popupWindow({ 
			height:500, 
			width:800, 
			top:50, 
			left:50 
		}); 
	}
	//share a recipe functionality
	if ($('.share-recipe a').exists()){
		$(".share-recipe a").fancybox({
			'scrolling'		: 'no',
			'titleShow'		: false,
			'onClosed'		: function() {
				$("#share-recipe").hide();
			}
		});
	};
		/*sample validation
		$("#login_form").bind("submit", function() {

			if ($("#login_name").val().length < 1 || $("#login_pass").val().length < 1) {
				$("#login_error").show();
				$.fancybox.resize();
				return false;
			}
		
			$.fancybox.showActivity();
		
			$.ajax({
				type		: "POST",
				cache	: false,
				url		: "/data/login.php",
				data		: $(this).serializeArray(),
				success: function(data) {
					$.fancybox(data);
				}
			});
		
			return false;
		});*/
	//split archive page article list into 3
	if($('.archive-article-list').exists()){
		$('.archive-article-list').easyListSplitter({ colNumber: 3 });
	}
	if($('#archives .more').exists()){
		$('#archives .more').easyListSplitter({ colNumber: 2 });
	}
});

$(window).load(function(){
	$('.tab-content:eq(0)').show();
	$('.rotate-content').each(function(i){
		$(this).show().after('<div class="rotate-nav-container"><p class="title">Slideshow</p><div class="rotate-nav nav'+i+'"></div></div>').cycle( {
			pager: '.nav'+i, 
			timeout: 0,
			before: function (curr, next, opts, fwd){
		        var $ht = $(this).height();
		        if ($ht == '0'){
		        	$(this).parent().css('height', 'auto');	
		        } else {
			        $(this).parent().css('height', $ht);
			    }
			},
			cssFirst: { position: 'relative' }						
		});
	});
	if (($('.rotate-content > div').size()) <= 1) { //check if theres more than one slide
		$('.rotate-nav-container p.title').hide();
	}
});

//Powers the Editions Search by making keywords from the dropdown choices.

function keywordMaker() {
	var year = $('.year').val();
	var month = $('.month').val();
	if (year != null && month != null)
	{
  	$('#keywords2').val(month + " " + year);
	}
}

$(document).ready(function() {
	if ($('#keywords2').exists()){
		keywordMaker();
		$('.year').change(function() {
			keywordMaker();
			});
		$('.month').change(function() {
			keywordMaker();
			});
	}
});
