
/**
 * Project Namespace
 */
var CCF = CCF || {};

CCF.tools = {
	getBlockMinHeight: function(){
		var right = $('.right', $('#home')), rh = right.outerHeight();
		return parseInt(Math.ceil(rh/2));
	},
			
	ajustBlocksHeight: function(){
		var home = $('#home');
		if(home.length)
		{
			var left = $('.left', home);
			$('.block', left).css({'minHeight': CCF.tools.getBlockMinHeight()});
		}
				
		return CCF.tools;
	},
			
	ajustBlockHeight: function(sel){
		var el = $(sel);
		if(el.length)
		{
			var next = el.next();
			if(next.length && next.hasClass('block'))
			{
				var nH = next.outerHeight(), h = el.outerHeight();
				if(h < nH)
				{
					el.css({minHeight: nH});
					next.css({minHeight: nH});
				}
				else
				{
					var minHeight = CCF.tools.getBlockMinHeight();
					el.css({minHeight: minHeight});
					next.css({minHeight: minHeight});
				}
			}
		}
	}, 
	
	viewSchedule: function(el, collection)
	{
		if($(el).hasClass('active')) return;
		
		var rel = $(el).attr('rel'), target = $('#' + rel);
		if(target.is('div') || target.is('li'))
		{	
			$('div.theater, li.theater').css({display: 'none'});
			target.css({display: 'block'});
			collection.removeClass('active');
			$(el).addClass('active');
		}
		
		return;
	}
};

/**
 * Plugins
 */
(function($){

	$.fn.boxHover = function()
	{
	  	this.each(function(i, el)
	  	{
	    	$(el).bind('mouseenter', function(){
				var c = $(this), css = c.attr('class').split(' ')[0].replace('hover|', '');
				if(c.is('div')) 
				{
					c = $('.media a:first', c);
				}
				
				var span = $('span.' + css, c);
				span.stop(true, false).animate({bottom: 0}, {duration: 250, easing: 'easeInSine'});
			}).bind('mouseleave', function(){
				var c = $(this), css = c.attr('class').split(' ')[0].replace('hover|', '');
				if(c.is('div')) 
				{
					c = $('.media a:first', c);
				}
				
				var span = $('span.' + css, c);
				span.stop(true, false).animate({bottom: -1*c.innerHeight()});
			});
	    });
	   
	   return this;
  	};
  	
  	$.fn.titleMagik = function()
  	{
  		this.each(function(i, el)
  		{
  			$(el).css({cursor: 'pointer'}).bind('click', function(e){
				var url = $(this).attr('rel');
				if(url)
				{
					window.location.href = url;
				}
				return false;
			});
  		});
  		
  		return this;
  	};
  	
  	$.fn.blank = function()
  	{
  		this.each(function(i, el)
  		{
  			$(el).attr('target', '_blank');
  		});
  		
  		return this;
  	};
  	
  	$.fn.labelMagik = function()
  	{
  		this.each(function(i, el)
  		{
  			var label = $('label', el), input = label.next();
			input.bind('focus', function(){
				label.css({display: 'none'});
			});
				
			input.bind('blur', function(){
				if($.trim($(this).val()) == label.text() || $.trim($(this).val()) == '')
				{
					label.css({display: 'block'});
					$(this).val('');
				}
			});
  		});
  		
  		return this;
  	};
  	
  	$.fn.selectMagik = function()
  	{
  		this.each(function()
  		{
  			
  		});
  		
  		return this;
  	};
  	
  	$.fn.formMagik = function()
  	{
  		this.each(function()
  		{
  			
  		});
  		
  		return this;
  	};
  	
  	$.fn.gallery = function()
  	{
  		this.each(function(i, el)
  		{
  			var params = {
  				'autoDimensions': false,
				'width': 730,
				'height': 630,
				'transitionIn': 'none',
				'transitionOut': 'none',
				'centerOnScroll': true, 
				'onStart': function()
				{
					var map = $('#map');
					if(map.is('iframe'))
					{
						map.css({display: 'none'});
					}
				},
				'onClosed': function()
				{
					var map = $('#map');
					if(map.is('iframe'))
					{
						map.css({display: 'block'});
					}
				}
  			};
  			
  			$(el).bind('click', function(e){
  				e.preventDefault();
				var c = $(this), url = c.attr('href'), iframe = '<iframe src="'+ url +'" frameborder="0" scrolling="no" width="730" height="625" allowTransparency="true" style="visibility: hidden;"></iframe>';
				
				$.fancybox.showActivity();
				window.setTimeout(function(){
					$.fancybox(iframe, params);
				}, 500);
							
				return false;
  			});
  		});
  		
  		return this;
  	}
  
})(jQuery);


