$(document).ready(function(){
	//spriteImage = new Image(); 
	//spriteImage.src = imgUrl+'/images/wall-sprite.png';
	$('#preload').attr('src',imgUrl+'/images/wall-sprite.jpg');
	$('#author').val('');
	$('#email').val('');
	$('#url').val('');

	$('ul.items li a[title]').qtip({
      position: {
         corner: {
            target: 'rightTop',
            tooltip: 'leftMiddle'
         }
      },
      style: {
         name: 'light',
         padding: '7px 13px',
         width: {
            max: 210,
            min: 0
         },
		  background: '#333',
		  color: '#fff',
		  textAlign: 'left',
		  opacity: 0.8,
		  border: {
			 width: 0,
			 radius: 5,
			 color: '#000'
		  },
         tip: true
      }
   });
   
   if($('.slickr-flickr-gallery ul li').length > 0)
   {
		$('.slickr-flickr-gallery ul li a').bind('click',function(){
			var loc = $(this).attr('href');
			newWindow(loc);
			return false;
		
		});
		$('#sidebar .slickr-flickr-gallery img').bind('mouseover',function(){
			$(this).css('background','#aaa');
		});
		$('#sidebar .slickr-flickr-gallery img').bind('mouseout',function(){
			$(this).css('background','none');
		});
   }
   
   if($('.twf li a').length>0){
   $('.twf li a').live('click',function(){
		
		window.open(this.href,'_blank');
		return false;

   
   });}
   
   if($('.team-members').length>0){
	   $('span.egg').live('click',function(){
			
			$('.team-members').shuffle();
			zindexforie();

	   
	   });
	
	}
	zindexforie();
	
	$('#team-list ul li').hover(function(){
		$(this).find('div.engineer').css('background-position','-114px 0');
		$(this).children('.designation').css('display','block');
		/*$('.designation',$(this)).clone().appendTo('.tooltip');
		var posTip = $('.designation',$(this)).parent().position(); 
		var posLeft = posTip.left + 3;
		var posTop = posTip.top + 135;
		$('.tooltip').css({'left':posLeft, 'top':posTop, 'position':'absolute'});
		$('.designation',$('#tooltip')).show();*/
		
	}, function(){
		$(this).find('div.engineer').css('background-position','0 0');
		$(this).children('.designation').css('display','none');
		/*//$('.tooltip').html('');
		$('.tooltip').css({'left':'-9999px', 'top':'-9999px', 'position':'relative'});*/

	});
	
	
	$("ul.tabs").tabs("div.panes > div");
	// setup for rounded corner
	$(".tabs li a").corner( "20px left #fff");
	//contact us page tab code
	//$("#contact-us").tabs();
	// portfolio page scrollbar
	$('#image_wrap #portfolio-scrollable').scrollable({size:1,speed:500, circular: true}).navigator({
     navi:'.scrollable ul'
    });
	//$("#portfolio-thuumb-wrapper .scrollable").scrollable({size:6,speed:100});
	//thumbnail scrollable
	if($("#portfolio-thuumb-wrapper .scrollable").length > 0)
	{
	$("#portfolio-thuumb-wrapper .scrollable").scrollable(); 
	var scrollable = $("#portfolio-thuumb-wrapper .scrollable").data("scrollable");
	var size = 6; 
	
	scrollable.onSeek(function(event, index) {	 
		if (this.getIndex() >= this.getSize() - size) 
		{
			$("#slider a.next").addClass("disabled");
		}
	});
	scrollable.onBeforeSeek(function(event, index) {
		if (this.getIndex() >= this.getSize() - size)
		{
			if (index > this.getIndex())
			{
				return false;
			}
		}
	});	 
	}
	$(".scrollable-slide").scrollable();
	var ctr = 0;
	var coordy = 0;
	for(var i=0; i<348; i++)
	{
		
		var coordx= ctr*31;
		$('#tweet-wall').append('<li><span class="box" style="background-position: -'+coordx+'px -'+coordy+'px;"></span></li>');
		ctr++;
		if(ctr>28)
		{
			ctr = 0;
			coordy = coordy +31;
		}
	}
	
	$("#preload").load(function(){
		if($('#tweet-wall').length != 0){
		generateWall();
	}
	});
	//end of portfolio page
	
});//end of ready()

var fresh_tweets = [];
var tweet_inserter = false;
var db_poller = false;
var tweet = null;	
var tag="";	

