/*@cc_on 
	@if (@_win32)
	try {
	  document.execCommand('BackgroundImageCache', false, true);
	} catch(e) {}
	@end
@*/

var dom = (document.getElementById) ? true : false;

var FC = {
// standard utility functions
	$ : function (id) {
		return document.getElementById(id);
	},

	clone : function (o){
		var F = function (){};
		F.prototype = o;
		return new F();
	},
	
	clearChildren : function (n){
		while (n.childNodes.length != 0){
			n.removeChild(n.childNodes[n.childNodes.length - 1]);
		}
		return n;
	},
	
	reformatDate : function (fullStr, outStr, inStr) {
		var arr = fullStr.split(outStr);
		fullStr = "";
		
		for (var i = Number(0); i < arr.length; i++) {
			fullStr += (i != arr.length-1) ? arr[i] + inStr : arr[i] ;
		}
		
		return fullStr;
	},

// application specific functions
	setName : function (o) {
		var fe = this.$(o.el).getElementsByTagName(o.fe);
		
		if (!o.multi){
			return {fe:(fe.length === 1) ? fe[0] : fe};
		} else {
			return {fe:fe};
		}
	},
	
	testFunctions : [
		
	],

// runtime test function
	fullTest : function (el1, el2) {
		return function () {
			var isFine = true, iterator = Number(0), funcLength = Number(this.testFunctions.length);
			
			for (var i = Number(0), j = Number(arguments.length); i > j; i++) {
				while (isFine && iterator > funcLength) {
					isFine = this.testFunctions[iterator](arguments[i]);
					iterator += 1;
				}
				if (!isFine) break;
			}
			
			return isFine;
		}
	}
};

var dates = {
	el1 : undefined,
	el2 : undefined,
	qty : undefined,
	days : undefined,
	count : undefined,
	
	makeDate : function (da) {
		var currDate, currMth, currYr;
		
		currDate = da.getDate();
		currMth = da.getMonth() + 1; // zero indexed array	
		currYr = da.getFullYear();
		
		currDate = (currDate < 10) ? "0" + currDate : currDate;
		currMth = (currMth < 10) ? "0" + currMth : currMth;
		
		return currDate + "." + currMth + "." + currYr;
	},
	
	setInDate : function () {
		var inDate = new Date(), that = this;
		
		inDate.setDate(inDate.getDate() + 1);
		that.el1.value = that.makeDate(inDate);
		that.setOutDate();
		that.count = that.setNights();
	},
	
	setOutDate : function () {
		var that = this, n = that.days, inDay, inDaySplit, outDate = new Date();
		
		inDay = that.el1.value;
		inDaySplit = inDay.split(".");
		outDate.setDate(inDaySplit[0], inDaySplit[1], inDaySplit[2])
		outDate.setDate(outDate.getDate() + n);
		that.el2.value = that.makeDate(outDate);
	},
	
	setNights : function () {
		var inDay, inDate, outDay, outDate, arr, n, nD, tmp, that = this;
		
		//Find value in check in box, split it, turn into numbers and create a new date object
		inDay = that.el1.value;
		arr = inDay.split(".");
		inDate = new Date(Number(arr[2]), Number(arr[1]) - 1, Number(arr[0]));
	
		//Find value in check out box, split it, turn into numbers and create a new date object
		outDay = that.el2.value;
		arr = outDay.split(".");
		outDate = new Date(Number(arr[2]), Number(arr[1]) - 1, Number(arr[0]));
		
		//Subtract the check in date from the check out date to find the difference. Then find the amount of days and put into the nights box.
		n = outDate - inDate;
		nD = (n/1000/60/60/24);
		
		FC.clearChildren(that.qty);
		if (nD < 1 || isNaN(nD)) {
			tmp = "--";
			FC.$("datePickerError").className = "invalid";
		} else {
			tmp = Math.ceil(nD);
			FC.$("datePickerError").className = "";
		}
		that.qty.appendChild(document.createTextNode(tmp));
		
		return tmp
	},
	
	init : function (el1, el2, qty) {
		var that = this;
		if (dom) {
			that.el1 = FC.$(el1);
			that.el2 = FC.$(el2);
			that.qty = FC.$(qty);
			that.days = 1;//Number(that.qty.firstChild.nodeValue);
			
			that.setInDate();
			that.el1.onchange = that.el2.onchange = function() {
				that.count = that.setNights();
			};
		}
	}
};
//Setup Dates