/**
 * Carousel
 */
CCF.carousel = function(container, items, options){
	var defaultOptions = {vertical: false, scrollLength: 72, disabled: false, css: 'disabled', display: 5, padding: 0};
	this.options = $.extend({}, defaultOptions, options);
	this.container = $(container);
	this.wrapper = $('.c_container', this.container);
	this.size = (this.options.vertival)? this.wrapper.innerHeight() : this.wrapper.innerWidth;
	this.scroller = $('ul', this.wrapper);
	this.items = items;
	this.current = 0;
		
	return this.initialize();
};
	
$.extend(CCF.carousel.prototype, {
	initialize: function()
	{
		var that = this, disabled = true;
			
		if(this.scroller.length)
		{
			var size = this.items.length * this.options.scrollLength;
			var fn = (this.options.vertical)? 'height' : 'width';
			this.scroller[fn](size);
			
			if((this.wrapper[fn]() + this.options.padding) < size)
			{
				disabled = false;
				
				$('.next', this.container).bind('click', function(e){
					e.preventDefault();
					that.scrollNext();
					return false;
				});
					
				$('.prev', this.container).bind('click', function(e){
					e.preventDefault();
					that.scrollPrev();
					return false;
				});
				
				if(!this.scrollable('prev'))
				{
					that.setDisabledState(false, true);
					if(!this.scrollable('next'))
					{
						that.setDisabledState(true, true);
					}
				}
			}
		}
			
		if(disabled) 
		{
			that.setDisabledState(true, true);
			if(this.options.vertical)
			{
				var minHeight = parseInt(size);
				this.wrapper.css({minHeight: minHeight});
			}
		}

		return this;
	},
		
	scrollNext: function()
	{
		if(this.scrollable('next'))
		{
			this.processing = true;
			
			var path = arguments[0] || 1, target = this.current + path;
			this.animate(target, this, function(that, path){
				that.processing = false;
				that.current = that.current + path;
				
				var prop = {visibility: 'visible'};
				$('.prev', that.container).css(prop);
				if(!that.scrollable('next'))
				{
					prop = {visibility: 'hidden'};
				}
				$('.next', that.container).css(prop);
			}, path);
		}
			
		return this;
	}, 
		
	scrollPrev: function()
	{
		if(this.scrollable('prev'))
		{
			this.processing = true;
			
			var path = arguments[0] || 1, target = this.current - path;
			this.animate(target, this, function(that, path){
				that.processing = false;
				that.current = that.current - path;
					
				var prop = {visibility: 'visible'};
				$('.next', that.container).css(prop);
				if(!that.scrollable('prev'))
				{
					prop = {visibility: 'hidden'};
				}
				$('.prev', that.container).css(prop);
			}, path);
		}
			
		return this;
	},
	
	scrollTo: function(path, direction)
	{
		if(direction == 'next')
		{
			this.scrollNext(path);
		}
		else
		{
			this.scrollPrev(path);
		}
		
		return this;
	},
	
	animate: function(t, context, callback)
	{
		var size = t*this.options.scrollLength;
		if((size > 0)) size *= -1;
			
		var prop = {left: size};
		if(this.options.vertical)
		{
			prop = {top: size};
		}
		
		var that = this, path = arguments[3] || 1, duration = path * 300;
		this.scroller.animate(prop, {duration: duration, easing: 'easeInBack', complete: function(){
			callback(context, path);
		}});
			
		return this;
	},
		
	scrollable: function(direction)
	{
		var r = true;
	
		if(this.processing) r = false;
		else
		{
			if(direction == 'next')
			{
				var ref = this.current + this.options.display;
				if(ref >= this.items.length)
				{
					r = false;
				}
			}
			else
			{
				if(this.current == 0)
				{
					r = false;
				}
			}
		}
			
		return r;
	},
		
	setDisabledState: function(next, prev)
	{
		if(next)
		{
			$('.next', this.container).css({visibility: 'hidden'});
		}
			
		if(prev)
		{
			$('.prev', this.container).css({visibility: 'hidden'});
		}
			
		return this;
	}
});

