/* Author: Ryan Jackson @ bbandm */

var latitude;
var longitude;
var maplink;
var mapdest;


$(document).ready( function() {
	$('.closeit').append('<div class="closer"></div>');
	$('.openit').prepend('<div class="opener"></div>');

	/* Add corners to things that need corners IF the browser isn't capable IE9 */
//	if ( !$('body').hasClass('borderradius') ) {
		$('.no-borderradius .corners').append('<div class="corner-lt"></div><div class="corner-lb"></div><div class="corner-rt"></div><div class="corner-rb"></div>');
//	}

	/* Initiate the simple fader on the main marketing area */
	$('#slider').innerfade({
		speed: 'slow',
		timeout: 6000,
		type: 'sequence',
		containerheight: '280px'
	});
	
	$('.numerals li a').click( function(e) {
		e.preventDefault();
		
		/* Verify that this element is not actually currently selected */
		if ( !$(this).parent().hasClass('selected') ) {
			clickeditem = $(this);
		
			/* 	Animate a shrink class here */
			$('.numerals li.selected a').animate({
			    	width: '1'
				}, 500, function() {

					/* Remove the .selected class all elements (could just be for this items parent) */
					$('.numerals li.selected').removeClass('selected');

					/* Make sure this is set to 1px on start, overriding the default settings from CSS */
					clickeditem.css('width', '1px');

					/* Add .selected to the items's parent */
					clickeditem.parent().addClass('selected');

					/* Animate the opening of the element. */
					clickeditem.animate({
							width: '390'
						}, 500, function() {
							console.log('grow finished');
						});					
			});
		}
	})
	
	
	$('.hourlink').click( function(e) {
		e.preventDefault();
		$(this).closest('.column').removeClass('viewlocation viewhours viewcontact');
		$(this).closest('.column').addClass('viewhours');
	});

	$('.contactlink').click( function(e) {
		e.preventDefault();
		$(this).closest('.column').removeClass('viewlocation viewhours viewcontact');
		$(this).closest('.column').addClass('viewcontact');
	});
	
	$('.maplink').click( function(e) {
		e.preventDefault();

		$(this).closest('.column').removeClass('viewlocation viewhours viewcontact');

		/* Retain the data from the latest map button request. This can then be used later by other functions. */
		window.maplink = $(this);

		/* Get the desired location and pare it down to the bare essentials */
		window.mapdest = $(this).attr('href');
		window.mapdest = window.mapdest.substring(26, window.mapdest.length-5);


		/* Run the process that's going to check for html5 capabilities or the other */
		get_location();
	});
	$('.submit').click( function(e) {
		e.preventDefault();
		
		$mapstart = $(this).parent().children('.location').val();
		
		if ($mapstart != '' || $mapstart != NULL ) {
			window.location.href = 'http://maps.google.com/maps?saddr=' + escape( $mapstart ) + "&daddr=" + window.mapdest;
		}
	});
	$('.decline').click( function(e) {
		e.preventDefault();
		window.location.href = 'http://maps.google.com/?q=' + window.mapdest + '&z=15';
	});


	/* Add presentation only code to question pages */
	$('.question h2').prepend('<span>Q</span><span class="expand">+</span><span class="compress">-</span>');
	  	if ( $('body').hasClass('en') ) {
			$(".question").each(function() {
		    	$(this).find("p:first").prepend('<span>A</span>');
			});
		} else {
			$(".question").each(function() {
		    	$(this).find("p:first").prepend('<span>R</span>');
			});
		}
	
	$('.question p, .question ul, .question div').toggle();
	$('.question').addClass('compressed');

	$('.question').click( function() {
		$(this).children('p, ul, div').slideToggle('fast');

		if ( $(this).hasClass('expanded') ) {
			$(this).removeClass('expanded');
			$(this).addClass('compressed');
		} else {
			$(this).addClass('expanded');
			$(this).removeClass('compressed');
		};
		$(this).css('margin', '8px 0');
	});
	
	$('.brand_link').click( function(e){
		e.preventDefault();
		
		window.location = 'http://cardy.bbandm.ca/preview/?to=' + $(this).attr('href');
	})
});

function toggles() {
}
function get_location($object) {
	/* Check to see if we can do HTML5 geolocation */
	if (Modernizr.geolocation) {
		/* call location service. Success -> parselocation() function. Failure -> Fall back on default location prompt */
		navigator.geolocation.getCurrentPosition(parselocation, oldschoollocation);
		console.log( 'test lat ' + window.latitude );
	} else {
		/* call the same default function for users who don't want us to track them */
		oldschoollocation();
	}
}

function oldschoollocation() {
	/* Eventually move this to "oldschool location" function  */
//	$(this).closest('.column').addClass('viewlocation');
	window.maplink.closest('.column').addClass('viewlocation');
	
	console.log('reverting');
}

function parselocation(position) {
	console.log('variables declared');
	window.latitude = position.coords.latitude;
	window.longitude = position.coords.longitude;
	console.log( window.latitude );
	console.log( window.longitude );

	var query =	"http://maps.google.com/maps?saddr=" + window.latitude + '+' + window.longitude  + "&daddr=" + window.mapdest ; 
	window.location.href = query;
}





















