var t;
var currentexpandedlistdiv = '';
var LastSeenExpandedListItemID = 0;

function startCount(categoryid) {
	var time = document.getElementById('reviewrotations_' + categoryid).value;
	t = setTimeout("changeReviewContent(" + categoryid + ")", time);
}

function stopCount() {
	clearTimeout(t);
}

function changeReviewContent(categoryid) {
	var sfEls = document.getElementById("shortreviewsbuttonsdiv_" + categoryid).getElementsByTagName("table");
	var tchilds = sfEls[0].tBodies[0].childNodes[0];
	var ind = 0;
	var ind1 = 0;
	var objectid = 0;

	if (currentreviewdiv == '') {
		for (var i=0; i<tchilds.childNodes.length; i++) {
			if (tchilds.childNodes[i].childNodes[0].style.visibility == 'visible') {
				ind = i;
				objectid = tchilds.childNodes[i].childNodes[0].id.replace('imagetop_', '');
			}
		}
	}
	else {
		for (var i=0; i<tchilds.childNodes.length; i++) {
			if (tchilds.childNodes[i].childNodes[0].id == 'imagetop_' + currentreviewdiv) {
				ind = i;				
			}
		}
		
		if (ind == (tchilds.childNodes.length-1))				
			objectid = tchilds.childNodes[0].childNodes[0].id.replace('imagetop_', '');
		else
			objectid = tchilds.childNodes[(ind+1)].childNodes[0].id.replace('imagetop_', '');
	}	

	tchilds.childNodes[ind].childNodes[0].style.visibility = 'hidden';
	tchilds.childNodes[ind].childNodes[1].childNodes[0].style.backgroundImage = 'url(\'/app_cm2/images/button_noactiv_0.gif\')';
	tchilds.childNodes[ind].childNodes[1].childNodes[1].style.backgroundImage = 'url(\'/app_cm2/images/button_noactiv_2.gif\')';
	tchilds.childNodes[ind].childNodes[1].childNodes[2].style.backgroundImage = 'url(\'/app_cm2/images/button_noactiv_1.gif\')';			
	tchilds.childNodes[ind].childNodes[1].childNodes[2].style.color	= '#f7a35e';
	document.getElementById('shortreviewscontentid_' + currentreviewdiv).style.display = 'none';	

	ind1 = ((ind == (tchilds.childNodes.length-1)) ? 0 : (ind+1));
	tchilds.childNodes[ind1].childNodes[0].style.visibility = 'visible';
	tchilds.childNodes[ind1].childNodes[1].childNodes[0].style.backgroundImage = 'url(\'/app_cm2/images/button_activ_0.gif\')';
	tchilds.childNodes[ind1].childNodes[1].childNodes[1].style.backgroundImage = 'url(\'/app_cm2/images/button_activ_2.gif\')';
	tchilds.childNodes[ind1].childNodes[1].childNodes[2].style.backgroundImage = 'url(\'/app_cm2/images/button_activ_1.gif\')';			
	tchilds.childNodes[ind1].childNodes[1].childNodes[2].style.color = '#ffffff';
	
	document.getElementById('shortreviewscontentid_' + objectid).style.display = 'block';	
	
	currentreviewdiv = objectid;
	stopCount();
	startCount(categoryid);	
}

function showReviewContent(categoryid, objectid) {
	stopCount();
	
	if (currentreviewdiv != objectid) {	
		document.getElementById('imagetop_' + objectid).style.visibility = 'hidden';
		document.getElementById('image0_' + objectid).style.backgroundImage = 'url(\'/app_cm2/images/button_activ_0.gif\')';
		document.getElementById('image2_' + objectid).style.backgroundImage = 'url(\'/app_cm2/images/button_activ_2.gif\')';
		document.getElementById('image1_' + objectid).style.backgroundImage = 'url(\'/app_cm2/images/button_activ_1.gif\')';
		//document.getElementById('image1_' + objectid).style.color = '#ffffff';
	}
}

function hideReviewContent(categoryid, objectid) {
	var sfEls = document.getElementById("shortreviewsbuttonsdiv_" + categoryid).getElementsByTagName("table");
	var tchilds = sfEls[0].tBodies[0].childNodes[0];
	for (var i=0; i<tchilds.childNodes.length; i++) {
		if (currentreviewdiv != objectid) {
			document.getElementById('imagetop_' + objectid).style.visibility = 'hidden';
			document.getElementById('image0_' + objectid).style.backgroundImage = 'url(\'/app_cm2/images/button_noactiv_0.gif\')';
			document.getElementById('image2_' + objectid).style.backgroundImage = 'url(\'/app_cm2/images/button_noactiv_2.gif\')';
			document.getElementById('image1_' + objectid).style.backgroundImage = 'url(\'/app_cm2/images/button_noactiv_1.gif\')';
			document.getElementById('image1_' + objectid).style.color = '#f7a35e';
			
		}
	}
	
	startCount(categoryid);
}

