// JavaScript Document

// IE8 doesn't support trim()
if (typeof String.prototype.trim !== 'function') {
	String.prototype.trim = function() 
	{     
		return this.replace(/^\s+|\s+$/g, '');    
	};
}  

jQuery(document).ready(function() {
	// Initialize search form
	jQuery('.header .search .searchfield').addClass('searchblur');
	
	// Search form focus
	jQuery('.header .search .searchfield').focus(function () {
		if (this.value == searchDefaultValue) {
			this.value = '';
		} else {
			this.select();
		}
		jQuery(this).addClass('searchfocus').removeClass('searchblur');
	});
	
	// Search form blur
	jQuery('.header .search .searchfield').blur(function () {
		if (jQuery.trim(this.value) == '') {  
			this.value = searchDefaultValue;  
			jQuery(this).addClass('searchblur').removeClass('searchfocus');
		}
	});
		
	// Enable custom drop-down lists. Style only siba layouts and footer*/
	jQuery('.siba-layout select').each(function() {
		// Don't do aui-fields
		if (!jQuery(this).hasClass('.aui-field-select')) {
			var width = jQuery(this).outerWidth();
			jQuery(this).selectmenu({
					menuWidth: 192,
					positionOptions: {
						at: 'left bottom',
						offset: '0 0'
					},
					maxHeight: 350,
					width: 192 
				});
		}
	}); 

	jQuery('.footer select').each(function() {
		var width = jQuery(this).outerWidth();
		jQuery(this).selectmenu({
				menuWidth: width - 20,
				positionOptions: {
					at: 'left bottom',
					offset: '8 0'
				},
				width: width 
			});
	});
	
	
	// Set equal heights on columns within each column group
	jQuery('.siba-layout-column').parent().each(function() {
		var heights = jQuery(this).children('.column').map(function() {
			return jQuery(this).height();
		}).get();
		var maxHeight = Math.max.apply(Math, heights);
		jQuery(this).children('.column').height(maxHeight);
	});
	
	// Calculate drop-down menu width
	jQuery('ul.nav2-dropdown').each(function() {
		jQuery(this).css('max-width', Math.max(180, jQuery(this).parent().width() - 4));
		jQuery(this).css('min-width', Math.max(160, jQuery(this).parent().width() - 4));
	});
	
	// Open drop-down menu
	jQuery('ul.nav2-dropdown').parent().mouseover(function() {
		jQuery(this).addClass('focused');
		jQuery(this).find('ul.nav2-dropdown').removeClass('hidden');
	});
	
	// Close drop-down menu
	jQuery('ul.nav2-dropdown').parent().mouseout(function() {
		jQuery(this).removeClass('focused');
		jQuery(this).find('ul.nav2-dropdown').addClass('hidden');
	});
	
	if (!Modernizr.boxshadow) {
		jQuery('.siba-frontpage-2-3-cols-layout .siba-layout-content,.siba-frontpage-1-3-cols-layout .siba-layout-content,.siba-frontpage-1-4-cols-layout .siba-layout-content').wrap('<div class="shadow" />');
		jQuery('.sidebox').wrap('<div class="shadow" />');
	}
	
	// Remove border from three column layout if there is no content
	var temp = jQuery(".siba-3-cols-layout #column-1 .portlet-column-content").html()
	if (temp != null && temp.trim().length < 1)
		jQuery(".siba-3-cols-layout #column-2").css("border-left", "none");
	
	// Remove border from two column layout if there is no content
	temp = jQuery(".siba-2-cols-wide-narrow-layout #column-1 .portlet-column-content").html()
	if (temp != null && temp.trim().length < 1)
		jQuery(".siba-2-cols-wide-narrow-layout #column-2").css("border-left", "none");
	temp = jQuery(".siba-2-cols-narrow-wide-layout #column-1 .portlet-column-content").html()
	if (temp != null && temp.trim().length < 1)
		jQuery(".siba-2-cols-narrow-wide-layout #column-2").css("border-left", "none");

});

