// Todays date, formatted as 'YYYY-MM-DD'
var todaysDate = new Date();
var todaysDateMonth = todaysDate.getMonth() + 1;
var todaysDateDay = todaysDate.getDate();
var todaysDateYear = todaysDate.getFullYear();
var todaysDateFormatted = todaysDateYear + "-" + todaysDateMonth + "-" + todaysDateDay;

$(document).ready(function(){

	// For all links with the class 'todays_events', link to current event listings for today's date forward
	$('.todays_events').attr('href', 'http://www.barrie.ca/pages/events.aspx?StartDate=' + todaysDateFormatted);

	// Jcarousel
	jQuery('#features_carousel').jcarousel({
		scroll: 4,
		animation: 700,
		wrap: 'circular'
	});

	// Tab Control
		$(".tab_content").hide(); 
		$(".tab_container li a:first").addClass("active").show(); 
		$(".tab_content:first").show(); 

		$(".tab_container li a").click(function() {
			$(".tab_container li a").removeClass("active"); 
			$(this).addClass("active"); 
			$(".tab_content").hide();
			var activeTab = $(this).attr("href"); 
			$(activeTab).fadeIn(); 
			return false;
		});
	

   	// Hide Related Pages & Downloads Webparts if no items are returned
    if($.trim($(".feature.downloads div").text()) == "") { 
  		$(".feature.downloads").hide(); 
	} 
	if($.trim($(".feature.related div").text()) == "") { 
  		$(".feature.related ").hide(); 
	} 

	// Text Resizer
	if($.cookie('TEXT_SIZE')) {
		$('#page,.side_nav').addClass($.cookie('TEXT_SIZE'));			
		$('.font_resize a.'+$.cookie('TEXT_SIZE')).addClass('selected');
	} else {
		$('.font_resize .regular').addClass('selected');	
	}
	
	$('.font_resize a').click(function() {
		var textSize = $(this).attr('class');
		$('#page, .side_nav').removeClass('regular medium large').addClass(textSize);
		$('.font_resize a').removeClass('selected');
		$(this).addClass('selected');
		$.cookie('TEXT_SIZE',textSize, { path: '/', expires: 10000 });
		return false;
	});

	// Navigation Dropdown Menu
	function topNavMenuOver(){
		$(this).find(".sub").show();
	}
	function topNavMenuOut(){ 
		$(this).find(".sub").hide();
	}
	$("a.close_button").click(function() {
		$(this).parent().hide();	
	});

	var config = {    
		 sensitivity: 2,   
		 interval: 30, 
		 over: topNavMenuOver,
		 timeout: 0,
		 out: topNavMenuOut 
	};
	$("ul#topnav li").hoverIntent(config);


	/* AddThis dropdown sharing menu */
	$(function() {
    var delay = 300;
    
    function hideMenu() {
        if (!$('.custom_button').data('in') && !$('.hover_menu').data('in') && !$('.hover_menu').data('hidden')) {
            $('.hover_menu').fadeOut('fast');
            $('.custom_button').removeClass('active');
            $('.hover_menu').data('hidden', true);
        }
    }
    
    $('.custom_button, .hover_menu').mouseenter(function() {
        $('.hover_menu').fadeIn('fast');
        $('.custom_button').addClass('active');
        $(this).data('in', true);
        $('.hover_menu').data('hidden', false);
    }).mouseleave(function() {
        $(this).data('in', false);
        setTimeout(hideMenu, delay);
    });    
    
	});
   	
	// Table toggle rows
	//$("tr.row_details").hide();
	$("td.row_toggle").parent().css("cursor", "pointer");
    $("td.row_toggle").parent().click(function(){
          $(this).next("tr.row_details").toggle();
    	  $(this).toggleClass("toggle_row_header");
    	  $(this).find("td.row_toggle").toggleClass("row_toggle_up");
    });
    
    
    // Landing page carousel
    $('#cycle_wrap #cycle').cycle({ 
    		fx:     'fade', 
    		speed:  '500', 
    		timeout: 6000, 
    		next:   '#cycle_wrap #cycle_next', 
   	 		prev:   '#cycle_wrap #cycle_prev' 
	});

    
    // Side Navigation height - match to page content height
    var pageLayoutColHeight = Math.max($('.side_nav').height(),$('.layout').height());
    $('.side_nav').height(pageLayoutColHeight);	 
		         
	// Side Navigation Menu - Add 'fancybox' class to any links with href containing 'photoviewer'
    $('.side_nav a[href*="photoviewer"]').addClass("photobox");     
    
    // Side Navigation Menu - Assign focus class to selected parent group
    $(".level2:first").parents().prev().find(".level1").addClass("parent_selected");
     
	// Alternating body table rows
	$(function(){  $("table.table_grid tbody tr:odd").addClass("oddrow");});
	$(function(){  $("table.table_parks tbody tr:odd").addClass("oddrow");});
	
	// Smooth scroll to top
	$(function(){
		$('a[href*=#top]').click(function() {
		if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') 
			&& location.hostname == this.hostname) {
				var $target = $(this.hash);
				$target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
				if ($target.length) {
					var targetOffset = $target.offset().top;
					$('html,body').animate({scrollTop: targetOffset}, 300);
					return false;
				}
			}
		});
	});
		
	// Fade Toggle Function
	// Usage:  $(this).find("temp").fadeToggle('fast');
	jQuery.fn.fadeToggle = function(speed, easing, callback) {
		return this.animate({opacity: 'toggle'}, speed, easing, callback);
	};

	// Default Text for Text Fields
	// Usage:  <input class="defaultText" title="e.g. someone@example.com" type="text" />
	 $(".defaultText").focus(function(srcc)
    {
        if ($(this).val() == $(this)[0].title)
        {
            $(this).removeClass("defaultTextActive");
            $(this).val("");
        }
    });
    $(".defaultText").blur(function()
    {
        if ($(this).val() == "")
        {
            $(this).addClass("defaultTextActive");
            $(this).val($(this)[0].title);
        }
    });
    $(".defaultText").focus();
	$(".defaultText").blur();	   
	
	// Start FancyBox Modal popup
	$(".fancybox").fancybox();
	
	// Start Custom PhotoBox Modal popup
	$(".photobox").fancybox({ 
		'padding'             : 0, 
		'autoScale'   : false, 
		'transitionIn'        : 'none', 
		'transitionOut'       : 'none', 
		'title'               : $(this).title
		//'frameWidth'               : 960, 
		//'frameHeight'              : 540 
		}); 
	
	// Start Custom VideoBox Modal popup
	$(".videobox").click(function(event){ 
		$.fancybox({ 
		'padding'             : 0, 
		'margin'			  : 0,
		'autoScale'   		  : false, 
		'transitionIn'        : 'none', 
		'transitionOut'       : 'none', 
		'title'               : this.title, 
		//'frameWidth'          : 325, 
		//'frameHeight'         : 344, 
		'href'                : this.href.replace(new RegExp("watch\\?v=", "i"), 'v/') + '&autoplay=1', 
		'type'                : 'swf',   
		'swf'                 : {
						'wmode'				:'transparent',
						'allowfullscreen'	:'true'
		}
		});
		return false;
	});  
	
	//Share through Email 
	$(".emailbox").click(function(event){ 
			$.fancybox({ 
			'padding'             : 0, 
			'margin'			  : 0,
			'autoScale'   		  : false, 
			'transitionIn'        : 'none', 
			'transitionOut'       : 'none', 
			'title'               : this.title, 
			'width'          : 310, 
			'height'         : 340, 
			'href'                : this.href, 
			'type'                : 'iframe',   
			'swf'                 : {
							'wmode'				:'transparent',
							'allowfullscreen'	:'true'
			}
			});
			return false;
		});  		
	
	// Hide / Show Toggle for event listings
	$(".event_list_group .hide").hide();
	$(".event_list_group a.toggle").click(function(event){
		if ('[ - ] Hide Event Details'  == $(this).text( )) {
		$(this).text('[ + ] Show Event Details');	
		} else {1
		$(this).text('[ - ] Hide Event Details');	
		}
		$(this).parents(".event_list_group").find(".hide").slideToggle("fast");
		return false;
	}); 
	
	// Hide / Show Toggle for event listings - Condensed Version
	$(".event_list_group.condensed .hide").hide();
	$(".event_list_group.condensed .details,.event_list_group.condensed .hide").click(function(){
		$(this).parents(".event_list_group.condensed").find(".hide").slideToggle("fast");
	}); 
	$(".event_list_group.condensed").hover(function(){
		$(this).find(".instructions").toggle();
	}); 
	

});


