jQuery(function ($) {
	// $("a.setting_tabs").css({
		// textDecoration:'none';
		// fontSize:'14px';
	// });
	DD_roundies.addRule("input[type='submit'],input[type='button'],input[type='text'],input[type='password'],select", "4px", true);

	DD_roundies.addRule('fieldset', '6px', true);
	DD_roundies.addRule('fieldset legend', '4px', true);
	
	if ($("#signin_login")) $("#signin_login").focus();
	if ( $("#signin_email") && $("#signin_email").val() != "" ) $("#signin_password").focus();
	if ( $("#signup_email") && $("#signup_email").val() != "" ) $("#signup_password").focus();

	$("#signup").bind( 'submit' , function(e){
		try{
			var tz = jzTimezoneDetector.determine_timezone();
			if (tz != null) $("#signup #utz").val( tz.timezone.olson_tz );
			var d = new Date();
			$("#signup #ut").val( d.getTime() );
		}catch(e){
		}
	});
	$(".forgot").click(function(e){
		e.preventDefault();
		var u = $(this).attr("href");
		var l = $(this).attr("forgot");
		if (l != null && l.length > 0 ){
			u += "?email=" + $("#" + l).val();
		}
		window.location.href = u;
	});
	
	$("form select[disabled='disabled']").css({
		"color": "#9B9B9B",
		"background-color":"#DBDBDB",
		"font-style":"italic"
	});

	$("#readability-unlink").bind('click', function(e){
		e.preventDefault();
		confirm("#confirm-unlink","Do you really want to unlink your Readability account ? ", function () {
			top.location.href = url_readability_unlink;
		});
	});
	
	$('a.qtip[title],a.qtip1[title]').qtip({
		content: $(this).attr("class"),
		show: 'mouseover',
		hide: 'mouseout',
		position: {
		  corner: {
			 target: 'topRight',
			 tooltip: 'bottomLeft'
		  }
	   },
		style: { 
			name: 'cream', // Inherit from preset style
			tip:'bottomLeft',
			border: {
				width: 4,
				radius: 2
			}
		}
	})
	$('a.qtip2[title]').qtip({
		content: $(this).attr("class"),
		show: 'mouseover',
		hide: 'mouseout',
		position: {
		  corner: {
			 target: 'bottomLeft',
			 tooltip: 'topRight'
		  }
	   },
		style: { 
			name: 'cream', // Inherit from preset style
			tip:'topRight',
			border: {
				width: 4,
				radius: 2
			}
		}
	})
});	

function togglepwdforgot(){
	$("#password_forgot_res").html("");
	$("#password_forgot_res").hide();
	$("#password_forgot").show();
}
function confirm(selector,message, callback) {
	$(selector).modal({
		closeHTML: "<a href='#' title='Close' class='modal-close'>x</a>",
		position: ["20%",],
		overlayId: 'confirm-unlink-overlay',
		containerId: 'confirm-unlink-container', 
		onShow: function (dialog) {
			var modal = this;
			$('.message', dialog.data[0]).append(message);
			// if the user clicks "yes"
			$('.yes', dialog.data[0]).click(function () {
				// call the callback
				if ($.isFunction(callback)) {
					callback.apply();
				}
				// close the dialog
				modal.close(); // or $.modal.close();
			});
		}
	});
}

function do_btn_save(e){
	e.preventDefault();
	$("#fround").submit();
}
function do_btn_receive_now(e){
	e.preventDefault();
	if ( $("#fround").valid() == false) return;
	$("#wnd_receive_now").modal({
		closeHTML: "<a href='#' title='Close' class='modal-close'>x</a>",
		position: ["20%",],
		overlayId: 'wnd_receive_now-overlay',
		containerId: 'wnd_receive_now-container', 
		onShow: function (dialog) {
			var modal = this;
			message = "Please wait a moment <img src='" + res_path + "/img/ajax-load_1.gif' border='0' alt=''/>";
			var msg = $('.message', dialog.data[0]);
			msg.html(message);
			$.ajax({
				type: 'POST',
				url: url_receive_now,
				data: $("#fround").serialize(),
				success: function(data, textStatus, jqXHR){
					if (data.error != null && data.error.length > 0 ){
						msg.html( data.error[0] );
					}else if(data.status != null && data.status[0] == 'ok'){
						msg.html( "Check your inbox in a few minutes");// " + data.sentto );
					}else{
						msg.html( "An error occured ... please try again later" );
					}
				},
				dataType: "json"
			});
		}
	});
}
function do_btn_generate_mag(e){
	e.preventDefault();
	if ( $("#fgeneratemag").valid() == false) return;
	$("#popin_generate_mag").modal({
		closeHTML: "<a href='#' title='Close' class='modal-close'>x</a>",
		position: ["20%",],
		overlayId: 'popin_generate_mag-overlay',
		containerId: 'popin_generate_mag-container', 
		onShow: function (dialog) {
			var modal = this;
			var nbCheck = $(':checkbox:checked').length;
			if (nbCheck == 0) {
				message = "You should select at least one item.";
				var msg = $('.message', dialog.data[0]);
				msg.html(message);
				$('.yes').hide();
			}
			$('.yes', dialog.data[0]).click(function () {
				$('.yes').hide();
				var format = $('#format:checked').val();
				var titlemag = $('#titlemag').val();
				message = "Please wait a moment <img src='" + res_path + "/img/ajax-load_1.gif' border='0' alt=''/>";
				var msg = $('.message', dialog.data[0]);
				msg.html(message);
				$.ajax({
					type: 'POST',
					url: url_generate_mag+"?format="+encodeURIComponent(format)+"&titlemag="+encodeURIComponent(titlemag),
					data: $("#fgeneratemag").serialize(),
					success: function(data, textStatus, jqXHR){
						if (data.error != null && data.error.length > 0 ){
							msg.html( data.error[0] );
						}else if(data.status != null && data.status[0] == 'ok'){
							msg.html( "Check your inbox (or your <a href='index.html?action=my_issues'>mag list</a>) in a few minutes");// " + data.sentto );
							$(".mass_action").each(function() {
								$(this).attr('checked',false);
							});
							$("#checkmass").attr('checked',false);
						}else{
							msg.html( "An error occured ... please try again later" );
						}
					},
					dataType: "json"
				});
			});
		}
	});
}

function do_btn_add_bk(e){
	e.preventDefault();
	$("#url_to_bookmark").val("");
	$("#add_bk_msg").html("&nbsp;");
	$("#popin_add_new_bk").modal({
		closeHTML: "<a href='#' title='Close' class='modal-close'>x</a>",
		position: ["20%",],
		overlayId: 'popin_add_new_bk-overlay',
		containerId: 'popin_add_new_bk-container', 
		onShow: function (dialog) {
			var modal = this;
		}
	});
}
