var loop = null;

$(document).ready(function() {
	// DEFAULTS FOR DATEPICKER:
	$.datepicker.setDefaults({
		showOn: 'both',
		showAnim: 'fadeIn',
		dateFormat: 'yy-mm-dd',
		minDate: new Date(),
		maxDate: DateAdd(new Date(), 410),
		buttonImageOnly: true,
		buttonImage: '/css/img/icon-calendar.gif',
		buttonText: 'Calendar'
	});

	// CHECK IN DATEPICKER:
	$('#qs_datepicker_from').datepicker({
		onSelect: function(dateText, inst) { checkInChanged(dateText, inst); }
	});

	// CHECK OUT DATEPICKER:
	$('#qs_datepicker_to').datepicker({
		onSelect: function(dateText, inst) { checkOutChanged(dateText, inst); }
	});

	// BOXES ON FRONT PAGE:
	$('.fp-boxes .left').each(function() {
	    var leftHeight = $(this).height();
	    var rightHeight = $($(this).next()[0]).height();

        if ($($(this).next()).length) {
      	    if (leftHeight > rightHeight) {
    			$($(this).next()[0]).height(leftHeight);
    		} else if (leftHeight < rightHeight) {
                $(this).height(rightHeight);
    		}
		}
	});

	// QUICK SEARCH SWITCHER:
	$('ol.switchers li input').click(function() {
		$('ol.switchers li').removeClass('on');
		$(this).parent().addClass('on');
		$(this).blur();
		
		odin.qs.settings.searchType = $(this).attr('stype');
		
		switch (odin.qs.settings.searchType) {
			case 'Hotels': odin.qs.settings.urls = 'Hotels|Passengers|Payment|Receipt'; break;
			case 'Services': odin.qs.settings.urls = 'Services|Passengers|Cars|Payment|Receipt'; break;
			case 'Packages': odin.qs.settings.urls = 'Package|Passengers|Payment|Receipt'; break;
			case 'Cars': odin.qs.settings.urls = 'Cars|Passengers|Services|Payment|Receipt'; break;
		}
		
		QuickSearchContent($(this).attr('stype'));
	});

	$('#qs_packagecategory_id').change(function() {
		odin.loadPackagesIntoSelect(odin.qs.settings.owner, 'qs_package_id', $(this).val(), 'Select Tour', 'No tours of this type', 'en-GB');
	});

	$('#qs_package_id').change(function() {
		odin.loadPackageDates('Select Date', 'dd.MM.yyyy');
	});
});

LoopDaLoop = function(settings) {
	this.init($.extend( {
		container: null,
		items: null,
		useDots: false,
		dotsContainer: null,
		interval: 2,
		onLoad: function() {},
		onSwitchStart: function() {},
		onSwitchEnd: function() {}
	}, settings));
}

$.extend(LoopDaLoop.prototype, {
	init: function(se) {
	    this.settings = se;
	    this.looping = false;
	    this.current = 0;
	    this.count = 0;
	    this.canLoad = false;
	    this.timer = null;
		this.pause = this.settings.interval * 1000;

		if (this.settings.items != null && this.settings.items.length > 0) {
			this.count = this.settings.items.length;
			this.canLoad = true;
			var d = this.settings.useDots;
			var c = this.settings.dotsContainer;

			$(this.settings.items).each(function(indx, item) {
			    if (indx == 0) {
					$(item).attr('ldl_index', indx);
				} else {
					$(item).attr('ldl_index', indx).hide();
				}
				
				if (d) {
					if (indx == 0) {
						$('<li></li>').html('1').addClass('on').appendTo($('#' + c + ' ol'));
					} else {
						$('<li></li>').html(indx + 1).appendTo($('#' + c + ' ol'));
					}
				}
			});
		}
		
		if (this.canLoad) {
			$('#' + this.settings.container).attr('ldl_count', this.count).attr('ldl_current', '0');
			
		    this.settings.onLoad();
			this.start();
		}
	},
	start: function() {
		this.timer = setInterval(function() {
			loop.next();
		}, this.pause);
	},
	stop: function() {

	},
	next: function() {
		$(this.settings.items).hide();

		if (this.current == (this.count - 1)) {
			this.current = 0;
		} else {
			this.current++;
		}

		this.show();
	},
	prev: function() {
		$(this.settings.items).hide();

		if (this.current == 0) {
			this.current = this.count;
		} else {
			this.current--;
		}

		this.show();
	},
	show: function() {
		this.settings.onSwitchStart();

		$(this.settings.items).eq(this.current).show();
		
		if (this.settings.useDots) {
			$('#' + this.settings.dotsContainer + ' ol li').removeClass('on');
			$('#' + this.settings.dotsContainer + ' ol li:eq(' + this.current + ')').addClass('on');
		}

		this.settings.onSwitchEnd();
	}
});

