/**
 *  mobile javascript for JWJ  
 *  this is included on all pages
 */
 

/**
 * STYLESHEET FUNCTIONS
 * These implement different versions by changing cookie values and reloading
 */

function switchtomobilenarrow(){ 
	eraseCookie('fullsitestyle');
	// reload to reset javascript
	location.reload(); 
};

function switchtomobilefull() {
	createCookie('fullsitestyle','fullsitestyle' );
	// reload to reset javascript
	location.reload(); 
};

function checkquerystrings() {
	if (querystring('reset')) {
		eraseCookie('fullsitestyle');
		eraseCookie('panelstatus'); 
	};
	if (querystring('mobile')=='narrow') { 
		$('#wrapper').addClass('querystringnarrow');
	};
}

/**
 * HELPER FUNCTIONS
 *
 */

function ismobile(){ 
	if (querystring('mobile')  ) {return true; };
	return ( screen.width < 600 );
};

function isfullstyle(){ 
	// returns true if this user has chosen full style
	if (querystring('mobile')=='full'  || querystring('mobile')=='wide' ) return true;
	if (querystring('mobile')=='narrow')return false;	
	return readCookie('fullsitestyle') != null ;
};

function isnarrowstyle(){ return !isfullstyle(); };

/**
 * COOKIE FUNCTIONS
 *
 */

function createCookie(name,value,days)
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
function eraseCookie(name)
{
	createCookie(name,"",-1);
}

/**
 * INITIALIZE STYLE SHEETS AND BODY CLASSES
 * 
 */




if ( ismobile() ) {
	
	// diagonostic. checks if ?reset=true is set in url and erases cookies if it is.
	checkquerystrings();

	// update classes (strictly, shouldn't do jquery call till dom ready, but html is OK
	// could this be the odd problem with firefox 8 on osx?
	// can these be moved to initialize common , or will we FOUC?
	$('html').addClass('ismobile');
	$('html').addClass( ( isfullstyle() ) ? 'mobilefull' : 'mobilenarrow' );	
	
	// include stylesheet
	if ( isfullstyle()) {
		document.write('<link class="mobilestyles" rel="stylesheet" type="text/css" href="/css/mobilefull.css"  />') ;
	}
	else  {
		document.write('<link class="mobilestyles" rel="stylesheet" type="text/css" href="/css/mobile.css"  />') ;
	};


/**
 *  DOM READY initialization
 */

	$( initializecommon );
	
	if ( isnarrowstyle() ) {
		$( initializenarrow );
	};

//end ismobile
}

function initializecommon(){ 

// diagonostic. checks if ?reset=true is set in url and erases cookies if it is.
	checkquerystrings();
	
// BEHAVIOUR hide the address bar
	//setTimeout(function() { window.scrollTo(0, 1) }, 100);
	window.scrollTo(0, 1) 

// BEHAVIOR change styles
	$('html.mobilefull .switchstyles a').click(function(){
		  switchtomobilenarrow();
	});
	
	$('html.mobilenarrow .switchstyles a').click(function(){
		  switchtomobilefull();
	}); 
}

function initializenarrow(){ 


//	CHANGE the logo text and tagline
// we could avoid this by having #mobilelogo .ismobile .mobilenarrow specific entries in header-ssi and then adjusting mobile.css
	$('#logo').html('Journey with Jesus');
	$('#tagline').html('<p>A weekly webzine for the global church.<br />Daniel B. Clendenin, PhD.</p>').show();
	
// CLEANUP hide  the heading on individual essays
$('#content>p:contains(Weekly essays by Dan Clendenin)').hide();
	
	
// CLEANUP adjust the title of the new articles panel to shorten month
	function shortenmonth( title ) {
		var months = [ 
		[ 'january', 'Jan'],[ 'february', 'Feb'],[ 'march', 'Mar'],	[ 'april', 'Apr'],[ 'may', 'May'],	[ 'june', 'Jun'],	
		[ 'july', 'Jul'],[ 'august', 'Aug'],[ 'september', 'Sep'],[ 'october', 'Oct'],[ 'november', 'Nov'],[ 'december', 'Dec']];
		
		for ( var month = 0; month<12; ++month) {
			var reg = new RegExp(months[month][0], "i");
			title = title.replace( reg , months[month][1]);
		}
		return title;
	}
	
// CLEANUP the latest essays module heading
	$('#left-column .module-new h2 br').replaceWith('<span>&nbsp;</span>');
	$('#left-column .module-new h2').html( shortenmonth($('#left-column .module-new h2').html()) ).show();
	
// CLEANUP leading spaces from essay paragraphs
	$('.essaycontent>p').each(function(){
		var x = $(this).html();
		$(this).html(x.replace(/^(&nbsp;)*/,""));
	});	
	
// BEHAVIOR initialize the panel to open or closed
	if (readCookie('panelstatus')) { 
			$('#left-column .module-new ul').addClass('panelclosed').hide();
			$('#left-column .module-new h2 ').addClass('panelclosed');
	}
	
// BEHAVIOUR click on lastest essays heading opens and closes paneL (remembered by cookie)
	$('#left-column .module-new h2 ').click(function(){ 
		if ($(this).hasClass('panelclosed')) { 
			$('#left-column .module-new ul').removeClass('panelclosed').show('fast');
			$(this).removeClass('panelclosed');
			eraseCookie('panelstatus');
		} else { 
			$('#left-column .module-new ul').addClass('panelclosed').hide('fast');
			$(this).addClass('panelclosed');
			createCookie('panelstatus', 'panelclosed') ;
		};
	});

// BEHAVIOR click on header returns to home page
	$('#header').click(function() {
			location.href = "/index.shtml";
	});
	
// clean up archive table on the essay index page
// could use function to shorten dates on everything in the first column
if ( location.href.indexOf('Essays/index')>-1 ){
/*	$('.archive tr td:not([colspan]):last-child:').remove();*/
	$('.archive tr td:not([colspan]):last-child:').html('');
/* 		$('.archive td[colspan]').addClass('yearmarker').click(function(){scroll(0,0)}); */
	$('.archive tr th').remove();
};

// clean up podcasting tables and heading
$('.podcasting tr td:nth-child(2) p:nth-child(2)').
	each(function(){
		$(this).appendTo(
			$(this).parent().parent().find('td:first-child')).
		addClass('playmp3');
});


/* $('.podcasting h4').eq(0).css({'word-wrap':'break-word', 'width':'200px'}); */

$('.podcasting tr td:nth-child(2)').html('&nbsp;');

$('.podcasting table, .podcasting td').removeAttr('width').removeAttr('colspan');



//end initializenarrow
}

 






