$(document).ready(function() {
		var html = '<iframe allowtransparency="yes" scrolling="no" width="1020px" height="187px" marginheight="0" marginwidth="0" frameborder="0" src="ps_new_nav.php"></iframe>';
		//$('html body div#mainWrap div#header table tbody tr td:first').html(html);
		$('html body div#mainWrap div#header table tbody tr td:first').load('ps_new_nav.php');
		$('#tabs').css('width', '1000px');
	});	

var filesCounter = 0;
	function initCheckboxes() {
		var c = document.getElementsByTagName('input');
		var i;
		
		for(i = 0; i < c.length; i++) {
			if(c[i].type == 'checkbox')  {
				c[i].checked = false;
				c[i].onclick = function() {
					id = this.id.replace(/ch_/g, '');
					if(this.checked) {
						loadFiles(id, 'add');
						if(document.getElementById('sch_'+id)) {
							document.getElementById('sch_'+id).checked = true;
						}
						filesCounter++;
						showCounter();
					} else {
						if(document.getElementById('sch_'+id)) {
							document.getElementById('sch_'+id).checked = false;
						}
						loadFiles(id, 'remove');
					}
				}	
			}
		}
	}
	

	function initSubCheckboxes() {
		var c = document.getElementById('campaignData').getElementsByTagName('input');
		var i;
		for(i = 0; i < c.length; i++) {
			if(c[i].type == 'checkbox')  {
				c[i].checked = false;
				c[i].onclick = function() {
					id = this.id.replace(/sch_/g, '');
					if(this.checked) {
						loadFiles(id, 'add');
						if(document.getElementById('ch_'+id)) {
							document.getElementById('ch_'+id).checked = true;
						}
						filesCounter++;
						showCounter();
					} else {
						if(document.getElementById('ch_'+id)) {
							document.getElementById('ch_'+id).checked = false;
						}
						loadFiles(id, 'remove');
					}
				}	
			}
		}
	}
	
	initCheckboxes();
		
	function showCounter() {
		document.getElementById('dlTab').innerHTML = 'Download basket ('+filesCounter+')';
		if(filesCounter != 0) {
			document.getElementById('downloadButton').style.display = 'block';
		} else {
			document.getElementById('downloadButton').style.display = 'none';
		}
	}

	function removeFiles(id, action) {
		if(confirm('Are you sure you want to remove this file?')) {
			loadFiles(id, action);
		}
	}	
	
	function clearBasket() {
		if(confirm('Are you sure you want to clear your download basket?'))  {
			var c = document.getElementsByTagName('input');
			var i;
			for(i = 0; i < c.length; i++) {
				if(c[i].type == 'checkbox')  {
					if(c[i].checked) {
						c[i].checked = false;
					}
				}
			}
			
			loadFiles(0, 'clear');
			filesCounter = 0;
			showCounter();
		}
		document.getElementById('downloadBox').style.display = 'none';
		var iframe = document.getElementById('downloadFrame');
		iframe.src = '';
	}
	
	function downloadFiles() {
		document.getElementById('downloadBox').style.display = 'block';
		var iframe = document.getElementById('downloadFrame');
		iframe.src = '../psa-silo/jdownload/index.html';
	}
	
	function loadFiles(id, action) {
		var url = '../psa-silo/ajax.response.php?method=ajax&req='+action+'&id='+id;
		var a = new jajax(url, 'filesList');
		a.show_status = false;
		a.onInit = function() {
			jui.showLoading();
		}
		a.onSuccess = function() {
			if(action == 'add') {
				jui.showResult('File added to basket');
			} else if(action == 'remove') {
				jui.showResult('File removed from basket');
			} else if('clear') {
				jui.showResult('Files cleared from basket');
			}
		}
//		a.show_data = false;
		a.get();
		if(action == 'remove') {
			filesCounter--;			
			document.getElementById('ch_'+id).checked = false;
			showCounter();
		}		
	}
	
	function clearSession() {
		var url = '../psa-silo/ajax.response.php?method=ajax&req=clearSes';
		var a = new jajax(url, 'filesList');
		a.show_status = false;
		a.show_data = false;
		a.get();
	}
	
	function requestFiles(fileUrl) {
		$.ajax({
			type:'GET',
			url:fileUrl,
			beforeSend: function() {
				$('#loading').show();
				$('#campaignList').hide();
			},
			success: function(data, status) {
				$('#loading').hide();
				$('#campaignFiles').show();
				$('#campaignData').html(data);
				initSubCheckboxes();
			}
		});
	}

	
	function backToList() {
		$('#campaignFiles').hide();
		$('#campaignList').show();
	}
