/* default template js */
$(document).ready(function()
{
	/* prefill search button */
	if ( $('#searchterm').val()=='' ) 
	{ 
		input_show_default('#searchterm', 'Search..'); 
	}
	
	/* support for accordion */
	if($(".accordion").length)
	{
		$(function() {
			$(".accordion").accordion({
				active: false,
				collapsible: true,
				autoHeight: true,
				clearStyle: true
			});
		});
	}

	/* lightbox gallery support for CMS */
	if($(".seriesLightbox").length)
	{
		$(function() 
		{	
			var lightbox2 = $(".seriesLightbox").ariaLightbox({
				imageArray: [],
				altText: "alt",
				descText: "title",
				useDimmer: true,
				pos: "auto",
				title: "Gallery",
				em: false
			});		
			
		});		
	}
	if($(".slideshow").length)
	{
		$('.slideshow').cycle({ 
			prev:   '#prev', 
			next:   '#next', 
			timeout: 2000 
		});
	}
	
	/* support for tabs */
	if($(".tabs").length)
	{
		$(function() {
			$(".tabs").tabs();
		});
	}
	
	/* public menu */
	$("#goto_public").click(function() 
	{
		$("#publicMenu").show();
		$("#memberMenu").hide();
		$.cookie('menu_section_state', '1', { expires: 7, path: '/' });
	});
	
	/* members menu */
	$("#goto_members").click(function() 
	{
		$("#publicMenu").hide();
		$("#memberMenu").show();
		$.cookie('menu_section_state', '2', { expires: 7, path: '/' });
	});
	
	/* menu */    	
	//$.get('/_cache/menu/page_menu.html', function(data){
	//	$('#navwrapper').menu({ content: $('#navwrapper').html(), flyout: true, mode: 'dropdown', direction: 'right'	});
	//});
	$.jFastMenu("#pubnav");//('#navwrapper ul').droppy();
	$.jFastMenu("#memnav");
	
	/* date picker */
	if( $('.cmsform input.date').length )
	{
		$(function(){
			// Datepicker
			$('.cmsform input.date').datepicker({
				inline: true,
				dateFormat: 'yy-mm-dd',
				showOn: 'button', 
				buttonImage: '/_templates/feaweb/img/calendar_icon.gif',
				buttonImageOnly: true,
				showOn: 'both',
				numberOfMonths: 1
			});
					
		});
	}
	
	/* Page rating stats */
	if($(".stat").length)
	{
		var page_id = $('.stat').attr('id');
		
		if(!$.cookie(page_id))
		{
			$('.stat').rater({ postHref: 'rate/page' });
		}
	}
	
//	/* FAQ commenting  */
//	if($(".faq_comment").length)
//	{
//		$(".faq_comment").submit(function()
//		{
//
//			$.ajax({
//				   type: "POST",
//				   url: "/comment/faq/item_id/"+$(this).find("input[name='faq_id']").val(),
//				   data: {body: $(this).find("textarea[name='body']").val(), item_type: 'faq'},
//				   beforeSend: function(){
//
//					},
//				   success: function(msg){
//					alert('Thanks for your input.');
//					
//				}
//
//			});
//			$(this).remove();
//			//$(this).preventDefault();
//			return false;
//		});
//	}

	/* page commenting  */
	if($("form.page_comment").length)
	{
		$("form.page_comment").submit(function()
		{


//			$(this).remove();
			
			return false;
		});
		
	}
	
	/* page commenting  */
	if($(".submit_comment").length)
	{
		$(".submit_comment").click(function()
		{
			$.ajax({
				   type: "POST",
				   url: "/comment/page/item_id/"+$(this).attr('id').replace('_comment', '').replace('page_', ''),
				   data: {body: $(".page_comment textarea[name='body']").val(), data: $(".page_comment input[name='data']").val() },
				   beforeSend: function(){
					   if($(".page_comment textarea[name='body']").val()=="")
					   {
						   if(!$("table#comment_table tr#comment_notice").length)
						   {
							   $("table#comment_table tr:first").before('<tr id="comment_notice"><td></td><td><span>Please fill in the comments box before sending.</span></td></tr>')
							  // alert('here');
						   }
						   return false;
					   }
					},
				   success: function(msg){
						 $(".page_comment").replaceWith('Your comment has been submitted for approval.');
				}

			});

			
			return false;
		});
		
	}
	
	/* Comment body area */
	if($("textarea.comment_body").length)
	{
	
//		$("textarea.comment_body").focus(function(evt)
//		{
//			$(this).css({'height': '30px'});
//		});
//		
//		$(".field .comment_body").blur(function(evt)
//		{
//			if($(this).val()=='')
//			{
//				$(this).css({'height': '30px'});
//			}
//			
//		});
		
		$("textarea.comment_body").autogrow();
	};
	if($("span#count_before").length)
	{
		countdown(2010,02,28,$("span#count_before").text(),$("span#count_today").text());
	};
});

/* faq - show more */
function faq_show_more( span_id )
{
	$("#"+span_id).hide();
	$("#"+span_id+"_more").show();
}

function faq_show_rest( span_id )
{
	$("."+span_id).css('display','block');
	$('#viewall').css('display','none');
}
//
///* faq - rate item */
//function faq_rate( faq_id )
//{
//	$.ajax({
//		   type: "POST",
//		   url: "/rate/faq/item_id/"+faq_id,
//		   beforeSend: function(){
//
//			},
//		   success: function(msg){
//			alert('Thanks for your input.');
//		}
//
//	});
//}

/* faq - show more */
function show_comments( id )
{

	$("#"+id+"_comments").show();
}


function countdown(yr,m,d,before,current){
	//change the text below to reflect your own,
	var before = $("span#count_before").text();//"you can Win a 7-night stay by Activating your Membership card.";
	var current = $("span#count_today").text();//"Today is the Last day to Activate your card if you want to Win a 7-night stay";
	var montharray=new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
	theyear=yr;themonth=m;theday=d
	var today=new Date()
	var todayy=today.getYear()
	if (todayy < 1000)
	todayy+=1900
	var todaym=today.getMonth()
	var todayd=today.getDate()
	var todayh=today.getHours()
	var todaymin=today.getMinutes()
	var todaysec=today.getSeconds()
	var todaystring=montharray[todaym]+" "+todayd+", "+todayy+" "+todayh+":"+todaymin+":"+todaysec
	futurestring=montharray[m-1]+" "+d+", "+yr
	dd=Date.parse(futurestring)-Date.parse(todaystring)
	dday=Math.floor(dd/(60*60*1000*24)*1)
	dhour=Math.floor((dd%(60*60*1000*24))/(60*60*1000)*1)
	dmin=Math.floor(((dd%(60*60*1000*24))%(60*60*1000))/(60*1000)*1)
	dsec=Math.floor((((dd%(60*60*1000*24))%(60*60*1000))%(60*1000))/1000*1)
	if(dday==0&&dhour==0&&dmin==0&&dsec==1&&$("#count2").length())
	{
		$("#count2").text(current);
		return
	}
	else
	{
	$("#count2").text("Only "+dday+ " days, "+dhour+":"+dmin+":"+dsec+" until "+before);
	setTimeout("countdown(theyear,themonth,theday,'"+before+"','"+current+"')",1000);
	}
}