$(function() {
 $(".imFancy").fancybox({'type':'image'});
});
function getCalendar(year, month) {
	date = new Date(year, month - 1, 1, 0,0,0);
	
	txt = "<table class=\"calendar\"><tr class=\"calendar-row\">";
	dayOfWeek = date.getDay() - 1;
	dayOfWeek = (dayOfWeek==-1)?6:(dayOfWeek);
	if(month==4 || month==6 || month==9 || month==11)
		lastDay = 30;
	else
		if(month!=2)
			lastDay = 31;
		else if((year%4)==0)
				lastDay = 29;
			else
				lastDay = 28;

	startDate = new Date(date.valueOf() - (dayOfWeek)*86400000);
	
	endDate = new Date(year, month - 1, lastDay);
	
	lastDayOfWeek = endDate.getDay() - 1;
	lastDayOfWeek = (lastDayOfWeek==-1)?6:(lastDayOfWeek);
	
	endDate = new Date(endDate.valueOf() + (7 - (lastDayOfWeek+1))*86400000);
	endDate.setHours(12);	
	
	dayCounter = 0;
	
	while(true) {
	
		currDate = new Date(startDate.valueOf() + dayCounter*86400000);
		
		if(currDate > (endDate)) {
		//alert(date + " " + startDate + " " + currDate + " " + endDate);
		
			txt += "</tr>";
			break;
		}
		
		if(dayCounter%7==0) {
			txt += "</tr>";
			txt += "<tr class=\"calendar-row\">";
		}
		id = ""+(currDate.getDate()<10?"0":"")+currDate.getDate()+(currDate.getMonth()<9?"0":"")+(currDate.getMonth()+1)+""+(currDate.getFullYear());
		if(currDate.getMonth() == date.getMonth())
			txt += "<td id=\"" + id + "\">"+currDate.getDate()+"</td>";
		else
			txt += "<td id=\"" + id + "\" class=\"om\">"+currDate.getDate()+"</td>";
		
		dayCounter++;
	}

	return txt+"</table>";
}

function getCalendarFromDate(year, month, day, length) {


	if(isNaN(length) || length < 30)
		length = 35;

	date = new Date(year, month - 1, day, 0,0,0);
	
	txt = "<table class=\"calendar\"><tr class=\"calendar-row\">";
	dayOfWeek = date.getDay() - 1;
	dayOfWeek = (dayOfWeek==-1)?6:(dayOfWeek);
	if(month==4 || month==6 || month==9 || month==11)
		lastDay = 30;
	else
		if(month!=2)
			lastDay = 31;
		else if((year%4)==0)
				lastDay = 29;
			else
				lastDay = 28;

	startDate = new Date(date.valueOf() - (dayOfWeek)*86400000);
	
	endDate = new Date(year, month - 1, length);
	
	lastDayOfWeek = endDate.getDay() - 1;
	lastDayOfWeek = (lastDayOfWeek==-1)?6:(lastDayOfWeek);
	
	endDate = new Date(endDate.valueOf() + (7 - (lastDayOfWeek+1))*86400000);
	endDate.setHours(12);	
	
	dayCounter = 0;
	
	while(true) {
	
		currDate = new Date(startDate.valueOf() + dayCounter*86400000);
		
		if(currDate > (endDate)) {
		//alert(date + " " + startDate + " " + currDate + " " + endDate);
		
			txt += "</tr>";
			break;
		}
		
		if(dayCounter%7==0) {
			txt += "</tr>";
			txt += "<tr class=\"calendar-row\">";
		}
		id = ""+(currDate.getDate()<10?"0":"")+currDate.getDate()+(currDate.getMonth()<9?"0":"")+(currDate.getMonth()+1)+""+(currDate.getFullYear());
		if(currDate.getMonth() == date.getMonth())
			txt += "<td id=\"" + id + "\">"+currDate.getDate()+"</td>";
		else
			txt += "<td id=\"" + id + "\" class=\"om\">"+currDate.getDate()+"</td>";
		
		dayCounter++;
	}

	return txt+"</table>";
}

