/*
Global JavaScript for the site 
*/
var i, n, f;  
var FB       = null; 
var PHP      = {
	Global   : '/includes/ajax.global.php', 
	Twitter  : '/includes/ajax.twitter.php', 
	Facebook : '/includes/ajax.facebook.php', 
	Social   : '/includes/ajax.social.php'
}; 

/*
Basic sharing social features 
*/
var SHARE = {
	//
	Pop: function( s )
	{
		var box = document.getElementById( 'social-box' ) || null; 
		var sta = ( s && typeof s === 'string' ) ? s : 'hide'; 
		if( !box ){ return; }
		$utils.DimScreen( sta );
		box.style.display = ( /^(hide|close|quit|exit)$/i.test( sta ) ) ? 'none' : 'block'; 
	}, 
	Open: function( s )
	{
		var url = encodeURIComponent( top.location.href || 'http://rainnerlins.com/' ); 
		var txt = encodeURIComponent( document.title || 'Rainner Lins - Web Content Developer' ); 
		var srv = ( s && typeof s === 'string' ) ? s : 'none';  
		var inf = {}; 
		//
		if( srv === 'none' ){ return; } 
		switch( srv )
		{
			case 'email':    window.open( 'mailto:CHOOSE?subject='+ txt +'&body='+ url ) || alert( 'Could not open!' ); return; 
			case 'twitter':  inf = { h:450, w:700, u:'http://twitter.com/share?&url='+ url +'&text='+ txt }; break; 
			case 'facebook': inf = { h:450, w:700, u:'http://facebook.com/sharer.php?&u='+ url +'&t='+ txt }; break; 
			case 'google':   inf = { h:450, w:700, u:'https://m.google.com/app/plus/x/?v=compose&hideloc=1&content='+ url }; break; 
			default:         inf = { h:520, w:700, u:'http://addthis.com/bookmark.php?url='+ url +'&title='+ txt }; 
		}
		//
		var p = $utils.PopPos( inf.w, inf.h ); 
		var w = window.open( inf.u, p.name, p.params ); 
		if( w ){ w.moveTo( p.left, p.top ); }else{ alert( 'Please allow pop-ups for this feature to work.' ); } 
	}
};

/*
Fetch the latest Tweets
*/
var TWEETS = {
	// 
	LOAD: function( target, limit )
	{
		target = ( typeof target === 'string' ) ? document.getElementById( target ) : target; 
		limit  = ( limit ) ? limit : 3; 
		// 
		if( !target ){ return; } // need a place to load it into.. 
		$.getJSON( PHP.Twitter + '?action=tweets&id=58718848&limit=' + limit, function( data )  
		{
			var str = ''; 
			if( !data.tweets ){ $( target ).html( 'Woah! not sure what happened but i couldn\'t deliver the goods. Try again later..' ); }
			else
			{
				for( var i=0; i < data.tweets.length; i++ )
				{
					var t = data.tweets[i]; 
					var h = ( i < ( data.tweets.length - 1 ) )  ? '<hr />' : ''; 
					// 
					str += '' + 
					'<p>' + 
						'<a href="http://twitter.com/'+t.screen_name+'" target="_blank"><img class="fl br round shadow" src="'+t.profile_image+'" height="48" alt="" /></a>' + 
						'<a class="bold" href="http://twitter.com/'+t.screen_name+'" target="_blank">@'+t.screen_name+'</a>: <br /> ' + 
						'<span class="bold small">' + t.posted_date + '</span> <br /> ' + 
						'<span class="italic grey">' + t.tweet_text + '</span>' + h + 
					'</p>' + 
					"\n\n"; 
				}
			}
			$( target ).attr( 'class','' ).html( str );
		}); 
	}
};

