// CONFIGURATION OPTIONS FOR KANUGA EFFECTS
// Homepage Promo Rotation
var transition_speed  = 400;  // milliseconds
var cycle_delay_time  = 6000; // milliseconds

// Tab Header open/close
var open_time         = 1000; // milliseconds
var close_time        = 1000; // milliseconds
var double_open_time  = 600;  // milliseconds for open portion of close->open
var double_close_time = 600;  // milliseconds for close portion of close->open

// Debug
var debug = false;

function mediaTab(filter)
{
	$debug_msg = 'Call Media Tab: ';
	var $tabHash = '#photos-videos-'+filter; // console.log($tabHash); // return false;
	$debug_msg += 'Use Hash: ' + $tabHash;
	if (debug) { console.log($debug_msg); }
	autoTab($tabHash);
}

function autoTab(hash)
{
	var $hashArray = hash.split('-');
	switch($hashArray[0])
	{
		// map
		case '#map':
			$('div#propertymap div.button').click();
		break;

		// photos-videos
		case '#photos':
			$debug_msg = 'Media Tab: ';
			if ($hashArray[2]!=null) 
			{ 
				$debug_msg += 'Add Filter: ' + $hashArray[2];
				media_filter($hashArray[2]); // console.log('Filter MEDIA tab on category: '+$hashArray[2]);
			}
			$('div#photosandvideos div.button').click();
			if (debug) { console.log($debug_msg); }
		break;

		// directory
		case '#directory':
			if ($hashArray[1]!=null) 
			{ 
				var $url = '/components/tab-directory/' + $hashArray[1]; console.log($url);
				$.get($url, function(data) { $('#staff').replaceWith($(data).find('#staff')); });
			}
			$('div#directory div.button').click();
		break;

		// calendar
		case '#calendar':
			if ($hashArray[1]!=null) 
			{ 
				var $year = $hashArray[1];
				var $month = ($hashArray[2]==null) ? '01' : $hashArray[2];
				var $url = '/components/tab-calendar/' + $year + '/' + $month; // console.log($url);
				$.get($url, function(data) { $('#calendar_content').replaceWith($(data).find('#calendar_content')); });
			}
			$('div#calendar div.button').click();
		break;
	}		
}

function media_filter($category_id)
{

	$url_filters = '/components/tab-media-filters/' + $category_id;
	$.get($url_filters, function(data) { $('#media_filters').replaceWith(data); });
	
	$url_thumb_photos = '/components/tab-media-photos/P1/' + $category_id;
	$.get($url_thumb_photos, function(data) 
	{ 
		$('#gallery_thumbs_photos').replaceWith(data); 
		$('#gallery_thumbs_photo_1').click();
	});
	
	$url_thumb_videos = '/components/tab-media-videos/P1/' + $category_id;
	$.get($url_thumb_videos, function(data) 
	{ 
		$('#gallery_thumbs_videos').replaceWith(data); 
		$('#gallery_thumbs_videos').addClass('hidden');
	});
	
	return false;
	
}