posX = 0; 
posY = 0;
document.onmousemove = HandleEvent;

function HandleEvent (e) {
	if(document.layers || (!document.all && document.getElementById)) {
		posX = e.layerX;
		//posY = e.layerY-12.5;
		posY = e.layerY;
		//alert("x = " + x + " y = " + y); //alert
	}
	else {
		//posX=event.x; posY=event.y+document.body.scrollTop-25;
		posX=event.x; posY=event.y+document.body.scrollTop;
		//alert("x = " + posX + " y = " + posY); //alert
	}
	return true;
}

function showLayer(divName, absX, absY) {
	if(posX>=window.screen.availWidth-200) {
		posX = window.screen.availWidth - 200;
	}
	if(absX!=0) posX = absX;
	if(absY!=0) posY = absY;
	if(document.all) {
		m = document.all[divName];
		m.style.left = posX;
		m.style.top = posY;
		m.style.visibility = "visible";
	}
	if(!document.all && document.getElementById) {
		m = document.getElementById(divName);
		m.style.left = posX +"px";
		m.style.top = posY +"px";
		m.style.visibility="visible";
	}
}

function closeLayer(divName) {
	if(document.all)
		m = document.all[divName];
	if(!document.all && document.getElementById)
		m = document.getElementById(divName);
	
	m.style.visibility = "hidden";
}

function toggleLayer(divName) {
	if(document.all)
		m = document.all[divName];
	if(!document.all && document.getElementById)
		m = document.getElementById(divName);
		
	if(m.style.visibility == "hidden")
		m.style.visibility = "visible";
	else
		m.style.visibility = "hidden";
}

function displayLayer(divName) {
	if(document.all)
		m = document.all[divName];
	if(!document.all && document.getElementById)
		m = document.getElementById(divName);
		
	if(m.style.display == "none")
		m.style.display = "";
	else
		m.style.display = "none";
}

function getAjax()
  {
  
  try
    {
    // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
    }
  catch (e)
    {
    // Internet Explorer
    try
      {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      }
    catch (e)
      {
      try
        {
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
      catch (e)
        {
        alert("Your browser does not support AJAX!");
        return null;
        }
      }
    }
	return xmlHttp;
  }

function printArt(category, identifier) {
	window.open("?print=1&cat="+parseInt(category)+"&id="+parseInt(identifier), "printArticle", "location=0,status=0,scrollbars=0,width=600,height=700");
}

function hl(identity, mark) {
	var mk = parseInt(mark);
	var i = 0;
	if(mk > 0) {
		for(i = 1; i <= mk; i++) {
			var obj = document.getElementById("rt_"+identity+"_"+i);
			if(obj != undefined) {
				toggle(obj, 1);
			}
		}
	} else {
		for(i = 1; i <= 5; i++) {
			var obj = document.getElementById("rt_"+identity+"_"+i);
			toggle(obj, 0);
		}
	}
}

function toggle(img, mode) {
	var src = img.src;
	//src = src.substring(0, src.lastIndex
	//alert(src);
	var ext = src.substring(src.lastIndexOf("."));
	src = src.replace(ext, "");
	re1 = /_on/gi;
	re2 = /_off/gi;
	src = src.replace(re1, "");
	src = src.replace(re2, "");
	if(mode == 1)
		src += "_on";
	else if(mode == 0)
		src += "_off";
	img.src = src + ext;
}

function rate(id, val) {
	
	var frm = document.forms["rate_"+id];
	if(frm != undefined) {
		frm.rank.value = parseInt(val);
		frm.submit();
	}
}

  function _utf8_decode(utftext) {
        var string = "";

        var i = 0;
        var c = c1 = c2 = 0;

        while ( i < utftext.length ) {

            c = utftext.charCodeAt(i);

            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i+1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i+1);
                c3 = utftext.charCodeAt(i+2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }

        }

        return string;
    }

