var CustomCheckbox = function(params){
	 var that = {};

	 var checkboxClick = function() {
		var checkedClass = "";
		var re = new RegExp("(^|\\s)checkbox_checked(\\s|$)");

		if(re.test(that.trigger.className)){
			var className = that.trigger.className.replace(re,'');
			that.trigger.className = className;
			that.checkbox.value = "";
		} else {
			that.trigger.className += " checkbox_checked";
			that.checkbox.value = that.value;
		}
		return false;
	}

	function checkboxKeyPress(event){
		var kC  = (window.event) ? window.event.keyCode : event.keyCode;
		if(kC == 32 || kC == 13){
			checkboxClick();
			return false;
		}
	}

	if (!params)
		return;
	if (!params.trigger || !document.getElementById(params.trigger))
		return;
	if (!params.checkbox || !document.getElementById(params.checkbox))
		return;

	that.trigger = document.getElementById(params.trigger);
	that.checkbox = document.getElementById(params.checkbox);
	that.value = params.value || '1';

	that.trigger.onkeydown = checkboxKeyPress;
	that.trigger.onclick = checkboxClick;
}

function rating (mark,act) {

	if (act == 1) {
		var rf = jQuery('#ratingform')[0];
		rf.action = rf.action+'?'+Math.random();
		rf.reiting.value = mark;
		rf.submit();
	} else if (act == 2) {
		var rv = jQuery('#ratingview')[0];
		var rs = jQuery('#ratingset')[0];
		rv.innerHTML = '<span class="label">Рейтинг:</span> <b>'+mark+'</b>';
		rs.style.display = 'none';
		rv.style.display = 'block';
	} else {
		var d = jQuery('#ratingset .rating_line')[0];
		d.style.width = (mark * 16)+'px';
	}

 return false;
}


jQuery.fn.accordion = function(options) {
	var list = jQuery(this);
	return this.each(function() {
		jQuery(this).find('dd').hide();
		jQuery(this).find('dt a').click(function() {

			if (this.href.indexOf('#') == -1)
				return;
			var current = jQuery(this.parentNode.parentNode).find('dd:visible');
			var next = jQuery(this.parentNode).find('+dd');
			if(next[0] != current[0]) {
				current.slideUp();
				current.prev('dt').removeClass('selected');
			} else {
				return false;
			}


			if (next.is(':visible')) {
				next.slideUp();
			} else {
				next.slideDown();
			}

			jQuery(this.parentNode).addClass('selected');

			return false;
		});


		var subItemSelected = jQuery(this).find("dd > ul > li.selected");
		var titleIemSelected = jQuery(this).find('dt.selected');
		if(subItemSelected[0]) {
			subItemSelected.parents('dd').show();
			subItemSelected.parents('dd').prev('dt').addClass('selected');
		}
		if (titleIemSelected[0])
		{
			titleIemSelected.next('dd').show();
		}
	});
};
function gebi (elem)
{
 return document.getElementById(elem);
}



function customInputFile(file, file_wrapper){
	var that = this;

	var inputFileChange = function(){
		file = this.value;
		reWin = /.*\\(.*)/;
		var fileTitle = file.replace(reWin, "$1");
		reUnix = /.*\/(.*)/;
		fileTitle = fileTitle.replace(reUnix, "$1");

		var RegExExt =/.*\.(.*)/;
		var ext = fileTitle.replace(RegExExt, "$1");

		var pos;

		jQuery(that.file_wrapper).find('input').eq('1').val(fileTitle);

	}

	var inputFileMouseMove = function(e){
		if (typeof(e) == 'undefined') e = window.event;
		if (typeof e.pageY == 'undefined' &&  typeof e.clientX == 'number' && document.documentElement)
		{
			e.pageX = e.clientX + document.documentElement.scrollLeft;
			e.pageY = e.clientY + document.documentElement.scrollTop;
		};

		var ox = oy = 0;
		var elem = that.file_wrapper;

		if (elem.offsetParent)
		{
			ox = elem.offsetLeft;
			oy = elem.offsetTop;
			while (elem = elem.offsetParent)
			{
				ox += elem.offsetLeft;
				oy += elem.offsetTop;
			};
		};
		var x = e.pageX - ox;
		var y = e.pageY - oy;
		var w = that.file.offsetWidth;
		var h = that.file.offsetHeight;

		that.file.style.top	= y - (h / 2) + 'px';
		that.file.style.left = x - (w - 30) + 'px';
	}

	if (!file || !gebi(file))
		return;


	that.file = gebi(file);
	that.file_wrapper = jQuery(that.file).parents('.file_wrapper')[0];

	that.file_wrapper.onmousemove = inputFileMouseMove;
	that.file.onchange = inputFileChange;
}
function pageX(elem) {
        return elem.offsetParent ?
               elem.offsetLeft + pageX( elem.offsetParent ) :
               elem.offsetLeft;
}
// Определение координаты элемента по вертикали
function pageY(elem) {
        return elem.offsetParent ?
               elem.offsetTop + pageY( elem.offsetParent ) :
               elem.offsetTop;
}
function getBodyScrollTop()
{
  return self.pageYOffset || (document.documentElement && document.documentElement.scrollTop) || (document.body && document.body.scrollTop);
}

