//Tweet messages.
var Tweets 	= [];

//the lock is used to lock interaction when switching messages
var lock 	= 1;

//Replacing text
function replaceMessages() {
    var Msg;
    var MsgArray;
    var maxChar = 21;
    var Msg = "";
    var Char;
    var i;
    var Row;

    for (j = 0; j < Tweets.length; j++) {

        Tweets[j].div = "#Message" + (j + 1);
        MsgArray = Tweets[j].Message.split(" ");

        //Div Message block
        Msg += '<div class="Message" id="Message' + (j + 1) + '">';

        //Div Profiel block
		Msg += '<div class="profile"><a href="' + Tweets[j].AuthorUrl + '" target="_blank"><img class="profilePic" src="'+Tweets[j].ProfileImageUrl+'" /></a>';
        Msg += '<div class="tweet-meta">' + Tweets[j].Timestring + ' geleden door <a href="' + Tweets[j].AuthorUrl + '" target="_blank"><span>' + Tweets[j].AuthorName + '</span></a><iframe class="facebook" width="250" src="http://www.facebook.com/plugins/like.php?href=' + encodeURI(Tweets[j].TweetUrl) + '&amp;layout=button_count&amp;show_faces=false&amp;action=like&amp;color:#ffffff&amp;font=arial&amp;colorscheme=light"  allowTransparency="true" scrolling="no" frameborder="0"></iframe></div>';	
        Msg += '</div>';
        Msg += '<div class="tweet"><div class="tweetrow"><span>';

        //Div Quote
        Char = 0;
        for (i = 0; i < MsgArray.length; i++) {
            Char += MsgArray[i].length;
            if (Char >= maxChar) {
                Char = 0;
                Msg += '</span></div><div class="tweetrow"><span>';
            };
            Msg += MsgArray[i] + " ";
        }

        Msg += "</div></div>";

        Msg += '</div>';
		        
    }

    //Replaces %%Message%% with contents of different Messages.
    $("#messages").html(Msg);
	$("#messages").hide();
	
    //Printing background contents
    // dynamic generations breaks resize function atm
    /*$("#backgrounds").html(Bg);*/
}

//Set the avatar buttons
function setAvatars(){
	var Str = "";
	
	for (i = 0; i < Tweets.length; i++) {
		Str += '<a class="tweeter_btn"><span></span><img id="tweeter_' + (i + 1) + '" src="' + Tweets[i].ProfileImageUrl + '" /></a>';
	}
	
	$("#tweeters").html(Str);
	
	//Set up Hover and click actions to buttons
	for (j = 0; j < Tweets.length; j++) {
	
		/*$("#supersize").after('<img  id="background0" src="' + Tweets[j].PhotoUrl + '" alt="" />');*/
		var imgId = "img#background" + j;
	 $(imgId).attr("src", Tweets[j].PhotoUrl);
	 
		$("#tweeters #tweeter_" + (j + 1)).click(function(){
			if(lock == 0){
				lock = 1;
				showTweet($(this).attr("id").substring(8, 9))
			}
		});
		var opacity;
		$("#tweeters #tweeter_" + (j + 1)).hover(function(){
			if (lock == 0) {
				/*$(this).stop().animate({
					queue:false,
					"opacity": "1"
				}, 300);*/
				$(this).css({
					
					"opacity": "1"
				});
			}
		}, function(){
			if (lock == 0 && $(this).attr('class') != "active") {
				$(this).css({
					
					"opacity": "0.6"
				});
			}
		});
	}	
}

//loops through Tweets and hides all linked divs, shows given tweet.
var curBG;
var curTweet;

function activeSlideshow(){
	var currentId = $("#tweeters .active").attr("id").substring(8, 9);
	if(currentId < 5) {
		currentId++;
		var newId = currentId;
		$("#tweeters #tweeter_" + newId).click();
	} else {
		$("#tweeters #tweeter_1").click();
	}
}



