//initialize control variables
var wc_start = 0;
var wc_end = 3;

var s_start = 0;
var s_end = 3;

var hm_start = 0;
var hm_end = 3;

//define binding routines and initialize ajax data
$(document).ready(function() {
	//Twitter inits
	whos_cookin(0);	
	shouts(0);	
	horses_mouth(0);
	
	//Flickr inits
	init_flickr_widget();
	
	//WC Blog inits
	init_wcblog_widget();
	
	//WC del.icio.us inits
	init_wcdelicious_widget();
	
	//Twitter bindings
	$('#friends .next').click(function(){
		whos_cookin(2);
	});	
	$('#friends .prev').click(function(){
		whos_cookin(1);
	});
	
	$('#replies .next').click(function(){
		shouts(2);
	});	
	$('#replies .prev').click(function(){
		shouts(1);
	});
	
	$('#archive .next').click(function(){
		horses_mouth(2);
	});	
	$('#archive .prev').click(function(){
		horses_mouth(1);
	});
});

//define controlling functions
//Twitter -- START
function whos_cookin(dir){
	if(dir == 0){	//initialization operation		
		wc_start = 0;
		wc_end = 3;
	}
	if(dir == 1){	//previous operation
		if(wc_start >= 3 && wc_start <= 15 && wc_end >= 6 && wc_end <= 18){
			wc_start = wc_start - 3;
			wc_end = wc_end - 3;
		}else{
			return 0;
		}
	}
	if(dir == 2){	//next operation
		if(wc_start >= 0 && wc_start <= 12 && wc_end >= 3 && wc_end <= 15){
			wc_start = wc_start + 3;
			wc_end = wc_end + 3;
		}else{
			return 0;
		}
	}
	var url = 'api/twitter/index.php?q=1&s=' + wc_start + '&e=' + wc_end;
	//alert(url);
	$("#friends .feed").html('<div class="loading"></div>');
	$('#friends .loading').html('<img src="images/loader.gif" />'); //Show loader
	$.get(url, function(data){
			
		$("#friends .feed").html(data);
		
		$('#friends .loading').html(''); //Hide loader
    }); // End inline function
}

function shouts(dir){
	if(dir == 0){	//initialization operation		
		s_start = 0;
		s_end = 3;
	}
	if(dir == 1){	//previous operation
		if(s_start >= 3 && s_start <= 15 && s_end >= 6 && s_end <= 18){
			s_start = s_start - 3;
			s_end = s_end - 3;
		}else{
			return 0;
		}
	}
	if(dir == 2){	//next operation
		if(s_start >= 0 && s_start <= 12 && s_end >= 3 && s_end <= 15){
			s_start = s_start + 3;
			s_end = s_end + 3;
		}else{
			return 0;
		}
	}
	var url = 'api/twitter/index.php?q=2&s=' + s_start + '&e=' + s_end;
	//alert(url);
	$("#replies .feed").html('<div class="loading"></div>');
	$('#replies .loading').html('<img src="images/loader.gif" />'); //Show loader
	$.get(url, function(data){
		$("#replies .feed").html(data);
		
		$('#replies .loading').html(''); //Hide loader
    }); // End inline function
}

function horses_mouth(dir){
	if(dir == 0){	//initialization operation
		hm_start = 0;
		hm_end = 3;
	}
	if(dir == 1){	//previous operation
		if(hm_start >= 3 && hm_start <= 15 && hm_end >= 6 && hm_end <= 18){
			hm_start = hm_start - 3;
			hm_end = hm_end - 3;
		}else{
			return 0;
		}
	}
	if(dir == 2){	//next operation
		if(hm_start >= 0 && hm_start <= 12 && hm_end >= 3 && hm_end <= 15){
			hm_start = hm_start + 3;
			hm_end = hm_end + 3;
		}else{
			return 0;
		}
	}
	var url = 'api/twitter/index.php?q=3&s=' + hm_start + '&e=' + hm_end;
	//alert(url);
	$("#archive .feed").html('<div class="loading"></div>');
	$('#archive .loading').html('<img src="images/loader.gif" />'); //Show loader
	$.get(url, function(data){
		if(data == null){
			alert('hi');
			if(dir == 0){	//initialization operation		
				hm_start = 0;
				hm_end = 3;
			}
			if(dir == 1){	//previous operation
				hm_start = hm_start - 3;
				hm_end = hm_end - 3;
			}
			if(dir == 2){	//next operation
				hm_start = hm_start + 3;
				hm_end = hm_end + 3;
			}
			horses_mouth(3);//call with no change in start and end values
		}
		$("#archive .feed").html(data);
		
		$('#archive .loading').html(''); //Hide loader
    }); // End inline function
}
//Twitter -- END
//Flickr -- START
function init_flickr_widget(){
	var url = 'api/flickr/index.php?c=6';
	//alert(url);
	$(".flickr-img").html('<div class="loading"></div>');
	$('.flickr-img .loading').html('<img src="images/loader.gif" />'); //Show loader
	$.get(url, function(data){
		$(".flickr-img").html(data);
		
		$('.flickr-img .loading').html(''); //Hide loader
    }); // End inline function
}
//Flickr -- END
//WC Blog -- START
function init_wcblog_widget(){
	var url = 'api/wcblog/index.php?c=5';
	//alert(url);
	$("#blog .star").html('<li><div class="loading"></div></li>');
	$('#blog .star .loading').html('<img src="images/loader.gif" />'); //Show loader
	$.get(url, function(data){
		$("#blog .star").html(data);
		
		$('#blog .star .loading').html(''); //Hide loader
    }); // End inline function
}
//WC Blog -- END
//WC del.icio.us -- START
function init_wcdelicious_widget(){
	var url = 'api/wcdelicious/index.php?c=5';
	//alert(url);
	$("#in-news .star").html('<li><div class="loading"></div></li>');
	$('#in-news .star .loading').html('<img src="images/loader.gif" />'); //Show loader
	$.get(url, function(data){
		$("#in-news .star").html(data);
		
		$('#in-news .star .loading').html(''); //Hide loader
    }); // End inline function
}
//WC del.icio.us -- END
