var transTime = 250;
var fadeOutTime = 400;
var animSequence = { 11: 22, 22: 33, 33: 13, 13: 31, 31: 21, 21: 12, 12: 32, 32:23, 23:0 }
var twitterRssUrl = "";
//var twitterRssUrl = "http://api.twitter.com/1/statuses/user_timeline.json?screen_name=maxignu";
var imageList = [ "amadei", "askinosie", "bonnat", "cluzelt", "domori", "grenada", "kalari", "madecasse", "mast", "olive", "pacari", "patric", "pralus", "republica", "tcho", "valrhona"];
var eventFeedUrl="http://www.gnagnu.com/thechocolategarage/geteventfeed.php";

$(function(){
    ltie7 = $.browser.msie && /MSIE\s(5\.5|6\.)/.test(navigator.userAgent);
    ltie6 = $.browser.msie && /MSIE (6\.)/.test(navigator.userAgent);
    isff3 = /Firefox\/3/.test(navigator.userAgent);
    isie7 = false /*@cc_on || @_jscript_version == 5.7 @*/;
    if (ltie7) {
        transTime = 0;
        fadeOutTime = 0;
    }
    if (getUrlParam("_animateMozaic")!="") { // animate only on request e.g. index.html?_animateMozaic=True
    	animateMozaic();
    } else {
    	showTabs("#tabsHome");
    }
    //stopped working - dependent on other server    getEvents();
    showDesignOptions();
    initTabs();
});

function getEvents() {
	$.ajax({
		url: eventFeedUrl,
		dataType: "jsonp",
		success: function(string){
		$("#eventsDiv").append(string);
		}
	});	
}

function fillRandomTiles() {
	r = imageList.sort(function() {return 0.5 - Math.random()}).slice(0, 9);
	cnt = 0;
    for(var i = 1; i < 4; i++)
    {
    	for(var j=1; j < 4; j++) {
    		$("#tile"+i+j+" img").attr("src","img/tiles_scan_sepia/tile_"+r[cnt]+".jpg");
    		cnt++;
    	}
    }	
}


function showDesignOptions() {
    if (getUrlParam("_logo")!="") { // no parameter passed, animate!
    	$("#logo").attr("src","img/"+getUrlParam("_logo")+".png");
    }	
}

function animateMozaic() {
	fillRandomTiles();
    $("#leftColumn").css({opacity: 0, width: "96px"});
    showTile(11); // triggers the rest	
}

function showTile(s) {
	$("#tile"+s+" img").animate({ opacity: 1},{ duration: 700, complete: function(){
//		alert(animSequence[s]);
		if (animSequence[s]!=0) {
			hideTile(s);
			showTile(animSequence[s]);
		} else {
			$("#mozaicTable img").css("opacity",1);
			setTimeout("finalAnim();",1500);
		}
	}});
}
function hideTile(s) {
	$("#tile"+s+" img").animate({ opacity: 1},{duration: 300}).animate({ opacity: 0},{ duration: 200 });
}
function finalAnim() {
	$("#logo").animate({marginLeft:"-4px"},{duration:1000});
    $("#leftColumn").animate({width: "196px"},{duration:1000, queue:true,complete: function(){
    	$("#leftColumn").animate({opacity:0.9},{duration:fadeOutTime});
    	$("#mozaicTable").hide();
    	showTabs("#tabsHome");
    }});
}

//function animateMozaic() {
//	$("#mozaicTable").queue(function(){
//		$("#tile11 img").animate({opacity: 1},{duration:3000});
//		$(this).dequeue();
//	}).queue(function(){
//		$("#tile22 img").animate({opacity: 1},{duration:3000});
//		$(this).dequeue();
//	})
//}

function initTabs() {
	$("#tabsMenuWrapper a").click(function() {
		showTabs($(this).attr("href"));
	});	
	$(".link_text a").click(function() {
		showTabs($(this).attr("href"));
	});	
}

function showTabs(s) {
	$(".tabSelected").removeClass("tabSelected").fadeOut(fadeOutTime,function(){$(s).fadeIn(fadeOutTime).addClass("tabSelected");});
	$("#tabsMenuWrapper li").removeClass("on");
	$(s+"Li").addClass("on");
}


//****************************************
// HELPER FUNCTIONS
//****************************************

function getUrlVars()
{
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}

function preload(a) {
	$(a).each(function(){
		$("<img/>")[0].src = this;
	})
}

function getUrlParam( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

//****************************************
// AJAX requests to retrieve Facebook wall
// DEPRECATED
//****************************************

function getFBPosts(n,f) {
	$.ajax({
		url: twitterRssUrl + "&count=" + n,
		dataType: "jsonp",
		success: f
	});
}

function formatJsonGeneral(data) {
	var r="";
	$.each(data,function(p){
		t = data[p].text 
		r += "<p>" + t + " (posted " + relative_time(data[p].created_at) + ")</p>\r\n";
	});
//	alert(r);
	return r;
}

function postIsEvent(t) {
}

//String.prototype.linkify = function() {
//	return this.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/, function(m) {
//		return m.link(m);
//	});
//};

function relative_time(time_value) {
	  var values = time_value.split(" ");
	  time_value = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];
	  var parsed_date = Date.parse(time_value);
	  var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
	  var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
	  delta = delta + (relative_to.getTimezoneOffset() * 60);

	  var r = '';
	  if (delta < 60) {
		r = 'a minute ago';
	  } else if(delta < (45*60)) {
		r = (parseInt(delta / 60)).toString() + ' minutes ago';
	  } else if(delta < (90*60)) {
		r = 'an hour ago';
	  } else if(delta < (24*60*60)) {
		r = '' + (parseInt(delta / 3600)).toString() + ' hours ago';
	  } else if(delta < (48*60*60)) {
		r = '1 day ago';
	  } else {
		r = (parseInt(delta / 86400)).toString() + ' days ago';
	  }
	  return r;
}

