/*var ajax = new AJAX();
*/
var selected = null;
var timestamp = -1;
var selectedView = -1;
var schedule = false;
var selectedOldClass = '';
var places;
var placements;
var edit = false;
var manage = -1;

function resizeImage(imgObj, divObj)
{	
	imgObj.style.height = (divObj.offsetHeight - 2) + 'px';
	imgObj.style.width = (divObj.offsetWidth- 2) + 'px';
}

function uploadComplete(response)
{
	
	try
	{
		var respData = response.parseJSON();
	} 
	catch (SyntaxisError)
	{
		myAlert.showMessage(response);
		document.getElementById('viewPreview').innerHTML = '';
		return;
	}
	
	addPromo(document.getElementById('placeUpl').value, respData['filename']);
}

function beforeUpload ()
{
}

function addPromoResponse(requestObj, customData)
{
	try
	{
		var responseData = requestObj.responseText.parseJSON();
	} 
	catch (SyntaxisError)
	{
		myAlert.showMessage(requestObj.responseText);
		return;
	}
	
	// Add
	places[customData].content[responseData['contentId']] = responseData['content'];
	if (places[customData].updateContent)
		places[customData].updateContent();
	
	showManage(customData, responseData['contentId']);
}

function addPromo(place, content)
{
	var post = Object();
	post['action'] = 'addPromo';
	post['placeId'] = place;
	post['place'] = placements[place];
	post['content'] = content;
	post['url'] = document.getElementById('newUrl').value;
	post['name'] = document.getElementById('newName').value;
	var tmp = document.getElementById('newTrack');
	post['tracking'] = tmp.options[tmp.selectedIndex].value;
	ajax.addRequest("schedule.ajax.php", null, post, addPromoResponse, place);	
}

function showNewPromo(requestObj, customData)
{
	try
	{
		var responseData = requestObj.responseText.parseJSON();
	} 
	catch (SyntaxisError)
	{
		myAlert.showMessage(requestObj.responseText);
		return;
	}
	
	myPopup.setCloseOnOverlay(false);
	myPopup.setWidth(450);
	myPopup.setContent(responseData['body']);
	myPopup.start();	
}

function newPromo(place)
{
	var post = Object();
	post['action'] = 'newPromoForm';
	post['placeId'] = place;
	post['place'] = placements[place];
	ajax.addRequest("schedule.ajax.php", null, post, showNewPromo, null);
}

function updateResponse(requestObj, customData)
{
	try
	{
		var responseData = requestObj.responseText.parseJSON();
	} 
	catch (SyntaxisError)
	{
		myAlert.showMessage(requestObj.responseText);
		return;
	}
	
	for(var i in places)
	{
		if (typeof places[i] == 'object')
		{
			if (places[i].content[customData])
			{
				delete places[i].content[customData];
				places[i].content[responseData['id']] = responseData['content'];
			}
			for (var x = 0; x < places[i].data.length;x++)
			{
				if (places[i].data[x]['contentId'] == customData)
				{
					delete places[i].data[x];
					places[i].data[x] = responseData['content'];		
					places[i].displayData(0);		
				}
			}
		}
	}
	myPopup.end();
}

function updatePromo()
{
	var post = Object();
	post['action'] = 'updatePromo';
	post['contentId'] = document.getElementById('manageId').value;
	post['url'] = document.getElementById('manageUrl').value;
	post['name'] = document.getElementById('manageName').value;
	post['place'] = placements[manage];
	var tmp = document.getElementById('manageTrack');
	post['tracking'] = tmp.options[tmp.selectedIndex].value;
	ajax.addRequest("schedule.ajax.php", null, post.toJSONString(), updateResponse, post['contentId']);
}

function delPromoResponse(requestObj, customData)
{
	try
	{
		var responseData = requestObj.responseText.parseJSON();
	} 
	catch (SyntaxisError)
	{
		myAlert.showMessage(requestObj.responseText);
		return;
	}
	
	for(var i in places)
	{
		if (typeof places[i] == 'object')
		{
			if (places[i].content[customData])
				delete places[i].content[customData];
		}
		if (places[i].updateContent)
			places[i].updateContent();
	}
	showManage(manage);
}

function delPromo(conf)
{
	if (!conf)
		return;
		
	var sel = document.getElementById('manageSelect');
	var post = Object();
	post['action'] = 'deletePromo';
	post['content'] = sel.options[sel.selectedIndex].value;
	ajax.addRequest("schedule.ajax.php", null, post.toJSONString(), delPromoResponse, post['content']);
}

