var Meadz = {}; // create a namespace

$(document).ready(function(){
    
    do_main_menu();   
});

var do_main_menu = function() {
    
    $('#nav > li.topnav').hover(
        function(){ 
          $(this).addClass("open").children("div.submenu").show().addClass("open");
        },
        function(){ 
          $(this).removeClass("open").children("div.submenu").hide().removeClass("open");
        }
    );
};


function tb_init(domChunk){
	$(domChunk).click(function(){
	var t = this.title || this.name || null;
	var a = this.href || this.alt;
	var g = this.rel || false;
	tb_show(t,a,g);
	this.blur();
	return false;
	});
}


function truncate_words(trunc,len){
	if (trunc.length > len) {
	    trunc = trunc.substring(0, len);
	    trunc = trunc.replace(/\w+$/, '');
		trunc += "&hellip;"
	}
	return trunc;
}
function disable_submit_buttons(){
	$(clicked).attr("disabled",true);
	clicked = null;
}
//the purpose of the setTimeout is to allow for multiple submit buttons - if the button is disabled then its value doesnt get posted
$.fn.submitMagic = function(){
	this.each(function(){
		if (!jQuery.browser.msie)
			$('button[@type=submit], input[@type=button], input[@type=submit]', this)
				.after('<span class="progress" title="Submitting form, please wait."></span>')
		$('button[@type=submit], input[@type=button], input[@type=submit]', this)
		.click(function(){
			clicked = this;
			setTimeout("disable_submit_buttons()",20);
		})
	});
	return this;
}

function show_submit_buttons() {
	if (jQuery.browser.msie)
		$('span.progress').fadeOutAndRemove(2000);
	else
		$("form").removeClass("show_progress");
	$('button[@type=submit],input[@type=submit],input[type=button]').attr("disabled", false);
}

/**
* hoverIntent r5 // 2007.03.27 // jQuery 1.1.2+
* <http://cherne.net/brian/resources/jquery.hoverIntent.html>
* 
* @param  f  onMouseOver function || An object with configuration options
* @param  g  onMouseOut function  || Nothing (use configuration options object)
* @author    Brian Cherne <brian@cherne.net>
*/
(function(jQuery){jQuery.fn.hoverIntent=function(f,g){var cfg={sensitivity:7,interval:100,timeout:0};cfg=jQuery.extend(cfg,g?{over:f,out:g}:f);var cX,cY,pX,pY;var track=function(ev){cX=ev.pageX;cY=ev.pageY;};var compare=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);if((Math.abs(pX-cX)+Math.abs(pY-cY))<cfg.sensitivity){jQuery(ob).unbind("mousemove",track);ob.hoverIntent_s=1;return cfg.over.apply(ob,[ev]);}else{pX=cX;pY=cY;ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}};var delay=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);ob.hoverIntent_s=0;return cfg.out.apply(ob,[ev]);};var handleHover=function(e){var p=(e.type=="mouseover"?e.fromElement:e.toElement)||e.relatedTarget;while(p&&p!=this){try{p=p.parentNode;}catch(e){p=this;}}if(p==this){return false;}var ev=jQuery.extend({},e);var ob=this;if(ob.hoverIntent_t){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);}if(e.type=="mouseover"){pX=ev.pageX;pY=ev.pageY;jQuery(ob).bind("mousemove",track);if(ob.hoverIntent_s!=1){ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}}else{jQuery(ob).unbind("mousemove",track);if(ob.hoverIntent_s==1){ob.hoverIntent_t=setTimeout(function(){delay(ev,ob);},cfg.timeout);}}};return this.mouseover(handleHover).mouseout(handleHover);};})(jQuery);

SideMenu = {
    init : function(module, page_count, current_page){
        var pages = jQuery('#' + module + '_pagination').append("<ul></ul>").children("ul");
		for(i=1;i<=page_count;i++) 
			pages.append('<li id="'+ module + '_page_' + i + '"><a href="#" onclick="return SideMenu.load_module_page(\'' + module + '\',' + i + ')">' + i + '</a></li>');
		jQuery('#' + module + '_page_' + current_page).addClass("currentpage");
        // jQuery('#' + module).each(function(){
        //   jQuery(this).css("height", this.clientHeight + "px");
        // });
    },
    load_module_page: function(module, page) {
		jQuery('#' + module + ' div.current').removeClass("current");
		jQuery('#' + module + '_menu_page_' + page).addClass("current");
		jQuery('#' + module + '_pagination li.currentpage').removeClass("currentpage");
		jQuery('#' + module + '_page_' + page).addClass("currentpage");
		jQuery.post("/remember_menu_tab", {menu: module, page: page, folder: menu_folder}); // folder is a global var defined when the menu that uses this function is created
		return false;
	}
}

function Clicktoprint() { 
             var disp_setting="toolbar=yes,location=no,directories=yes,menubar=yes,"; 
             disp_setting+="scrollbars=yes,width=650, height=600, left=100, top=25"; 
             var content_vlue = document.getElementById("content").innerHTML;  
             var docprint=window.open("","",disp_setting); 
             docprint.document.open(); 
             docprint.document.write('<html><head><title>Chemchemi Ya Ukweli</title>'); 
             docprint.document.write('</head><body onLoad="self.print()"><center>');          
             docprint.document.write(content_vlue);          
             docprint.document.write('</center></body></html>'); 
             docprint.document.close(); 
             docprint.focus(); 
}
function mailpage() {
             mail_str = "mailto:?subject=Check out this page: " + document.title;
             mail_str += "&body=I thought you might be interested in: " + document.title;
             mail_str += ". You can view it at: " + location.href;
             location.href = mail_str;
}
function openWindow(path) {
	window.open(path, "", "width=280,height=330,dependent=yes,status=no,toolbar=no,location=no,scrollbars=no");
}
function bookmarkSite(title,url) {
    if (window.sidebar) // firefox
	        window.sidebar.addPanel(title, url, "");
    else if(window.opera && window.print){ // opera
	    var elem = document.createElement('a');
	    elem.setAttribute('href',url);
	    elem.setAttribute('title',title);
	    elem.setAttribute('rel','sidebar');
	    elem.click();
    } 
    else if(document.all)// ie
	        window.external.AddFavorite(url, title);
}