/*
SMS form stuff 
*/
var SMS = {
	// 
	State   : 0, 
	Working : false, 
	Stat    : 'Fill this out to text my phone..', 
	Text    : 'Enter your text here..', 
	Numb    : 'Your number..',  
	// 
	Display: function( t )
	{
		var box  = document.getElementById( 'sms-wrap' ) || null; 
		var stat = document.getElementById( 'sms-stat' ) || null; 
		var form = document.getElementById( 'sms-form' ) || null; 
		var view = ( /^(open|show|pop|display|enable|send)$/i.test( t ) ) ? 'show' : 'hide'; 
		$utils.DimScreen( view ); 
		//
		if( !box ){ return; }
		if( stat ){ $utils.Txt( stat, 'default', SMS.Stat ); } 
		if( view === 'show' )
		{ 
			box.style.display = 'block'; 
			form.from.value = SMS.Numb; 
			form.text.value = SMS.Text; 
			SMS.State = 1; 
		}
		else
		{ 
			box.style.display = 'none'; 
			SMS.State = 0; 
		}
	}, 
	// 
	Send: function( f )
	{
		if( !f ){ return; } // no form instance 
		if( this.State < 1 || this.Working ){ return; } // hidden, or busy 
		//
		var stat = document.getElementById( 'sms-stat' ) || null; 
		var post = {
			action : 'SendText', 
			rcpt   : String( f.rcpt.value ), 
			text   : String( f.text.value ).replace( /([\r\n\t\s]+)/gim, ' ' ), 
			from   : String( f.from.value ).replace( /([^0-9]+)/gim, '' ) 
		}; 
		//
		if( post.from.length < 10 || post.from ===  SMS.Numb )
		{
			$utils.Txt( stat, 'warn', 'Enter your number so i can reply..' ); 
			f.from.focus(); return; 
		}
		if( post.text.length < 10 || post.text ===  SMS.Text )
		{
			$utils.Txt( stat, 'warn', 'Type in your text and send away..' ); 
			f.text.focus(); return;
		}
		if( !/\s+/.test( post.text ) )
		{
			$utils.Txt( stat, 'deny', 'Please don\'t spam! Yes, it works..' ); 
			f.text.focus(); return;
		}
		//
		$utils.Txt( stat, 'load', 'Sending your text message..' );  
		$.post( PHP.Global, post, function( data )
		{
			var d = $utils.ParseJSON( data ); 
			if( d && d.status !== 'ok' ){ $utils.Txt( stat, 'error', d.info ); } 
			else
			{
				$utils.Txt( stat, 'ok', d.info ); 
				setTimeout( function(){ SMS.Display( 'hide' ); }, 3000 ); 
			}
			SMS.Working = false; 
		}); 
		SMS.Working = true; 
	}
}; 

/*
Contact page form 
*/
var CONTACT = {
	//
	Working : false, 
	Send    : function( f )
	{
		if( !f ){ return; } // no form instance 
		if( this.Working ){ return; } // busy ..
		//
		var stat = document.getElementById( 'cnt-stat' ) || null; 
		var post = {
			action : 'SendEmail', 
			name   : String( f.name.value ).replace( /([\r\n\t\s]+)/gim, ' ' ), 
			email  : String( f.email.value ).replace( /([\r\n\t\s]+)/gim, '' ), 
			body   : String( f.body.value ).replace( /[ ]{2,}/gim, ' ' )
		}; 
		//
		if( post.name.length < 3 )
		{
			$utils.Txt( stat, 'warn', 'Enter your name, go on..' ); 
			f.name.focus(); return;
		}
		if( post.email.length < 6 || !/[\@\.]+/g.test( post.email ) )
		{
			$utils.Txt( stat, 'warn', 'Without a good return address this wont work!' ); 
			f.email.focus(); return; 
		}
		if( post.body.length < 10 || !/[\s]+/g.test( post.body ) )
		{
			$utils.Txt( stat, 'warn', 'You\'re not trying to spam are you? Be nice!' ); 
			f.body.focus(); return;
		}
		//
		$utils.Txt( stat, 'load', 'Sending your text message..' );  
		$.post( PHP.Global, post, function( data )
		{
			var d = $utils.ParseJSON( data ); 
			if( d && d.status !== 'ok' ){ $utils.Txt( stat, 'error', d.info ); } 
			else
			{
				f.name.value  = ''; 
				f.email.value = ''; 
				f.body.value  = ''; 
				$utils.Txt( stat, 'ok', d.info ); 
			}
			CONTACT.Working = false; 
		}); 
		CONTACT.Working = true; 
	}
}; 

