/**
 * scripts.js
 *
 * Holds any custom JS that is used globally in the application.
 */

// Hash that holds any required settings
var settings = {
	messages 			: 'messages',
	errorMessages : 'errorMessages'
};

/*
 * Create the namespaces
 */

// Declare global symbol
var videoCenter;

// If it is undefined yet, make it an object
if (!videoCenter) videoCenter = {};

/**
 * navigation button id used to page to the previous page of results
 * @type String
 */
videoCenter.PREV_BUTTON = 'prevPageButton';

/**
 * navigation button id used to page to the next page of results
 * @type String
 */
videoCenter.NEXT_BUTTON = 'nextPageButton';

/**
 * the page number to use for the next page navigation button
 * @type Number
 */
videoCenter.nextPage = 2;

/**
 * the page number to use for the previous page navigation button
 * @type Number
 */
videoCenter.prevPage = 0;

/**
 * the object which has to be paginated
 * @type Object 
 */
videoCenter.paginator = {}


/*
 * Events 
 */
window.addEvent('domready', function() { videoCenter.init() } );


/**
 * Called on every page (window) load
 */
videoCenter.init = function()
{
	
	/*
	 * Actually an IE6 Hack for the navigation. In all modern browsers the 
	 * drop-down menu should work with switched off JavaScript and CSS only.
	 */

/*	
	$$('.navL1').addEvent('mouseover', function() {
		var link = this.getElement('a');
		var navL2 = this.getElement('ul');
		this.setStyle('background', 'url(\'gfx/navSlantRightActive.gif\') top right no-repeat;')
		if (link) link.setStyle('color', '#8d1738');
		if (navL2) navL2.setStyle('display', 'block');
	});

	$$('.navL1').addEvent('mouseout', function() {
		var link = this.getElement('a');
		var navL2 = this.getElement('ul');
		this.setStyle('background', 'url(\'gfx/navSlantRight.gif\') top right no-repeat;')
		if (link) link.setStyle('color', '#eceded');
		if (navL2) navL2.setStyle('display', 'none');
	});
*/

	// Check if the messages/errorMessages element exists
	var messages = $(settings.messages);
	var errorMessages = $(settings.errorMessages);
	
	// If it exists, slide it in
	if (messages) {
		var slideMessages = new Fx.Slide(messages, { mode : 'horizontal' });
		
		// Hide it first to get the effect
		slideMessages.hide();
		slideMessages.slideIn();
	}
	if (errorMessages) {
		var errorMessages = new Fx.Slide(errorMessages, { mode : 'horizontal' });
		
		// Hide it first to get the effect
		errorMessages.hide();
		errorMessages.slideIn();
	}
}
/** EOF function */

/**
 * Simple date constructor for today.
 * 
 * @return {Object} A custom date object.
 */
videoCenter.today = function()
{
	// Get date
	var date = new Date();
	var year = date.getFullYear();
	var month = date.getMonth() + 1;
	var day = date.getDate();
	if (month <= 9) {
		month = '0' + month;
	}
	if (day <= 9) {
		day = '0' + day;
	}
	
	var today = {
		day : day,
		month : month,
		year : year
	}
	
	return today;
}
/** EOF function */

/**
 * Sends an AJAX request for receiving HTML.
 *
 * @param {String}  url    The request url.
 * @param {Object}  params The request parameters.
 * @param {String}  update The DOM id of the container to update.
 * @param {Boolean} only_gfx_indicator If set, show only the gfx loading indicator without text.
 */
videoCenter.sendRequestHtml = function(url, params, update, /* optional */ only_gfx_indicator)
{
	var resultDiv = $(update);
	only_gfx_indicator = only_gfx_indicator || false;
	
	var options = {
		url : url,
		data : params,
		update : resultDiv,
		onRequest : function() {
			if (only_gfx_indicator) {
				resultDiv.innerHTML = '<img src="../gfx/loader.gif" alt="" />';
			} else {
				resultDiv.innerHTML = 'Loading ... &nbsp;' + 
					'<img src="../gfx/loader.gif" alt="" />';
			}
		},
		onSuccess : function(responseTree, responseElements, responseHTML, responseJavaScript) {
			var html = responseHTML;
		},
		onComplete : function() {
		}
	};
	
	var request = new Request.HTML(options);
	request.send();
}
/** EOF function */

/**
 * Sends an AJAX request.
 *
 * @param {String} url The request url.
 * @param {Object} params The request parameters.
 */
