function refreshTips(viewid){
	window.addEvent('domready', function() {
	var JTooltips = new Tips($$('.hasTip'+viewid),
		{ className: 'zc_popup'});
	});
	window.addEvent('domready', function(){ 
	var JTooltips = new Tips($$('.hasTip'), 
		{ maxTitleChars: 50, fixed: false}); 
	});

}

function refreshModals(view){
window.addEvent('domready', function() {
	SqueezeBox.initialize({});
	$$("a.modal"+view).each(function(el) {
		el.addEvent("click", function(e) {
			new Event(e).stopPropagation();
			new Event(e).stop();
			SqueezeBox.fromElement(el);
			return false;
		});
	});
});
}

function runPluginJS(){
window.addEvent('domready', function() {
	$$(".zplugin").each(function(el){
		var pfunction = new Function(el.title);
		pfunction();
	});
});
}

// this cleans up the popup box that may not have received mouseout event in ajax view
function cleanPopup(){
	elem = document.getElementById('zc_popup');
	if(elem != null)
	elem.parentNode.removeChild(elem);
}

function refreshZ(viewid){
	//if(window.jQuery)
	//	jQuery.noConflict();
	cleanPopup();
	refreshTips(viewid);
	refreshModals(viewid);
	runPluginJS();
	setCalendarState(viewid);
	mydate = new Date();
	if(typeof(zc_monthstart) != 'undefined')
		showMonthBrowser(viewid,zc_monthstart, mydate.getFullYear() + "-01-01");
}

function setCalendarState(viewid){
	elem = document.getElementById('zc_cal_cal_' + viewid);
	if(elem != null){
		state = readCookie("zc_calendar_"+viewid);
		if(state != null){
			elem.style.display = state;
		}
	}
}

function toggleCalendar(viewid){
	elem = document.getElementById('zc_cal_cal_' + viewid);
	if(elem != null){
		if(elem.style.display == "inline") {
			elem.style.display = "none";
			createCookie("zc_calendar_"+viewid,"none");
		}
		else {
			elem.style.display = "inline";
			createCookie("zc_calendar_"+viewid,"inline");
		}
	}
}

function createCookie(name,value,hours) {
	if(!hours)
		hours = 24;
	var date = new Date();
	date.setTime(date.getTime()+(hours*60*60*1000));
	var expires = "; expires="+date.toGMTString();
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function addMonth(dateobj,addmonth){
	newmonth = dateobj.getMonth() + addmonth;
	addyear = 0;
	if(newmonth > 11){
		addyear = Math.floor(newmonth / 12);
		newmonth = newmonth % 12;
	}
	dateobj.setMonth(newmonth);
	dateobj.setFullYear(dateobj.getFullYear() + addyear);
	return dateobj;
}

function showMonthBrowser(viewid,monthstart,strdate) {
 	function pad(n){return n<10 ? '0'+n : n};
 	thislink = document.getElementById("zc_thislink");
	monthdiv = document.getElementById("zc_monthbrowserpopup");
	if(monthdiv && thislink){
		//monthnames = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
		monthnames = getMonthNames();
		months = new Array();
		curyear = strdate.substr(0,4);
		curmonth = strdate.substr(5,2) - 1;
		curday = strdate.substr(8,2);
		curdate =  new Date(curyear,curmonth,curday)
		nextyear = new Date(curyear,curmonth,curday) 
		prevyear = new Date(curyear,curmonth,curday);
		nextyear.setFullYear(nextyear.getFullYear() + 1);
		prevyear.setFullYear(prevyear.getFullYear() - 1);
		strnextyear = nextyear.getFullYear() + "-" + pad((nextyear.getMonth()+1)) + "-" + pad(nextyear.getDate());
		strprevyear = prevyear.getFullYear() + "-" + pad((prevyear.getMonth()+1)) + "-" + pad(prevyear.getDate());
		prevurl = "index2.php?option=com_zcalendar&task=monthbrowser&viewid=" + viewid + "&date=" + strprevyear;
		nexturl = "index2.php?option=com_zcalendar&task=monthbrowser&viewid=" + viewid + "&date=" + strnextyear;

		udate = new Date(curyear, monthstart, 1);
		udatebegin = new Date(curyear, curmonth, 1);
		if(udate > udatebegin)
			udate.setFullYear(udate.getFullYear() - 1);
		for(i=0; i < 12; i++){
			idate = udate.getFullYear() + "-" + pad((udate.getMonth()+1)) + "-" + pad(udate.getDate());
			inum = udate.getMonth();
			//url =  document.location.protocol + "//" + document.location.host + "/index2.php?option=com_zcalendar&view=calendar&date=" + idate + "&format=html&ajax=1&viewid=" + viewid + "&type=raw&vmode=month";
			url = thislink.href.replace("option=com_zcalendar","option=com_zcalendar&date=" + idate);
			months[i] = new Array(monthnames[udate.getMonth()],inum,url,udate.getFullYear());
			udate = addMonth(udate,1);
		}
		//htmlstr = "<ul><div class='leftmonthbutton'><a href='javascript:loadURL(\"" + prevurl + "\",\"zc_monthbrowserpopup\")'>&lt;</a></div><div class='rightmonthbutton'><a href='javascript:loadURL(\"" + nexturl + "\",\"zc_monthbrowserpopup\")'>&gt;</a></div><div>\n";
		htmlstr = "<ul><div class='leftmonthbutton'><a href='javascript:showMonthBrowser(" + viewid + "," + monthstart + "," + "\"" + strprevyear + "\"" + ");'>&lt;</a></div><div class='rightmonthbutton'><a href='javascript:showMonthBrowser(" + viewid + "," + monthstart + "," + "\"" + strnextyear + "\"" + ");'>&gt;</a></div><div>\n";
		count = 0;
		for(i=0; i < 12; i++){
			count++;
			year = "";
			if(months[i][1] == 0 || count == 1)
				year = " " + months[i][3];
			//htmlstr += "<li><a href=\"javascript:loadURL" + viewid + "('" + months[i][2] + "')\">" + months[i][0] + year+ "</a></li>\n";
			htmlstr += "<li><a href=\"" + months[i][2] + "\">" + months[i][0] + year + "</a></li>\n";
		}
		htmlstr += "</div></ul>\n";
		monthdiv.innerHTML = htmlstr;		
	}
}