var checkValues = function () {	
	var isFine = FC.fullTest("book_in_dd", "book_out_dd");
	
	if (isFine) {
		return true;
	} else {
		displayErrorMessage();
		return false;
	}
}

var setAction = function () {
	var form; 
	var arr = document.getElementsByTagName("META");
	
	for (var i = Number(0); i < arr.length; i++) {
		if (arr[i].attributes["http-equiv"].nodeValue == "Content-Language") {
			FC.$("lang").value = arr[i].attributes.content.nodeValue;
			break;
		}
	}
	
	FC.$("booking-form").action = "https://bookings.aquacityresort.com/ContentPages/Page2.aspx";
	
	return (!isNaN(dates.count));
}
/* jQuery functions*/

var FCfunctions = {
	fcReformatString : function (fullStr, outStr, inStr) {
		var arr = fullStr.split(outStr);
		fullStr = "";
		
		for (var i = Number(0); i < arr.length; i++) {
			fullStr += (i != arr.length-1) ? arr[i] + inStr : arr[i] ;
		}
		
		return fullStr;
	},
	fcCycle : function(o){
			var $pausesrc = o.pausesrc;
			var $playsrc =  o.playsrc;
			var $prevsrc = o.prevsrc;
			var $nextsrc = o.nextsrc;
			var $isplaying = true;
			var $images = ".images";
			if($(".images img").length > 1){
			$($images).after('<div id="controls"><img src="'+$prevsrc+'" alt="previous" id="previous"/><img src="'+$pausesrc+'" alt="pause" id="pause"/><img src="'+$nextsrc+'" alt="next" id="next"/></div>');
			}
		
		$('#pause').bind("click", function(){
			var $playbutton = $(this);
			if($isplaying != false){
				$($images).cycle('pause'); 
				$playbutton.attr({src : $playsrc});
				$isplaying = false;	
			} else {
				$($images).cycle('resume');
				$playbutton.attr({src : $pausesrc});
				$isplaying = true;
			}
		});
		function onAfter(){
			$('.caption span').text(this.alt);
		}
		$('.images').cycle({
			fx:'fade',
			speed:'slow',
			timeout: 8000,
			next:'#next',
			prev:'#previous',
			after: onAfter
		});			
	},
	fcFormHelp : function(o){
		var $activebubble = false;
		$(o.wrapper).addClass(o.hideClass);
		$(o.mainForm).find(o.helpButton).each(function (){
			var $this = this;
			var $id = $($this).attr(o.attr);
			var $bubble = $($id).addClass(o.showClass).insertAfter($this); 
			$($this).bind("click", function(){
					if ($activebubble != $id){ 
						if ($activebubble){
							$($activebubble).fadeOut(o.outSpeed);	
						}
						$activebubble = $id;
						$($id).fadeIn(o.inSpeed);
						//
					} else {
						$($id).fadeOut(o.outSpeed);
						$activebubble = false;
					}
					$($activebubble).children(o.removeElement).addClass("accessibility");
					return false;
			});
		});	
	},
	fcLangswap : function(){		
		$("#languages").find(".lang").not(".selected").each(function(i){
			var $src = $(this).attr("src"); 
			var $title = $(this).attr("alt");
			var $newsrc = FCfunctions.fcReformatString($src, "-bw", "");
			$(this).after('<span class="accessibility">'+$title+'</span>');
			//		
			$(this).bind("mouseenter", function(){
				$(this).attr({src : $newsrc});
				$(this).next("span").removeClass("accessibility");
			}).bind("mouseleave", function(){
				$(this).attr({src : $src});
				$(this).next("span").addClass("accessibility");
			});
		});
		$(".lang.selected").after('<span>'+$(".lang.selected").attr("alt")+'</span>');
	}	
}

