// Get the tweets
if (window.location.hostname != 'miraclefrooties.com.au') {
	//window.location = 'http://miraclefrooties.com.au';
};



//console.log();

$(document).ready(function(){
    var el = $('#tweets');
	if (typeof(tweets) != 'undefined') {
		//console.debug(tweets);
		$(tweets).each(function(i, tweet){
			//console.debug(tweet);
			var thedate = new Date(Date.parse(tweet.created_at));
			var thedatestr = prettyDate(thedate);//thedate.getHours() + ':' + thedate.getMinutes();
			var divstr = '<a href="http://twitter.com/' + tweet.user.screen_name + '/status/' + tweet.id + '" target="_new"><div id="tw' + tweet.id + '" class="tweet"><img width="48" height="48" src="' + tweet.user.profile_image_url + '" ><p class="text">' + tweet.text + '<br />&nbsp;<b>' + tweet.user.screen_name + '</b> &nbsp;-&nbsp;<b>' + thedatestr + '</b></p></div></a>';
			el.append(divstr);
		});
	}
	
	$('#whyBuyLink').toggle(function() {
		$('#whyBuy').slideDown();
	}, function() {
		$('#whyBuy').slideUp();
	});
	var tracked = false;
	$('form').submit(function() {
		if (tracked == false) {
			tracked = true;
			//console.debug('tracking');
			pageTracker._trackPageview('/buy/?quantity=' + $('#quantityField').val());
			setTimeout(function() {
				$('#submit').click();
			}, 300);
			setTimeout(function() {
				tracked = false;
			}, 600);
		}
		return false;
	});

});

var mf = {
    feed: {}
};

mf.feed.bought = function(){

	/*FB.Facebook.apiClient.feed_publishUserAction(60287906436, null, null, null);
return;*/
    var comment_data = {
        "verb": 'bought some ',
        "noun": ' Miracle Frooties',
        "body": 'Sour foods become sweet!',
        "fullbody": 'Sour foods become sweet!<br/>Will you be invited to the taste tripping party?',
        "images": [{
            'src': 'http://www.miraclefrooties.com.au/images/box.gif',
            'href': 'http://www.miraclefrooties.com.au'
        }]
    };

    FB.Connect.showFeedDialog(36297870015, comment_data, null, null, null, FB.RequireConnect.promptConnect);
};

$(document).ready(function(){
	$('.favicons a[href^="http://"]').filter(function(){
	     return this.hostname && this.hostname !== location.hostname;
	   }).each(function() {
	     var link = $(this);
	     var faviconURL = link.attr('href').replace(/^(http:\/\/[^\/]+).*$/, '$1') +'/favicon.ico';
	     var faviconIMG =
	        $('<img src="favicon.png" alt="" />');
		// link.before(faviconIMG);
		 link.css('background-image', 'url(' + faviconURL + ')');
		 //link.addClass('');
	     var extImg = new Image();
	     extImg.src = faviconURL;
	     if (extImg.complete)
	       faviconIMG.attr('src', faviconURL);
	     else
	       extImg.onload = function() { faviconIMG.attr('src', faviconURL); };
	});
});



$(document).ready(function() {

	var quantity = 1;
	
	$('#plus').click(function(){
		quantity++;
		showTotal();
		//console.debug(quantity);
	});
	
	$('#minus').click(function(){
		quantity>1?quantity--:null;
		showTotal();
	});
	
	$('#shipping').change(showTotal);
	
	function showTotal() {
		
		$('#quantityBox span').text(quantity);
		$('#quantityField').val(quantity);


		var price;
		switch(quantity) {
			case 1:
				price = 30;
				break;
			case 2: 
				price = 27;
				break;
			default:
				price = 25;
				break;
		}
		
		var total = quantity * price;
		
		$('#amountField').val(price);
		
		var shipping = parseInt($('#shipping').val());
		total += shipping;
		$('#shippingField').val(shipping);
		
		$('#total span').text('$' + total);
		//console.debug(total);
	}

	showTotal();
});



/*
 * JavaScript Pretty Date
 * Copyright (c) 2008 John Resig (jquery.com)
 * Licensed under the MIT license.
 */

// Takes an ISO time and returns a string representing how
// long ago the date represents.
function prettyDate(date){
	/*var date = new Date((time || "").replace(/-/g,"/").replace(/[TZ]/g," ")),*/
		var diff = (((new Date()).getTime() - date.getTime()) / 1000);
		var day_diff = Math.floor(diff / 86400);
	
	//alert('diff' + day_diff);
	
	if ( isNaN(day_diff) || day_diff < 0 || day_diff >= 31 )
		return '';
			
	return day_diff == 0 && (
			diff < 60 && "just now" ||
			diff < 120 && "1 minute ago" ||
			diff < 3600 && Math.floor( diff / 60 ) + " minutes ago" ||
			diff < 7200 && "1 hour ago" ||
			diff < 86400 && Math.floor( diff / 3600 ) + " hours ago") ||
		day_diff == 1 && "Yesterday" ||
		day_diff < 7 && day_diff + " days ago" ||
		day_diff < 31 && Math.ceil( day_diff / 7 ) + " weeks ago";
}