function showTweet(id) {
    id = id - 1;


    for (var i = 0; i < Tweets.length; i++) {
        imgId = "img#background" + i
        $(imgId).stop().fadeOut(500);

        if (i == id) {
            curBG = id;
            curTweet = Tweets[i].div;
			$("#tweeters #tweeter_" + (i + 1)).addClass("active");
            $("#tweeters #tweeter_" + (i + 1)).animate({ opacity: '1' }, { queue: false, duration: 500 });
			$("#tweeters #tweeter_" + (i + 1)).parent().css("cursor","default");
			$(curTweet).fadeOut(501, showTweetItem);
        }
        else {
            $(Tweets[i].div).hide();
            $("#tweeters #tweeter_" + (i + 1)).removeClass("active");
			$("#tweeters #tweeter_" + (i + 1)).animate({ opacity: '0.60' }, { queue: false, duration: 500 });
			$("#tweeters #tweeter_" + (i + 1)).parent().css("cursor","pointer");
        }
    }
}


function showTweetItem() {
   	
	$(curTweet).css("display","none");
	$("#messages").show();
	$(curTweet).stop().delay(1000).fadeIn(1000,function(){
		/*alert('message complete, releasing lock');*/
		lock = 0;
	});
	
    imgId = "img#background" + curBG;
    $(imgId).stop().fadeIn(1000);
}

function gotoWebsite() {
    //_gaq.push(['_trackEvent', 'Homepage', 'doorklik']);
	window.location = "/tamtam/werk/";
}


function positionImage(){
	var windowHeight = $(window).height();
	
	$("#supersize img a").each(function(){
		thisHeight = $(this).height();
		if(thisHeight > windowHeight) {
			var heightDifference = thisHeight - windowHeight;
			var minusDistance = heightDifference / 2;
			minusDistance = "-" + minusDistance + "px";
			$(this).css({"margin-top":minusDistance, "border-top":"10px solid red", "display":"block"});
		}
	});

}

//jQuery document ready.
$(document).ready(function() {
	  	
		$('#menu-header li, #menu-header-engels li').hover(
  			function () {
    			$(this).addClass("current-menu-item");
  			},
  			function() {
    			$(this).removeClass("current-menu-item");
  			}
		);
		 
		$('#about').hover( function(){
			 $("#aboutContent").stop().animate({
			 	queue: false,
			    opacity: 1,
			    width: '296px',
			    height: '117px'
			  }, 500);
		},function(){
     		 $("#aboutContent").stop().animate({
			 	queue: false,
			    opacity: 0.01,
			    width: '90px',
			    height: '20px'
			  }, 500);
		});
		
		 $("#aboutContent").stop().animate({
			 	queue: false,
			    opacity: 0.01,
			   width: '90px',
			    height: '20px'
			  }, 1);
			  
		 $(document).mousemove(function(e){
	        $("#follower").show();
	        $("#follower").css({
	            top: (e.pageY + 15) + "px",
	            left: (e.pageX + 15) + "px"
	        });
		});
		
		/* $('#overlay, a.gotowebsite').click(gotoWebsite); */
		
		
		$("#wrapper div").hover(function(){
			$("#follower").stop().animate({
				queue: false,
				opacity: 0
			}, 200);
			
		}, function(){
			$("#follower").stop().animate({
				queue: false,
				opacity: 1
			}, 500);
		});

		$("#tweeters").mouseenter(function(){
			
			clearInterval(activeSlidesh);
		});
		$("#messages").mouseenter(function(){
			
			clearInterval(activeSlidesh);
		});
		


    $.getJSON('http://www.tamtam.nl/wp-content/get_tweets.php', function(data) {
        Tweets = data;

        //read out Messages and place it in page.
        replaceMessages();

        //set avatars
        setAvatars();

        //show specific Message.
        
		showTweet(1);
		/*$('.logo_1').css({"margin-top":'-100px'});*/
		
		$('.logo_2').css("background-position",'150px -25px');
		$('.logo_1').delay(2500).animate({"margin-top":'0px'},2000, 'easeOutBounce',function(){
			$('.logo_2').animate({backgroundPosition:'0px -25px' }, 1500, 'easeOutQuad');
		});
				
		//setting up resizeble bg's
        //

    });

  var activeSlidesh = setInterval('activeSlideshow()', 10000);
  $('#supersize').supersized();
  $('html').css({"overflow":"hidden"});   
// jQuery('body').append('<div style="margin: 25% auto; width: 126px; height: 22px"><img src="frontpage/img/ajax-loader.gif" /></div>');

});

