function init_page(page) {

}

function global_login() {
	var attributes = new Array();
	attributes['user'] = document.getElementById("login_email").value;
	attributes['pass'] = document.getElementById("login_password").value;
	attributes['act'] = 1;
	ajax_send("login.php", attributes, global_loginDone);
}

function global_logout() {
	var attributes = new Array();
	attributes['act'] = 2;
	ajax_send("login.php", attributes, global_loginDone);
}

function global_loginDone(response) {
	if (response.length > 0)
		alert(response);
	else
		location.reload(true);
}

function edit_loadGeneric(type, id) {
	var attributes = new Array();
	attributes['contentID'] = id;
	attributes['contentType'] = type;
	document.getElementById("editPanel").innerHTML = "טוען מידע...";
	ajax_send("edit_generic.php", attributes, edit_loadPageDone);
	
	document.getElementById("content").scrollTop = 0;
}

function edit_loadPage(page, script) {
	var attributes = new Array();
	attributes['id'] = page;
	document.getElementById("editPanel").innerHTML = "טוען מידע...";
	ajax_send(script, attributes, edit_loadPageDone);
	
	document.getElementById("content").scrollTop = 0;
}

function edit_loadPageDone(response) {
	var editPanel = document.getElementById("editPanel");
	editPanel.innerHTML = response;

	if (document.getElementById("editContent") != null) {
		var oFCKeditor = new FCKeditor( 'editContent' ) ;
		oFCKeditor.BasePath = "/fckeditor/" ;
		oFCKeditor.ReplaceTextarea() ;
	}

	/*if (editPanel) {
		var editor = FCKEDITOR.replace('editContent', {
			toolbar : [ 
				[ 'Source', '-', 'Bold', 'Italic', 'Underline', 'Strike', 'Subscript', 'Superscript', '-','Link','Unlink','Anchor'],
				['NumberedList','BulletedList','-','Outdent','Indent','Blockquote','JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock'],
				['Styles','Format','Font','FontSize','TextColor','BGColor','Image','Flash','Table','HorizontalRule','Undo','Redo','-','Find','Replace','PasteFromWord', 'SpellChecker']
			],
			height : "500px"
		});
	}*/
}

function edit_savePage(script) {
	var attributes = new Array();
	var fields = document.getElementsByName("editFields"), i;
	
	for (i=0;i<fields.length;i++)
		attributes[fields[i].getAttribute("field")] = fields[i].value;

	if (typeof FCKeditorAPI != 'undefined')
		if (FCKeditorAPI.GetInstance('editContent') != null)
			attributes['text'] = FCKeditorAPI.GetInstance('editContent').GetData();
	
	ajax_send(script, attributes, edit_savePageDone);

	var errorsDiv = document.getElementById("editErrors");
	errorsDiv.innerHTML = "שומר...";
	errorsDiv.style.display = "block";
	errorsDiv.className = "success";
}

function edit_savePageDone(response) {
	var errorsDiv = document.getElementById("editErrors");
	var fields = document.getElementsByName("editFields"), i;
	for (i=0;i<fields.length;i++)
		fields[i].style.background = "white";
		
	errorsDiv.innerHTML = "";
	errorsDiv.style.display = "block";

	if (response.length > 0) {
		errorsDiv.className = "errors";
		var parts = response.split("|");
		var i;
		for (i=0;i<parts.length-1;i+=2) {
			document.getElementById(parts[i]).style.background = "#ff9999";
			errorsDiv.innerHTML += parts[i+1]+"<br>";
		}
	} else {
		errorsDiv.className = "success";
		errorsDiv.innerHTML = "השמירה הסתיימה בהצלחה.";
		if (document.getElementById("editNewPage"))
			document.getElementById("editNewPage").value = 0;
		if (document.getElementById("editIsNew"))
			document.getElementById("editIsNew").value = 0;
		try {
			document.getElementById("editOrigPage").value = document.getElementById("editName").value;
		} catch (e) {}
		edit_reloadFileList();
	}
}

function edit_reloadFileList() {
	ajax_send("reload_file_list.php", new Array(), edit_reloadFileListDone);
}

function edit_reloadFileListDone(response) {
	document.getElementById("fileListPanelHome").innerHTML = response;
}

function edit_deletePage(type) {
	if (confirm("מחיקת העמוד היא פעולה סופית שאי אפשר לשחזר\nהאם למחוק את העמוד?")) {
		var attributes = new Array();
		var fields = document.getElementsByName("editFields"), i;
		
		for (i=0;i<fields.length;i++)
			attributes[fields[i].getAttribute("field")] = fields[i].value;
		attributes['type'] = type;
		ajax_send("delete_item.php", attributes, edit_deletePageDone);
		document.getElementById("editPanel").innerHTML = "העמוד נמחק.";
	}
}

function edit_deletePageDone(response) {
	edit_reloadFileList();
}