function deletePromo()
{
	myAlert.showQuestion('Are you sure you want to delete this promo?<br /> This promo will also be deleted from future days.', 'delPromo(');
}

function selectMngTracking(id)
{
	var tr = document.getElementById('manageTrack');
	
	for (var x = 0; x < tr.options.length; x++)
	{
		if (tr.options[x].value == id)
			tr.options[x].selected = true;
	}	
}

function displayPromo()
{
	var sel = document.getElementById('manageSelect');
	if (sel.selectedIndex != -1 && sel.options[sel.selectedIndex])
		places[manage].displayManage(sel.options[sel.selectedIndex].value);
	else
		places[manage].displayManage(-1);
}

function managePopup(requestObj, customData)
{
	try
	{
		var manageData = requestObj.responseText.parseJSON();
	} 
	catch (SyntaxisError)
	{
		myAlert.showMessage(requestObj.responseText);
		return;
	}

	myPopup.setCloseOnOverlay(false);
	myPopup.setWidth(600);
	myPopup.setContent(manageData['body']);
	manage = customData[0];
	if (customData[1])
	{
		var sel = document.getElementById('manageSelect');
		for (var x = 0; x < sel.options.length; x++)
		{
			if (sel.options[x].value == customData[1])
				sel.options[x].selected = true;
		}
	}
	else if (document.getElementById('manageSelect').options[0])
		document.getElementById('manageSelect').options[0].selected = true;
	myPopup.start();
	displayPromo();
}

function showManage(id, sel)
{
	var post = Object();
	post['action'] = 'getManage';
	post['place'] = id;
	post['content'] = places[id].content;
	ajax.addRequest("schedule.ajax.php", null, post.toJSONString(), managePopup, Array(id, sel));
}

function popupSpecs(requestObj, customData)
{
	try
	{
		var popupData = requestObj.responseText.parseJSON();
	} 
	catch (SyntaxisError)
	{
		myAlert.showMessage(requestObj.responseText);
		return;
	}
	myAlert.showMessage(popupData['body']);
}

function showSpecs(folder)
{
	var post = Object();
	post['action'] = 'getSpecs';
	post['folder'] = folder;
	ajax.addRequest("schedule.ajax.php", null, post.toJSONString(), popupSpecs, null);	
}

function showPreview(requestObj, customData)
{
	try
	{
		var previewData = requestObj.responseText.parseJSON();
	} 
	catch (SyntaxisError)
	{
		myAlert.showMessage(requestObj.responseText);
		return;
	}
	
	myPopup.setCloseOnOverlay(false);
	myPopup.setWidth(640);
	myPopup.setContent('<div class="alert"><div id="preview"></div><input type="button" value="Ok" onclick="javascript:myPopup.end()" /></div>');
	var swf = new SWFObject('beaurivage.swf', 'previewSwfD', "600px", "630px", "8", "#000000");
	swf.addParam("wmode", "window");
	swf.addVariable("xmlPreview", previewData['xml']);
	swf.write('preview');
	myPopup.start();
}

function previewSchedule()
{
	var post = Object();
	post['action'] = 'previewSchedule';
	post['places'] = places;
	post['view'] = selectedView;
	post['alert'] = document.getElementById('alert').checked;
	ajax.addRequest("schedule.ajax.php", null, post.toJSONString(), showPreview, null);	
}

function removeResponse(requestObj, customData)
{
	try
	{
		var saveData = requestObj.responseText.parseJSON();
	} 
	catch (SyntaxisError)
	{
		myAlert.showMessage(requestObj.responseText);
		return;
	}
	
	var tds = document.getElementById('calenderTable').getElementsByTagName('td');
	for (x = 0; x < tds.length; x++)
	{
		if (tds[x].getAttribute('timestamp') == timestamp)
		{
			tds[x].className = 'this notScheduledSelect';
			selectedOldClass = 'this';
		}
	}
	selectDate(null, timestamp);
}

function delSchedule(confirm)
{
	if (!confirm)
		return;
		
	var post = Object();
	post['action'] = 'removeSchedule';
	post['schedule'] = schedule['id'];
	ajax.addRequest("schedule.ajax.php", null, post.toJSONString(), removeResponse, null);	
}

function removeSchedule()
{
	if (!schedule)
		return;
	
	myAlert.showQuestion('Are you sure you want to delete this schedule?', 'delSchedule(');
}