function LoadQuickSearchOnAllPages(ownerInfo, targetUrl) {
	var setUrls = 'Hotels|Passengers|Payment|Receipt';
	var setSearchType = 'Hotels';

	if (odin.hasValidCookie) {
	    setSearchType = odin.bookingData.get('search_type');
		$('ol.switchers li').removeClass('on');
		$('ol.switchers li input').removeAttr('selected');
		$('ol.switchers li input[stype="' + setSearchType + '"]').attr('checked','checked').parent().addClass('on');

		switch (odin.bookingData.get('search_type')) {
			case 'Hotels': setUrls = 'Hotels|Passengers|Payment|Receipt'; break;
			case 'Services': setUrls = 'Services|Passengers|Cars|Payment|Receipt'; break;
			case 'Packages': setUrls = 'Package|Passengers|Payment|Receipt'; break;
			case 'Cars': setUrls = 'Cars|Passengers|Services|Payment|Receipt'; break;
		}
	}

    odin.initSearch({
		start: targetUrl,
		owner: ownerInfo,
		urls: setUrls,
		searchType: setSearchType
	});

	$('#qs_searchbtn').click(function() {
		odin.qs.doSearch();
		return false;
	});

	QuickSearchContent(setSearchType);

	if (setSearchType == 'Packages') {
		odin.loadPackagesIntoSelect(odin.qs.settings.owner, 'qs_package_id', odin.bookingData.get('package_category_id'), 'Select Tour', 'No tours of this type', 'en-GB');
	}
}

function QuickSearchContent(searchType) {
	if (searchType == 'Packages') {
		$('.qs .all').hide();
		$('.qs #package_selectors').show();
		$('#qs_searchbtn').val('Go');
	} else {
		$('.qs .all').show();
		$('.qs #package_selectors').hide();
		$('#qs_searchbtn').val('Search');
	}
}

function checkInChanged(dateText, inst) {
	var checkInDate = $('#qs_datepicker_from').datepicker('getDate');
	var checkOutDate = $('#qs_datepicker_to').datepicker('getDate');

	if (checkInDate >= checkOutDate) {
		var newCheckOutDate = new Date(inst.currentYear, inst.currentMonth, inst.currentDay);
		newCheckOutDate.setDate(newCheckOutDate.getDate() + 1);
		$('#qs_datepicker_to').datepicker('setDate', newCheckOutDate)
	}
}

function checkOutChanged(dateText, inst) {
	var checkInDate = $('#qs_datepicker_from').datepicker('getDate');
	var checkOutDate = $('#qs_datepicker_to').datepicker('getDate');

	if (checkInDate >= checkOutDate) {
		var newCheckInDate = new Date(inst.currentYear, inst.currentMonth, inst.currentDay);
		newCheckInDate.setDate(newCheckInDate.getDate() - 1);
		$('#qs_datepicker_from').datepicker('setDate', newCheckInDate)
	}
}

function checkPriceQuoteInChanged(dateText, inst) {
	var checkInDate = $('#cp_datepicker_from').datepicker('getDate');
	var checkOutDate = $('#cp_datepicker_to').datepicker('getDate');

	if (checkInDate >= checkOutDate) {
		var newCheckOutDate = new Date(inst.currentYear, inst.currentMonth, inst.currentDay);
		newCheckOutDate.setDate(newCheckOutDate.getDate() + 1);
		$('#cp_datepicker_to').datepicker('setDate', newCheckOutDate)
	}
}

function checkPriceQuoteOutChanged(dateText, inst) {
	var checkInDate = $('#cp_datepicker_from').datepicker('getDate');
	var checkOutDate = $('#cp_datepicker_to').datepicker('getDate');

	if (checkInDate >= checkOutDate) {
		var newCheckInDate = new Date(inst.currentYear, inst.currentMonth, inst.currentDay);
		newCheckInDate.setDate(newCheckInDate.getDate() - 1);
		$('#cp_datepicker_from').datepicker('setDate', newCheckInDate)
	}
}

function DateAdd(date, daysToAdd) {
	var returnDate = date;
	returnDate.setDate(returnDate.getDate() + daysToAdd);
	return returnDate;
}

 curN = new Array(5);
curN[0] = 'ISK';
curN[1] = 'GBP';
curN[2] = 'USD';
curN[3] = 'DKK';
curN[4] = 'EUR';

var Isk_total = 1.0;
var Isk_total_old = 0.0;

function setIsk(IdArr) {
	Isk_total_old=Isk_total;
	Isk_total = Math.round((Number(correctin($('#' + curN[IdArr]).val())) * cur[IdArr])*100)/100;
	if((''+Isk_total) == "NaN") {
		Isk_total = Isk_total_old;
	}
	updateValues();
}
function updateValues() {
	for(i=0; i<=4; i++) {
		$('#' + curN[i]).val(correct((Math.round((Isk_total/cur[i])*100))/100));
	}
}

function correct(inpvalue) {
	var fixvalue = '' + inpvalue;
	var fixvaluetemp = "";

	if(fixvalue.indexOf(".") > -1) {
		var fixvaluearr = fixvalue.split(".");

		for(var i=fixvaluearr[0].length; i > 0; i--) {
			if(i%3 == 0 && i != fixvaluearr[0].length) {
				fixvaluetemp += ".";
			}
			fixvaluetemp += fixvaluearr[0].charAt((fixvaluearr[0].length-i));
		}
		oldvalue=fixvalue;
		fixvalue=fixvaluetemp + "," + fixvaluearr[1];
	}
	else {
		for(var i=fixvalue.length; i > 0; i--) {
			if(i%3 == 0 && i != fixvalue.length) {
				fixvaluetemp += ".";
			}
			fixvaluetemp += fixvalue.charAt((fixvalue.length-i));
		}

		fixvalue=fixvaluetemp;
		oldvalue=fixvalue;
	}

	return fixvalue;
}

function correctin(inpvalue) {
	var fixvalue = '' + inpvalue;
	fixvalue = fixvalue.split(".").join("");
	fixvalue = fixvalue.split(",").join(".");
	return fixvalue;
}