function edit_toggleCategory(cat) {
	var parent = cat.parentNode.parentNode;
	var children = parent.childNodes;
	var i, child;
	
	for (i=0;i<children.length;i++) {
		child = children[i];
		if (child == cat.parentNode) {
			break;
		}
		
		if (child.getAttribute("type") != "parent")
			child.style.display = "none";
	}
	for (i=i+1;i<children.length;i++) {
		child = children[i];
		if (child.getAttribute("type") == "parent")
			break;
		
		if (child.style.display == "")
			child.style.display = "none";
		else
			child.style.display = "";
	}
	for (i=i+1;i<children.length;i++) {
		child = children[i];
		if (child.getAttribute("type") != "parent")
			child.style.display = "none";
	}
}

function contact_validate() {
	var items = new Array('name', 'email', 'phone', 'subject', 'text');
	var obj, errors=0;
	
	for (var item in items) {
		obj = document.getElementById("contact_" + items[item]);
		if (obj == null)
			continue;
		if (obj.getAttribute("required") == 1) {
			if (obj.value.length == 0) {
				obj.className = "error";
				errors++;
			} else {
				obj.className = "";
			}
		}
	}
	
	return (errors==0);
}

var fadeOutObj = null;
var fadeInObj = null;
var fading = false;
var fadeSpeed = 25;

function fadeSwitch() {
	if (!fadeInObj)
		return;
	if (fading)
		return;
	

	fading = true;
	
	
	if (fadeInObj.style.MozOpacity != undefined) { //Firefox
		if (fadeOutObj)
			fadeOutObj.style.MozOpacity = 1;
		fadeInObj.style.MozOpacity = 0;
	} else if (fadeInObj.filters != undefined) { //IE
		if (fadeOutObj)
			fadeOutObj.style.filter = "alpha(opacity=100)";
		fadeInObj.style.filter = "alpha(opacity=0)";
	} else { //Doesn't support fade effects
		if (fadeOutObj)
			fadeOutObj.style.display = 'none';
		fadeInObj.style.display = '';
		fading = false;
		return;
	}
	fadeInObj.style.display = '';
	
	if (fadeOutObj)
		fadeOutObj.setAttribute("fade", 100);
	fadeInObj.setAttribute("fade", 0);
	
	setTimeout("fadeStep()", fadeSpeed);
}

function fadeStep() {
	var fadeOutVal
	if (fadeOutObj)
		fadeOutVal = Number(fadeOutObj.getAttribute("fade"));
	else
		fadeOutVal = 0;
		
	var fadeInVal = Number(fadeInObj.getAttribute("fade"));
	
	fadeOutVal -= 10;
	fadeInVal += 10;
	
	if (fadeInObj.style.MozOpacity != undefined) { //Firefox
		if (fadeOutObj)
			fadeOutObj.style.MozOpacity = fadeOutVal / 100;
		fadeInObj.style.MozOpacity = fadeInVal / 100;
	} else if (fadeInObj.filters != undefined) { //IE
		if (fadeOutObj)
			fadeOutObj.style.filter = "alpha(opacity=" + fadeOutVal + ")";
		fadeInObj.style.filter = "alpha(opacity=" + fadeInVal + ")";
	}
	
	if (fadeOutObj)
		fadeOutObj.setAttribute("fade", fadeOutVal);
	fadeInObj.setAttribute("fade", fadeInVal);
	
	if (fadeOutVal > 0 || fadeInVal < 100)
		setTimeout("fadeStep()", fadeSpeed);
	else {
		if (fadeOutObj)
			fadeOutObj.style.display = 'none';
		fadeOutObj = null;
		fadeInObj = null;
		fading = false;
	}
}

var isThumbnailsMoving = false;
function thumbnails_move(delta) {
	var scroller = document.getElementById("thumbnails_inner");
	var to = Number(scroller.getAttribute("movex"));
	var width = Number(scroller.style.width.substr(0, scroller.style.width.length-2));
	
	to += delta;
	
	if (to < (0-width) + 42)
		to = (0-width) + 42;
	if (to > 0)
		to = 0;

	scroller.setAttribute("movex", to);
	
	if (!isThumbnailsMoving) {
		thumbnails_start();
	}
}

var mousedown=0;

function thumbnails_next() {
	thumbnails_move(-100);
}

function thumbnails_back() {
	thumbnails_move(100);
}

function thumbnails_start() {
	isThumbnailsMoving = true;
	thumbnails_tick();
}

function thumbnails_tick() {
	var scroller = document.getElementById("thumbnails_inner");
	var to = Number(scroller.getAttribute("movex"));
	var right = Number(scroller.style.right.substr(0, scroller.style.right.length-2));
	var mov, destination;

        if (mousedown != 0) {
            to += 10 * mousedown;
            scroller.setAttribute("movex", to);
        } else {
            return;
        }

	mov = (to - right) / 3;
	
	if (Math.abs(mov) < 0.1) {
                scroller.style.right = to + "px";
		isThumbnailsMoving = false;
		return;
	}
	destination = right + mov;

	scroller.style.right = destination + "px";
	
	setTimeout("thumbnails_tick()", 50);
}

function thumbnails_moveInstantly(position) {
    var scroller = document.getElementById("thumbnails_inner");
    scroller.style.right = position + "px";
    scroller.setAttribute("movex", position)
}