var _autoSwitch = true;
var imgPath = '';

function prepare_news_flash() {

	$(".readfull").hover(
      function () {
        $(this).next("img").fadeIn("fast");
      }, 
      function () {
        $(this).next("img").fadeOut("slow");
      }
    );

	
	$("#newsflash").after("<div id='newsflash_selector'></div>");
	var i=0;
	$("#newsflash")
		.css("float", "left")
		.css("width", "580px")
		.css("height", "170px")
		.children("li")
		.css("float", "left")
		.css("width", "275px")
		.each(function( intIndex ){
			if(intIndex%2 == 0) {
				$("#newsflash_selector").append("<span href='#' id='nf_"+i+"'>" + (i+1) + "</span>");
				$("#nf_"+i).click(function(){
					_autoSwitch = false;
					switch_news_flash(this.id.split("_")[1]);
				});
				i++;
			}
		});
		
	switch_news_flash(0);
	
	}
	
function get_active_page_id() {
	var r_val = 99;
	$("#newsflash_selector").children("span").each(function() { if($(this).hasClass("active")) { r_val = $(this).attr("id").split("_")[1]; }});
	return r_val;
}
	
function switch_news_flash(page) {
	$("#newsflash_selector").children("span").each(function() { $(this).removeClass("active"); });
	$("#newsflash").children("li").each(function() { $(this).hide(); });
	$("#newsflash").children("li:eq("+(page*2)+")").fadeIn("slow");
	$("#newsflash").children("li:eq("+((page*2)+1)+")").fadeIn("slow");
	$("#newsflash_selector").children("span:eq("+page+")").addClass("active");
}

$(document).ready(function(){
	var _data = '';
	var _temp = '';
	var _f = true;
	var img = '';
	var i=0;
	var url = "http://search.twitter.com/search.json?q=from:ChromeNieuws&rpp=8&callback=?";
		$.getJSON(url, function(data){ 
			
			$.each(data.results, function(i, item) { if(_f) { _temp = item.text; _f = false; } });
					
			if(_temp != _data || _data == '') {
				$.each(data.results, function(i, item) { 
					$("#tweets ul").append("<li onmouseover='document.getElementById(\"tweet_span_"+i+"\").style.display=\"block\";' onmouseout='document.getElementById(\"tweet_span_"+i+"\").style.display=\"none\";'>"
					  + item.text.replace(/(\w+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+)/gi, "<a href=\"$1\" target=\"_blank\">$1</a>").replace(/[\@]+([A-Za-z0-9-_]+)/gi, "@<a target=\"_blank\" href=\"http://www.twitter.com/$1\">$1</a>")+""
					  + "<span id='tweet_span_"+i+"'><img src='" + item.profile_image_url + "' align='left'><b>"+item.from_user+"</b><br>"+item.created_at.split(" +")[0]+"</span>"
					  + "</li>"); 	
					  
					i++;
				}); 
				$("#loading-tweets").hide();
			}
		
		}); 
		
	 
	 $("#more-pages").css("left","224px");
	 var h = ($("#more-pages").height()-5);
	 $("#more-pages").css("top","-"+h+"px");
	 
	 $("#more-pages-button").click(function(event){
		 $("#more-pages").fadeIn("fast").delay(8000).fadeOut("slow");
     });
	
	$("#mainmenu li a").hover(function(){
		if(!$(this).parent().hasClass('active')) {
			$(this).children("span").css("color", "#fff").
				stop().
				parent().
				animate({backgroundPosition:"(right -400px)"}, {duration:200}).
				parent().
				animate({backgroundPosition:"(left -400px)"}, {duration:200});
		}
	}, function(){
		if(!$(this).parent().hasClass('active')) {
			$(this).children("span").css("color", "#888").
				stop().
				parent().
				animate({backgroundPosition:"(right 58px)"}, {duration:300}).
				parent().
				animate({backgroundPosition:"(left 58px)"}, {duration:300});
		}
	});
	$("#mainmenu li a").click(function() {
		$(this).parent().removeClass().addClass("active");
	});
	
	 prepare_news_flash();
	 auto_switch();
	
	//$('#mainmenu').toolTip('Mooi menu he!?', { delay: 2000 });
});

