/* Copyright (c) 2009 WhiteInteractive
 *
 * Version: 4.0
 *
 * Requires: jQuery 1.3.1+, jQuery Cookie plugin
 */

(function($){

		  
	$.visualTweaks = function() {
		// ********************************************************************** 
		// Make any section or page specific visual tweaks that can't be achieved
		// with CSS alone.
		// **********************************************************************
		
		// Wrap the H1 and H2 Tags with a 'top' and 'bottom' graphic
		$('h1').before('<div class="h1Top"></div>');
		$('h1').after('<div class="h1Bottom"></div>');
		
	}
	
	$.fontSize = function() {
		// ********************************************************************** 
		// If the fontsize cookie is set then the site fontsize is increased
		// **********************************************************************
		
		if ($.cookie('fontsize') == "1") {
			$('body').css("font-size","1.2em");
		} else if ($.cookie('fontsize') == "2") {
			$('body').css("font-size","1.4em");
		}	
	}
	
	function saveFontSize(i) {
		// ********************************************************************** 
		// Saves the font size as a cookie or deletes as appropriate
		// **********************************************************************
		
		if (i == '1') {
			$.cookie('fontsize','1');
			$('body').css("font-size","1.1em");
		} else if (i == '2') {
			$.cookie('fontsize','2');
			$('body').css("font-size","1.2em");
		} else { 
			$.cookie('fontsize', null);
			$('body').css("font-size","1em");
		}
	}
	
	$.createFontSizeLinks = function() {
		// ********************************************************************** 
		// Attaches events to the font sizing links
		// **********************************************************************
		
		$('#fontSize0').bind('click',function(event) {
			saveFontSize('0');
		});
		
		$('#fontSize1').bind('click',function(event) {
			saveFontSize('1');
		});
		
		$('#fontSize2').bind('click',function(event) {
			saveFontSize('2');
		});
	}
	
	$.createChaletViewer = function() {
		// ********************************************************************** 
		// Attaches events to the thumbnails on the Chalet intro page
		// **********************************************************************
		
		$('#chalet1S').bind('click',function(event) {
			changeChaletPicture(1);
		});
		
		$('#chalet2S').bind('click',function(event) {
			changeChaletPicture(2);
		});
		
		$('#chalet3S').bind('click',function(event) {
			changeChaletPicture(3);
		});
		
		$('#chalet4S').bind('click',function(event) {
			changeChaletPicture(4);
		});
		
		$('#chalet5S').bind('click',function(event) {
			changeChaletPicture(5);
		});

	}
	
	function changeChaletPicture(i) {
		// ********************************************************************** 
		// Changes the main chalet image on the Chalet intro page
		// **********************************************************************
		
		$('#chaletPicture').html('<img src="../lib/pictures/my_pictures/chalet-' + i + 'L.jpg" alt="Alp shack chalet" width="627" height="407" />');
	}
	
	$.createGalleryViewer = function() {
		// ********************************************************************** 
		// Attaches events to the thumbnails and scroll arrows on the Gallery page
		// **********************************************************************
		
		$('#galleryScrollLeft').bind('click',function(event) {
			$('#galleryMidImage').css("left","-200px");
			scrollThumbsLeft();
		});
		
		$('#galleryScrollRight').bind('click',function(event) {
			$('#galleryMidImage').css("left","-200px");
			scrollThumbsRight();
		});
		
		$('.thumbnail').bind('click',function(event) {
			var objID = this.id;
			changeGalleryPicture(objID);
		});
		
		$('.thumbnail').bind('mouseover',function(event) {
			var objID = this.id;
			changeMidPicture(objID);
		});

	}
	
	function scrollThumbsLeft() {
		// ********************************************************************** 
		// Scrolls the gallery thumbs left
		// **********************************************************************
		
		var imgLeft;
		var paneOffset;
		
		paneOffset = $('#galleryThumbs').position();
		imgLeft = paneOffset.left;
		
		imgLeft = imgLeft + 118;
		if (imgLeft >= 31) {
			imgLeft = 31;
		}

		$('#galleryThumbs').css("left",String(imgLeft) + "px");
	}
	
	function scrollThumbsRight() {
		// ********************************************************************** 
		// Scrolls the gallery thumbs right
		// **********************************************************************
		
		var imgLeft;
		var paneOffset;
		
		paneOffset = $('#galleryThumbs').position();
		imgLeft = paneOffset.left;

		imgLeft = imgLeft - 118;
		if (imgLeft <= -913) {
			imgLeft = -913;
		}

		$('#galleryThumbs').css("left",String(imgLeft) + "px");
	}
	
	function changeGalleryPicture(objID) {
		// ********************************************************************** 
		// Changes the main gallery image
		// **********************************************************************
		
		var iNum;
		
		if (objID.length > 5) {
			if(objID.substring(0,5) == "thumb") {
				iNum = objID.substring(5);
			}
		}
		
		$('#galleryPicture').html('<img src="../lib/pictures/gallery/gallery' + iNum + '.jpg" alt="Alp shack gallery" width="627" height="407" />');
	}
	
	function changeMidPicture(objID) {
		// ********************************************************************** 
		// Changes the mid gallery image
		// **********************************************************************
		
		var iLeft;
		var iOffSet;
		var iNum;
		
		if (objID.length > 5) {
			if(objID.substring(0,5) == "thumb") {
				iNum = objID.substring(5);
			}
		}
		
		iLeft = (iNum-1) * 118;
		iLeft = iLeft - 13;
		iOffSet = $('#galleryThumbs').position();
		iLeft = iOffSet.left + iLeft;
		
		if (iNum == 1) {
			iLeft = iOffSet.left;	
		}
		
		if (iNum == 13) {
			iLeft = iOffSet.left + 1373;	
		}
		
		$('#galleryMidImage').css("left",String(iLeft) + "px");
		$('#galleryMidImage').html('<a id="raisedMidImage" href="#content"><img src="../lib/pictures/gallery/mid' + iNum + '.jpg" alt="" width="137" height="92" /></a>');
		
		$('#raisedMidImage').bind('click',function(event) {
			var objID = 'thumb' + iNum;
			changeGalleryPicture(objID);
		});
	}
	
	$.bookingFormSetup = function() {
		
		var strVal = "";

		strVal = $('#B_PartyCount').val();
		setNumberofPeople(parseInt(strVal));
		
		$('#B_PartyCount, select').change(function() {
			var i = $('#B_PartyCount').val();
			setNumberofPeople(i);
		});
		
		$('#B_ArrivalDate, select').change(function() {
			var i = $('#B_ArrivalDate').val();
			setPrice(i,0);
		});
		
		$('#B_Offer').bind('blur',function(event) {
			var strCode = $('#B_Offer').val();
			var i = $('#B_ArrivalDate').val();
			strCode = strCode.toLowerCase();
			
			if (strCode == 'ride09') {
				var currentTime = new Date();
				var month = currentTime.getMonth() + 1;
				var day = currentTime.getDate();
				var year = currentTime.getFullYear();

				if (year <= 2009 && month <= 12 && day <= 31) {
					setPrice(i,15);
					$('#offerMsg').html('15% discount applied');
				} else {
					setPrice(i,0);
					$('#offerMsg').html('Code is no longer valid');
				}
			} else if(strCode.length>0) {
				$('#offerMsg').html('Code is not valid');
				setPrice(i,0);
			} else {
				$('#offerMsg').html('');
				setPrice(i,0);
			}
		});
	}
	
	function setPrice(dt,discount) {
	
		var strData = $('#priceData').val();
		var dataArray = strData.split(",");
		var strValues;
		var valuesArray;
		var intCount = 0;
		var intVal = 0;
		
		while (intCount < dataArray.length) {
			strValues = dataArray[intCount];
			valuesArray = strValues.split("|");
			if (valuesArray[0] == dt) {
				intVal = parseInt(valuesArray[1]);
				if (discount > 0) {
					intVal = intVal - ((intVal/100) * discount);
				}
				$('#priceLabel').html('<span id="lblPrice">&pound;' + CurrencyFormatted(intVal) + '</span>');	
			}
			intCount = intCount + 1;
		}
	}
	
	function setNumberofPeople(intNum) {
		
		var MAXPEOPLE = 18;
		var intCount = 1;

		while (intCount < MAXPEOPLE) {
			if (intCount > 2){
				if (intCount > intNum) {
					$('#rop' + intCount).hide();
				} else {
					$('#rop' + intCount).show();
				}
			}
			intCount = intCount + 1;
		}
	}
	
	function CurrencyFormatted(amount) {
		
		var i = parseFloat(amount);
		if(isNaN(i)) { i = 0.00; }
		var minus = '';
		if(i < 0) { minus = '-'; }
		i = Math.abs(i);
		i = parseInt((i + .005) * 100);
		i = i / 100;
		s = new String(i);
		if(s.indexOf('.') < 0) { s += '.00'; }
		if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
		s = minus + s;
		return s;
	}

	
	
})(jQuery);