videoCenter.sendRequest = function(url, params)
{
	
	var options = {
		url : url,
		data : params,
		onRequest : function() {
		},
		onComplete : function() {
		},
		onSuccess : function(responseText, responseXML) {
			if (!responseText) {
				alert(
					"Es ist ein unbekannter Fehler aufgetreten! Ihre Anfrage konnte " +
					"leider nicht erfolgreich bearbeitet werden."
				);
			};
		}
	};
	
	var request = new Request(options);
	request.send();
}
/** EOF function */

/**
 * Displays a small 'popup' window to indicate that a file upload is 
 * in progress.
 * 
 * @param {String} element The DOM element to display
 * @param {Number} width   The width of the DOM element to set
 * @param {Number} height  The height of the DOM element to set
 */
videoCenter.showOverlay = function(element, width, height)
{
	var container = $(element);
	var windowSize = window.getSize();
	var windowScroll = window.getScroll();
	
	var containerOffsetX = Math.ceil((windowSize.x / 2) - (width / 2));
	var containerOffsetY = Math.ceil((windowSize.y / 2) - (height / 2)) + 
		windowScroll.y;
	
	container.setStyles({
		position : 'absolute',
		padding : '20px',
		top : containerOffsetY + 'px',
		left : containerOffsetX + 'px', 
		width : width + 'px',
		height : height + 'px',
		display : 'block',
		background : 'url(../gfx/overlayBG.png) top left no-repeat',
		color : '#eceded',
		'z-index' : '200'
	});
}


/**
 * Simple helper function which sets two or more DOM elements to the
 * same height or a given height.
 *
 * @param {String} el1      The DOM id of the first element
 * @param {String} el2      The DOM id of the second element
 * @param {String} selector A css selectors string
 * @param {Number} height   The optional height in pixels
 */
videoCenter.adjustHeight = function(el1, el2, selector, height /* optional */) 
{
	
	if (!((el1 && el2) || selector)) {
		
		// No elements, nothing to do...
		return;
		
	} else if (el1 && el2) {
		
		// Get elements by id
		var element1 = $(el1);
		var element2 = $(el2);
		
		// Get size of elements
		var sizeOfElement1 = element1.getSize();
		var sizeOfElement2 = element2.getSize();
		
	} else if (selector) {
		
		// Get elements by css selectors string
		var elements = $$(selector);
		if (elements)
			var nrOfElements = elements.length;
		
		if (nrOfElements) {
			
			// Get size of each element
			var i = 0;
			var sizeOfElement = new Array();
			elements.each(function(el) {
				sizeOfElement[i] = el.getSize();
				i++;
			});
			
			// Reset the counter and get the highest element
			i = 0;
			var height = 0;
			var maxHeight = 0;
		
			sizeOfElement.each(function(size) {
				height = size.y;
				if (maxHeight < height) 
					maxHeight = height;
			});
			
			// Set all elements to the max height
			elements.each(function(el) {
				el.style.height = maxHeight + 'px';
			});
		}
	}
}
/** EOF function */


/**
 * Tracks the number of filled in characters, e.g. in a textarea or 
 * input field.
 *
 * @param {String} trackField   The DOM id of the field to track
 * @param {Number} limit        The limit of tokens for this field 
 * @param {String} counter      The DOM id of the element which displays the 
 *                              number of residual characters
 * @param {String} counterLabel The DOM id of the element that labels the counter
 */
videoCenter.tokenTracker = function(trackField, limit, counter, counterLabel)
{

	if (!trackField || !limit || !counter || !counterLabel) 
		return alert("Wrong parameters");
	
	var text = $(trackField).value;
	var textLength = text.length;
	var rest = limit - textLength;
	
	if (rest >= 0) {
		$(counterLabel).innerHTML = "Verbleibende Zeichen: ";
		$(counter).innerHTML = rest;
	} else { // (rest < 0)
		$(counterLabel).innerHTML = "Zeichen über dem Limit: ";
		$(counter).innerHTML = rest * (-1);
  }
}
/** EOF function */


/**
 * Updates the variables used by the navigation buttons and the 'enabled' 
 * status of the buttons based upon the current page number passed in.
 * 
 * @param {Number} page The current page number
 * @param {String} prevButton The DOM id of the previous button
 * @param {String} nextButton The DOM id of the next button
 */
videoCenter.updatePagination = function(page, prevButton, nextButton) {
	
	if ($('pageControl')) 
		$('pageControl').style.display = 'block';
		
	videoCenter.nextPage = page + 1;
  videoCenter.prevPage = page - 1;

  $(nextButton).style.display = 'inline';
  $(prevButton).style.display = 'inline';

  if (videoCenter.prevPage < 1) {
    $(prevButton).disabled = true;
  } else {
    $(prevButton).disabled = false;
  }
  $(nextButton).disabled = false;
};