$(document).ready(function(){
	$("a.box").fancybox({
		overlayOpacity: 0.8,
		callbackOnClose: function(){
			if($.browser.mozilla)
            {
                location.hash = '#';
            }
            else
            {
                location.hash = '';
            }
		},
		callbackOnShow: function(){
			var current = this['itemCurrent'],
				item = this['itemArray'][current];
				$link = $('a.box[href=\'' + item.href + '\']'),
				hash_slug_array = $link.attr('id').split('-'),
				hash_slug = hash_slug_array.slice(0, hash_slug_array.length-1).join('-');
	        
			location.hash = hash_slug;
		},
		zoomSpeedIn: 500,
		zoomSpeedOut: 500,
		zoomSpeedChange: 500
	});
	
	$('.section_action_h').data('state', 1);
	
	determine_image = function($this, image)
	{
		if($this.data('hover'))
		{
			image = image + '_active.gif';
		}
		else
		{
			image =  image + '.gif';
		}
		
		return image;
	}

	color_arrow = function(t, counter)
	{
		var $this = $(t),
			image = 'up';
		
		if($this.data('state') == 0)
		{
			image = 'down';	
		}
		
		image = determine_image($this, image);
		
		$('#section_action_'+counter).attr('src', MEDIA_URL + 'img/arrows/' + image);
	}
	
	toggle_state = function($this, counter)
	{
		var image;
		
		if($this.data('state') == 0)
		{
			$this.data('state', 1);
			image = 'up';
		}
		else
		{
			$this.data('state', 0);
			image = 'down';
		}
		
		image = determine_image($this, image);
		
		$('#section_action_'+counter).attr('src', MEDIA_URL + 'img/arrows/' + image);
	}
	
	$('.section_action_h').mouseover(function(){
		var $this = $(this);
		$this.data('hover', true);
		color_arrow(this, $this.attr('id').split('_')[3]);
	});
	
	$('.section_action_h').mouseout(function(){
		var $this = $(this);
		$this.data('hover', false);
		color_arrow(this, $this.attr('id').split('_')[3]);
	});
	
	$('.section_action_h').click(function(){
		$this = $(this);
		counter = $this.attr('id').split('_')[3];
		
		$('#section_' + counter).slideToggle();
		
		toggle_state($this, counter);
	});
	
	
	// Deal with bookmarking hash.
	if(location.hash != '')
    {
		var hash_slug = location.hash,
			$link;
		
		if(hash_slug.charAt(0) != '#')
		{
			hash_slug = '#' + hash_slug;
		}
		
		// Display fancybox.
		$link = $(hash_slug + '-a');
		
		if($link)
		{
			$link.click();
		}
    }
});