function getBodyScrollLeft()
{
  return self.pageXOffset || (document.documentElement && document.documentElement.scrollLeft) || (document.body && document.body.scrollLeft);
}
function artifactAlt(obj, evnt, show) {
	if (!obj) return;
	var div = gebi(obj.getAttribute('div_id'));
	if (!div) return;
	var act1 = obj.getAttribute('act1');
	var act2 = obj.getAttribute('act2');
	if (show == 2) {
		document.onmousemove=function(e) {artifactAlt(obj, e||event, 1)} 
		div.style.display = 'block';
		if (act1 || act2) {
			if (obj.tagName == 'IMAGE') {
				obj.src = ("images/itemact-"+ act1) + (act2 +".gif")
			} else {
				obj.style.backgroundImage = 'url(' + ("images/itemact-"+ act1) + (act2 +".gif") + ')'
			}
		}
	}
	if (!show) {
		if (act1 || act2) {
			if (obj.tagName == 'IMAGE') {
				obj.src = "images/d.gif"
			} else {
				obj.style.backgroundImage = 'url(' + "images/d.gif" + ')'
			}
		}
		div.style.display = 'none';
		document.onmousemove=function(){}
		return;
	}
	var ua = navigator.userAgent.toLowerCase();
	// Определим Internet Explorer
	isIE = (ua.indexOf("msie") != -1 && ua.indexOf("opera") == -1);
	// Opera
	isOpera = (ua.indexOf("opera") != -1);
	// Gecko = Mozilla + Firefox + Netscape
	isGecko = (ua.indexOf("gecko") != -1);
	// Safari, используется в MAC OS
	isSafari = (ua.indexOf("safari") != -1);
	
	if(isIE){
		var exs=event.x;
		var eys=event.y;
	}else{
		var ex = evnt.clientX - pageX(obj);
		var ey = evnt.clientY - pageY(obj);
		
		var exs = ex + self.pageXOffset;
		var eys = ey + self.pageYOffset ;
	}
	if (act1 || act2) {
		obj.style.cursor = 'default'
		obj.onclick = function(){showArtifactInfo(obj.getAttribute('aid'))}
		var coord = getCoords(obj)
		var cont = gebi("item_list")
		var rel_x = (ex + cont.scrollLeft - coord.l)
		if (rel_x >= 40) {
			var rel_y = (ey + cont.scrollTop - coord.t)
			if (act1 != 0 && rel_y < 20) {
				obj.onclick = function(){try{artifactAct(obj, act1)}catch(e){}}
				try{obj.style.cursor = 'hand'} catch(e){}
				try{obj.style.cursor = 'pointer'} catch(e){}
			}
			if (act2 != 0 && rel_y >= 40) {
				obj.onclick = function(){try{artifactAct(obj, act2)}catch(e){}}
				try{obj.style.cursor = 'hand'} catch(e){}
				try{obj.style.cursor = 'pointer'} catch(e){}
			}
		}
	}
	//alert(evnt.clientX+":"+div.offsetWidth+":"+(document.body.clientWidth - 7));
	if (evnt.clientX+div.offsetWidth> document.body.clientWidth - 7){
		var x=exs - div.offsetWidth - 10;
	}else{
		var x=exs + 10;
	}
	//var x = exs + div.offsetWidth > document.body.clientWidth - 7 ?  : exs + 10;
    if (evnt.clientY + div.offsetHeight > document.body.clientHeight - 7)
    	var y =  eys - div.offsetHeight - 10;
    else
    	var y=eys + 10;
    

    /*if (x < 0 ) {
     x = evnt.clientX - pageX(obj) - div.offsetWidth/2
     alert("true");
    }
    if (x < 7 ) {
      x = 7
    }*/
    if (x > document.body.clientWidth - div.offsetWidth - 7) {
      x= document.body.clientWidth - div.offsetWidth - 7
    }

	
	div.style.left = x + 'px';
	div.style.top = y + 'px';
}
