// Function to check whether a surrounding frameset exists or not
function generateFrameset() {
	
	// See if a GUI frame exists under the top
	if (document.getElementById('deactivate_player')) {
		
	} else {
	if (!top.GUI) {
		
		// Get the current URL string
		var url = window.location.href;

		// Check that it has a ? in it
		if (url.indexOf('?') == (url.length - 1)) {
			window.location.href = url + 'player=true';
		} else if ((url.indexOf('?') > -1)) {
			window.location.href = url + '&player=true';
		} else {
			window.location.href = url + '?player=true';
		}
		
	} 
	}
	
}

function setPageTitle(title) {
	top.document.title = title;
}

function playerLoadAlbum(bandcamp_id) {
	
	// Ask them if they want to load the track
	if (confirm("Are you sure you want to listen to this album?\nAny Synoiz music you currently have\nplaying will be stopped.")) {
		
		top.GUI.location.href = '/player.php?album_id=' + bandcamp_id + '&auto=true';
		
	}
	
}

function cleanFrameset() {
	
	if (top.GUI) {
	
		// Hide the navigation layer
		if (document.getElementById('navigation_holder')) {
			document.getElementById('navigation_holder').style.display = "none";
		}
		if (document.getElementById('body')) {
			document.getElementById('body').className = "body_shift";
		}
		
	}
	
}

// Change the site style to another stylesheet
function switchStyle(id) {

	// Check the URL
	var url = document.location.href;

	if (document.getElementById(id)) {
		
		var value = document.getElementById(id).options[document.getElementById(id).selectedIndex].value;
		if (value) {
			
			if (url.indexOf("?")) {
				document.location = url + '&style=' + value;
			} else {
				document.location = url + '?style=' + value;
			}
			
		}
		
	}

}

// Empty a field
function emptyField(field,removeValue) {
	
	if (document.getElementById(field)) {
		
		if (removeValue) {
			
			if (document.getElementById(field).value == removeValue) {
				
				document.getElementById(field).value = "";
				
			}
			
		} else {
			
			document.getElementById(field).value = "";
			
		}
		
	}
	
}

// Fill a field
function fillField(field,fillValue) {
	
	if (document.getElementById(field)) {

		if (!document.getElementById(field).value) {
			
			document.getElementById(field).value = fillValue;
			
		}
		
	}
	
}


function optionSelect(page) {
	
	document.location = page;
	
}

function changePageType(id) {

	if (!id) {
		id = 0;
	}
	
	var ref_page_types = new Array();
	ref_page_types[0] = new Array("folder_icon.gif", "Folder", "Folder: Allow this page to contain other pages and appear on the website"); 
	ref_page_types[1] = new Array("folder_hidden_icon.gif", "Hidden Folder", "Hidden Folder: Allow this page to contain other pages but be hidden from the site");
	ref_page_types[2] = new Array("page_icon.gif", "Page", "Page: Allow this page to appear on the website but not to contain other pages");

	// Change the details
	if (document.getElementById('page_type_description')) {
		document.getElementById('page_type_description').innerHTML = ref_page_types[id][2];
	}
	if (document.getElementById('page_type_icon')) {
		document.getElementById('page_type_icon').src = '/images/icons/' + ref_page_types[id][0];
	}
	if (document.getElementById('page_type')) {
		document.getElementById('page_type').disabled = "";
	}
	
}


function changePageFilename(warning) {
	
	if (warning) {
		
		if (!check) {
			alert("Please note that by changing the filename you will change all \nsubfolders/pages which may affect user's bookmarks or search \nengine rankings, please only change the filename if it is in\n error or has not been on the site long.");
			check = true;
		}
	
	}
		
	if (document.getElementById('page_filename')) {
	
		document.getElementById('page_trail_add').innerHTML = document.getElementById('page_filename').value;

	}
		
}


function addDatepicker(element_name,remove) {
	if (remove) {
		$(element_name).datepicker('destroy');
	} else {
		$(function() {
			$(element_name).datepicker({dateFormat: 'dd/mm/yy', firstDay: 1});
		});
	}
}

function ajax_query(url,data) {
	var ajax_return=$.ajax(
		{
			url:'/libraries/ajax/'+url+'?'+data,
			async:false
		}
	).responseText;
	return ajax_return;
}

function articleFileAdd(section_id) {
	var file_row = ajax_query('article_manage_file.php','section_id=' + section_id);
	if (file_row) {
		
		// Get the current content of the article file box and append the new row to the end
		if (document.getElementById('article_file_upload')) {
			$("#article_file_upload").append(file_row);
			//document.getElementById('article_file_upload').innerHTML = document.getElementById('article_file_upload').innerHTML + file_row;
		}
		
	}
}

function articleFileOrder(section_id,order) {
	
	// Make the order string valid for being a single entity in the querystring
	order = order.replace(/&/g,"|");
	var file_return = ajax_query('article_manage_file.php','section_id=' + section_id + '&mode=1&order=' + order);
	if (file_return) {
		
		alert(file_return);
		
	}
}

function toggle(id,height,read_more,read_less) {
	var element = $(id);
	if (element.css("overflow") == "hidden") {
		element.css("height","auto");
		element.css("overflow","visible");
	} else {
		element.css("height",height + "px");
		element.css("overflow","hidden");
	}
}

