$(document).ready(function() {
	var grandOpening = new Date("September 1, 2011 00:00:00");
	$('#defaultCountdown').countdown({until: grandOpening, format: 'dHM'});
	
	$("ul.tabs li a").hover(function() {
		$(this).addClass('active');
		var menuID = '';
		$('.menu').hide();
		menuID = $(this).attr("rel");
		$("#"+menuID).show();
	},function() {
		$(this).removeClass('active');
		$('.menu').hide();
	});
	/*
	$('#banners').hover(function() {
			$('#banners').animate({'height': '365px'}, 'fast');
		},function() {
			$('#banners').animate({'height': '285px'}, 'fast');
		});
	*/
	$(".menu").hover(function() {
		var triggerID = '';
		triggerID = $(this).attr("id");
		$("ul.tabs li a[rel="+triggerID+"]").addClass('active');
		$(this).show();
	},function() {
		var triggerID = '';
		triggerID = $(this).attr("id");
		$("ul.tabs li a[rel="+triggerID+"]").removeClass('active');
		$(this).hide();
	});
	$(".menu .column>ul>li>ul").addClass('subMenu');
	//General
	$('.box:eq(2)').css('border', 'none');
	$('.news:last, .listings:last, .middlebox:last').css('border', 'none');
	//Store landing Images
	$('.left_images img:eq(1), .left_images img:eq(3)').css({float: 'left', marginLeft: '10px'});						
	//Cycle
	$('.cycle').cycle({
	  fx: 'fade', 
	  speed: 900, 
	  timeout: 4000, 
	  next:	'#rightarrow',
	  prev:	'#leftarrow'
	});
	//Fancy Box
	$(".iframe").fancybox({
		'width'				: 850,
		'height'			: "95%",
		'autoScale'			: false,
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'overlayColor'		: '#000',
		'overlayOpacity'	: '0.7',
		'type'				: 'iframe',
		'titleShow'		: false
	});
	$(".iframeSmall").fancybox({
		'width'				: 450,
		'height'			: 360,
		'autoScale'			: false,
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'overlayColor'		: '#000',
		'overlayOpacity'	: '0.7',
		'type'				: 'iframe',
		'titleShow'		: false
	});
	$(".popup").fancybox(
	{
		'overlayColor'		: '#000',
		'overlayOpacity'	: '0.7',
		'titleShow'			: false
	});
	
	$("a.blank").attr("target","_blank");
	
	$("a[href='http://wilco.zendesk.com/anonymous_requests/new']").click(function() {
		Zenbox.render(); return false;
	});
	$("a[href='http://wilco.zendesk.com/anonymous_requests/new']").removeAttr('target');

	$(".autosize").fancybox(
	{
		'autoScale'		: true,
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'overlayColor'		: '#000',
		'overlayOpacity'	: '0.7'
	});
	

	//View All Toggle Expand/Collapase
	// $('.toggle_container .viewall').hide();
	$(".viewall_btn").toggle(
		function(){
			$(this).addClass("expanded"); 
		}, 
		function () {
			$(this).removeClass("expanded");
	});
	$(".viewall_btn_onload").toggle(
		function(){
			$(this).removeClass("expanded_onload"); 
		}, 
		function () {
			$(this).addClass("expanded_onload");
	});
	
	
	$(".toggle_container .viewall_btn, .toggle_container .viewall_btn_onload").click(function(){
		$(this).parent(".toggle_container").children('.viewall').slideToggle("slow");
	});
	//View All Text
	$(".toggle_container .viewall_text").click(function(){
		$(this).parent("ul").parent('.toggle_container').children('.viewall').slideToggle("slow");
	});
	
	$("#creditApp").bind("keypress", function(e) {
	  if (e.keyCode == 34) return false;
	});
	
});
//Calendar
$(function () {
	$('input.datepicker').datepick({dateFormat: 'yyyy-mm-dd'});
	$('.date_has_event').each(function () {
		// options
		var distance = 10;
		var time = 250;
		var hideDelay = 500;

		var hideDelayTimer = null;

		// tracker
		var beingShown = false;
		var shown = false;

		var trigger = $(this);
		var popup = $('.events ul', this).css('opacity', 0);

		// set the mouseover and mouseout on both element
		$([trigger.get(0), popup.get(0)]).mouseover(function () {
			// stops the hide event if we move from the trigger to the popup element
			if (hideDelayTimer) clearTimeout(hideDelayTimer);

			// don't trigger the animation again if we're being shown, or already visible
			if (beingShown || shown) {
				return;
			} else {
				beingShown = true;

				// reset position of popup box
				popup.css({
					bottom: 20,
					left: -76,
					display: 'block' // brings the popup back in to view
				})

				// (we're using chaining on the popup) now animate it's opacity and position
				.animate({
					bottom: '+=' + distance + 'px',
					opacity: 1
				}, time, 'swing', function() {
					// once the animation is complete, set the tracker variables
					beingShown = false;
					shown = true;
				});
			}
		}).mouseout(function () {
			// reset the timer if we get fired again - avoids double animations
			if (hideDelayTimer) clearTimeout(hideDelayTimer);

			// store the timer so that it can be cleared in the mouseover if required
			hideDelayTimer = setTimeout(function () {
				hideDelayTimer = null;
				popup.animate({
					bottom: '-=' + distance + 'px',
					opacity: 0
				}, time, 'swing', function () {
					// once the animate is complete, set the tracker variables
					shown = false;
					// hide the popup entirely after the effect (opacity alone doesn't do the job)
					popup.css('display', 'none');
				});
			}, hideDelay);
		});
	});
});

function openMap(theLocation,theType) {
	document.getElementById('fancyLink').href = 'http://www.wilco.coop/maps/'+theType+'/&location='+theLocation;
	$('#fancyLink').click();
}

function linkClick(theLinkId) {
	$('#'+theLinkId).click();
}

function exitMap() {
	if(parent.frames.length) {
  	parent.$.gfancybox.close();
	}else{
		document.location='http://www.wilco.coop/';
	}
}
