// global variable
var COOL = {};


$(document).ready(function() {
	// login nav, tip bubble
	$('#navLogin .help').qtip({
		content: 'Creating an account and logging into coolchurch.com will enhance your online experience on our website. You will be able to find a lifegroup, register for events, view your giving history, and more! For more information, contact <a href="mailto:Webmaster@coolchurch.com">Webmaster@coolchurch.com</a>.',
		show: 'mouseover',
		hide: {
			when: 'mouseout',
			fixed: true 
		},
		position: {
			corner: {
				target: 'bottomLeft',
				tooltip: 'topRight'
			}
		},
		style: { 
			name: 'cream', // Inherit from preset style
			border: {
				width: 1,
				radius: 8
			},
			width: 300,
			tip: 'topRight'
		}
	});
	
	// datepicker
	$('.datepicker').datepicker({
		showOtherMonths: true,
		selectOtherMonths: true
	});
	$('.datepickerMinToday').datepicker({
		showOtherMonths: true,
		selectOtherMonths: true,
		minDate: 0
	});
	$('.datepickerMonthsYears').datepicker({
		showOtherMonths: true,
		selectOtherMonths: true,
		changeMonth: true,
		changeYear: true
	});
	// dynamically update date range min and max
	$(".dateRange[data-dateRangeType=to]").each(function() {
		var sDateRangeToId = this.id;
		var sDateRangeFromId = $(this).attr("data-dateRangeAssocId");

		// from max = to min
		$("#" + sDateRangeFromId).datepicker( "option", "onSelect", function( dateText, datePicker ) {
			$("#" + sDateRangeToId).datepicker( 'option', 'minDate', new Date(dateText) );
		});

		// to max = from min
		$("#" + sDateRangeToId).datepicker( "option", "onSelect", function( dateText, datePicker ) {
			$("#" + sDateRangeFromId).datepicker( 'option', 'maxDate', new Date(dateText) );
		});
	}); 
	
	// top nav
	// call supersubs first, then superfish, so that subs are not display:none when measuring. 
	// call before initialising containing tabs for same reason. 
	$("ul#navSubList").supersubs({ 
		minWidth:    12, // minimum width of sub-menus in em units 
		maxWidth:    27, // maximum width of sub-menus in em units 
		extraWidth:  1 // extra width can ensure lines don't sometimes turn over due to slight rounding differences and font-family
	}).superfish();
	
	// side nav
	/*$("#navContent ul:first").addClass('sf-menu-vertical').addClass('sf-vertical').supersubs({ 
		minWidth:    12, // minimum width of sub-menus in em units 
		maxWidth:    27, // maximum width of sub-menus in em units 
		extraWidth:  1 // extra width can ensure lines don't sometimes turn over due to slight rounding differences and font-family
	}).superfish();*/
	
	// colorbox modals
	// onClosed fires onOpen
	$(".colorboxFit").colorbox({
		opactiy:0.4
	});
	$(".colorboxBio").colorbox({
		opactiy:0.4,
		onComplete: function() {
			// save reference to colorbox 'a' tag
			eLink = $.fn.colorbox.element();
			
			// get attributes
			sBio = $.trim( $(eLink).attr("title") );
			sPhone = $.trim( $(eLink).parents("tr:first").children(".phone").html() );
			sEmail = $.trim( $(eLink).parents("tr:first").find(".email a").html() );
			sName = $.trim( $(eLink).parents("tr:first").find(".name a").html() );
			sTitle = $.trim( $(eLink).parents("tr:first").find(".name em").html() );
			
			// create html
			sHtml = "<b>" + sTitle + "</b>";
			if( sPhone ) {
				sPhone = $.trim( sPhone.replace("&nbsp;", "") );
				if( sPhone.length ) {
					sHtml+= "<br /><em>Phone: </em>" + sPhone;
				}
			}
			if( sEmail ) {
				sEmail = $.trim( sEmail.replace("&nbsp;", "") );
				if( sEmail.length ) {
					sHtml+= "<br /><em>Email: </em><a href='mailto:" + sEmail + "'>" + sEmail + "</a>";
				}
			}
			if( sBio ) {
				sBio = $.trim( sBio.replace("&nbsp;", "") );
				if( sBio.length ) {
					sHtml+= "<br />" + sBio;
				}
			}
			
			// insert html
			$("<div style='margin: 5px 5px 0 5px;'>"+sHtml+"</div>").insertAfter("#cboxPhoto");
			$("#cboxTitle").html(sName);
			
			$.fn.colorbox.resize();
		} 
	});
	$(".colorbox").colorbox({
		iframe:true,
		opactiy:0.4,
		width:770,
		height:550
	});
	$(".colorboxBig").colorbox({
		iframe:true,
		opactiy:0.4,
		width:870,
		height:"92%"
	});
	$(".colorboxBiggest").colorbox({
		iframe:true,
		opactiy:0.4,
		width:"96%",
		height:"96%"
	});
});

/* convert prev/next links to history buttons */
function onOpenColorbox() {
	//console.info("onOpenColorbox");
	$("#cboxNext,#cboxPrevious").show();
	$("#cboxNext").click(onClickColorboxNext);
	$("#cboxPrevious").click(onClickColorboxPrevious);
}
function onClickColorboxNext(e) {
	//console.info("onClickColorboxNext");
	history.forward();
}
function onClickColorboxPrevious(e) {
	//console.info("onClickColorboxPrevious");
	history.back();
}