/*
Comments processing  
*/
var COMMENT = { 
	// 
	Busy   : false, 
	DefIMG : '/images/default-user.jpg', 
	// 
	Avatar: function( f, e, s )
	{
		if( !f || !e || !$utils.Is('email',e) ){ return; } 
		$.post( PHP.Global, { action:'GetAvatar', email:e, size:s }, function( data )
		{
			var d = $utils.ParseJSON( data ); 
			var v = ( d.status === 'ok' ) ? true : false; 
			f.comImage.value = ( v ) ? d.image : ''; 
			f.style.background = ( v ) ? 'url('+d.image+') right top no-repeat' : 'url('+COMMENT.DefIMG+') right top no-repeat'; 
		}); 
	}, 
	// 
	Login: function()
	{
		var f = document.getElementById( 'comForm' ) || null; 
		$.post( PHP.Global, { action:'GuestInfo' }, function( data )
		{
			var d = $utils.ParseJSON( data ); 
			if( d && d.data )
			{
				var img = ( d.data.image !== '' ) ? d.data.image : COMMENT.DefIMG; 
				if( f )
				{
					f.style.background = 'url( '+ img +' ) right top no-repeat'; 
					f.comImage.value   = ( d.data.image !== '' ) ? d.data.image : ''; 
					f.comName.value    = ( d.data.name !== '' )  ? d.data.name  : 'Your name'; 
					f.comEmail.value   = ( d.data.email !== '' ) ? d.data.email : 'E-mail address'; 
					f.comLink.value    = ( d.data.link !== '' )  ? d.data.link  : 'Web link'; 
				}
			}
		}); 
	}, 
	// 
	Format: function( d ) 
	{
		d.name  = ( d.link !== '' )  ? '<a href="'+d.link+'" target="_blank">'+d.name+'</a>' : d.name; 
		d.image = ( d.image !== '' ) ? d.image : COMMENT.DefIMG; 
		// 
		var html = '' + 
		'<div id="comPost-'+( d.id || 0 )+'" class="comPost"> ' + 
			'<hr /> ' + 
			'<div class="comTop">' + 
				'<img class="fl br round" src="'+ String( d.image || COMMENT.DefIMG ) +'" width="32" height="32" alt="" /> ' + 
				'<big class="white">'+ String( d.name || 'Guest' )+'</big> <br /> ' + 
				'<small class="grey">'+ String( d.date || 'No data specified' ) +'</small> ' + 
				'<br class="cfix" /> ' + 
			'</div>' + 
			'<div class="comBody">' + 
				String( d.comment || '<p>Empty comment entry</p>' ) + 
				'<br class="cfix" /> ' + 
			'</div>' + 
		'</div>' + " \n"; 
		return html; 
	}, 
	//
	Insert: function( d ) 
	{
		var comHead = document.getElementById( 'comHeader' ) || null;  
		var comWrap = document.getElementById( 'comWrap' ) || null;  
		var comDeft = document.getElementById( 'comDefault' ) || null;  
		// 
		if( !d || !comWrap ){ return; } 
		for( x in d ){ d[x] = decodeURIComponent( d[x] ); } 
		var h = COMMENT.Format( d ); 
		//
		if( comDeft ){ comDeft.style.display = 'none'; }  
		comHead.innerHTML = 'User Comments &nbsp; ' + String( d.count || 0 ); 
		setTimeout( function(){ $( comWrap ).append( h ).children().last().css( { opacity:0 } ).fadeTo( 1000, 1 ); }, 1000 ); 
	}, 
	// 
	Post: function( f )
	{
		if( !f ){ return; }
		var stat = document.getElementById( 'comStatus' );  
		// 
		if( COMMENT.Busy )
		{
			$utils.Txt( stat, 'load', 'Still working on it! If it takes too long, refresh the page.' ); return; 
		}
		if( !$forms.check( 'length', f.comName, 3 ) || !$forms.check( 'text', f.comName, 'Your name' ) ) 
		{
			$utils.Txt( stat, 'warn', 'Please enter your name.' ); return; 
		} 
		if( !$forms.check( 'email', f.comEmail ) || !$forms.check( 'text', f.comEmail, 'E-mail address' ) ) 
		{
			$utils.Txt( stat, 'warn', 'Please enter a valid e-mail address.' ); return; 
		}
		if( !$forms.check( 'length', f.comText, 3 ) || !$forms.check( 'text', f.comText, 'Your comment' ) ) 
		{
			$utils.Txt( stat, 'warn', 'Please enter your comment.' ); return; 
		} 
		if( !/\s/gi.test( f.comText.value ) ) 
		{
			$utils.Txt( stat, 'error', 'Please don\'t spam. Be cool!' ); return; 
		} 
		if( f.comLink.value == 'Web link' ){ f.comLink.value = ''; }
		//
		var p  = {
			action  : 'PostComment', 
			table   : f.comTable.value  || '', 
			rowid   : f.comRow.value    || '', 
			image   : f.comImage.value  || '', 
			source  : f.comSource.value || '', 
			name    : f.comName.value   || '', 
			email   : f.comEmail.value  || '', 
			link    : f.comLink.value   || '', 
			comment : f.comText.value   || '', 
			subc    : f.comSub.checked ? 1 : 0 
		}; 
		//
		$utils.Txt( stat, 'load', 'Saving your comment, one moment..' ); 
		$.post( PHP.Global, p, function( data )
		{
			var d = $utils.ParseJSON( data ); 
			if( d.status !== 'ok' ){ $utils.Txt( stat, 'error', d.info ); } 
			else
			{
				$utils.Txt( stat, 'success', 'Your comment has been submitted. Thanks.' ); 
				$.extend( p, d ); 
				f.comText.value = '';   
				COMMENT.Insert( p ); 
			}
			COMMENT.Busy = null; 
		});
		COMMENT.Busy = true;  
	}, 
	// 
	Fetch: function( tb, id, order )
	{
		var comHead = document.getElementById( 'comHeader' ) || null;  
		var comWrap = document.getElementById( 'comWrap' ) || null;  
		var p       = { action:'LoadComments', table:tb, rowid:id, sort:order || '' }; 
		// 
		if( !comWrap ){ return; }
		// 
		$.post( PHP.Global, p, function( data )
		{
			var h = ''; 
			var d = $utils.ParseJSON( data ); 
			if( d && d.rows )
			{
				if( d.rows.length < 1 ){ return; } 
				for( var i=0; i < d.rows.length; i++ )
				{
					var c = d.rows[i]; 
					h += COMMENT.Format( c ); 
				}
				comHead.innerHTML = 'User Comments &nbsp; ' + String( d.count || 0 ); 
				comWrap.innerHTML = h;
			}
		});
	}
}; 

