/**
 * Controls Elements coded for LTB by H2G Internetagentur, 5000 Aarau
 *
 * The controls instance handles the left/right arrows on
 * either the head layout picture and the large cinema display
 * image. The control instance expects the methods below from
 * the data instance to be given on the initial procedure:
 *
 * - function: next()
 * - function: prev()
 * - function: hasNext()
 * - function: hasPrev()
 *
 * On init procedure the method applies two control layers
 * on the fly into the given carrier container and binds
 * listener methods for executing the listed methods on
 * the data model.
 *
 * @author michael.kuck@h2g.ch
 * @version 1.01 25-11-2010
 */
var controls = {
	data: false,
	mouseon: false,
	init: function(container, data) {
		
		var $this = this;
		
		// set data object reference
		this.data = data;
		
		// set up controller elements (next | previous)
		var prev = $("<div></div>").addClass('controls').append($("<div></div>").addClass('prev'));
		var next = $("<div></div>").addClass('controls').append($("<div></div>").addClass('next'));
		
		// extracting height of carrier container
		var height = (parseInt($(container).css('height')) != 0) ? $(container).css('height') : $(container).height()+'px';
		
		// resize and positioning control elements
		$(prev).css({'left':'0px','height':height}).find('.prev').css({'height':height});
		$(next).css({'right':'0px','height':height}).find('.next').css({'height':height});
		
		// bind controls to observer
		this.observe(prev); this.observe(next);
		
		// observing mouseenter/mouseleave on main container
		$(container).bind('mouseenter', function() {
			$this.mouseon = true
		}).bind('mouseleave', function() {
			$this.mouseon = false;
			controls.hide(prev);
			controls.hide(next);
		});
		
		// bind resize event to parent container
		$(window).bind('resize', function() {
			
			// requesting current height of controls carrier
			var height = (parseInt($(container).css('height')) != 0) ? $(container).css('height') : $(container).height()+'px';
			
			// resizing controls to new height
			$(prev).css({'height':height}).find('.prev').css({'height':height});
			$(next).css({'height':height}).find('.next').css({'height':height});
			
		});
		
		// attach controls to container
		$(container).append(prev).append(next);
		
	},
	observe: function(control) {
		$(control).bind('mouseenter', function() {
			
			// preparing hasnext/hasprev boolean vars for fading controls
			var hasNext = (typeof controls.data.hasNext == "function") ? controls.data.hasNext() : false;
			var hasPrev = (typeof controls.data.hasPrev == "function") ? controls.data.hasPrev() : false;
			
			// getting control direction information
			var direction = $(this).find('div').attr('class');
			
			// handling controls (previous)
			if ((direction == 'prev') && hasPrev) {
				controls.prev(this);
				controls.show(this);
			}
			
			// handling controls (next)
			if ((direction == 'next') && hasNext) {
				controls.next(this);
				controls.show(this);
			}
			
		}).bind('mouseleave', function() {
			controls.hide($(this).unbind('click'));
		});
	},
	show: function(control) {
		// avoid black transparency fading on IE eg. Chrome
		if (jQuery.browser.msie || jQuery.browser.chrome) {
			$(control).find('div').css({'display':'block'});
		} else { $(control).find('div').fadeIn('fast'); }
	},
	hide: function(control) {
		// avoid black transparency fading on IE eg. Chrome
		if (jQuery.browser.msie || jQuery.browser.chrome) {
			$(control).find('div').css({'display':'none'});
		} else { $(control).find('div').fadeOut('fast'); }
	},
	hideAll: function(control) {
		$(control).parent().find('.controls').each(function() {
			controls.hide(this);
		});
	},
	prev: function(control) {
		$(control).bind('click', function() {
			controls.hideAll(this);
			// calling previous method on data object
			if (typeof controls.data.prev == "function")
				controls.data.prev(function() {
					// callback executed after animation slide
					if (controls.data.hasPrev() && controls.mouseon) {
						controls.show(control);
					}
				});
		});
	},
	next: function(control) {
		$(control).bind('click', function() {
			controls.hideAll(this);
			// calling next method on data object
			if (typeof controls.data.next == "function")
				controls.data.next(function() {
					// callback executed after animation slide
					if (controls.data.hasNext() && controls.mouseon) {
						controls.show(control);
					}
				});
		});
	},
	debug: function($msg) {
		if (window.console && window.console.log)
			window.console.log('debug: '+$msg);
	}
}
