/**
 * Shows or hides the description when you click a show. This function is used on all pages
 * where shows are displayed and clickable.
 */
function toggleInfo(id) {
	var showDiv = document.getElementById('pDesc' + id);
	var pHeaderDiv = document.getElementById('progHeader' + id);
	if (showDiv.style.display == 'block') {
		showDiv.style.display = 'none';
		pHeaderDiv.className = pHeaderDiv.className.replace('progHeaderSelected', 'progHeader');
	} else {
		showDiv.style.display = 'block';
		pHeaderDiv.className = pHeaderDiv.className.replace('progHeader', 'progHeaderSelected');
	}
}

/**
 * Handles clicks on the next day button on the "TV-Tabl�" page.
 */
function nextDay(channel, days) {
	document.location.href = '/channel/' + channel + '?days=' + days;
}


/**
 * Handles clicks on the previous day button on the "TV-Tabl�" page.
 */
function previousDay(channel, days) {
	document.location.href = '/channel/' + channel + '?days=' + days;
}

/**
 * Handles onchange action of the channel select dropdown on the TV-Tabl� page.
 */
function selectChannelFromDropDown() {
	var chSelector = document.getElementById('channelSelector');
	var selectedValue = chSelector[chSelector.selectedIndex].value;
	if (selectedValue != '-') { // only switch if we have chosen a channel different from the one we're currently browsing
		document.location.href = '/channel/' + selectedValue;
	} else {
		chSelector.selectedIndex = 0;
	}
}

function selectChannel(chId) {
	var chan = document.getElementById('ch' + chId);
	if (chan.className.indexOf('chDeselected') > -1) {
		removeFromCookie(chId);
		chan.className = chan.className.replace('chDeselected', 'chSelected');
	} else {
		addToCookie(chId);
		chan.className = chan.className.replace('chSelected', 'chDeselected');
	}
}

/**
 * Adds channel to cookies
 */
function addToCookie(chan) {
	var cookieValues = deserializeString(getChannelCookie());
	cookieValues[cookieValues.length] = chan;
	saveChannelCookie(serializeArray(cookieValues));
}

/**
 * Removes channel from cookies
 */
function removeFromCookie(chan) {
	var cookieValues = deserializeString(getChannelCookie());
	var removeIndex = -1;
	for (var i = 0; i < cookieValues.length; i++) {
		if (cookieValues[i] == chan) {
			removeIndex = i;
			break;
		}
	}
	if (removeIndex > -1 && cookieValues.length > 1) {
		cookieValues.splice(removeIndex, 1);
	} 
	else {
		cookieValues = new Array();
	}
	saveChannelCookie(serializeArray(cookieValues));
}

/**
 * Transforms comma based string into array
 */
function deserializeString(cook) {
	if (cook && cook.length > 0)
	{
		return cook.split(':');
	}
	return new Array();
}

/**
 * Transforms array into semi colon based string
 */
function serializeArray(arr) {
	var str = "";
	for (var i = 0; i < arr.length; i++) { 
		str += arr[i] + ":";
	}
	str = str.substring(0, str.length - 1);
	return str;
}

function getChannelCookie() {
	return readCookie('tviappsdisabled');
}

function saveChannelCookie(val) {
	document.cookie = 'tviappsdisabled=' + val + '; expires=Thu, 2 Aug 2020 20:47:11 UTC; path=/';
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}