function checkReviewContent(categoryid, objectid) {	
	var sfEls = document.getElementById("shortreviewsbuttonsdiv_" + categoryid).getElementsByTagName("table");
	var tchilds = sfEls[0].tBodies[0].childNodes[0];
	for (var i=0; i<tchilds.childNodes.length; i++) {
		tchilds.childNodes[i].childNodes[0].style.visibility = 'hidden';
		tchilds.childNodes[i].childNodes[1].childNodes[0].style.backgroundImage	= 'url(\'/app_cm2/images/button_noactiv_0.gif\')';
		tchilds.childNodes[i].childNodes[1].childNodes[1].style.backgroundImage	= 'url(\'/app_cm2/images/button_noactiv_2.gif\')';
		tchilds.childNodes[i].childNodes[1].childNodes[2].style.backgroundImage	= 'url(\'/app_cm2/images/button_noactiv_1.gif\')';			
		tchilds.childNodes[i].childNodes[1].childNodes[2].style.color = '#f7a35e';
	}
	
	var divEls = document.getElementById("shortreviewsdivid_" + categoryid).getElementsByTagName("div");
	for (var i=0; i<divEls.length; i++) {
		if (divEls[i].id.indexOf("shortreviewscontentid_") != -1)
			divEls[i].style.display = 'none';
	}
		
	document.getElementById('imagetop_' + objectid).style.visibility = 'visible';
	document.getElementById('image0_' + objectid).style.backgroundImage = 'url(\'/app_cm2/images/button_activ_0.gif\')';
	document.getElementById('image2_' + objectid).style.backgroundImage = 'url(\'/app_cm2/images/button_activ_2.gif\')';
	document.getElementById('image1_' + objectid).style.backgroundImage = 'url(\'/app_cm2/images/button_activ_1.gif\')';
	document.getElementById('image1_' + objectid).style.color = '#ffffff';
	document.getElementById("shortreviewscontentid_" + objectid).style.display = 'block';	
			
	currentreviewdiv = objectid;
}

function showFullContent(id) {
	document.getElementById('morebutton_' + id).style.display = 'none';
	document.getElementById('fullcontent_' + id).style.display = 'block';
	document.getElementById('hidebutton_' + id).style.display = 'block';		
}
function hideFullContent(id) {
	document.getElementById('morebutton_' + id).style.display = 'block';
	document.getElementById('fullcontent_' + id).style.display = 'none';
	document.getElementById('hidebutton_' + id).style.display = 'none';		
}

function showFullContent(componentid, pageid) {
	document.getElementById('morebutton_' + componentid + '_' + pageid).style.display = 'none';
	document.getElementById('fullcontent_' + componentid + '_' + pageid).style.display = 'block';
	document.getElementById('hidebutton_' + componentid + '_' + pageid).style.display = 'block';		
}

function hideFullContent(componentid, pageid) {
	document.getElementById('morebutton_' + componentid + '_' + pageid).style.display = 'block';
	document.getElementById('fullcontent_' + componentid + '_' + pageid).style.display = 'none';
	document.getElementById('hidebutton_' + componentid + '_' + pageid).style.display = 'none';	
	document.getElementById('fullcontent_' + componentid + '_' + pageid).innerHTML = '';	
}

function showHideExpandedListContent(objectid, listid, themesystemname) {
	if (themesystemname == '')
		themesystemname = 'main';
	var item = document.getElementById('expandedlist_' + objectid);
	if (item.style.display == 'block') {
		item.style.display = 'none';
		item.previousSibling.firstChild.firstChild.src = '/themes/' + themesystemname + '/images/plus.gif';
		return;
	}
	var list = document.getElementById('expandedlist_' + listid);
	var imgs = list.getElementsByTagName('img');
	for (var i=0; i<imgs.length; i++)
		imgs[i].src = '/themes/' + themesystemname + '/images/plus.gif';
	var divs = list.getElementsByTagName('div');
	for (var i=0; i<divs.length; i++) {
		if (divs[i].id.indexOf('expandedlist_') == 0)
			divs[i].style.display = 'none';
	}
	
	var i=0;
	while (item.className != 'expandedlist') {
		item.previousSibling.firstChild.firstChild.src = '/themes/' + themesystemname + '/images/minus.gif';
		item.style.display = 'block';
		item = item.parentNode;
		i++;
		if (i == 20)
			break;
	}

	window.location='#expandedlisthreflocation_' + objectid;
}

function replaceNullString(text) {
	var t = text;
	var re = new RegExp(' ','g');
	var a = t.replace(re, '');
	return (a);
}

