// Start of with completed being hidden;
var boolHiddenArray = new Array();
boolHiddenArray["B"] = true;
boolHiddenArray["E"] = true;
boolHiddenArray["M"] = true;
boolHiddenArray["H"] = true;
boolHiddenArray["X"] = true;

function updateComplete(group, task) {
	// Fetch the current completed tasks
	var diary_cookie=getCookie(diary_name);
	if (diary_cookie != null && diary_cookie != "") {
		var cookie_array = diary_cookie.split(";");
	} else {
		var cookie_array = new Array();
	}
	// If we aren't hiding the completed tasks for the selected group, then exit;
	var chkTask = document.getElementById('chk' + group + task);
	// Make sure the checkbox isn't null before working with it;
	if (chkTask != null) {
		// We only care if it is checked;
		if (chkTask.checked) {
			var reqTask = document.getElementById('taskReq' + group + task);
			var ansTask = document.getElementById('taskAns' + group + task);
			// Make sure the corresponding rows aren't null before we try to hide them;
			if (reqTask != null) reqTask.style.display = "none";
			if (ansTask != null) ansTask.style.display = "none";
			// Add task to cookie
			cookie_array.push(group + "-" + task);
		} else {
			for (var i = 0; i < cookie_array.length; i++) {
				gt = cookie_array[i].split("-");
				if (gt[0] == group && gt[1] == task) {
					// Remove task from cookie
					cookie_array.splice(i,1);
				}
			}
		}
		// Save cookie
		var new_diary_cookie = cookie_array.join(";");
		setCookie(diary_name, new_diary_cookie, 365);
	}
}

function setCheckmarks() {
	// Fetch the current completed tasks
	var diary_cookie=getCookie(diary_name);
	if (diary_cookie != null && diary_cookie != "") {
		var cookie_array = diary_cookie.split(";");
	} else {
		var cookie_array = new Array();
	}
	// Mark checkboxes and hide row
	for (var i = 0; i < cookie_array.length; i++) {
		gt = cookie_array[i].split("-");
		var chkTask = document.getElementById('chk' + gt[0] + gt[1]);
		if (chkTask != null) {
			chkTask.checked = true;
			var reqTask = document.getElementById('taskReq' + gt[0] + gt[1]);
			var ansTask = document.getElementById('taskAns' + gt[0] + gt[1]);
			// Make sure the corresponding rows aren't null before we try to hide them;
			if (reqTask != null) reqTask.style.display = "none";
			if (ansTask != null) ansTask.style.display = "none";
		}
	}
}

function hideTasks(group, hide) {
	boolHiddenArray[group] = hide;
	var frmTask = document.getElementById('tasks' + group);
	// Make sure the form for the selected group of tasks isn't null before working with it;
	if (frmTask != null) {
		// Loop through all the elements in the form;
		for (var i = 0; i < frmTask.elements.length; i++) {
			// Make sure we are only dealing with checkboxes;
			if (frmTask.elements[i].type != "checkbox") continue;
			// Try to get the task index value from the name of the element;
			task = frmTask.elements[i].name.replace(/[^0-9]/g, '');
			// Make sure what we are working with is a number, and is greater than zero (all indexes for tasks start at 1);
			if (!isNaN(task) && task > 0) {
				var chkTask = document.getElementById('chk' + group + task);
				var reqTask = document.getElementById('taskReq' + group + task);
				var ansTask = document.getElementById('taskAns' + group + task);
				// Make sure all html elements we want to work with aren't null before changing them;
				if (chkTask != null) {
					// If we are hiding the selected group, then we have to see if the checkbox is checked;
					// else we can just display it (we don't care if checkbox is checked);
					if (boolHiddenArray[group]) {
						if (chkTask.checked) {
							if (reqTask != null) reqTask.style.display = "none";
							if (ansTask != null) ansTask.style.display = "none";
						}
					} else {
						if (reqTask != null) reqTask.style.display = "table-row";
						if (ansTask != null) ansTask.style.display = "table-row";
					}
				}
			}
		}
	}
}
	
