$(document).ready(function(){
	
	// Since 2.0.18 this code were removed from global.js
	$('div.Messages').live('click', function() {
		$(this).fadeOut('fast', function() {
			$(this).remove();
		});
	});
	
	// 2) Pinned footer
	var PinFooter = function() {
		$('#Foot').PinFooter({
			Method:'ExpandBody'
		});
	};

	PinFooter();
	
	$('.Tabs a').first().ajaxSuccess(PinFooter);
	
	var WebRoot = ((typeof gdn != 'undefined' && gdn.definition('WebRoot', '')) || '') + '/';
	WebRoot	= WebRoot.replace(/\/\//g, "/").replace(":/", "://");
	
	var GetScript = function(Url, Callback, Cache) {
		if (typeof(Cache) == 'undefined') Cache = true;
		$.ajax({
			type: "GET",
			url: Url,
			success: Callback,
			dataType: "script",
			cache: Cache
		});
	}
	
	if($.fn.scrollTo && $("div.Errors ul li").length > 0){
		$(document).scrollTo('div.Errors', 800, {offset:-10}); // scroll time length in ms
	}
	
	if ($.fn.menu) $('#Menu').menu({showDelay: 0, hideDelay: 50});
	
	var GardenDomain = ((typeof gdn != 'undefined' && gdn.definition('GardenDomain', '')) || '') + '/';
	GardenDomain = GardenDomain.replace(/\/\//g, "/").replace(":/", "://");
	var GardenHost = parse_url(GardenDomain, 'HOST');
	
	if (typeof($.livequery) == 'function') {
		$('#Popup textarea.TextBox').livequery(function(){
			$(this).css({width:'95%'});
			$(this).prev('label')
				.css('float', 'none')
				.css('width', 'auto');
		});
		
		$('#Popup ul li label.CheckBoxLabel').livequery(function(){
			var p = $(this).parents('ul');
			if (!p.hasClass('CheckBoxList')) {
				$(this)
					.css('float', 'none')
					.css('width', 'auto');
			}
		});
		
		$('#Popup label[for$="Gender"]').livequery(function(){
			var p = $(this).parent();
			if (p.is('li')) p.addClass('Clear');
			p.find('label.RadioLabel').css('width', 'auto');
		});
		
		// External links opens in new window
		$("a[rel~='external'], a[rel=nofollow]").livequery(function(){
			var AnchorHost = parse_url(this.href, 'HOST');
			if (GardenHost != AnchorHost) this.target = "_blank";
		});
	}
	
	var Logo = '<a class="Logo" href="'+GardenDomain+'">'+ $("#Head").find('.Logo').html() + '</a>';
	$("#Head .Logo").replaceWith(Logo);
	
	//console.log($('#OnlineEditor'), $('#OnlineEditor').length);
	if ($.fn.scrollTo && $('#OnlineEditor').length > 0) $(document).scrollTo('#OnlineEditor', 800, {offset:-80});
	
	// Detect CSS3 Support 
	// http://net.tutsplus.com/tutorials/html-css-techniques/quick-tip-detect-css-support-in-browsers-with-javascript/
	var IsSupportCssProperty = (function() {
		var Dummy = document.createElement('dummy');
		var Vendors = 'Khtml O Ms Webkit Moz '.split(' ');
		var Length = Vendors.length;
		var Px, CssProperty;

		return function(PropertyName) {
			for (var Index = Vendors.length - 1; Index >= 0; --Index) {
				Px = Vendors[Index];
				CssProperty = PropertyName;
				if (Px.length > 0) CssProperty = Px + PropertyName.charAt(0).toUpperCase() + PropertyName.substr(1);
				if (CssProperty in Dummy.style) return true;
			}
			return false;
		};
	})();
	
	var bColumnCount = IsSupportCssProperty('columnCount');
	
	//bColumnCount = false; // TEST CODE
	
	if (!bColumnCount) {
		if ($.fn.columnize) {
			$('.ColumnCount2').columnize({columns:2});
			$('.ColumnCount3').columnize({columns:3});
			$('.ColumnCount4').columnize({columns:4});
			$('.ColumnCount5').columnize({columns:5});
		}
	}
	
	if ($("input[placeholder]").length > 0) {
		if (typeof($.placeHeld) != 'function') {
			GetScript(WebRoot + 'plugins/Morf/js/jquery.placeheld.js', function(){
				$("input[placeholder]").placeHeld();
			});
		}
	}
	
	
	// For opera
   // Format email addresses
   $('span.Email.EmailUnformatted').each(function(i, el) {
      var el = $(el);
      el.removeClass('EmailUnformatted');
	  var email = $(this).html().replace(/<em[^>]*>dot<\/em>/ig, '.').replace(/<strong[^>]*>at<\/strong>/ig, '@');
      el.html('<a href="mailto:' + email + '">' + email + '</a>');
   });


});
