/*--------------------------------------------------------------------
Basic Javascript Sheet - Frontoffice

Version: 1.0
Copyright: 2008, dicode® VOF - understanding internet
W: www.dicode.nl
E: info@dicode.nl
T: 0570-750680
--------------------------------------------------------------------*/

/* Global
--------------------------------------------------------------------*/

/* remove url dotted border */
$(document).ready(function() {
	
	var arr = document.getElementsByTagName('a');
	for(i = 0; i < arr.length; i++)
		arr[i].onfocus = new Function("this.blur()");
	
	var arr = document.getElementsByTagName('input');
	for(i = 0; i < arr.length; i++)
		if(arr[i].className == 'button')
			arr[i].onfocus = new Function("this.blur()");
			
	//FormElements.init();

	if( siteurl != window.location.href ) {
		$('body').addClass('next');
		$('#header').addClass('next');
		$('#paper').addClass('next');
		$('#paper').html('');
	}

	$('.popupwindow').each( function(index) {
		$(this).popupwindow();
	});

    $('img.hover').each(function() {
        if ($(this).attr('src').match(/-on\.(.+)$/i)) {
            $(this).removeClass('hover');
        }
    });

    $('img.hover').hover(function() {
        s = $(this).attr('src').replace(/\.([^.]+)$/i, '-on.$1');
        $(this).attr('src', s);
    }, function() {
        s = $(this).attr('src').replace(/-on\.([^.]+)$/i, '.$1');
        $(this).attr('src', s);
    });

    $('.eo').emptyonclick();
    
    addTwitter();
});

String.prototype.parseURL = function() {
	return this.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/, function(url) {
		return url.link(url).replace('<a', '<a rel="fullfront"');
	});
};

String.prototype.parseUsername = function() {
	return this.replace(/[@]+[A-Za-z0-9-_]+/, function(u) {
		var username = u.replace("@","")
		return u.link("http://twitter.com/"+username).replace('<a', '<a rel="fullfront"');
	});
};

String.prototype.parseHashtag = function() {
	return this.replace(/[#]+[A-Za-z0-9-_]+/, function(t) {
		var tag = t.replace("#","%23")
		return t.link("http://search.twitter.com/search?q="+tag).replace('<a', '<a rel="fullfront"');
	});
};

function addTwitter() {
	$.jTwitter('lostomatos', 4, function(userdata){
		var $start = 0;
		var $total = $('tr.tweet').length;
		$.each(userdata.reverse(), function(i,item){
			var $date = new Date();
			var $curr = new Date(item.created_at);
			var $one_hour = 1000*60*60;
			var $one_day = 1000*60*60*24;
			var $tmp = '';
			
			if(  ( Math.ceil($date.getTime() - $curr.getTime()) / $one_hour ) < 24 )
				$tmp = $.sprintf('%d uur geleden', Math.ceil($date.getTime() - $curr.getTime()) / $one_hour );
			else if(  ( Math.ceil($date.getTime() - $curr.getTime()) / $one_hour ) >= 24 && ( Math.ceil($date.getTime() - $curr.getTime()) / $one_hour ) < 48 )
				$tmp = '1 dag geleden';
			else
				$tmp = $.sprintf('%d dagen geleden', Math.ceil($date.getTime() - $curr.getTime()) / $one_day );

			if( ! $($.sprintf('#id_tweet_%d', item.id)).length ) {
				createTwitterRow($.sprintf('%s <br/><span>%s</span>', item.text.parseURL().parseUsername().parseHashtag(), $tmp));
				$total++;
				if( $total > 3 ) {
					$('tr.tweet:last').prev('tr').remove();
					$('tr.tweet:last').remove();
					$total--;
				}
			}
		});
		$("#twittercontent a").each( function(index) {
			var myprofiles = {
						fullfront: {
							height:768,
							width:1024,
							toolbar:1,
							scrollbars:1,
							status:1,
							resizable:1,
							left:0,
							top:0,
							center:1,
							createnew:0,
							location:1,
							menubar:1
						}
					};			
			$(this).popupwindow(myprofiles);
		});

	});
}

function createTwitterRow( $content ) {
	
	
	
	var $tr = document.createElement('tr');
	
	var $td = document.createElement('td');

	/* horizontal line

	$td.colSpan = 2;
	

	
	$($td).addClass('line');
	$($td).append($.sprintf('<img src="%simages/img_line-small.jpg" alt="" />', siteurl));
	
	
	
	$($tr).append($td);

	*/
	
	/* chat icon part 

	$('#twittercontent').prepend($tr);
	
	
	
	$tr = document.createElement('tr');
	$td = document.createElement('td');

	 $($td).append($.sprintf('<img src="%simages/icn_chat.jpg" alt="" />', siteurl)); 
	
	$($tr).append($td);
	
	*/
	
	$td = document.createElement('td');
	$($td).addClass('content');
	$($td).html($content);
	
	$($tr).append($td);
	
	$('#twittercontent').prepend($tr);
	
	
}