function getCookie(c_name) {
	if (document.cookie.length>0) {
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1) {
		    c_start=c_start + c_name.length+1;
		    c_end=document.cookie.indexOf(";",c_start);
		    if (c_end==-1) c_end=document.cookie.length;
		    return unescape(document.cookie.substring(c_start,c_end));
		}
	}
	return "";
}

function setCookie(c_name,value,expiredays) {
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toUTCString());
}

// 276887-upload-diaryjs

$(document).ready(function()
{
    $('.map area').each(function()
    {
$(this).qtip(
   {
      content: {
         title: {
            text: tipDataHeader($(this).attr('alt')),
         },
         text: tipDataBody($(this).attr('alt'))
         },
      position: {
         target: $(document.body),
         corner: 'center'
      },
      show: {
         when: 'click',
         solo: true
      },
      hide: 'unfocus',
      style: {
        title: { 
            'font-family': 'arial',
             'font-size': 14
        },
         width: { 
            max: popupWidth($(this).attr('alt')) 
            },
         padding: '14px',
         border: {
            width: 9,
            radius: 9,
            color: '#666'
         },
         name: 'light'
      },
      api: {
         beforeShow: function()
         {
            $('#qtip-blanket').fadeIn(this.options.show.effect.length);
         },
         beforeHide: function()
         {
            $('#qtip-blanket').fadeOut(this.options.hide.effect.length);
         }
      }
    });
});
   $('<div id="qtip-blanket">')
      .css({
         position: 'absolute',
         top: $(document).scrollTop(),
         left: 0,
         height: $(document).height(),
         width: '100%',
         opacity: 0.5,
         backgroundColor: 'black',
         zIndex: 5999
      })
      .appendTo(document.body)
      .hide();
});
function tipDataHeader(id) {
        return diaryInfo[id][0];
}
function popupWidth(id) {
    if (id == "beginner" || id == "easy" || id == "medium" || id == "hard" || id == "elite") {
        return "525";
        }
    else { 
        return "400"; 
        }
}
function rewardStatInfo(id) {
    return '<table class="item" border="1" cellspacing="0" cellpadding="4"><tr align="center"><td colspan="5">Attack</td><td colspan="6">Defence</td><td colspan="3">Other</td></tr><tr><td>Stab</span></td><td>Slash</span></td><td>Crush</span></td><td>Magic</span></td><td>Range</span></td><td>Stab</span></td><td>Slash</span></td><td>Crush</span></td><td>Magic</span></td><td>Range</span></td><td>Summ</span></td><td>Strength</span></td><td>Prayer</span></td><td>Ranged Str</span></td></tr><tr><td>' + rewardStats[id][0] + '</td><td>' + rewardStats[id][1] + '</td><td>' + rewardStats[id][2] + '</td><td>' + rewardStats[id][3] + '</td><td>' + rewardStats[id][4] + '</td><td>' + rewardStats[id][5] + '</td><td>' + rewardStats[id][6] + '</td><td>' + rewardStats[id][7] + '</td><td>' + rewardStats[id][8] + '</td><td>' + rewardStats[id][9] + '</td><td>' + rewardStats[id][10] + '</td><td>' + rewardStats[id][11] + '</td><td>' + rewardStats[id][12] + '</td><td>' + rewardStats[id][13] + '</td></tr></table>';
}
function tipDataBody(id) {
    if (id == "beginner" || id == "easy" || id == "medium" || id == "hard" || id == "elite") {
        return '<font face="Arial" size="2"><b>List of perks (worn): </b>' + diaryInfo[id][1] + '<p><b>Other perks: </b>' + diaryInfo[id][2] + '</p><p><b>Lamp: </b>' + diaryInfo[id][3] + '</p><p><b>Speak to: </b>' + diaryInfo[id][4] + '</p><p>' + rewardStatInfo(id); + '</p></font>';
    }
    else {
    return '<font face="Arial" size="2">' + diaryInfo[id][1] + '<p><b>Quest Requirements: </b> ' + diaryInfo[id][2] + '</p><p><b>Other Requirements: </b>' + diaryInfo[id][3] +'</p></font>';
    }
}