function showMainReferenceDiv(id) {
     var oldid = document.cstreferences_form.cstreferenceid.value;
     var newid = id;
     
     if (oldid != '' && oldid > 0)
          document.getElementById('cstreference_' + oldid).style.display = 'none';
     document.getElementById('cstreference_' + newid).style.display = 'block';
     
     oldid = newid;
}

var mainRefInterval = 0;
var mainRefTimeout = null;
function showMainRef(id) {
	if (mainRefTimeout)
		clearTimeout(mainRefTimeout);
	$$('div.mbox_c').each(function(obj) {
		if (obj.id != 'mref_' + id) {
			obj.addClassName('mrefnonactive');
			obj.removeClassName('mrefactive');
		}
		else {
			obj.addClassName('mrefactive');
			obj.removeClassName('mrefnonactive');
		}
	});
	if (mainRefInterval > 0)
		mainRefTimeout = setTimeout("rotateMainRef();", mainRefInterval);
}

function rotateMainRef(interval) {
	if (interval > 0) {
		mainRefInterval = interval;
		mainRefTimeout = setTimeout("rotateMainRef();", mainRefInterval);
	}
	else {
		var ind = 0;
		var items = $$('div.mbox_c');
		items.each(function(obj, index) {
			if (obj.hasClassName('mrefactive'))
				ind = index;
		});
		ind++;
		if (items.size() <= ind)
			ind = 0;
		
		showMainRef(items[ind].id.substring(5));
	}
}


var mainNewsInterval = 0;
var mainNewsTimeout = null;
function showMainNews(id) {
	if (mainNewsTimeout)
		clearTimeout(mainNewsTimeout);
	$('div.mainboxnews').each(function(ind, obj) {
		if (obj.id != 'news_' + id) {
			$(obj).addClass('mrefnonactive');
			$(obj).removeClass('mrefactive');
		}
		else {
			$(obj).addClass('mrefactive');
			$(obj).removeClass('mrefnonactive');
		}
	});
	if (mainNewsInterval > 0)
		mainNewsTimeout = setTimeout("rotateMainNews();", mainNewsInterval * 1000);
}

function rotateMainNews(interval) {	
	if (interval > 0) {
		mainNewsInterval = interval;
		mainNewsTimeout = setTimeout("rotateMainNews();", mainNewsInterval * 1000);
	}
	else {
		var ind = 0;
		var items = $('div.mainboxnews');
		items.each(function(index, obj) {
			if ($(obj).hasClass('mrefactive'))
				ind = index;
		});
		
		ind++;
		if (items.size() <= ind)
			ind = 0;
				
		showMainNews(items[ind].id.substring(5));
	}
}

var calendarDays = {};
var currentYear = null;
var currentMonth = null;
function calendarSet(year, month) {
	if (year == null) {
		var d = $("#datepicker_calendar").datepicker("getDate");
		year = d.getFullYear();
		month = d.getMonth()+1;
	}
	$("#datepicker_calendar").datepicker("disable");
	calendarDays = {};
	currentYear = year;
	currentMonth = month;
	category = $('#calcategoryid').val();
	$.post('ajax/calendar', { year: year, month: month, category: category }, function(data) {
	  	var t = data.split('\n');
	  	for (var i=0; i<t.length; i++) {
		  	if (t[i].length > 0)
		  		calendarDays[t[i]] = 1;
	  	}
	  	$("#datepicker_calendar").unmask()
		$("#datepicker_calendar").datepicker("enable");
		$("#datepicker_calendar").datepicker("refresh");
	});
	$("#datepicker_calendar").mask();
}


$(document).ready(function(){    
	$(".lightbox").lightbox({
	    fitToScreen: true,
	    imageClickClose: false
    });    

	$("#datepicker_calendar").each(function() {
		$("#datepicker_calendar").datepicker({		
			onChangeMonthYear: function(a, b) {
				calendarSet(a, b);
			},
	
			beforeShowDay: function(d) {
				var tm = d.getMonth()+1;
				var td = d.getDate();
				var t = d.getFullYear() + '-' + (tm < 10 ? '0' + tm : tm) + '-' + (td < 10 ? '0' + td :td);
				if (calendarDays[t])
					return [true, 'marked'];
	
				return [true, ''];
			},
			
			onSelect: function(d) {
				category = $('#calcategoryid').val();
				page = $('#pagename').val();
				if (page != '')
					window.location.href = page + '?category=' + category + '&date=' + d;
			}
			
		});	
		var d = new Date();
		var t = $("#datepicker_calendar").datepicker("getDate");
		if (t.getFullYear() != d.getFullYear() || t.getMonth() != d.getMonth())
			$("#datepicker_calendar").datepicker("setDate", d);
		else
			calendarSet(d.getFullYear(), d.getMonth()+1);
	});
});

