var original_bg;

function initPage(is_home){//, bgs){	//, o_bg) {
	initSlides();
	
	var ch = $('.container_12').height();
	var wh = $(window).height();
	if(wh > ch) {
		var h = Math.floor($(window).height() - $('.container_12').height() - $('.footer').height()*1.5);
		h = (h < 0) ? 80 : h;
		$('.footer').css('margin-top', h+'px');	
	}
	if(is_home == true) getTumblr();
	
	//original_bg = o_bg
	
	//bindSettings(bgs);
}


function initSlides() {
	$("#slides").cycle({
		fx					:'fade',
		speed				:500,
		timeout				:7000,
		delay				:0,
		pause				:false,
		fastOnEvent			:250,
		requeueOnImageNotLoaded	:true,
		pager				:"#slides_navigation",
		pagerAnchorBuilder		:anchorBuilder,
		pauseOnPagerHover		:0
	});
}

function anchorBuilder(ind, elem) {
	var ret = '<a href="javascript:;" title="'+$(elem).attr('title')+'" class="slides_anchor">&nbsp;</a>';
	
	return ret;
}

function getTumblr() {
	
	$.ajax(
		{
			type		:'GET',
			dataType	:'json',
			url		:"http://makehello.tumblr.com/api/read/json?callback=?&filter=text&num=5",
			timeout	:5000,
			success	:handleTumblrSuccess,
			error	:handleTumblrError//,
			//complete	:function(jqXHR, status){alert(status);}
		}
	);
	/*
	
	$.getJSON("http://makehello.tumblr.com/api/read/json?callback=?&filter=text&num=7", 
	  function(data) {
		  handleTumblrSuccess(data)
		  });
	 */
}

var copy_ary = [];
var img_ary = [];

function handleTumblrSuccess(data) {
	var text_limit = 70;
	var contents = '';
	var count = 0;
	 $(data.posts).each(function(i, post) {
	   var type = post['type'];
	   var title = '';
	   var copy = '';
	   var url = post['url'];
	   
	   switch(type) {
		case 'regular':
		title = (post['regular-title'].length > 0) ? post['regular-title'] : post['regular-body'];
		copy = post['regular-body'];
		break;
		
		case 'link':
		title = post['link-text'];
		copy = post['link-description'];
		break;
		
		case 'quote':
		title = post['quote-text'];
		copy = post['quote-text'] + ' - ' + post['quote-source'];
		break;
		
		case 'photo':
		title = post['photo-caption'];
		copy = post['photo-caption']; 
		img_ary[count] = post['photo-url-100'];
		break;
		
		case 'video':
		title = post['video-caption'];
		copy = post['video-caption'] + '<br />[video link]';
		break;
	   }
	   
	   copy_ary[count] = copy;
	   count++;
	   
		if(title.length > text_limit) {
			var end = title.indexOf(' ', text_limit);
			if(end == -1) end = text_limit;
			title = title.substring(0, end) + '&hellip;';
		}
		
		if(title.length > 0) contents += '<li><a class="text_ds tubmlr_headline" href="'+url+'" alt="'+count+'" target="_blank">'+title+'</a></li>'
	 });
	   
	   $('#tumblr_feed').fadeOut(1000, function(){
		   $('#tumblr_feed').html(contents);
		   $('#tumblr_feed').fadeIn();
		   
		   bindTumblr();
		   });
}

function handleTumblrError(jqHXR, status, error) {
	   var contents = '<li>Tumblr did not respond.</li><li>[ <a id="tumblr_retry" href="javascript:;">try again</a> ]</li>';
	   $('#tumblr_feed').fadeOut(1000, function(){
			$('#tumblr_feed').html(contents);
			$('#tumblr_retry').click(function(e){
				$('#tumblr_feed').html('<li style="font-size:18px;">Loading...</li>');
				getTumblr();
				});
			$('#tumblr_feed').fadeIn();
		   
		   });
}

var showing = false;

function bindTumblr() {
	//alert('bind');
	$('.tubmlr_headline').mouseout(function(e){
			$('#floater').hide();
		});
	$('.tubmlr_headline').mouseover(function(e){
		clearTimeout(timeout);
		$('#floater').hide();
		var alt = $(this).attr('alt') - 1;
		var str = copy_ary[alt];
		if(str.length > 0){
			showing = true;
			if(img_ary[alt]) {
				str = '<img src="'+img_ary[alt]+'" /><p>'+str+'</p>';
			} else {
				str = '<p>'+str+'</p>';
			}
			$('#floater').html(str);
			var position = $(this).offset();
			var top = Math.floor(position.top - $('#floater').height() * 1/2);
			var left = Math.floor(position.left - $('#floater').width() - 55);
			$('#floater').css('top', top);
			$('#floater').css('left', left);
			$('#floater').fadeIn('fast');
			
		}
		
		});
}

function addMitchell(do_delay) {
	var delay = (do_delay == false ? 0 : 2000);
	
	var mitchell = '<div id="bg_credit" class="box_ds_4"><p>Background based on a pattern stolen from <a href="http://sirmikeofmitchell.com/index.php?/patterns/" target="_blank" id="bg_anchor">Mike Mitchell</a>.</p></div>';
	$('.container').append(mitchell);
	$('#bg_credit').delay(delay).slideToggle('slow', function(){$('#bg_credit').delay(3000).slideToggle();});
	$('#bg_credit').click(function(){
		$('#bg_credit').dequeue();
		});
	$('#bg_anchor').click(function(){
		$('#bg_credit').dequeue();
		});
}

var timeout;
/*
var bgs = [];

function bindSettings(bgs) {
	$('.settings').mouseover(function(e){
		clearTimeout(timeout);
		var contents = '<ul>';
		for (var i=0; i < bgs.length; i++) {
			contents += '<li><a href="javascript:;" class="bg_setting" id="'+i+'" rel="'+bgs[i]+'">BG'+(i+1)+'</a></li>';
		}
		i++;
		//contents += '<li><a href="javascript:;" class="bg_setting" id="'+i+'" rel="reset">TODAY&rsquo;S BG</a></li></ul>';
		contents += '</ul>';
		
		$('#floater').html(contents);
		
		var position = $(this).offset();
		var top = Math.floor(position.top - 55);
		var left = Math.floor(position.left - 100);
		
		$('#floater').css('top', top);
		$('#floater').css('left', left);
			
		$('.bg_setting').click(function(){
				var rel = $(this).attr('rel');
				var post_data = {bg: rel, original_bg:original_bg};
				$.post('include/session_bg.php',
					  post_data,
					  function(data){
						  handleBGSet(data);
					  },
					  'json');
				clearTimeout(timeout);
				$('#floater').hide();
			});
		$('#floater').fadeIn('fast', function(){
			timeout = setTimeout(function(){
				$('#floater').fadeOut();},
				2500);
			});
			$('.bg_setting').mouseover(function(){
				clearTimeout(timeout);
			});
			$('.bg_setting').mouseout(function(){
				//clearTimeout(timeout);
				timeout = setTimeout(function(){
					$('#floater').fadeOut();},
					2500);
			});
	})
}

function handleBGSet(data) {
	var new_bg = (data.bg != null ? data.bg : original_bg);
	$('#body').css('background', 'url(graphics/'+new_bg+') repeat center #000');
	if(new_bg.indexOf('sirmitchell', 0) > 0) {addMitchell(false);}
}
*/