function saveResponse(requestObj, customData)
{
	try
	{
		var saveData = requestObj.responseText.parseJSON();
	} 
	catch (SyntaxisError)
	{
		myAlert.showMessage(requestObj.responseText);
		return;
	}
	
	var tds = document.getElementById('calenderTable').getElementsByTagName('td');
	for (x = 0; x < tds.length; x++)
	{
		if (tds[x].getAttribute('timestamp') == timestamp)
		{
			tds[x].className = 'this scheduledSelect';
			selectedOldClass = 'this scheduled';
		}
	}
	selectDate(null, timestamp);
}

function saveSchedule()
{
	var post = Object();
	post['action'] = 'saveSchedule';
	post['schedule'] = schedule;
	post['selectedView'] = selectedView;
	post['timestamp'] = timestamp;
	post['places'] = places;
	post['alert'] = document.getElementById('alert').checked;
	ajax.addRequest("schedule.ajax.php", null, post.toJSONString(), saveResponse, null);	
}

function setPromos(place)
{
	var multi = document.getElementById('promosSelected');
	var single = document.getElementById('promoSelected');
	if (multi)
	{
		var tmpData = Object(); //places[place].data = Object();
		for(x = 0; x < multi.options.length; x++)
		{
			if (places[place].content[multi.options[x].value])
				tmpData[x] = places[place].content[multi.options[x].value];
			else
			{
				var z;
				for (var y in places[place].data)
				{
					if (places[place].data[y]['contentId'] == multi.options[x].value)
						z = y;
				}
				tmpData[x] = places[place].data[z];
			}
		}
		places[place].data = tmpData;
		places[place].data.length = multi.options.length;
	}
	else
	{
		if (single.selectedIndex != -1)
			places[place].data = Array(places[place].content[single.options[single.selectedIndex].value]);
	}

	places[place].displayData(0);
	myPopup.end();
}

function switchOpt(list, id1, id2)
{
	var opts = document.getElementById(list).options;
	var text1 = opts[id1].text;
	var text2 = opts[id2].text;
	var val1 = opts[id1].value;
	var val2 = opts[id2].value;
	var name1 = opts[id1].name;
	var name2 = opts[id2].name;
	
	opts[id1] = new Option(text2, val2, name2);
	opts[id2] = new Option(text1, val1, name1);
}

function moveUp()
{
	var opts = document.getElementById('promosSelected');
	for(x = 1; x < opts.options.length; x++)
	{
		if(opts.options[x].selected && opts.options[x-1])
		{
			switchOpt('promosSelected', x, x-1);
			opts[x-1].selected = true;
		}
	}
}

function moveDown()
{
	var opts = document.getElementById('promosSelected');
	for(x = opts.options.length-2; x >= 0; x--)
	{
		if(opts.options[x].selected && opts.options[x+1])
		{
			switchOpt('promosSelected', x, x+1);
			opts[x+1].selected = true;
		}
	}
}

function moveUp2()
{
	var opts = document.getElementById('promosSelected2');
	for(x = 1; x < opts.options.length; x++)
	{
		if(opts.options[x].selected && opts.options[x-1])
		{
			switchOpt('promosSelected2', x, x-1);
			opts[x-1].selected = true;
		}
	}
}

function moveDown2()
{
	var opts = document.getElementById('promosSelected2');
	for(x = opts.options.length-2; x >= 0; x--)
	{
		if(opts.options[x].selected && opts.options[x+1])
		{
			switchOpt('promosSelected2', x, x+1);
			opts[x+1].selected = true;
		}
	}
}

function moveToSelected()
{
	var list = document.getElementById('promoList');
	var sel = document.getElementById('promosSelected');
	for (x = list.options.length-1; x >= 0; x--)
	{
		if (list.options[x].selected)
		{
			var opt = list.options[x];
			var o = document.createElement('option');
			o.text = opt.text;
			o.value = opt.value;
			try
			{
				sel.add(o, null);
			}
			catch ( e )
			{
				sel.add( o ) ;
			}
			o.selected = true;
			list.remove(x);
		}
	}
}

function moveToList()
{
	var list = document.getElementById('promoList');
	var sel = document.getElementById('promosSelected');
	for (x = sel.options.length-1; x >= 0; x--)
	{
		if (sel.options[x].selected)
		{
			var opt = sel.options[x];
			var o = document.createElement('option');
			o.text = opt.text;
			o.value = opt.value;
			try
			{
				list.add(o, null);
			}
			catch ( e )
			{
				list.add( o ) ;
			}
			o.selected = true;
			sel.remove(x);
		}
	}
}