/**
 * Gallery
 */
 
CCF.gallery = function(container, options)
{
	var defaultOptions = {auto: false, delay: 7000, scroller: null};
	this.options = $.extend({}, defaultOptions, options);
	this.container = $(container);
	this.viewer = $('.g_viewer', this.container);
	this.infos = $('.g_info', this.container);
	this.els = $('.caroussel .c_container a', this.container) || null;
	this.timeout = null;
	this.processing = false;
		
	return this.initialize();
};
	
$.extend(CCF.gallery.prototype, {
	initialize: function(){
		if(this.els)
		{
			var that = this;
			this.els.bind('click', function(evt){
				evt.preventDefault();
				if(!that.processing) 
				{
					that.els.removeClass('active');
					that.process($(this));
				}
				return false;
			});
			
			if(!this.options.ajax)
			{
				this.options.auto = true;
				this.timeout = window.setTimeout(function(){
					that.autoProcess(true);
				}, this.options.delay);	
			}
		}
		
		return this;
	}, 
	
	process: function(el)
	{
		this.processing = true;
		if(!this.options.ajax)
		{
			this.simpleProcessing(el);
		}
		else
		{
			this.ajaxProcessing(el);
		}
		return this;
	}, 
	
	autoProcess: function()
	{
		var refresh = arguments[0] || false;
		
		if(refresh)
		{
		
			var active = 0, that = this;
			this.els.each(function(i, elt){
				if($(elt).hasClass('active'))
				{
					if(i < (that.els.length - 1))
					{
						active = i + 1;
					}
					return;
				}
			});
			
			$(this.els.eq(active)).trigger('click');
			if(this.options.scroller)
			{
				var scroller = this.options.scroller, display = scroller.options.display, length = scroller.items.length, current = scroller.current;
				var path = ((current - active) + display);
				if(path <= 0)
				{
					scroller.scrollTo((-1*path) + 1, 'next');
				}
				else
				{
					if((path - display) > 0)
					{
						scroller.scrollTo((path - display), 'prev');
					}
				}
			}
		}
		
		return this;
	},
	
	ajaxProcessing: function(el)
	{
		var uri = el.attr('href'), target = $('.' + el.attr('rel'), this.container), that = this;
		
		$.ajax({
			url: uri,
			context: target,
			dataType: 'html',
			success: function(r)
			{
				$(this).html(r);
				that.processing = false;
				$(el).addClass('active');
			}
		});
			
		return this;
	}, 
		
	simpleProcessing: function(el)
	{
		var img = new Image(), that = this;
		var url = el.attr('href');
		var title = $('.title', el).text();
		var resume = $('.resume', el).text();
		var date = $('.date', el).text();
		
		if(this.timeout)
		{
			window.clearTimeout(this.timeout);
			this.timeout = null;
		}
			
		img.onload = function(){
			$('img', that.viewer).replaceWith(img);
			$('h5', that.infos).text(title);
			$('p:first', that.infos).text(resume);
			$('.date', that.infos).text(date);
			$('a', that.viewer).attr('href', url);
			that.processing = false;
			el.addClass('active');
			
			that.timeout = window.setTimeout(function(){
				that.autoProcess(true);
			}, that.options.delay);	
		};
			 
		img.src = el.attr('rel');
	}
});