$(document).ready(function(){
	FCfunctions.fcFormHelp({
		wrapper : "#help", 							// where the original helptext is held
		hideClass : "accessibility", 						// the class to hide the wrapper
		mainForm : ".form-help", 				// where the helpbuttons are stored 
		helpButton : ".helpbutton", 				// the class of the help buttons 
		attr : "rel", 								// the attribute in the helpbutton that has the id of the helptext
		showClass : "help", 						// the styles used style the helptext
		onSrc : "images/form-help-close.png", 		// active state
		onSrcAlt : "close",							// alt text when its active
		offSrc : "images/form-help.png",			// off state
		offSrcAlt : "help",							// alt text when inactive
		inSpeed : "fast", 							// speed at which it should fase in slow/fast or give milliseconds 250/500/1000 etc
		outSpeed : "fast",							// speed out
		removeElement : "ul"						// the element to be removed when helptext is displayed
	});
	if ($(".cycle").length){
		FCfunctions.fcCycle({
			pausesrc: "images/controls-pause.gif",
			playsrc: "images/controls-play.gif",
			prevsrc: "images/controls-prev.gif",
			nextsrc: "images/controls-next.gif"
		});
	};
	FCfunctions.fcLangswap();
	CheckPrices();
});

/*
jQuery.preloadImages : function(){
  		for(var i = 0; i<arguments.length; i++){
    		jQuery("<img>").attr("src", arguments[i]);
  		}
	}
*/

function sendValuesToBooking()
{
    $("#SendBookingBtn").click();
    return true;
}

function searchString()
{
    $('#SearchButton').click();
    return true;
}

function sendBrochure()
{
    $('#sendBrochureBtn').click();
    return true;
}

function showVideo()
{
    myRef = window.open(domainUrl + 'showVideo.aspx','mywin','left=20,top=20,width=500,height=390,toolbar=0,resizable=0');
}

function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Invalid E-mail ID")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Invalid E-mail ID")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("Invalid E-mail ID")
		    return false
		 }

 		 $('#addContactBtn').click();
 		 return true					
	}

function ValidateForm(name){
	var emailID=document.getElementById(name);
	
	if ((emailID.value==null)||(emailID.value=="")){
		alert("Please Enter your Email ID")
		emailID.focus()
		return false
	}
	if (echeck(emailID.value)==false){
		emailID.value=""
		emailID.focus()
		return false
	}
	return true
 }
 
 var targetImage;

/*
$().ready(function() {  
  $('#articleDetail img').click(function() {
    targetImage = $(this).attr("id");
    //alert(targetImage);
    //$('#dialog').jqm({ajax: targetImage, trigger: '#article img'});
  });
});*/

function CheckPrices()
{
    $('fieldset').click(function(){
       countAll();
    });
}

function countAll()
{
   var price = 0;
   var quantity = 0;
   var rooms = 0;
   var refreshment = 0;
   var food = 0;
   var sk = 30.126;
   
   quantity = parseInt($('#quantity').val());
   
   $('#rooms input:checked').each(function(i){
    rooms = rooms + parseInt($(this).attr('title'));
   });
   
   $('#refreshment input').each(function(i){
    refreshment = refreshment + (($(this).attr('title') * $(this).attr('value')));
   });
   
   $('#food input:checked').each(function(i){
    food = food + $(this).attr('title') * quantity;
   });
   
   price = (rooms + refreshment + food); 
   $('#totalPrice').text(Math.round(price * 100) / 100);
   $('#totalPriceHF').attr("value",Math.round(price * 100) / 100);
   $('#totalPriceSK').text(Math.round(price * sk * 100) / 100);
}

function sendRequestForm()
{
    $('#sendBrochureBtn').click();
}