/* Global declarations */
var host;
var mouseDown = false;
var imagePage = 1;


/* A function when hovering over a menu item 
*
* @param element object
*/
function displaySubMenu(element){
	//Reset all of the menu item's styles
	$$("#menu ul li span").each(function(span){
		span.className = "";
	});
	
	$$("#menu ul li img").each(function(img){
		img.style.display = "none";
	});	
	
	//Change the main menu item's style
	var span = element.getElementsByTagName("span");
	span[0].className = "menu_item_selected";
	
	var img = element.getElementsByTagName("img");
	for (var i = 0; i <img.length; i++){
		img[i].style.display = 'inline';
	}
	
	
	//Display the submenu if it's hidden
	if ($("sub_menu").style.display == 'none'){
		$("sub_" +element.id).style.display = "";
		new Effect.SlideDown("sub_menu", { duration: 0.3, queue: 'end'});
	}
	
	//Otherwise display a different sub menu
	else {
		//Hide the currently visible sub menu item
		$$("#sub_menu div").each(function(div){
			var span = div.getElementsByTagName("span");
			for (var i = 0; i <span.length; i++){
				span[i].className = "";
			}
			
			if (div.style.display != "none"){
				new Effect.Fade(div.id, { duration: 0.1, queue: 'end'});
			}
		});
		
		//Display the new sub menu item
		new Effect.Appear("sub_" +element.id, { duration: 0.1, queue: 'end'});
	}
}


/* Check if an e-mail address is valid or not 
*
* @param email string
*/
function checkEmail(email){
	return /^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/.test(email);
}


/**
 * Convert all of the textareas with the wysiwyg class to fckeditor textarea
 *
 * @param path string
 */
function convertTextareasToFckEditor(path){
	//Get all of the textareas
	$$('textarea.wysiwyg').each(function(textarea){
		
		//Create the FCK editor object and replace the textarea
		var oFCKeditor = new FCKeditor(textarea.name);
		oFCKeditor.BasePath	= path +"scripts/fckeditor/";
		oFCKeditor.ReplaceTextarea(); 
		
	});
}


/**
 * Create dragable items for the image scroller
 */
function createScrollerDraggables(){
	Sortable.create("sortable_list", {
		handles:$$('#sortable_list img.draggable'),
		onUpdate: function() {
			//Reset the row colors
			var className = "row1";
			var items = new Array();
			
			$$("#sortable_list li").each(function(li){
				items[items.length] = li.id.substr(14);
				li.className = className;				
				className = (className == "row2") ? "row1" : "row2";
			})
			
			//Alter the order in the database
			new Ajax.Request(host +'admin/scroller/reorder', {
				method:'post',
				parameters: {'items[]':items, 'ajax':1},
				onSuccess: function(transport){
					//We could do something here but it isn't really necessary as of yet...
				}
			});			
			
		}
	});
}


/**
 * Create dragable items for the image list
 */
function createImageDraggables(){	
	Sortable.create("sortable_list", {
		handles:$$('#sortable_list .thumbnail'),
		onUpdate: function() {
			//Get all of the items in order
			var items = new Array();
			$$("#sortable_list li").each(function(li){
				items[items.length] = li.id.substr(10);
			});
			
			//Alter the order in the database
			new Ajax.Request(host +'admin/afbeeldingen/reorder', {
				method:'post',
				parameters: {'items[]':items, 'ajax':1},
				onSuccess: function(transport){
					//We could do something here but it isn't really necessary as of yet...
				}
			});
		}
	});
}


/**
 * Create dragable items for the video list
 */
function createVideoDraggables(){
	Sortable.create("sortable_list", {
		handles:$$('#sortable_list img.draggable'),
		onUpdate: function() {
			//Reset the row colors
			var className = "row1";
			var items = new Array();
			
			$$("#sortable_list li").each(function(li){
				items[items.length] = li.id.substr(14);
				li.className = className;				
				className = (className == "row2") ? "row1" : "row2";
			})
			
			//Alter the order in the database
			new Ajax.Request(host +'admin/videos/reorder', {
				method:'post',
				parameters: {'items[]':items, 'ajax':1},
				onSuccess: function(transport){
					//We could do something here but it isn't really necessary as of yet...
				}
			});			
			
		}
	});
}


/**
 * Display the actions for the thumbnails in the admin
 * 
 * @param object element
 * @return void
 */
function displayThumbnailActions(element){
	if (!mouseDown){
		var actions = $$("#" +element.id +" .actions");
		actions[0].style.display = 'block';
	}
}


/**
 * Hide the actions for the thumbnails in the admin
 * 
 * @param object element
 * @return void
 */
function hideThumbnailActions(element){
	if (!mouseDown){
		var actions = $$("#" +element.id +" .actions");
		actions[0].style.display = 'none';
	}
}


/** 
* Tell us that the mouse has been clicked
* 
* @param boolean clicked
* @return void
*/
function mouseClicked(clicked){
	mouseDown = clicked;
}


/** 
* Delete an image
* 
* @param object element
* @return void
*/
function deleteImage(element){	
	//Remove from the database
	new Ajax.Request(host +'admin/afbeeldingen/verwijderen', {
		method:'post',
		parameters: {'id':element.getOffsetParent().getOffsetParent().id.substr(10), 'ajax':1},
		onSuccess: function(transport){			
			//Hide the item from display
			Effect.DropOut(element.getOffsetParent().getOffsetParent().id);
		}
	});
}


/** 
* Display an image in the images screen
* 
* @param object element
* @return void
*/
function displayImage(element){		
	//Change the image
	$("main_image").src = element.src.replace(/\/thumbs\//, "/preview/");
	
	//Change the caption
	$("main_image_caption").update(element.alt);
	
	//Change the link
	var a = $$("#main_image_save a");
	a[0].href = element.src.replace(/\/thumbs\//, "/hi_res/");	
}


/** 
* Update the page of the iamges section
* 
* @param bool up
* @return void
*/
function changeImagePage(up){
	new Ajax.Request(
		host +'afbeeldingen/getpage',
		{
			method: 'post',
			
			parameters: {
				'ajax': 1, 
				'currentPage': imagePage, 
				'move': ((up) ? 1 : -1)},
				
			onSuccess: function(transport){
				
				//Remember the current page
				imagePage = transport.responseJSON.currentPage;
				
				//Delete all of the old images
				$$(".front_thumbnail").each(function(div){
					div.remove();
				});
				
				//Show the new images
				transport.responseJSON.images.each(function(image){
					//Create the div
					var div = new Element('div');
					div.addClassName('front_thumbnail');
					
					//Create the image
					var img = new Element('img', {
						'src': host +'public/files/images/thumbs/' +image.file,
						'alt': image.caption
					});
					Event.observe(img, 'click', function(){ displayImage(this) });
					
					//Insert the div and the image
					div.insert(img, {"position": "content"});
					$("thumbnails").insert(div, {"position": "content"});
				});
				
				var div = new Element('div', {'class': 'clear'});
				$("thumbnails").insert(div, {position: content});
			}
		}
	);
}
