// images for nav
var activeNav_image='/template/images/home_slide_on.png';
var inactiveNav_image='/template/images/home_slide_off.png';

// slide switcher
var currentSlide=0;
function switchSlide(slide, speed){

	// default speed
	if(!speed) speed=400;

	// if no slide, get the next
	if('undefined'===typeof slide){
		// first set to current slide, then 
		slide=currentSlide;
		slide++;

		// check the slide exists
		bannerCount = jQuery('.service-banner').length;
		if(slide >= bannerCount){
			slide=0;
		}
	}

	// update register
	currentSlide = slide;

	jQuery('.slide-nav li img').attr('src', inactiveNav_image);
	jQuery('.service-banner').hide();

	// show the banner
	jQuery('.service-banner').eq(slide).fadeIn(speed);

	// permanent dot
	jQuery('.slide-nav li img').eq(slide).attr('src', activeNav_image);

}

// handle customer banner
jQuery(document).ready(function(){

	// count banners and display buttons
	banners = jQuery('.service-banner');
	navImage=activeNav_image;
	for(n=0; n<banners.length; n++){
		title = jQuery('h1', jQuery('.service-banner').eq(n)).text();
		jQuery('.slide-nav').append('<li style="cursor:pointer;"><a title="'+title+'"><img width="16" height="16" alt="" src="'+navImage+'"></a></li>');
		navImage=inactiveNav_image;
	}

	// add button functionality
	jQuery('.slide-nav li a').click(function(){
		switchSlide(jQuery(this).parent().index());
	});

	// add main click functionality for the whole thing
	jQuery('.service-banner').click(function(){
		switchSlide();
	});

	// press first button
	switchSlide(0, 250);

//	jQuery('.slide-nav').first().click();
//	jQuery('.top-cont').fadeOut(0).fadeIn(2500);
//	jQuery('.top-cont .right p').slideUp(0).slideDown(1000);
//	jQuery('.top-cont .inner-logo').css({position:'relative', marginRight:-150}).animate({marginRight:'0'}, 1100, 'swing');

	// scroll in the text
//	var text = jQuery('.top-cont .left p').text();
//	jQuery('.top-cont .left p').text('');
//	for(var n=0; n<text.length; n++){
//		jQuery('.top-cont .left p').delay(200 * n).text(text.substr(0,n));
//	}

});

function replaceURLWithHTMLLinks(text) {
//    var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/i;
    var exp = /(\b(https?|ftp|file):\/\/)([-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/i;
    return text.replace(exp,"<a target='_blank' href='$2://$3'>$3</a>"); 
}

function replaceURLWithTwitterLinks(text) {
	text=text.replace(/[@]+[A-Za-z0-9-_]+/g, function(u) {
		var username = u.replace("@","")
		return '<a target="_blank" href="http://twitter.com/'+username+'">'+username+'</a>';
		return link;
	});

	text=text.replace(/[#]+[A-Za-z0-9-_]+/g, function(u) {
		var hashtag = u.replace("#","")
		return '<a target="_blank" href="http://twitter.com/search?q=%23'+hashtag+'">#'+hashtag+'</a>';
		return link;
	});

	return text;
};

jQuery(document).ready(function(){
	updateTwitter(function(){});
	jQuery('#twitter_control').click(function(){
	        jQuery('#posts').slideUp(200, function(){
			updateTwitter(function(){
			        jQuery('#posts').slideDown(200);
			});
		});
	});
});

function updateTwitter(callback){
	// number of posts - not guaranteed as taking direct tweets only
	var scanPosts=15;
	var numPosts=4;
	var postsPrinted=0;

	jQuery.jTwitter('netfuse', scanPosts, function(data){
		jQuery('#posts').empty();
		jQuery.each(data, function(i, post){

			// ignore replies - "return true" == "continue;"
			if(post.in_reply_to_user_id_str) return true;

			// limited output - "return false" == "break;"
			if(postsPrinted >= numPosts) return false;

			// add post to html
			jQuery('#posts').append(
				'<div class="post">'
				+' <div class="txt">'
				+'   <div class="dis-left">'
					+'      <img src="' + post.user.profile_image_url + '" width="48" height="48" alt="" />'
					+'   </div>'
					+'   <div class="dis-right">'
					+'   <p>' + replaceURLWithTwitterLinks(replaceURLWithHTMLLinks(post.text)) + '</p>'
					+'   <h6>'+ post.created_at.substr(0,16) +'<b> by ' + post.user.screen_name + '</b></h6>'
				+'   </div>'
				+'   <div class="clear"></div>'
				+' </div>'
				+'</div>'
			);

			// our counter needs incrementing
			postsPrinted++;
		});
		callback();
	});
}



