$(function(){
	Linko.environment.init();
	
	if(Linko.system.isDazzlerInstalled()){
		$('a.manager-link').show();
		$('.teaser-wrapper.installed').show();
		
		$('.teaser-wrapper.installed .teaser-text-link a').attr('href', Linko.system.isMac ? plugin_url_mac : plugin_url_win);
	}
	else if(!Linko.environment.data.os.match(/Win/) && Linko.environment.data.os != 'Mac Snow Leopard'){
		/*Platform is not Windows or Mac Snow Leopard*/
		$('.teaser-wrapper.not-supported').show();
	}
	else{
		$('.teaser-wrapper.not-installed').show();
	}

	var setTweet = function(text){
		$('.tweet-text').html(text);
	};

	var scrollTweet = function(){
		var tweet = $('.tweet-text');
		var overflow = tweet.outerWidth() - $('.tweet-text-wrapper').innerWidth();
		if ( overflow < 2 )
			return;

		// Twitter scroller settings
		var interval = 20;
		var speed = 1;
		var wait = 1000;
		
		var offset = 0;
		var loop = function(){
			offset += speed;
			if ( offset>overflow ){
				offset = overflow;
				speed = -speed;
				pauseLoop();
			}
			else if ( offset<0 ){
				offset = 0;
				speed = -speed;
				pauseLoop();
			}

			tweet.css('margin-left', -offset);
		};

		var intervalHandle = null;
		var pauseLoop = function(){
			if ( intervalHandle )
				clearInterval(intervalHandle);
			setTimeout(function(){
				intervalHandle = setInterval(loop, interval);
			}, wait)
		};
		pauseLoop();
	};

	var newestTweet = Linko.util.cookie.get('newestTweet');
	if ( newestTweet ){
		// this is document.written in base.html if the cookie is found
		//setTweet(decodeURIComponent(newestTweet));
		scrollTweet();
	}
	else{
		$.ajax({
			'url': 'http://api.twitter.com/1/statuses/user_timeline.json',
			'dataType': 'jsonp',
			'data': {
				'screen_name': 'Dazzboard',
				'count': '1',
				'trim_user': '1',
				'include_rts': '0',
				'include_entities': '0'
			},
			'success': function(data){
				if ( !data || !data[0] || !data[0].text ){
					console.error('Invalid tweet', arguments);
					$('.tweet').hide();
					return;
				}
				
				newestTweet = data[0].text;

				newestTweet = Linko.util.htmlEncode(newestTweet);
				newestTweet = newestTweet.replace(/(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig, '<a href="$1" target="_blank">$1</a>');

				Linko.util.cookie.set('newestTweet', encodeURIComponent(newestTweet), 1/24);
				setTweet(newestTweet);
				scrollTweet();
			},
			'error': function(){
				console.error('Error getting tweet', arguments);
				$('.tweet').hide();
			}
		});
	}
	
	var emailDialogDiv = $('.email-dialog');
	var downloadDialogDiv = $('.download-dialog');
	var positionDialogs = function(){
		var documentWidth = $(document).width();
		var documentHeight = $(document).height();
		
		emailDialogDiv.add(downloadDialogDiv).each(function(){
			var me = $(this);
			
			me.css({
				'left': (documentWidth - me.width()) / 2,
				'top': (documentHeight - me.height()) / 2
			})
		});
	};
	positionDialogs();
	
	var checkInput = function(){
		var email = $('#confirm-email').val();
		if ( $('#confirm-checkbox').is(':checked') && email.match(/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i) != null ){
			$('#confirm', emailDialogDiv).removeClass('disabled').unbind('click').bind('click', function(e){
				e.stopPropagation();
				e.preventDefault();
				/*download_log is glogal*/
				$.post(download_log, {
					'email': email
				}, function(){
					Linko.util.cookie.set('email_ready', 1);
				});
				
				emailDialogDiv.hide(250);
				downloadDialogDiv.show(250);
			});
		}
		else
			$('#confirm', emailDialogDiv).addClass('disabled');
	};
	
	$('#download', downloadDialogDiv).bind('click', function(e){
		downloadDialogDiv.hide(250);
	});

	$('a', downloadDialogDiv).attr('href', Linko.system.isMac ? plugin_url_mac : plugin_url_win);
		
	$('#cancel', emailDialogDiv).bind('click', function(e){
		emailDialogDiv.hide(250);
	});

	$('.download-button').bind('click', function(e){
		e.stopPropagation();
		e.preventDefault();

		if (!Linko.util.cookie.get('email_ready')) {
			emailDialogDiv.show(250);

			$('#confirm-email').unbind('keyup').keyup(function(){
				checkInput();
			});

			$('#confirm-checkbox').unbind('click').click(function(){
				checkInput();
			});
		}
		else{
			downloadDialogDiv.show(250);
		}
	});
});

