
function pad(numericValue, targetDigitCount, leading){
	var stringValue = numericValue + '';

	while(stringValue.length<targetDigitCount){
		if(leading){
			stringValue = '0' + stringValue;
		}else{
			stringValue += '0';
		}
	}
	return stringValue;
}

function formatNumber(rawValue){
	var formattedValue = rawValue;
	var pennies = 0;
	
	if(rawValue >= 1){
		pennies = (rawValue * 100) % 100;
	}
	if(rawValue > 1000){
		/* insert a comma appropriately */
		var hundreds = (rawValue % 1000);
		var thousands = (rawValue - hundreds)/1000;
		hundreds -= (pennies/100);
		
		formattedValue = 	thousands + ',' + pad(hundreds, 3, true);
		if(pennies > 10){
			formattedValue += '.' +	pad(pennies, 2, false);
		}else if(pennies > 0){
			formattedValue += '.' +	pad(pennies, 2, true);
		}
	}

	return formattedValue;
}

function toggleSlideXY(target, delay){
	var action='show';
	if(target.is(':visible')){
		action='hide';
	}
	target.animate({height: action,width: action}, delay);

	return false;
}

var pathToRoot = -1;
function getRoot(){
	if(pathToRoot == -1){
		// cache this value
		pathToRoot = '';
		if(location.href.substr(0, 5) == 'file:'){ // only need to fudge it in local mode
			var key = 'public_html/';
			var pathFromRootPosition = location.href.indexOf(key, 0) + key.length;
			var pathFromRoot = location.href.substr(pathFromRootPosition, 999);
			var depth = pathFromRoot.split('/').length;
			
			for(i=depth; i>1; i-- ){
				pathToRoot += '../';
			}
			pathToRoot += '.';
		}
	}
	return pathToRoot;
}

function initialiseDynamicForms(){
	// download the dynamic forms etc after the rest of the page has finished loading, and setup the click handlers
	$('#cmb').load(getRoot() + '/dynamic/cmb-form.html',
				   	function(){
						
						$('#cmb [src]').each(function(){$(this).attr('src',getRoot() + $(this).attr('src'));});
						$('#cmb-link').click(function(){
							return toggleSlideXY($('#cmb'), 300);
							});						
						}
					);

 
}

function updateProductGroupPrices(){
	$('.product-group .price a em').each(function(index){
												  $(this).html('&pound;' + prices[index][(vatIncluded ? 1 : 0)]);
										});
}

function toggleVatStatus(){
	vatIncluded = !vatIncluded;
	if($('#configurator').length){
		updateActiveProduct(activeProduct);
	}else{
		updateProductGroupPrices();	
	}
	$("a.vat-status").each(function(){$(this).html((vatIncluded ? 'inc VAT' : 'ex VAT'))});
	$.cookie('vat_included', vatIncluded ? '1' : '0', {domain: '.greenretreats.co.uk', path: '/', expires: 1000});
}


$(document).ready(function(){
   initialiseDynamicForms();
   /* setup shiny tooltips */
	/* insert additional markup for rounded corners */
	$(".tooltip").wrapInner('<div class="content"></div>');
	$(".tooltip").prepend('<div class="top"></div>').append('<div class="bottom"></div>');
	/* hook in the shiny tooltips */
	$(".information").tooltip({ relative: true,position:'bottom right'});

	/*  */
	$("a.vat-status").click(function(){
				toggleVatStatus();
				return false;
			});
	// suckerfish:
	$('.popup-menu', '#header #main-nav').each(function(index){
			$(this).parent('li').mouseenter(function(){
					$(this).addClass('sfhover');
				});
			$(this).parent('li').mouseleave(function(){
					$(this).removeClass('sfhover');
				});
		});
	
	/* hook in the video player */
	$("#play-video").fancybox({
		'transitionIn'	:	'elastic',
		'transitionOut'	:	'elastic',
		'speedIn'		:	200, 
		'speedOut'		:	200,
		'overlayColor' : '#240016',
		'overlayOpacity' : 0.8,

		'scrolling': false,
		'titleShow': false,
		'padding'  : 0,
		'onStart'  : function() {
			$("#player").show();
			$f("player", {src: "http://s3.amazonaws.com/gr-promo-video/flowplayer-3.2.5.swf", wmode: 'opaque', cachebusting: false}, { 
				clip: { 
					url: 'mp4:greenretreats.f4v',
					provider: 'rtmp'
				}, 
				plugins: {  
					controls: {
						backgroundGradient: 'none',
						backgroundColor: 'transparent'
					},
					rtmp: {  
						url: 'http://s3.amazonaws.com/gr-promo-video/flowplayer.rtmp-3.2.3.swf',			
						netConnectionUrl: 'rtmp://s48mz9l8zw732.cloudfront.net/cfx/st'  
					}
				}
			});
		},
		'onClosed'  : function() {
			try{
				$("#player").hide();
				$f().unload();
			}catch(e){
				// IE8 sucks	
			}
		}
	});

 });

/* image preloader: */
$.fn.preload = function() {
    this.each(function(){
        $('<img/>')[0].src = this;
    });
}

$(['/images/button-request-brochure-active.png','/images/button-site-survey-active.png','/images/button-visit-us-active.png']).preload();