function moveToSelected2()
{
	var list = document.getElementById('promoList2');
	var sel = document.getElementById('promosSelected2');
	for (x = list.options.length-1; x >= 0; x--)
	{
		if (list.options[x].selected)
		{
			var opt = list.options[x];
			var o = document.createElement('option');
			o.text = opt.text;
			o.value = opt.value;
			try
			{
				sel.add(o, null);
			}
			catch ( e )
			{
				sel.add( o ) ;
			}
			o.selected = true;
			list.remove(x);
		}
	}
}

function moveToList2()
{
	var list = document.getElementById('promoList2');
	var sel = document.getElementById('promosSelected2');
	for (x = sel.options.length-1; x >= 0; x--)
	{
		if (sel.options[x].selected)
		{
			var opt = sel.options[x];
			var o = document.createElement('option');
			o.text = opt.text;
			o.value = opt.value;
			try
			{
				list.add(o, null);
			}
			catch ( e )
			{
				list.add( o ) ;
			}
			o.selected = true;
			sel.remove(x);
		}
	}
}
function selectPopup(requestObj, customData)
{
	try
	{
		var selectData = requestObj.responseText.parseJSON();
	} 
	catch (SyntaxisError)
	{
		myAlert.showMessage(requestObj.responseText);
		return;
	}

	myPopup.setCloseOnOverlay(false);
	myPopup.setWidth(480);
	myPopup.setContent(selectData['body']);
	myPopup.start();
}

function showSelect(id)
{
	var post = Object();
	post['action'] = 'getSelect';
	post['place'] = id;
	post['multi'] = places[id].multi;
	post['content'] = places[id].content;
	post['data'] = places[id].data;
	ajax.addRequest("schedule.ajax.php", null, post.toJSONString(), selectPopup, null);
}

function setMulti(id, length, nr)
{
	var left = false;
	var right = false;
	if (length > 1)
	{
		if (nr > 0)
			left = true;
		if (nr < (length - 1))
			right = true;
	}
	
	prev = document.getElementById('multiLeft' + id);
	next = document.getElementById('multiRight' + id);
	
	if (prev)
	{
		if (left)
		{
			prev.className = 'active';
			prev.setAttribute('placeId', id);
			prev.setAttribute('nr', parseInt(nr) -1);
			prev.onclick = function () { places[this.getAttribute('placeId')].displayData(this.getAttribute('nr')); };
		}
		else
		{
			prev.className = 'notActive';
			prev.onclick = '';
		}
	}
	if (next)
	{
		if (right)
		{
			next.className = 'active';
			next.setAttribute('placeId', id);
			next.setAttribute('nr', parseInt(nr) +1);
			next.onclick = function () { places[this.getAttribute('placeId')].displayData(this.getAttribute('nr')); };
		}
		else
		{
			next.className = 'notActive';
			next.onclick = '';
		}
	}
}

function processData(requestObj, customData)
{
	try
	{
		var placeData = requestObj.responseText.parseJSON();
	} 
	catch (SyntaxisError)
	{
		myAlert.showMessage(requestObj.responseText);
		return;
	}

	if (places[customData])
		places[customData].processResponse(placeData);
}

function getPlacementData(id, responseFunc)
{
	var post = Object();
	post['action'] = 'getPlacementData';
	post['viewType'] = selectedView;
	post['timestamp'] = timestamp;
	post['schedule'] = schedule;
	post['place'] = placements[id];
	ajax.addRequest("schedule.ajax.php", null, post.toJSONString(), processData, id);		
}

function dateHighl(obj, highl)
{
	if (obj == selected)
		return;
		
	if (highl)
	{		
		obj.setAttribute('oldClass', obj.className);
		if (obj.className == 'this')
			obj.className = 'this notScheduledSelect';
		else
			obj.className = 'this scheduledSelect';		
	}
	else
	{
		if (obj.getAttribute('oldClass'))
			obj.className = obj.getAttribute('oldClass');
	}
}

function highView(obj, highl)
{
	if (!obj || obj.getAttribute('viewtype') == selectedView)
		return;
	
	if (highl)
	{
		obj.setAttribute('oldClass', obj.className);
		obj.className = 'sel';
	}
	else
	{
		if (obj.getAttribute('oldClass'))
			obj.className = obj.getAttribute('oldClass');
	}
}

