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

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

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

/* remove url dotted border */
$(document).ready(function() {

	$('a').bind('focus', function() {
		this.blur();
	});
	
	$('*').ifixpng();
    
    $('.eo').emptyonclick();

    $('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);
    });

	$('.popupwindow').each( function(index) {
		$(this).popupwindow({ nofollow : {
			height:768,
			width:1024,
			toolbar:1,
			scrollbars:1,
			status:1,
			resizable:1,
			left:0,
			top:0,
			center:1,
			createnew:0,
			location:1,
			menubar:1
		},
		route : {
			height:768,
			width:640,
			toolbar:0,
			scrollbars:1,
			status:0,
			resizable:0,
			left:0,
			top:0,
			center:1,
			createnew:0,
			location:0,
			menubar:0
		}
		});
	});
	
    
});

function checkLoginFrm( $items_login ) {
	var $list = "";
	var $obj = "";
	
	$.each($items_login, function($index, $value) {
		if( $($.sprintf('#%s', $index)).val() == "" ) {
			$list += $.sprintf("<li>%s</li>", $value);
			if( ! $obj ) $obj = $($.sprintf('#%s', $index));
		}
	});
	
	if( $list != "" ) {	
		$.prompt($.sprintf('<h1>Verplichte velden</h1><p>U heeft de volgende verplichte velden niet ingevuld:</p><ul>%s</ul>', $list), {
					callback: function() {
						$obj.focus();
						$obj = null;
					}});
		return false;
	} else
		$('#id_loginfrm').submit();
}

function isEmail(strEmail) {
	var AtSym    = strEmail.indexOf('@')
	var Period   = strEmail.lastIndexOf('.')
	var Space    = strEmail.indexOf(' ')
	var Length   = strEmail.length - 1
	
	if( AtSym < 1 || Period <= AtSym + 1 || Period == Length || Space != -1 )
	 return false;

	return true;
}


/* twitter */
function parseDate(str) {
  var v=str.split(' ');
  return new Date(Date.parse(v[1]+" "+v[2]+", "+v[5]+" "+v[3]+" UTC"));
} 


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 style="word-wrap: break-word;" rel="fullfront"');
	});
};

String.prototype.parseUsername = function() {
	return this.replace(/[@]+[A-Za-z0-9-_]+/, function(u) {
		return 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 retweet() {
	window.open($.sprintf('http://twitter.com/home?status=RT @TWW_Oldenzaal %s', $tc));
}

function right(str, n){
    if (n <= 0)
       return "";
    else if (n > String(str).length)
       return str;
    else {
       var iLen = String(str).length;
       return String(str).substring(iLen, iLen - n);
    }
}

function addTwitter() {

	$.jTwitter('TWW_Oldenzaal', 1, function(userdata){

		var $start = 0;
		var $total = $('tr.tweet').length;
		$.each(userdata, function(i,item){
			if( $start > 0 )
				return;
			
			var $curr = parseDate(item.created_at);
			
			var $day = right("0"+$curr.getDate(), 2);
			var $month = right("0"+($curr.getUTCMonth()+1), 2);
			var $year = ""+$curr.getFullYear();
			
			var $tmp = $.sprintf('%s-%s-%d', $day, $month, $year.substring(1,4));

			$('#twittercontent').append($.sprintf('<div class="tweet"><span class="date"><b>%s</b></span>&nbsp;%s<br /><br /></div>', $tmp, item.text.parseURL().parseUsername().parseHashtag()));

			$start++;

		});
		
		$("#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).bind('click', function() {
				window.open( $(this).attr('href') );
				return false;
			});
			
		});

	});
}