/*
Kick old browsers 
*/
if( $.browser ) 
{
	// internet explorer 6 
	if( $.browser.msie && $.browser.version == "6.0" ){ top.location.href = '/e/browser'; } 
}

/*
Custom Functions 
*/
function topSearch( e, o )
{
	var frm = ( typeof o === 'string' ) ? document.getElementById( o ) : o; 
	var val = ( frm ) ? frm.value : null; 
	var key = null; 
	key = ( e && e.keyCode ) ? e.keyCode : key; 
	key = ( e && e.charCode ) ? e.charCode : key;  
	if( key && key !== 13 ){ return; } 
	if( !val || val === '' ){ $utils.Notice( 'show', 'warn', 'Enter a search query and try again!' ); return; } 
	if( val.length < 3 || val === 'Enter your search here..' ){ $utils.Notice( 'show', 'warn', 'Enter a search query and try again!' ); return; } 
	top.location.href = '/search/' + $utils.KeyStr( val );  
}

/*
On Ready 
*/
$(function()
{
	// decode some encrypted stuff 
	$( '.crypt' ).each( function()
	{ 
		if( !B64 ){ return; } 
		var o = $( this )[0]; 
		var t = $( this ).text(); 
		var n = o.tagName.toLowerCase(); 
		var h = ( n === 'a' ) ? $( o ).attr( 'href' ) : false; 
		if( false !== h ){ $( o ).attr( 'href', B64.decode( h ) ); }
		$( o ).text( B64.decode( t ) ).css( 'visibility', 'visible' ); 
	}); 
		
	// pre-configs
	if( $utils ){ $utils.PreCSS = { 'background-color':'#fff', 'color':'#333' }; } 
	if( $forms ){ $forms.PreCSS = { 'background-color':'#fff', 'color':'#333' }; } 
	
	// mouseover image effects  
	$( '.glow' ).css( { opacity:0.84 } ).hover( function(){ $( this ).fadeTo(300,1); }, function(){ $( this ).fadeTo(300,0.84); } ); 
	$( '.rotate' ).css( { rotate:'-6deg' } ); 
	
	// round edges 
	$( '.round, .fileInput' ).css( { 'border-radius':'8px', '-webkit-border-radius':'8px', '-moz-border-radius':'8px'} ); 
		
	// fade out disabled features 
	$( '.disabled' ).css( { opacity:0.30 } ).bind( 'click', function(){ return false; } );  
		
	// setup input objects, focus, autosize, .. 
	$( 'input, textarea' ).setupforms(); 
	
	// slimbox for images 
	$( '.slimbox' ).slimbox( {}, null ); 
	
	// setup tooltips
	$( '.tip' ).tooltip( $utils.PreCSS ); 
	
	// code highlighter boxes .. 
	$( 'pre' ).tinylight( { wordWrap:false, showScroller:true, maxHeight:'auto' } ); 
	
	// auto fill comment form 
	COMMENT.Login(); 
});



