// tiny mce
try {
	tinyMCE.init({
	    mode:                               "textareas",
	    theme:                              "advanced",
	    editor_selector:                    "editor",
	    height:                             "450px",
		width:                              "100%",
        content_css:                        "/includes/css/admin.tinymce.content.css",
        plugins:                            "advlink,ibrowser,paste",
	    theme_advanced_toolbar_location:    "top",
	    theme_advanced_toolbar_align:       "left",
	    theme_advanced_statusbar_location:  "bottom",
	    theme_advanced_resizing:            true,
		theme_advanced_buttons1 :           "formatselect,bold,italic,underline,justifyleft,justifycenter,justifyright,justifyfull,bullist,numlist,undo,redo,link,ibrowser,code,pastetext,pasteword,selectall",
        theme_advanced_buttons2 :           "",
        theme_advanced_buttons3 :           "",
        theme_advanced_toolbar_location :   "top",
        theme_advanced_toolbar_align :      "left",
        theme_advanced_statusbar_location : "bottom",
		theme_advanced_blockformats:        "p,address,pre,h2,h3,h4,h5,h6",
		paste_auto_cleanup_on_paste:        true
	});	
}
catch (e) {
	// do nothing
}

function html5Expression(type) {
	var 
		regex = new RegExp('type="'+ type +'"'),
		markup = jQuery("<div/>").append(jQuery(this).clone())
	;
	return type === this.type // for browsers that understand the expression
	|| regex.test(markup.html()); // catch others by inspection
}

// HTML5 expressions
jQuery.extend(jQuery.expr.filters, {
	number: function(elem) { return html5Expression.apply(elem, ['number']); },
	search: function (elem) { return html5Expression.apply(elem, ['search']); },
	url: function (elem) { return html5Expression.apply(elem, ['url']); },
	tel: function (elem) { return html5Expression.apply(elem, ['tel']); },
	email: function (elem) { return html5Expression.apply(elem, ['email']); },
	range: function (elem) { return html5Expression.apply(elem, ['range']); },
	color: function (elem) { return html5Expression.apply(elem, ['color']); },
	datetime: function (elem) { return html5Expression.apply(elem, ['datetime']); }
});

jQuery(function($) {
	
	// markdown dingus
	$('.dingus h3')
		.live('click', function() {
			var next = $(this).next();
			if (next.is(':hidden')) {
				$(this).addClass('expanded');
				next.slideDown();
			} else {
				next.slideUp();
				$(this).removeClass('expanded');
			}
		})
		.next()
		.hide();

	// file uploader
	$('input:file').uploader({
		'url': '/goose/vendor/jquery.fileuploader/vendor/php/backend.php',
		'indicator': '<img src="/goose/vendor/jquery.fileuploader/theme/indicator.gif" width="16" height="16" class="spinner" style="display:none" />',
		'trash': '<img src="/goose/vendor/jquery.fileuploader/theme/trash_on.gif" alt="remove" width="10" height="11" border="0" />'
	});
	

	(function($) {
		// ignore these special keys and period (190)
		var ignore = [8,9,13,33,34,35,36,37,38,39,40,46,190];

		// TODO: should use live
		$('input:number')
			.bind('keypress', function(event) {
				var code = $(this).data('keycode');
				return /[0-9\.]/.test(
					String.fromCharCode(code)
				) || $.inArray(code, ignore) !== -1;
			})
			.bind('keydown', function(event) {
				$(this).data('keycode', event.keyCode || event.which);
			});
	})($);
	
	$('.pretty-date').prettyDate();
	
	$('.editor').each(function () { });
	
	
	//  insert datepicker
	$('input:datetime, .datetime').datepicker({
		showOn: 'button',
		buttonImage: '/goose/img/calendar.gif',
		buttonImageOnly: true,
		dateFormat: 'yy-mm-dd',
		onSelect: function(dateText, inst) {
			var dt = new Date();
			var time = dt.getHours() +':'+ dt.getMinutes() +':'+ dt.getSeconds();
			$(this).val(dateText + ' ' + time);
		}
	});
	$('#ui-datepicker-div').wrap('<span class="goose" />');
	
	// textaera[maxlength]
	(function($) {
	
		// ignore these keys
		var ignore = [8,9,13,33,34,35,36,37,38,39,40,46];
		
		// use keypress instead of keydown as that's the only 
		// place keystrokes could be canceled in Opera
		var eventName = 'keypress';
		
		// handle textareas with maxlength attribute
		$('textarea[maxlength]')
			
			// this is where the magic happens
			.live(eventName, function(event) {
				var self = $(this),
					maxlength = self.attr('maxlength'),
					code = $.data(this, 'keycode');
			
				// check if maxlength has a value.
				// The value must be greater than 0
				if (maxlength && maxlength > 0) {
			
					// continue with this key stroke if maxlength
					// not reached or one of the ignored keys were pressed.
					return ( self.val().length < maxlength
						|| $.inArray(code, ignore) !== -1 );
					
				}
			})
			
			// store keyCode from the keydown event for later use
			.live('keydown', function(event) {
				$.data(this, 'keycode', event.keyCode || event.which);
			});
	
	})($);

	// add .textbox to the following
	$('input:text, input:password, textarea, select, input:number, input:datetime').addClass('textbox');
	
	// tabs
	$('.content .js-tabs').tabs();
});

