﻿var tipsBox, BOX_HEIGHT = 280;
var interval, count = 1;
var curTipPos, tipsCount = 0;

//פונקציה זו מאתחלת את מערכת הטיפים ומציגה את התיבה
function initTips(tipCount, lastTip, registered, combos) {
	//אתחל משתנים
	tipsCount = tipCount;
	curTipPos = lastTip;
	tipsBox = document.getElementById("tipsBox").style;

	//הפעל אנימציה
	interval = setInterval("showTipBox('"+registered+"')", 30);
}

//פונקציה זו מופעלת כאשר יש צורך להציג את חלון הטיפים
function showTipBox(registered) {
	if (tipsBox.height != BOX_HEIGHT + "px") {
		tipsBox.height = (20 * count++) + "px";
	}
	else {
		document.getElementById("closeButton").style.visibility = "visible";

		if (getCookie("welcomeMessage") != "seen") document.getElementById("welcomeMessage").style.visibility = "visible";

		document.getElementById("tipsTitle").style.visibility = "visible";
		document.getElementById("tipsContent").style.visibility = "visible";
		document.getElementById("tipsControls").style.visibility = "visible";

		showTip("next", registered);
		clearInterval(interval);
	}
}
//פונקציה זו מופעלת כאשר יש צורך לסגור את חלון הטיפים
function hideTipBox() {
	//הפסק את האנימציה
	clearInterval(interval);

	//שמור הגדרות בעוגיה
	setCookie();

	//סגור את החלון
	tipsBox.display = "none";
}

//פונקציה זו משמשת לניווט בין טיפים
function showTip(action, registered) {
	var xmlHttp = getXmlHttpObject();

	//התאם את מיקום הטיפ הנוכחי למצבי קיצון
	if (action == "next" && curTipPos >= tipsCount)
		curTipPos = 0;
	else if (action == "prev" && curTipPos <= 1)
		curTipPos = tipsCount+1;

	//טען את הטיפ
	xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState == 4) {
			var i = xmlHttp.responseText.indexOf("|")   //מפריד בין מיקום הטיפ לתוכנו

			//עידכון מס' הטיפ הנוכחי
			curTipPos = parseInt(xmlHttp.responseText.substring(0, i));

			//הצג את התוכן
			document.getElementById("tipsContent").innerHTML = xmlHttp.responseText.substring(i+1);

			//הצג את מיקום הטיפ הנוכחי
			document.getElementById("tipsCounter").innerHTML = "(" + tipsCount + " / " + curTipPos + ")";

			//שמור הגדרות בעוגיה
			setCookie();
		}
	};
	xmlHttp.open("GET", "showTips.asp?action=" + action + "&registered=" + registered + "&curTipPos=" + curTipPos, true);
	xmlHttp.setRequestHeader("Content-Type", "text/html; charset=UTF-8");
	xmlHttp.setRequestHeader("Content-Language", "he");
	xmlHttp.send(null);
}

//פונקציה שמציגה את הטיפים אחרי הודעת הפתיחה
function showTipsLayer() {
	document.getElementById("welcomeMessage").style.display = "none";
	document.getElementById("tipsLayer").style.display = "block";
}

//שומרת את העוגיה של הטיפים
function setCookie() {
	var d = new Date();
	var xmlHttp = getXmlHttpObject();

	if (document.getElementById("tipsLayer").style.display == "none") curTipPos = 0;

	xmlHttp.open("GET", "/includes/tipBoxSaveCookie.asp?showTips=" + document.getElementById("chkShowTips").checked + "&lastTip=" + curTipPos + "&lastShow=" + d.getDate() + "/" + (d.getMonth()+1) + "/" + d.getFullYear(), false);
	xmlHttp.send(null);
}