function ToggleLoginForm(id) {
	var elem;
	switch (id) {
		case 1: 
			elem = document.getElementById("userLogin");
			if(elem.value == "Email")	elem.value = "";
		break;
		case 2: 
			elem = document.getElementById("userPassword");
			if(elem.value == "Password")	elem.value = "";
		break;
	}
}


function checkCat(id, name) {
	var cont = document.getElementById("choosenCats");
	if(document.getElementById("cc_"+id).checked == 1) {
		document.getElementById("cc_"+id).checked = 0;
		if(cont) {
			if(cont.innerHTML.search(", "+ name) != -1) {
				cont.innerHTML = cont.innerHTML.replace(", "+ name,"");	
			} else if (cont.innerHTML.search(name+", ") != -1) {
				cont.innerHTML = cont.innerHTML.replace(name+", ","");	
			}	else {
				cont.innerHTML = cont.innerHTML.replace(name,"");	
			}
		}
		document.getElementById("label_"+id).style.color = "#fff";
		document.getElementById("label_"+id).style.backgroundPosition = "0 1px";
	} else {
		document.getElementById("cc_"+id).checked = 1;
		document.getElementById("label_"+id).style.color = "#E8F7DD";
		document.getElementById("label_"+id).style.backgroundPosition = "0 -12px";
		
		if(cont) {
	 		if(cont.innerHTML != "") {
				cont.innerHTML += ", "+name;
			} else {
			 cont.innerHTML = name;
			}
		}
	}
	return true;
}

function toggleMenu(id) {
	if (navigator.appName == "Microsoft Internet Explorer") { 
		Effect.toggle('menu_'+id, 'appear'); 
	}	else { 
		Effect.toggle('menu_'+id, 'slide'); 
	}	
}

function getCurPosition(e) {
    var e = e || window.event;
    if(e == undefined) {
    	return {x:0, y:0};
    }
    var cursor = {x:0, y:0};
    if (e.pageX || e.pageY) {
        cursor.x = e.pageX;
        cursor.y = e.pageY;
    } 
    else {
        var de = document.documentElement;
        var b = document.body;
        cursor.x = e.clientX + 
            (de.scrollLeft || b.scrollLeft) - (de.clientLeft || 0);
        cursor.y = e.clientY + 
            (de.scrollTop || b.scrollTop) - (de.clientTop || 0);
    }
    return cursor;
}

function showBox(content, w, h, hDiff) {
	var content = content || '';
	var width = w || 300;
	var height = h || 200;
	var heightDiff = hDiff || 0;
	var curs = getCurPosition();
	var pr = document.getElementById("editor");
	if(pr == undefined) {
		pr = document.createElement('div');  
		pr.style.display = "none"; 
		pr.id = 'editor';   
		document.body.appendChild(pr);
	}
	if(curs.y == 0 && curs.x == 0) {
		//FFshit
		pr.style.top = document.documentElement.scrollTop + (screen.height / 2 - h) + "px";
		pr.style.left = (screen.width / 2) - (w / 2) + "px";
	} else {
		pr.style.top = curs.y + 25 + heightDiff+"px";
		pr.style.left = curs.x + 20 + "px";	
	}
	pr.style.width = width + "px";
	pr.style.height = height + "px";
		
	Effect.toggle('editor', 'appear'); 
	new Draggable('editor', { starteffect: 'null' });
	//pr.innerHTML = '<a href="javascript:void(0);" onclick="closeBox();"><img src="/images/win_close.gif" width="14" height="14" alt="Закрыть окно" style="float: right; margin-top: 3px; margin-right: 3px;" border="0"></a>'+content;
	pr.innerHTML = '<input type="button" style="width: 14px; height:14px; background: url(/images/win_close.gif) no-repeat; float: right; margin-top: 3px; margin-right: 3px; border: 0; cursor: hand;" title="Закрыть окно" onclick="closeBox();">'+content;
	content = '';
	return false;
}

function closeBox(){
	Effect.toggle('editor', 'appear'); 
}

function clearTip(elem, val) {
	if(!elem.value) return '';
	if(!val) return '';
	if(elem.value == val) elem.value = '';	
	return true;
}

function Copy(textToCopy) {
	var pr = document.getElementById("tmpCopyArea");
	if(pr == undefined) {
		pr = document.createElement('textarea');   
		pr.id = 'tmpCopyArea';   
		pr.style.display = "none";
		document.body.appendChild(pr);
	}
	pr.value = textToCopy;
	var Copied = pr.createTextRange();
	Copied.execCommand("RemoveFormat");
	Copied.execCommand("Copy");
	if(document.getElementById("CopyImageHolder")) {
		document.getElementById("CopyImageHolder").innerHTML = "";
	}
}

function FilterSort(id) {
		//sortABC, #sortDate, #sortChart, #sortRate
		document.getElementById("sortABC").style.backgroundPosition = "0 0";
		document.getElementById("sortDate").style.backgroundPosition = "0 0";
		document.getElementById("sortChart").style.backgroundPosition = "0 0";
		document.getElementById("sortRate").style.backgroundPosition = "0 0";
		document.getElementById("filterSrt").value = id;
		switch(id) {
			case 1: document.getElementById("sortABC").style.backgroundPosition = "0 -20px"; break;
			case 2: document.getElementById("sortDate").style.backgroundPosition = "0 -20px"; break;
			case 3: document.getElementById("sortChart").style.backgroundPosition = "0 -20px"; break;
			case 4: document.getElementById("sortRate").style.backgroundPosition = "0 -20px"; break;
		}
}

function ToggleElement(elementID, displayStyle) {
	var dispStyle = displayStyle || "block";
	var elem = document.getElementById(elementID);
	
	if(elem.style.display == "none") {
		elem.style.display = dispStyle;
	} else {
		elem.style.display = "none";
	}
	
}

function submitMovieRating(){
	
	var fid = document.getElementById("mbMovieRate");
	var res = document.getElementById('mbVoteCont');
	var req = new JsHttpRequest();
  req.onreadystatechange = function() {
    if (req.readyState == 4) {
    	 res.innerHTML = req.responseText;
    	 if(req.responseJS.rate != "") {
    	 	document.getElementById("average_rating").innerHTML = req.responseJS.rate;
    	 	document.getElementById("total_votes").innerHTML = req.responseJS.votes;
    	 }
    }
  }
  req.open("POST", '/js/submitMovieRate.php', true);
  req.send( { 'f': fid } );
  res.innerHTML = '<img width="32" height="32" src="/images/loaders/bo2_wheel_32.gif" aling="middle">';
}

function GetElem(elem) {
	return document.getElementById(elem);
}