function setCalender(obj)
{
	// Reselect?
	if (obj == null)
	{
		obj = getObjByTimeStamp(timestamp);
		if (obj != null)
			obj.setAttribute('oldClass', selectedOldClass)
		selected = null;
	}
	
	if (selected != null && selected != obj)
	{
		selected.className = selectedOldClass;
	}
	
	if (selected != obj && obj != null)
	{
		selected = obj;			
		if (selected.getAttribute('oldClass'))
			selectedOldClass = selected.getAttribute('oldClass');
		else
		{
			selectedOldClass = selected.className;
			selected.setAttribute('oldClass', selectedOldClass);
		}
		if (selected.getAttribute('oldClass') == 'this scheduled')
			selected.className =  'this scheduledSelect';
		else
			selected.className = 'this notScheduledSelect';
		
		timestamp = selected.getAttribute('timestamp');
	}		
}

function setPlacements(requestObj, customData)
{
	try
	{
		var placeData = requestObj.responseText.parseJSON();
	} 
	catch (SyntaxisError)
	{
		myAlert.showMessage(requestObj.responseText);
		return;
	}

	// Display placements
	document.getElementById('scheduleRight').innerHTML = placeData['body'];
	
	// Set alert
	if (selectedView==schedule['viewType'])
	{
		if (schedule['alert'] == 'Y')
			alertOpt = true;
		else
			alertOpt = false;
	}
	else
		alertOpt = true;
	document.getElementById('alert').checked = alertOpt;
	
	// Init placements
	places = Object();
	placements = placeData['placements'];
	if (placeData['jsInit'])
		eval(placeData['jsInit']);
}

function getPlacements()
{
	if (selectedView == -1)
	{
		document.getElementById('scheduleRight').innerHTML = '';
	}
	else
	{
		var post = Object();
		post['action'] = 'getPlacements';
		post['viewType'] = selectedView;
		post['timestamp'] = timestamp;
		ajax.addRequest("schedule.ajax.php", null, post, setPlacements, null);	
		document.getElementById('scheduleRight').innerHTML = '';	
	}
}

function getObjByTimeStamp(timestamp)
{
	table = document.getElementById('calenderTable');
	tds = table.getElementsByTagName('td');

	for (x = 0; x < tds.length; x++)
	{
		if (tds[x].getAttribute('timestamp') == timestamp)
			return tds[x];
	}
}

function getViewByType(viewtype)
{
	div = document.getElementById('viewTypes');
	imgs = div.getElementsByTagName('img');
	
	for (x = 0; x < imgs.length; x++)
	{
		if (imgs[x].getAttribute('viewtype') == viewtype)
			return imgs[x];
	}
}

function setView(viewtype)
{
	if (viewtype != -1)
	{
		highView(getViewByType(viewtype), false);	
		highView(getViewByType(viewtype), true);
	}
	var tmp = selectedView;
	selectedView = viewtype;

	if (tmp != -1)
		highView(getViewByType(tmp), false);
		
	getPlacements();
}

function setDate(requestObj, customData)
{
	try
	{
		var dateData = requestObj.responseText.parseJSON();
	} 
	catch (SyntaxisError)
	{
		myAlert.showMessage(requestObj.responseText);
		return;
	}
	
	document.getElementById('views').innerHTML = dateData['views'];
	document.getElementById('calDate').innerHTML = dateData['txtDay'];

	if (customData == null)
		customData = getObjByTimeStamp(dateData['timestamp']);
	setCalender(customData);
	
	schedule = dateData['schedule'];
	selectedView = -1;
	if (schedule)
	{
		setView(schedule['viewType']);
	}
	else
	{
		setView(-1);
	}
}

function selectDate(obj, timestamp)
{
	var post = Object();
	post['action'] = 'selectDate';
	post['timestamp'] = timestamp;
	customData = obj;
	ajax.addRequest("schedule.ajax.php", null, post, setDate, customData);
}

function updateCalender(requestObj, customData)
{
	try
	{
		var calData = requestObj.responseText.parseJSON();
	} 
	catch (SyntaxisError)
	{
		myAlert.showMessage(requestObj.responseText);
		return;
	}
	
	document.getElementById('calTable').innerHTML = calData['body'];
	setCalender(null);
}

function getCalender(month, year)
{
	var post = Object();
	post['action'] = 'getCalender';
	post['month'] = month;
	post['year'] = year;
	ajax.addRequest("schedule.ajax.php", null, post, updateCalender, null);
}

function initScheduleIntf()
{
	getCalender(-1, -1);
	selectDate(null, -1);
}

function selectAll(selectBox,selectAll) {
	// have we been passed an ID
	if (typeof selectBox == "string") {
		selectBox = document.getElementById(selectBox);
	}
	// is the select box a multiple select box?
	if (selectBox.type == "select-multiple") {
		for (var i = 0; i < selectBox.options.length; i++) {
			selectBox.options[i].selected = selectAll;
		}
	}
}
