jQuery.noConflict();

function applyStyles() {
  jQuery('table.alt_rows tr:even, ul.alt_rows li:even').addClass('alt');
  jQuery('table.alt_rows tr:odd, ul.alt_rows li:odd').removeClass('alt');
}

function refreshLightboxes() {
  jQuery('.lbOn').unbind('click').click(function(e) {
    e.preventDefault();
    tb_show(this.innerHTML, this.href);
  });
}

function hideLightbox() {
  tb_remove();
}

var AdminPane = {

	show_pane: function(pane_tab) {
		$A($('admin_tabs').getElementsByTagName('li')).each(function(tab) {
			var pane = $(tab.id + "_content");
			if (tab.id == pane_tab) {
				pane.show();
				AdminPane.check_for_wysiwyg(pane);
			} else {
				pane.hide();
			}
		});
	},

	check_for_wysiwyg: function(pane) {

		$A(pane.getElementsByTagName('textarea')).each(function(textarea) {
			if ($(textarea).hasClassName('wysiwyg') && ! $(textarea).wysiwyg_loaded) {
				AdminPane.load_wysiwyg($(textarea));
			}
		});

	},

	load_wysiwyg: function(textarea) {
			FCKeditorAPI = null;
			__FCKeditorNS = null;
	    var oFCKeditor = new FCKeditor(textarea.id);
	   	oFCKeditor.ToolbarSet = 'Trabian';
			oFCKeditor.Width = textarea.getDimensions().width;
			oFCKeditor.Height = textarea.getDimensions().height;
	    oFCKeditor.ReplaceTextarea();
			textarea.wysiwyg_editor = oFCKeditor;
			var form = AdminPane.find_form(textarea);
			form.__onsubmit = form.onsubmit;
			form.onsubmit = function() {
				var editor = FCKeditorAPI.GetInstance(textarea.id);
		    editor.UpdateLinkedField();
				form.__onsubmit();
				return false;
			};
			textarea.wysiwyg_loaded = true;
	},

	find_form: function(textarea) {
		var parent = textarea.parentNode;
		while (parent.tagName.toLowerCase() != "form") {
			parent = parent.parentNode;
		}
		return parent;
	}

};

jQuery(document).ready(function($){

  applyStyles();
  
  refreshLightboxes();
  
  $('#search-query').focus(function() {
    if (! this.default_value) {
      this.default_value = this.value;
    }
    if (this.value == this.default_value) {
      this.value = "";
    }
  }).blur(function() {
    if (this.value == "") {
      this.value = this.default_value;
    }
  });
  
  var $global_li = $('#global-navigation ol li');
  
  $global_li.filter(':has(ul)').addClass('has-children');
  
  $global_li.hoverIntent(function() {
    $(this).addClass('hover').find('>span>a').addClass('hover');
  }, function() {
    $(this).removeClass('hover').find('>span>a').removeClass('hover');
  });
  
  jQuery('.adminwindow').click(function() {
    showPopup(this.href, 'yes', this.target);
  });
  
  jQuery('#admin_tabs li a').click(function() {
		var pane = this.parentNode.id;
		AdminPane.show_pane(pane);
		return false;    
  });
  
  jQuery('#hide_flash').click(function(e) {
    e.preventDefault();
    jQuery('#flash_notice').fadeOut();
  });

  jQuery('#admin_tabs li.default').find('a').click();

  jQuery('a[href=/home/about/i3]').attr('href', '/home/innovation/i3');
  
  jQuery('.embedded-publication-list').each(function() {
    var category = jQuery(this).attr('id');
    jQuery(this).load('/publications/content/ajax_list/' + category);
  });
  
  jQuery('#membership-chart').each(function() {
    var $chart = $(this);
    $chart.find('tbody tr:odd:not(.special)').addClass('alt');
    $chart.find('tbody td').wrapInner('<span></span>');
    $chart.find('tbody td:contains(yes)').addClass('yes');
  });
  
  $('#utility-navigation').each(function() {
    var $nav = $(this);
    
    $nav.find('.member-login').click(function(e) {
      e.preventDefault();
      $nav.find('.login-toggle-off').hide();
      $nav.find('.login-toggle-on').show();
      $nav.find('.email').focus();
    });
    
    $nav.find('.cancel').click(function(e) {
      $nav.find('.login-toggle-on').hide();
      $nav.find('.login-toggle-off').show();
    });
    
  });

  jQuery('.hentry:has(.read-more)').each(function() {

    var $post = $(this);

    $post.find('.read-more').appendTo($post.find('.content p:last').append(' '));

  });

});

function openWindow(url, width, height) {

    var newWidth = width + 50;
    var newHeight = height + 50;

    var leftPos = window.screenLeft + (window.document.body.clientWidth - newWidth) / 2;
    var topPos = window.screenTop + (window.document.body.clientHeight - newHeight) / 2;

    var newWindow = window.open(url, 'popup_window', 'toolbar=no,location=no,directories=no,status=no,menubar=no, scrollbars=yes,resizable=yes,width=' + newWidth + ',height=' + newHeight + ',left=' + leftPos + ',top=' + topPos);
    newWindow.focus();

}

function initPopup() {

    window.blur();

    if (!document.all) {
        window.sizeToContent();
        window.sizeToContent();
        // for reasons beyond understanding,
        // only if we call it twice we get the
        // correct size.
        //            window.addEventListener("unload", myclose, true);
        // center on parent
        var px1 = opener.screenX;
        var px2 = opener.screenX + opener.outerWidth;
        var py1 = opener.screenY;
        var py2 = opener.screenY + opener.outerHeight;
        var x = (px2 - px1 - window.outerWidth) / 2;
        var y = (py2 - py1 - window.outerHeight) / 2;
        window.moveTo(x, y);
        var body = document.body;
        window.innerHeight = body.offsetHeight;
        window.innerWidth = body.offsetWidth;

    } else {

        var _body = document.body;
        var contentblock = document.getElementById('contentblock');

        var winW = _body.clientWidth;
        var winH = _body.clientHeight;

        var popW = contentblock.clientWidth + 29;
        var popH = contentblock.clientHeight + 38;

        if (popW > window.screen.width) {
            popW = window.screen.width - 75;
        }

        if (popH > window.screen.height) {
            popH = window.screen.height - 75;
        }

        var leftPos = (window.screen.width - popW) / 2;
        var topPos = (window.screen.height - popH) / 2;

        window.resizeTo(popW + 10, popH + 40);
        if (window.screenLeft < 10 && window.screenLeft > 0) {
            window.moveTo(leftPos, topPos);
        }

    }

    window.focus();

}

function showPopup(url, resizable, windowname) {
    if (!resizable) {
        resizable = 'yes';
    }
    if (!windowname) {
        windowname = 'dialog_window';
    }
    var body = window.document.body;
    window.open(url, windowname, 'toolbar=no,location=no,directories=no,status=yes,menubar=no, scrollbars=yes,resizable=' + resizable + ',width=700,height=600,left=0,top=0');
}

function closePopup(url) {

    if (url != null) {
        window.location = url;
    }

    window.close();

}