﻿// Functions specific to a site.

(function($) {
	$(document).ready(function() {
		// Place document manipulation code here
		Cufon.replace('h1, .accord-header, #bottomlinks a');
		
		$('.c-w1 ol li').wrapInner('<span></span>');
		$('table tr td:last-child').addClass('last-cell');
		
		$('ul#accordian li a:first-child').each( function(){
			if($(this).attr('href') == '#') {
				$(this).attr('href', 'javascript:void[0]');
				$(this).parent().addClass('closed');
			}
		});
		$('ul#accordian > li').css( 'height', '22px');
		$('.showall').click( function() {
			$('#accordian li.closed').each( function() {
				if ($(this).hasClass('closed')) {
					$(this).css('height', 'auto');
					var accHeight = $(this).css('height');
					$(this).css('height', '22px');
					$(this).addClass('open').animate({ height: accHeight}, 200).removeClass('closed');
				}
			});
		});
		$('.hideall').click( function() {
			if ($('#accordian li').hasClass('open')) {
				$('#accordian li.open').animate({'height': '22px'}, 200).addClass('closed').removeClass('open');
			}
		});
		$('#accordian a').click( function() {
			if($(this).parent().hasClass('open')) {
				$(this).parent().animate({'height': '22px'}, 200).removeClass('open').addClass('closed');
			} else if ($(this).parent().hasClass('closed')) {
				//$('.open').animate({height: '22px'}, 200).removeClass('open');
				$(this).parent().css('height', 'auto');
				var accHeight = $(this).parent().css('height');
				$(this).parent().css('height', '22px');
				$(this).parent().addClass('open').animate({ height: accHeight}, 200).removeClass('closed');
			}
		});
		
		
		$(function(){
			var slides = $('ul.accordian li').length;
			var currentSlide = $('ul.accordian li');
			var i = 0;
			function slide() { 
				if (i >= slides -1){
					$('.open').removeClass('open');
					$(currentSlide).eq(i).addClass('open');
					i = 0;
					$(currentSlide).delay(6000);
					$(currentSlide).eq(i).animate({width: '708px'}, 500);
					$('.last-child').animate({width: '40px'}, 500, slide);
				} else {
					$(currentSlide).delay(6000);
					$('.open').removeClass('open');
					$(currentSlide).eq(i).addClass('open');
					$(currentSlide).eq(i).animate({width: '40px'}, 500);
					i++;
					$(currentSlide).eq(i).animate({width: '708px'}, 500, slide);
				}
			}
			
			slide();
			$('ul.accordian li > a.acc-btn').click( function(){
				$(currentSlide).clearQueue();
			});
		});
		
		
		$('.c-w1 ul.accordian li a.acc-btn').attr('href', 'javascript:void[0]');
		$('ul.accordian > li:last-child').addClass('last-child');
		$('ul.accordian > li:first-child').addClass('first-child');
		$('ul.accordian > li').css( 'width', '40px');
		$('ul.accordian > li.open').css( 'width', '708px');
		$('.accordian > li > a').click( function() {
			$('.accordian > li').clearQueue();
			if($(this).parent().hasClass('open')) {
				return true;
			} else {
				if($(this).parent().hasClass('last-child')) {
					
				}
				$('.open').animate({width: '40px'}, 500).removeClass('open');
				$(this).parent().addClass('open').animate({ width: '708px'}, 500);
			}
		});
		
		$('.map-abbotsford').hover(function () {
			$('.main-map').attr('src','/images/maps/map-abbotsford.png');
		});
		$('.map-mission').hover(function () {
			$('.main-map').attr('src','/images/maps/map-mission.png');
		});
		$('.map-maple-ridge').hover(function () {
			$('.main-map').attr('src','/images/maps/map-maple-ridge.png');
		});
		$('.map-pitt-meadows').hover(function () {
			$('.main-map').attr('src','/images/maps/map-pitt-meadows.png');
		});
		$('.map-port-coquitlam').hover(function () {
			$('.main-map').attr('src','/images/maps/map-port-coquitlam.png');
		});
		$('.map-coquitlam').hover(function () {
			$('.main-map').attr('src','/images/maps/map-coquitlam.png');
		});
		$('.map-burnaby').hover(function () {
			$('.main-map').attr('src','/images/maps/map-burnaby.png');
		});
		$('.map-new-westminister').hover(function () {
			$('.main-map').attr('src','/images/maps/map-new-westminister.png');
		});
		$('.map-north-vancouver').hover(function () {
			$('.main-map').attr('src','/images/maps/map-north-vancouver.png');
		});
		$('.map-west-vancouver').hover(function () {
			$('.main-map').attr('src','/images/maps/map-west-vancouver.png');
		});
		$('.map-bowen-island').hover(function () {
			$('.main-map').attr('src','/images/maps/map-bowen-island.png');
		});
		$('.map-Langley').hover(function () {
			$('.main-map').attr('src','/images/maps/map-langely.png');
		});
		$('.map-white-rock').hover(function () {
			$('.main-map').attr('src','/images/maps/map-white-rock.png');
		});
		$('.map-delta').hover(function () {
			$('.main-map').attr('src','/images/maps/map-delta.png');
		});
		$('.map-vancouver').hover(function () {
			$('.main-map').attr('src','/images/maps/map-vancouver.png');
		});
		$('.map-richmond').hover(function () {
			$('.main-map').attr('src','/images/maps/map-richmond.png');
		});
		$('.map-port-moody').hover(function () {
			$('.main-map').attr('src','/images/maps/map-port-moody.png');
		});
		$('.map-anmore').hover(function () {
			$('.main-map').attr('src','/images/maps/map-anmore.png');
		});
		$('.map-belcarra').hover(function () {
			$('.main-map').attr('src','/images/maps/map-belcarra.png');
		});
		$('.map-surrey').hover(function () {
			$('.main-map').attr('src','/images/maps/map-surrey.png');
		});
		
var ContentWidth = 100;
var TimeToSlide = 250.0;

var openAccordion = '';

function runAccordion(index)
{
  var nID = "Accordion" + index + "Content";
  if(openAccordion == nID)
    nID = '';
    
  setTimeout("animate(" + new Date().getTime() + "," + TimeToSlide + ",'" 
      + openAccordion + "','" + nID + "')", 33);
  
  openAccordion = nID;
}

function animate(lastTick, timeLeft, closingId, openingId)
{  
  var curTick = new Date().getTime();
  var elapsedTicks = curTick - lastTick;
  
  var opening = (openingId == '') ? null : document.getElementById(openingId);
  var closing = (closingId == '') ? null : document.getElementById(closingId);
 
  if(timeLeft <= elapsedTicks)
  {
    if(opening != null)
      opening.style.width = ContentHeight + '668px';
    
    if(closing != null)
    {
      closing.style.display = 'none';
      closing.style.width = '668px';
    }
    return;
  }
 
  timeLeft -= elapsedTicks;
  var newClosedHeight = Math.round((timeLeft/TimeToSlide) * ContentHeight);

  if(opening != null)
  {
    if(opening.style.display != 'block')
      opening.style.display = 'block';
    opening.style.width = (ContentWidth - newClosedWidth) + '50px';
  }
  
  if(closing != null)
    closing.style.height = newClosedHeight + 'px';

  setTimeout("animate(" + curTick + "," + timeLeft + ",'" 
      + closingId + "','" + openingId + "')", 33);
}
		
		

//	Superfish drop-down menus
//		$('.main-nav ul.L1').superfish({
//			delay: 500, // one second delay on mouseout
//			animation: { opacity: 'show' }, // fade-in and slide-down animation
//			speed: 'fast', // faster animation speed
//			autoArrows: false,
//			dropShadows: false
//		}); 		
	}); 
})(jQuery);
