$(document).ready(function () {

	varClickCount = 0;

	$('body').append('<div id="confirm"><div class="header"><span>Confirm</span></div><p class="message"></p><p>&nbsp;&nbsp;<input type="checkbox" value="hide" name="neveragain" class="neveragin" id="neveragin" /> <label for="neveragain">Remember my choice and do not prompt me again.</label></p><div class="buttons"><div class="no">No</div><div class="yes">Yes</div></div></div>');

	$('a.confirmdialog').click(function (e) {
	
		e.preventDefault();
		
		var tmpCookie = $.cookie("neveragin");
		var urlName = $(this).attr('rel');
		var urlTwitter = $(this).attr('href');
		
		switch ( tmpCookie ) {
		
			case "yes":
				window.open(urlTwitter,'twitter');
				break;
			
			case "no":
				varClickCount = varClickCount + 1;
				
				if( varClickCount == 3 ) {
					$('.neveragin').attr('checked', true);
					confirm('You keep trying to visit Twitter accounts that aren\'t include on PWTweets, however, you previously chose not to view them. Would you like to allow the visiting of these accounts?', function() {
						window.open(urlTwitter,'twitter');
					});
				}
				break;
				
			default:
				confirm("\"" + urlName  + "\" is not included on PWTweets.com. Would you like to view their page on Twitter.com?", function () {
					window.open(urlTwitter,'twitter');
				});
				
		}
		
	});
	
});

function confirm(message, callback) {
	$('#confirm').modal({
		closeHTML:"<a href='#' title='Close' class='modal-close'>x</a>",
		position: ["20%",],
		overlayId:'confirm-overlay',
		containerId:'confirm-container', 
		onShow: function (dialog) {
			$('.message', dialog.data[0]).append(message);

			$('.yes', dialog.data[0]).click(function () {
			
				if( $('.neveragin').is(':checked') == true ) {
					$.cookie("neveragin", "yes");
					$('.neveragin').attr('checked', false);
				}

				if ($.isFunction(callback)) {
					callback.apply();
				}

				$.modal.close();
			});

			$('.no', dialog.data[0]).click(function () {
			
				if( $('.neveragin').is(':checked') == true ) {
					$.cookie("neveragin", "no");
					$('.neveragin').attr('checked', false);
				}

				$.modal.close();
			});
		}
	});
}