$(function() {
	
	// Homepage Promo Rotation
	if($('#home_promos').size() > 0) {
		$('#home_promos').after('<div id="main_promo_nav">').cycle({
			speed:       transition_speed,
			timeout:     cycle_delay_time,
			pager:      '#main_promo_nav',
			pauseOnPagerHover: true
		});
	}
	
	// Tab Header open/close
	var $overflow_div = $('#tabs');
	var $buttons = $("div.button", $overflow_div);
	var $content = $("#content_middle .content");
	var $open_section = null;
	
	$('div#propertymap img.close').live('click', function () { $('div#propertymap div.button').click(); });
	$('div#photosandvideos img.close').live('click', function() { $('div#photosandvideos div.button').click(); });
	$('div#directory img.close').live('click', function() { $('div#directory div.button').click(); });
	$('div#calendar img.close').live('click', function() { $('div#calendar div.button').click(); });
	
	$buttons.click(function() {
		if(!$content.is(':animated')) {
			$section = $(this).parent();
			$buttons.css('cursor', 'default');
			if($section.css('top') == '0px') {
				// Currently open; performing close
				$section.animate({top: -600}, close_time);
				$overflow_div.animate({height: 45}, close_time);
				$content.animate({'paddingTop': '0px'}, close_time, null, function() { // was: padding: '0px 65px 50px 65px'
					$buttons.css('cursor', 'pointer');
				});
				$open_section = null;
			} else {
				// Currently closed; performing open
				if($open_section == null) {
					// No section currently open; perform simple open
					$section.animate({top: 0}, open_time);
					$overflow_div.animate({height: 645}, open_time);
					$content.animate({'paddingTop': '600px'}, open_time, null, function() { //was: padding: '600px 65px 50px 65px'
						$buttons.css('cursor', 'pointer');
					});
				} else {
					// Section currently open; close it first, then open new section
					$overflow_div.animate({height: 45}, double_close_time);
					$content.animate({'paddingTop': '0px'}, double_close_time); // was: padding: '0px 65px 50px 65px'
					$open_section.animate({top: -600}, double_close_time, null, function() {
						$section.animate({top: 0}, double_open_time);
						$overflow_div.animate({height: 645}, double_open_time);
						$content.animate({'paddingTop': '600px'}, double_open_time, null, function() { //was: padding: '600px 65px 50px 65px'
							$buttons.css('cursor', 'pointer');
						});
					});
				}
				$open_section = $section;
			}
		}
	});
	
	// Media
	
	
	// -- Scrolling Implementation
	
	$('.dropdown').jScrollPane({ showArrows: false });
	$('.dropdown').hide(); 
	$('.toggle').live('click', function() {
		var $dd = $(this).parent('div').children('.dropdown');
		$dd.toggle("fast");
	});
	
	// -- Filters
	$('#dropdown_media_primary_filter li a, #dropdown_media_secondary_filter li a').live('click', function () 
	{ 
		$category_id = $(this).attr('category_id')=='all' ? '' : $(this).attr('category_id'); // console.log('category_id: '+$category_id); return false;
		media_filter($category_id);
		return false;
	});	
	
	// -- Flip Tabs
	$('#gallery_tabs li').click(function() 
	{
		$('#gallery_tabs li').each(function()
		{ 
			$(this).removeClass('current'); 			
			$tab = $(this).attr('id').split('_')[1]; 
			$('#gallery_thumbs_'+$tab).addClass('hidden'); // console.log('#gallery_thumbs_'+$tab+' is hidden.');
			$('#tab_'+$tab).removeClass('current'); // console.log('#tab_'+$tab+' is not current.');
		});
		
		$(this).addClass('current');
		$tab = $(this).attr('id').split('_')[1]; 
		$('#gallery_thumbs_'+$tab).removeClass('hidden'); if (debug) { console.log('Media: Tabs: Hiding: '+$tab); }
		
		// $url = $(this).attr('href'); // console.log($url); 
		// $.get($url, function(data) { $('#calendar_content').replaceWith($(data).find('#calendar_content')); });
		
		return false;
		
	});
	
	// -- Do Pagination	for Photos
	$('#gallery_paginate_photos a').live('click', function () 
	{
		$url = $(this).attr('href'); // console.log($url); 
		$.get($url, function(data) 
		{ 
			$('#gallery_thumbs_photos').replaceWith(data);
			$('#gallery_thumbs_photo_1').click();
		});
		return false;
	});
	
	// -- Do Pagination	for Videos
	$('#gallery_paginate_videos a').live('click', function () 
	{
		$url = $(this).attr('href'); // console.log($url); 
		$.get($url, function(data) { $('#gallery_thumbs_videos').replaceWith(data); });
		return false;
	});
	
	// -- Load single photo or video
	$('#gallery_thumbs_photos li a, #gallery_thumbs_videos li a').live('click', function () 
	{
		$url = $(this).attr('href'); // console.log($url); 
		$.get($url, function(data) { $('#gallery_fullsize').replaceWith(data); });
		return false;
	});
	
	// -- Next / Prev on Single photo
	$('#gallery_fullsize div a').live('click', function ()
	{
		$url = $(this).attr('href'); // console.log($url); 
		$.get($url, function(data) 
		{ 
			$('#gallery_fullsize').replaceWith(data); 
		});
		return false;
	});

	// Directory

	// -- Scrolling Implementation
	
	var directory_pane = $('#directory .scroll');
	directory_pane.jScrollPane
	({
		showArrows: true,
		animateScroll: true
	});
	
	var directory_pane_api = directory_pane.data('jsp');
	
	$('#directory_category_filter li a').live('click', function () 
	{ 
		$label = $('#dropdown_staff div.toggle span').html($(this).html()); // console.log($label); return false;
		$url = $(this).attr('href'); // console.log($url); return false;
		$.get($url, function(data) { $('#staff').replaceWith($(data).find('#staff')); });
		$('#dropdown_staff div.toggle').click();
		return false;
	});	
	
	
	
	// -- Expand / Contract Profiles
	$('div.expand_staff_profile').live('click', function ()
	{
		
		$staff_id = $(this).attr('id').split('_')[2];
		$staff_row_id = 'staff_row_'+$staff_id;
		$staff_row = $('#'+$staff_row_id);
		$staff_profile_id = 'staff_profile_'+$staff_id;
		$staff_profile = $('#'+$staff_profile_id);
				
		$staff_rows = $('table#staff tr');
		$staff_rows.each(function(i) {
			$row_id = $(this).attr('id');
			if ($row_id==$staff_row_id || $row_id==$staff_profile_id)
			{
				// Do Nothing
			}
			else if ($(this).hasClass('staff_profile') && !$(this).hasClass('hidden')) 
			{ 
				// console.log(' -- Hide Profile');
				$(this).addClass('hidden'); 
			}
			else if ($(this).hasClass('staff_expanded'))
			{
				$(this).addClass('staff_row');
				$(this).removeClass('staff_expanded');
			}
		});
		
		if ($staff_row.hasClass('staff_row'))
		{
			$staff_row.addClass('staff_expanded');
			$staff_row.removeClass('staff_row');
			$staff_profile.removeClass('hidden');
			//console.log(' -- Scroll to Profile');
			directory_pane_api.reinitialise();
			directory_pane_api.scrollToElement($staff_row, true, true);
		}
		else
		{
			$staff_row.addClass('staff_row');
			$staff_row.removeClass('staff_expanded');
			$staff_profile.addClass('hidden');
			directory_pane_api.reinitialise();
		}
		
	});
	
	// Calendar 
	// -- Pagination & Link Functionality
	
	$('#calendar_next, #calendar_prev, .calendar_date_link, .calendar_cat_link').live('click', function () 
	{
		// console.log('Update Calendar'); 
		if ($(this).hasClass('calendar_date_link')) { $('#dropdown_calendar_dates div.toggle').click(); }
		$url = $(this).attr('href'); // console.log($url); 
		$.get($url, function(data) 
		{ 
			$('#calendar_content').replaceWith($(data).find('#calendar_content'));  
			$('.dropdown').jScrollPane({ showArrows: false }); 
			$('.dropdown').hide();
		});
		$('.dropdown').hide();
		return false;
	});

/**
 * Misc
 *
 */

	//$('#eNewsSignup').colorbox({width:'500px', height:'275px', iframe:true});
	
	$('.open').click(function() {
		$('div#'+$(this).attr('rel')+' div.button').click();
	});

/**
 * Map, Photos & Videos, Directory, Calendar
 *
 */

	var currentHash = window.location.hash;
	autoTab(currentHash);
	
});

