var interval; 
var shout_initiaited = false; 

$(document).ready( function() {
	
	// hide for iPhone / iPad
	if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i)) || (navigator.userAgent.match(/Android/i))) {
		return false;
	}

	// load dock 
	$.ajax({
		url: "/data/dock/",
		processData: false,
		dataType: "html",
		success: function(data) {
			
			// show the dick .. I'm mean dock
			$('body').append(data);
			
			var default_tab = $('#dock .tabs .active a').attr('rel');
						
			$('#dock .tabs a').click(function() {
				
				var rel = $(this).attr('rel');
				$('#dock .panel').hide();
				$('#dock .panel').removeClass('accessibility');
				$('#dock .'+rel).show();
				
				$('#dock .tabs li').removeClass('active');
				$(this).parent().addClass('active');
				
				if($(this).attr('rel') == 'shout') {
					if (!shout_initiaited) initiateShout(url, category, title);
					shout_initiaited = true;
				}
				
				return false;
			})
			
			// add click functionality for entries
			$('#dock .entry').click(function(){
				var href = $('.type a', this).attr('href');	
				window.location.href = href;
				
			})
			
			// state buttons
			
			$('#dock .close').click(function(){
				
				$.get('/data/dock/update/?dock-state=closed');
				$('#dock').toggleClass('closed');
				$('#dock').toggleClass('open');
				$('.state').toggleClass('accessibility');
				return false;
				
			})
			
			$('#dock .open').click(function(){
				
				$.get('/data/dock/update/?dock-state=open');
				$('#dock').toggleClass('closed');
				$('#dock').toggleClass('open');
				$('.state').toggleClass('accessibility');
				return false;
				
			})
			
			// share button click
			
			$('#dock .share-buttons p a').click(function(){
				
				var share_url = $(this).attr('href');
				var shout = $('#dock-shout').val();
				var url = $('#dock-shout').attr('rel');
				var go;
				
				if($(this).parent().hasClass('twitter')) go = share_url + shout;
				if($(this).parent().hasClass('facebook')) go = share_url + '?u=' + url + '&t=' + shout;
				
				window.location.href = go;
				
				return false;
				
			})
			
			// update the doc
			var url = $('link[rel="canonical"]').attr('href');
			url = (url.indexOf('?') > -1) ? url.substring(0, url.indexOf('?')) : url;
			var type = 'recently-viewed';
			var handle = $('meta[name="meta-id"]').attr('content');
			var category = $('meta[name="meta-category"]').attr('content');
			category = (category == "discussion-sub-page") ? "discussion" : category;
			
			var title = $('meta[name="meta-title"]').attr('content');
			
			var is_current_user = (($('body#members').length) && (handle == $('body#members').attr('rel'))) ? true : false;
						
			if (is_current_user == false && category != 'core' && category != 'review-index-distiller' && category != 'review-index-bottle' && category != 'event') {

				var title = $('meta[name="meta-title"]').attr('content');
				var image = $('meta[name="meta-image"]').attr('content');
				
				var dock_get = '/data/dock/update/?'+type+'||'+category+'||'+handle+'='+category+'||'+title+'||'+image+'||'+url;
								
				setTimeout(function() {
					
					$.post(dock_get, function(){
						
						$('#dock').addClass('updated');
						
					});
					
					
				},2000);


			};
			
		
			var shout = "";
			
			// initiate
			if (default_tab == 'shout') initiateShout(url, category, title);
						
		}
	});
	
});

function dockFocusShout() {
	$('#dock .tabs a[rel="shout"]').click();
}

function initiateCustomShout(url, shout) {
	
	dockFocusShout()
	
	// set up default options
	var defaults = {
		version:    '2.0.1',
		login:      'connosr',
		apiKey:     'R_18b1bfea6115e49be3659f78501902b9',
		history:    '0',
		longUrl:    url
	};

	// Build the URL to query
	var daurl = "http://api.bit.ly/shorten?"
	+"version="+defaults.version
	+"&longUrl="+defaults.longUrl
	+"&login="+defaults.login
	+"&apiKey="+defaults.apiKey
	+"&history="+defaults.history
	+"&format=json&callback=?";
	
	// Utilize the bit.ly API
	$.getJSON(daurl, function(data){
		
		var shortURL = data.results[url].shortUrl;
		
		shout = shout + " " + shortURL;

		dockShout(shout, url);

	});
}

function initiateShout(url, category, title) {
	
	// set up default options
	var defaults = {
		version:    '2.0.1',
		login:      'connosr',
		apiKey:     'R_18b1bfea6115e49be3659f78501902b9',
		history:    '0',
		longUrl:    url
	};

	// Build the URL to query
	var daurl = "http://api.bit.ly/shorten?"
	+"version="+defaults.version
	+"&longUrl="+defaults.longUrl
	+"&login="+defaults.login
	+"&apiKey="+defaults.apiKey
	+"&history="+defaults.history
	+"&format=json&callback=?";
	
	// Utilize the bit.ly API
	$.getJSON(daurl, function(data){
		
		var shortURL = data.results[url].shortUrl;

		// type specific

		switch (category) {	
		case "core":
			shout = "A social network for whisky lovers http://www.connosr.com";
			break;
		case "member":
			shout = "Check out " + title + "'s profile on Whisky Connosr " + shortURL;
			break;
		case "bottle":
			shout = title + " on Whisky Connosr " + shortURL;
			break;
		case "review":
			shout = "Review of " + title + " " + shortURL;
			break;
		case "distilled-article":
			shout = "Article: " + title + " " + shortURL;
			break;
		case "video":
			shout = "Video: " + title + " " + shortURL;
			break;
		case "whiskypod-video":
			shout = "Whisky video review: " + title + " " + shortURL;
			break;
		case "whiskypod-event":
			shout = "WhiskyPod videos: " + title + " " + shortURL;
			break;
		default:
			shout = title + " " + shortURL;
		}
		
		// page specific
		switch (url) {	
		case "http://www.connosr.com/wall/":
			shout = "The Whisky Wall a forum for the online whisky community http://www.connosr.com/wall";
			break;
		case "http://www.connosr.com/distilled/":
			shout = "Connosr Distilled - The Online Whisky Magazine http://www.connosr.com/distilled";
			break;
		case "http://www.connosr.com/blog/":
			shout = "Check out the Whisky Connosr blog http://www.connosr.com/blog";
			break;
		case "http://www.connosr.com/reviews/":
			shout = "Browse thousands of community whisky reviews at http://www.connosr.com/reviews";
			break;
		case "http://www.connosr.com/whiskypod/":
			shout = "The WhiskyPod a video review booth for whisky lovin' folks http://www.connosr.com/whiskypod";
			break;
		default:
			shout = shout;
		}

		dockShout(shout, url);

	});
}

function dockShout(message, url) {

	var string_array = new Array();

	string_array = message.split('');

	var count = 0;
	var length = string_array.length;
	
	$('#dock-shout').val('');
	$('#dock-shout').attr('rel', url);
	
	clearInterval(interval);
	
	interval = setInterval(function() {

		if (count == length-1) clearInterval(interval);

		$('#dock-shout').val($('#dock-shout').val() + string_array[count]);

		count ++;

	}, 20);
}