function auto_switch() {
	setTimeout(function() { 
		if(_autoSwitch) {
			var count = ($("#newsflash_selector").children("span").size())-1;
			var c_item = get_active_page_id();
			if(c_item < count) { c_item++; switch_news_flash(c_item);	} else { switch_news_flash(0); }
			auto_switch(); 
		}
	},12000);
}

var mouseX = 0;
var mouseY = 0;

$(document).mousemove( function(e) {
	var x = e.pageX;
	var y = e.pageY;

	
	for(i=0; i<=5; i++) {
		$("#tweet_span_"+i).css({ top:+(y-160)+"px", left:+(x-320)+"px"});
	}
});

/**
 * @author Alexander Farkas
 * v. 1.21
 */


(function($) {
	if(!document.defaultView || !document.defaultView.getComputedStyle){ // IE6-IE8
		var oldCurCSS = jQuery.curCSS;
		jQuery.curCSS = function(elem, name, force){
			if(name === 'background-position'){
				name = 'backgroundPosition';
			}
			if(name !== 'backgroundPosition' || !elem.currentStyle || elem.currentStyle[ name ]){
				return oldCurCSS.apply(this, arguments);
			}
			var style = elem.style;
			if ( !force && style && style[ name ] ){
				return style[ name ];
			}
			return oldCurCSS(elem, 'backgroundPositionX', force) +' '+ oldCurCSS(elem, 'backgroundPositionY', force);
		};
	}
	
	var oldAnim = $.fn.animate;
	$.fn.animate = function(prop){
		if('background-position' in prop){
			prop.backgroundPosition = prop['background-position'];
			delete prop['background-position'];
		}
		if('backgroundPosition' in prop){
			prop.backgroundPosition = '('+ prop.backgroundPosition;
		}
		return oldAnim.apply(this, arguments);
	};
	
	function toArray(strg){
		strg = strg.replace(/left|top/g,'0px');
		strg = strg.replace(/right|bottom/g,'100%');
		strg = strg.replace(/([0-9\.]+)(\s|\)|$)/g,"$1px$2");
		var res = strg.match(/(-?[0-9\.]+)(px|\%|em|pt)\s(-?[0-9\.]+)(px|\%|em|pt)/);
		return [parseFloat(res[1],10),res[2],parseFloat(res[3],10),res[4]];
	}
	
	$.fx.step. backgroundPosition = function(fx) {
		if (!fx.bgPosReady) {
			var start = $.curCSS(fx.elem,'backgroundPosition');
			
			if(!start){//FF2 no inline-style fallback
				start = '0px 0px';
			}
			
			start = toArray(start);
			
			fx.start = [start[0],start[2]];
			
			var end = toArray(fx.options.curAnim.backgroundPosition);
			fx.end = [end[0],end[2]];
			
			fx.unit = [end[1],end[3]];
			fx.bgPosReady = true;
		}
		//return;
		var nowPosX = [];
		nowPosX[0] = ((fx.end[0] - fx.start[0]) * fx.pos) + fx.start[0] + fx.unit[0];
		nowPosX[1] = ((fx.end[1] - fx.start[1]) * fx.pos) + fx.start[1] + fx.unit[1];           
		fx.elem.style.backgroundPosition = nowPosX[0]+' '+nowPosX[1];

	};
})(jQuery);
		
function set_arrow_left(id) {	
	$("#react_arrow_"+id).css("left", ((Math.floor($("#react_"+id).width()/2)-8)>8?(Math.floor($("#react_"+id).width()/2)-8):"8")+"px");
}
