/*
	project:	SeoPuzzle.ru
	Date:		22.04.2010
	Company:	LLC SeoPuzzle
				Ivanov Mikhail
*/

window.cur_showed = new Array();
window.in_showing = false;

function show(id, block, element) {
	if(window.in_showing) return false;
	var cur_id = window.cur_showed[block] ? window.cur_showed[block] : '';
	
	if(cur_id != id) {
		window.in_showing = true;
		$("#"+id).fadeIn(500, function() {
			window.in_showing = false;
		});
		
		if(cur_id && cur_id.length > 0) {
			$('#'+cur_id).hide();
			$("#"+cur_id+"_mute").css("display", ""); /* show mute */
		}
		
		$("#"+id+"_mute").hide(); /* hide mute */
		window.cur_showed[block] = id;
	} else { /* repeated click */
		if(element && $(element).attr("href") != "#show") return true;
		
		if(cur_id && cur_id.length > 0) {
			hide(cur_id, block);
		}
	}
	return false;
}

function hide(id, block) {
	if(window.in_showing) return false;
	
	window.in_showing = true;
	$('#'+id).fadeOut(500, function() {
		window.in_showing = false;
	});
	
	$("#"+id+"_mute").css("display", ""); /* show mute */
	window.cur_showed[block] = '';
	
	return false;
}


var marqueeCurEl = new Array();
var in_scroll = false;
var pre_int = null;

function chooseShow(id, a) {
	var re = /px/g;
	
	if(in_scroll) chooseHide(id);
	
	if(!in_scroll) {
		$("#"+id).show();
		
		var div_left = parseInt($(a).parent().css("left").replace(re, ""));
		$("#"+id).css("left", (47 + div_left) + "px");
	}
}

function chooseHide(id) {
	$("#"+id).hide();
}



/* 
  Scroll
*/

function scrollPrev(pId, pCount, pScrollCount, pSpeed, pStep) {
	if(!marqueeCurEl[pId]) { marqueeCurEl[pId] = 0; }
	if(!pScrollCount) pScrollCount = 1;
	if(!pSpeed) pSpeed = 20;
	if(!pStep) pStep = 26;
	
	if(in_scroll) { return false; }

	if(marqueeCurEl[pId] < pScrollCount) { pScrollCount = marqueeCurEl[pId]; }

	if(pScrollCount > 0) {
		in_scroll = true;
		blockScroll(pId, "right", pScrollCount, pSpeed, pStep);
		marqueeCurEl[pId] -= pScrollCount;
	}
	return false;
}


function scrollNext(pId, pCount, pVisibleCount, pScrollCount, pSpeed, pStep) {
	if(!marqueeCurEl[pId]) { marqueeCurEl[pId] = 0; }
	if(!pScrollCount) pScrollCount = 1;
	if(!pSpeed) pSpeed = 20;
	if(!pStep) pStep = 26;
	
	if(in_scroll) { return false; }

	if(pCount - (marqueeCurEl[pId] + pVisibleCount) < pScrollCount) { pScrollCount = pCount - (marqueeCurEl[pId] + pVisibleCount); }

	if(pScrollCount > 0) {
		in_scroll = true;
		blockScroll(pId, "left", pScrollCount, pSpeed, pStep);
		marqueeCurEl[pId] += pScrollCount;
	}
	return false;
}


function blockScroll(id, dir, scrollcount, speed, step) {
	var element = document.getElementById(id);
	var children = element.children;

	var childrenArray = new Array();
	for (var i = 0, l = children.length; i < l; i++) {
		if (children[i].nodeType != 3) {
			childrenArray.push(children[i]);
		}
	}
	
	var moveText = function() {
		var steps       = step,
			scrollcounts = scrollcount,
			scrolling    = childrenArray,
			current      = marqueeCurEl[id],
			direction    = dir;
		
		
		return function() {
			var re = /px/g;
			
			var leftNext = 0;
			var leftPrev = 0;
			if(scrolling[current+scrollcounts]) {
				leftNext = scrolling[current+scrollcounts].style.left;
				leftNext = parseInt(leftNext.replace(re, ""));
			}
			if(scrolling[current-scrollcounts]) {
				leftPrev = scrolling[current-scrollcounts].style.left;
				leftPrev = parseInt(leftPrev.replace(re, ""));
			}
			
			if((direction == "left"  && leftNext <= 0) || 
			   (direction == "right" && leftPrev >= 0))
			{
				pre_int = clearInterval(pre_int);
				in_scroll = false;
			}
			else
			{
				if(direction == "right") {
					leftFirst = scrolling[current-scrollcounts].style.left;
					leftFirst = parseInt(leftFirst.replace(re, ""));
					if(Math.abs(leftFirst) < steps) { lstep = Math.abs(leftFirst); } else { lstep = steps; }

					for(var i = 0; i < scrolling.length; i++) {
						var left = scrolling[i].style.left;
						left = parseInt(left.replace(re, ""));
						scrolling[i].style.left = (left + lstep) + "px";
					}
				} else if(direction == "left") {
					leftFirst = scrolling[current+scrollcounts].style.left;
					leftFirst = parseInt(leftFirst.replace(re, ""));
					if(Math.abs(leftFirst) < steps) { lstep = Math.abs(leftFirst); } else { lstep = steps; }

					for(i = 0; i < scrolling.length; i++) {
						left = scrolling[i].style.left;
						left = parseInt(left.replace(re, ""));
						scrolling[i].style.left = (left - lstep) + "px";
					}
				}
			}
		}
	}();
	
	pre_int = setInterval(moveText, speed);
}


$(document).ready(function() {
	$("#puzzle_main, #content .puzzle").dblclick(function() {
		$("<div></div>")
			.attr("id", "admin_form")
			.html(
				'<a href="#" onclick="$(\'#admin_form\').remove(); return false;">X</a>' +
				'<form mode="post" action="/users/login_do/">' +
					'<input type="text" name="login" />' +
					'<input type="password" name="password" />' +
					'<input type="hidden" value="' + window.location + '" name="from_page" />' +
					'<input type="submit" class="smt" value="Админить!" />' +
				'</form>' +
				'<form mode="post" action="/users/logout/">' +
					'<input type="hidden" value="' + window.location + '" name="from_page" />' +
					'<input type="submit" class="out" value="Забить!" style="float: left;" />' +
				'</form>' +
				'<span class="login">Логин</span>' +
				'<span class="pass">Пароль</span>'
			)
			.appendTo($("#content"));
		$("#admin_form input:first").focus();
		return false;
	});
});

