﻿jQuery(document).ready(function(){
	// enable lightbox
	//jQuery("img").filter( function(){ return jQuery(this).width() >= 200; } ).lightbox();
	jQuery('#ssw-googlesearch-trigger').click(function() { window.location = 'http://www.google.com/search?q=' + jQuery('#ssw-googlesearch-field').val() + '%20site:sharepoint.ssw.com.au'; return false; });
	jQuery('#ssw-googlesearch-field').keypress(function(evt) { if (evt.keyCode == 13) { jQuery('#ssw-googlesearch-trigger').click(); return false; } });
	
	//jQuery('.CssFriendly-Menu-Dropdown-ItemHost').append("<div class='clear'></div>");
	jQuery('.CssFriendly-Menu-Dropdown-ItemHost').append("<div class='rcl'><div></div></div>");
	jQuery('.CssFriendly-Menu-Flyout').append("<div class='rcr'><div></div></div>");

	jQuery('.CssFriendly-Menu-Dropdown').each(function() {
		var host = jQuery(this).children('.CssFriendly-Menu-Dropdown-ItemHost');
		var flyout = jQuery(this).children('.CssFriendly-Menu-Flyout');
		
		if (flyout.height() > host.height()) {
			host.height(flyout.height()+1);
		}
		else {
			flyout.height(host.height()-1);
		}
	});
	
	jQuery('.CssFriendly-Menu-Dropdown-ItemHost .rcl').each(function() { jQuery(this).width(jQuery(this).parent().width()+2); });
	jQuery('.CssFriendly-Menu-Dropdown-ItemHost').each(function() { jQuery(this).css('background-position', 'left ' + (jQuery(this).height() - 156) + 'px'); });
	jQuery('.CssFriendly-Menu-Horizontal li a.TopLevelNavItem').each(function() { jQuery(this).css('background-position', 'left ' + (jQuery(this).parent().find('.CssFriendly-Menu-Dropdown-ItemHost').height() - 121) + 'px'); });
	jQuery('.CssFriendly-Menu-Dropdown-ItemHost .rcl div').each(function() { jQuery(this).css('background-position', 'left bottom'); });
	jQuery('.CssFriendly-Menu-Flyout .rcr').each(function() { jQuery(this).width(jQuery(this).parent().width()+11); });
	
	jQuery('.CssFriendly-Menu-Dropdown-ItemHost a').each(function() {
			var obj = jQuery(this);
			var body = obj.text();
			body = body.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
			var length = 17;

			if(body.length > length) {
				var splitLocation = body.indexOf(' ', length);
				if(splitLocation != -1) {
					// truncate tip
					var splitLocation = body.indexOf(' ', length);
					var str1 = body.substring(0, splitLocation);
					var str2 = body.substring(splitLocation, body.length - 1);
					obj.html(str1 + "...");
				}
			} // end if
	});
	setupSwitchToAuthoring();
});


function setupSwitchToAuthoring(){
	jQuery("a.switchToAuthoring").click(function(){
		if (document.forms['aspnetForm']['MSOLayout_InDesignMode'] != null) 
			document.forms['aspnetForm']['MSOLayout_InDesignMode'].value = 1;
		if (document.forms['aspnetForm']['MSOAuthoringConsole_FormContext'] != null) 
			document.forms['aspnetForm']['MSOAuthoringConsole_FormContext'].value = 1;
		if (document.forms['aspnetForm']['MSOSPWebPartManager_DisplayModeName'] != null) 
			document.forms['aspnetForm']['MSOSPWebPartManager_DisplayModeName'].value = 'Design';
		__doPostBack('ctl00$SiteActionsMenuMain$SiteActionsMenuMain$ctl00$wsaEditPage_CmsActionControl','switchToAuthoring');
	});
}
