$(document).ready(function(){
	
	var hero_gallery_elements = $(document).find('#hero_gallery')

	if(hero_gallery_elements.length > 0)
	{

		var hero_gallery = new HeroGallery(hero_gallery_elements);
	}
	
});

/* Hero Gallery object */

HeroGallery = function(arg)
{
	var owner = this;
	var jquery_obj = arg;
	var current_image = 0;
	var paused = false;
	var pause_btn = new PauseButton(jquery_obj.find('#pause_btn'));
	var prev_btn = jquery_obj.find('.prev_btn');
	var next_btn = jquery_obj.find('.next_btn');
	this.images = [];
	this.headlines = [];
	
	/*
pause_btn.jquery_obj.bind('PAUSE_BUTTON_CLICKED', function(){
		if(paused == false)
		{
			console.log('stop');
			clearInterval(gallery_timer);
			paused = true;
		} else {
			console.log('start');
			gallery_timer = setInterval(owner.f_animate, 5000);
			paused = false;
		}
	});
*/
	
	prev_btn.click(function(){
		
		if(current_image == 0)
		{
			current_image = owner.images.length - 1;
		} else {
			current_image --;
		}

		owner.f_change_image(current_image);
		
	});
	
	next_btn.click(function(){
	
		
		if(current_image == owner.images.length - 1)
		{
			current_image = 0;
		} else {
			current_image ++;
		}

		owner.f_change_image(current_image);
	});
	
	jquery_obj.find('.images ul.image_list li').each(function(i){
		
		var image = new MyImage($(this))
			image.id = i;
			
			image.jquery_obj.css({'position': 'absolute'});
			      
			owner.images.push(image);
				
	});
	
	if(owner.images.length == 1)
	{
		prev_btn.css('display','none');
		next_btn.css('display','none');
	}
	 
	jquery_obj.find('.headline ul li').each(function(i){
		
		var headline = new Headline($(this))
			headline.id = i;
			
			owner.headlines.push(headline);
		
	});
	
	this.f_manage_gallery = function(arg_id)
	{
		owner.current_image = arg_id;
		
		this.f_change_image(arg_id);
	}
	
	this.f_change_image = function(arg_id)
	{
		owner.images[arg_id].jquery_obj.stop().fadeTo('fast', 1);
		owner.headlines[arg_id].jquery_obj.stop().fadeTo('fast', 1);
		
		for(i = 0; i < owner.images.length; i++)
		{
			if(owner.headlines[i].id != arg_id)
			{
				owner.headlines[i].jquery_obj.stop().fadeTo('fast', 0, function(){
					$(this).css('display','none');
				});
				
			}
			
			if(owner.images[i].id != arg_id)
			{
				owner.images[i].jquery_obj.stop().fadeTo('fast', 0);
			}				
		}
	}
	
	this.f_manage_nav = function(arg_id)
	{
		for(i = 0; i < owner.buttons.length; i ++)
		{
			owner.buttons[i].enabled = true;
			owner.buttons[i].jquery_obj.mouseover();
			
			if(arg_id != owner.buttons[i].id)
			{
				owner.buttons[i].jquery_obj.mouseleave();
			}
			
		}
		
		owner.buttons[arg_id].enabled = false;
	}
	
	/* Auto Animate */
	
	/*
this.f_animate = function()
	{

		if(owner.current_image  == owner.images.length - 1)
		{
			owner.current_image = 0;
		} else {
			owner.current_image ++;
		}
		
		owner.f_manage_gallery(owner.current_image);
		
	}
*/
	
	owner.f_manage_gallery(current_image);
	
	/* var gallery_timer = setInterval(owner.f_animate, 5000); */
	

	
}

MyImage = function(arg_obj, arg_total_images)
{
	var owner = this;
	
	this.jquery_obj = arg_obj;
	this.id = null;
	

}

Headline = function(arg_obj)
{
	var owner = this;
	
	this.jquery_obj = arg_obj;
	this.id = null;
		
}

PauseButton = function(arg_obj)
{
	var owner = this;
	var state = 'playing';
	
	this.jquery_obj = arg_obj;
	
	this.jquery_obj.click(function(){
	
		owner.jquery_obj.trigger('PAUSE_BUTTON_CLICKED');
		switch(state)
		{
			case 'playing':
				owner.jquery_obj.addClass('paused');
				
				state = 'paused'
			break;
			
			case 'paused':
				owner.jquery_obj.removeClass('paused');
				state = 'playing'
			break;
		}
	});
	
}