function generateWall(){
	var totalboxes = 348;
	var i =0;
	var show = 5; 
	var imageIndex = 0;
	array = [];
	var rnd, value, j;
	for (j = 0; j < totalboxes; j++) { // arrays are zero based, for 21 elements you want to go from index 0 to 20.
		array[j] = j + 1;
	}
	
	$('#tweet-wall').everyTime(100, 'controlled', function() {
		i++;
		
		imageIndex++;
				//$(this).prepend('<li class="animate-'+i+'"><span class="box" style="background-image: url('+imgUrl+'/images/wall-sprite.png); background-repeat: no-repeat; background-position: -'+coordx+'px -'+coordy+'px;"></span></li>');

		
		
		////console.log(i + '/' + coordt);
		//if(typeof imagesArray[imageIndex] == 'undefined')
			//imageIndex=0;
		//$(this).prepend('<li class="animate-'+i+'"><span class="box"><img alt="" src="'+imagesArray[imageIndex]+'"/></span></li>');
		////console.log(coord);
		
		if(array.length < show)
			show = array.length;
			
		for (j = 0; j < show; j++) { // pick numbers
		  rnd = Math.floor(Math.random() * array.length); 
		  value = array.splice(rnd,1)[0]; // remove the selected number from the array and get it in another variable
		
		  $('#tweet-wall li span').eq(value-1).css({'display':'block'});
		}
		
		if(array.length == 0)
		{
			$(this).stopTime('controlled');
		
			$('#tweet-wall').animate({
							opacity:'0.4' 
					},1500
					);
			/*$('li span', $(this)).each(function(j){
				
					$(this).animate({
							opacity:'0.4' 
					},1500
					);
			});*/
			poll_db(randomTag());
		}
	});
}
function randomTag(){
	var $cloudTag = $('#expert li').get();
	var rand = Math.floor(( ($cloudTag.length-1)-(0-1))*Math.random()) + 0;
	var tagClass=$('#expert li:eq('+rand+')').attr('class');
	$('#expert li.size1').attr('class',tagClass);
	$('#expert li.active').removeClass('active');
	$('#expert li:eq('+rand+')').attr('class','word size1 active');
	tag = $('#expert li:eq('+rand+')').text();
	return tag;
}
function shout(){
	$('#popup').hide();
	
	var totalboxes =348;
	var boxesRow = 29;
	var minRange = 3;
	var maxRange = boxesRow-1;
	
	var min = (boxesRow * 1); // to omit the last r0w
	var max = (totalboxes - (boxesRow * 1) ); // to omit the first row
	
	var no = Math.floor((max-(min-1))*Math.random()) + min;

	var rem = no % boxesRow;
	//console.log(minRange+'/'+maxRange+'/'+rem +'/' + no);
	if(rem > minRange && rem < (maxRange-10))
	{
		tweet = fresh_tweets.shift();
		if(fresh_tweets.length==0)
		{	
			fresh_tweets = [];
			poll_db(randomTag());
			return;
		}
		$('#tweet-wall li span.box').eq(no-1).html('').css({'background-position':'0 0'});
		
		$('#tweet-wall li span.box').eq(no-1).append('<img src=""/>');
		
		//console.log($('#tweet-wall li span.box').eq(no-1));
		var pos = $('img',$('#tweet-wall li span.box').eq(no-1)).position();
		$('img',$('#tweet-wall li span.box').eq(no-1)).attr('src',tweet['profile_image_url']).attr('title','@'+tweet['from_user']);
		$('#popup h4').text('@'+tweet['from_user']);
		$('#popup p').text(tweet['text']);
		$('#popup img').attr('src',tweet['profile_image_url']).css({'border':'8px solid #969698','width':'60px','height':'60px'}).show('fast');
		
		$('#popup').css({'top':pos.top-16+'px','left':pos.left-16 +'px'})
		.animate({
			 width:'toggle'
		},
		{
			duration: 500
		});
		setTimeout("shout()",8000);
	}
	else
		shout();	
}
function poll_db(tag)
{	

	var url = baseUrl + '/mainwall';
	$.ajax({
		url: url,
		type: "GET",
		dataType: "json",
		data: $.param({tags:tag}),
		success: function(tweets)
		{
			fresh_tweets = fresh_tweets.concat(tweets['results']);
			
			if(tweets[tweets.length-1] != undefined)
			{
				start_id = tweets['results'][tweets['results'].length-1]["id"];
			}
			if(fresh_tweets.length != 0)
			{
				shout();
			}
			else
			{
				setTimeout("poll_db(tag)", 10000);
			}
		},
		failure: function()
		{
			setTimeout("poll_db(tag)", 10000);
		}
	});
}
function newWindow(url){
		window.open(url,'_blank');
		
   }
   
 function zindexforie(){
 var zindexNo = 2000;
	$('#team-list ul li').each(function(){
		
		$(this).css({'z-index': zindexNo -=10});
	
	});
 }
