/*

Noesis Internet Solutions Ltd. Copyright © 2008

$Version: 09/04/2008


REQUIRES

jquery (http://jquery.com/)
*/

// var $j = jQuery.noConflict();


$(document).ready(function(){

		
		
			// Pop-Up Window for maps and note
					$(function() {
			
									$('.dispDetailMenu a[rel="nofollow"], a.loadPop').click(
											function(event){ 
													event.preventDefault();
													var href = $(this).attr('href');
													 var l = (screen.availWidth - 640) / 2;
													 var t = (screen.availHeight - 480) / 2;
													newwindow = window.open(href,'newwindow','scrollbars=1,width=640,height=480,left=' + l + ',top=' + t + ';');
													newwindow.focus();
												});
														
						});			
							
							
							
		// Load Calendar from list.lasso append relativ to link ////////////////////////////////////////////////////
							$(function(){	
													
									// Configurator			
									xOffset = 10;
									yOffset = 30;
									spinner = '/travelmate/images/loading.gif';
									
									// Box creator
									var showBox = function(e){
											var type = $(this).attr('class');
											var rel = $(this).attr('href');
											
											rel.replace(/window.location.host/, '');
											$('#loadBox').length ? $('#loadBox').remove() : $('body').append('<div id="loadBox"></div>');
											$('#loadBox').append('<div id="loaderIcon"><img src="' + spinner + '" /></div>');

											loader(rel,type,e.pageX,e.pageY);
											
											// unbind to enable click in window
											$(this).click(
															function(event){ 
																			event.preventDefault();
																			$(this).unbind('mouseout')
																			}
																	)
									}
									
										var rePosition = function(x,y){
									
												/*
												var h_win = $(window).height();
												var h_box = $('#loadBox').height();
												
												if((y + h_box) > h_win)
													y = h_win - h_box;
												*/
												$('#loadBox')
												.css('top',( y - xOffset) + 'px')
												.css('left',(x + yOffset) + 'px')
												.css({ 'border':'solid 1px black' })
												.fadeIn('fast');	
											}
									
										// Loader for box content
										var loader = function(rel,type,x,y) {
										
												
												switch(type)
													{
														case 'loadPage':
														
																 $('#loadBox').load(
																	rel,
																		function(){ 
																					rePosition(x,y);
																					// $('#loaderIcon').remove();
																				}
																	);
														  break;    
														case 'loadImage':
															$('#loaderIcon').remove();
															$('#loadBox').append('<img src="' + rel + '" \/>');
															rePosition(x,y);
														  break;
														default:
														 // noting
													}
											}
								
									// mouse event
									$('a.loadPage, a.loadImage').mouseout(function(){
																	$('#loadBox').css({ 'border':'none' })
																	$('#loadBox').remove();	
																 }).mouseover(showBox);

							});
							
				// open Dialog  ////////////////////////////////////////////////////////////////////////////////////////////////////////
				
				$('a.loadDialog').click( function(event){ event.preventDefault(); });
				$('a.loadDialog').click(function(){
				
						var spinner = '/travelmate/images/loading.gif';
						var href = $(this).attr('href');
						var title = $(this).attr('title');
						
						href.replace(/window.location.host/, '');
						$('#loadBox').length ? $('#loadBox').remove() : $('body').append('<div id="loadBox"></div>');
						$('#loadBox').append('<div id="loaderIcon"><img src="' + spinner + '" /></div>');
						
						$('#loadBox').load(
											href,
											function(){
														$('#loadBox').dialog({ 
																			closeOnEscape: true,
																			modal: true,
																			overlay: { opacity: 0.5, background: 'black' },
																			height: 520,
																			width: 580,
																			title: title
																			});
														$('#loadBox')
															.css({ 'overflow':'auto' })
															.css({ 'padding':'5px' });	
														
														$('div.ui-dialog-overlay').click(function(){ 
																	$('#loadBox').dialog("destroy");
																	$('#loadBox').remove();
																	});
																	
														}
											);
						
				});
				
				
				
				// Show hide message detail.lasso, reservation.lasso, avail.lasso ////////////////////////////////////////////////////
					$(function() {
								
								$('.tmMessageLink a').click( function(event){ event.preventDefault(); });
								
								$('.tmMessageLink a').click(function() {
								
									var href = $(this).attr('href');
									href.replace(/window.location.host/, '');
									
								if($('.tmHelpText').length > 0) {
									$('#tmMessage').hide();
									$('.tmHelpText').remove();
									} 
								else{
									 $('#tmMessage').load( href, function(){ $('#tmMessage').show(); } );
									}
								
								});
													
					});			
							
					// Show hide calendar detail.lasso, reservation.lasso, avail.lasso ////////////////////////////////////////////////////
					
					$(function() {
								
										$('.tmCalendarLink a').click( function(event){ event.preventDefault(); });
										
										$('.tmCalendarLink a').click(function() {
										
											var href = $(this).attr('href');
											href.replace(/window.location.host/, '');
											
										if($('#tmCalendar').length >= 1) {
											$('#tmMessage').hide();
											$('#tmCalendar').remove();
											} 
										else{
											 $('#tmMessage').load( href, function(){ $('#tmMessage').show(); } );
											}
										});
															
							});			
					
					// Light Box	 detail.lasso ////////////////////////////////////////////////////
					
					if(typeof $('#tmGallery a').lightBox == 'function') 
					$(function() {
									$('#tmGallery a').lightBox({
														overlayBgColor: '#000000',
														overlayOpacity: 0.6,
														imageLoading: '/travelmate/images/lightbox/loading.gif',
														imageBtnClose: '/travelmate/images/lightbox/close.gif',
														imageBtnPrev: '/travelmate/images/lightbox/prev.gif',
														imageBtnNext: '/travelmate/images/lightbox/next.gif',
														containerResizeSpeed: 350,
														txtImage: 'Image',
														txtOf: 'de'
														});
								});
								
						
					// Date Picker ////////////////////////////////////////////////////
					$(function() {
								$.datepicker.setDefaults($.datepicker.regional['']);
							});
				
				
								departMin = 0;
								// Arrival Pullowns
								// must english date format for pulldown callback
								$('#linkedArrival').datepicker($.extend({}, 
																$.datepicker.regional[langLabels],
																	{ 	defaultDate: "+1w", 
																		minDate: 0,
																		maxDate: 365,
																		beforeShow: readLinked,
																		onSelect: updateSelects,
																		showOn: 'both',
																		buttonImage: '/travelmate/images/datepicker/calendar.gif',
																		buttonImageOnly: true,
																		dateFormat: 'mm/dd/yy'
																	}
																	));
															
								// // $('#linkedArrival').datepicker.val(new Date());
								
								// Prepare to show a date picker linked to three select controls 
									function readLinked() { 
										 $('#linkedArrival').val($('#arrivalM').val() + '/' + 
											  $('#arrivalD').val() + '/' + $('#arrivalY').val()); 
										 return {}; 
									} 
									 
									// Update three select controls to match a date picker selection 
									function updateSelects(date) { 
										
										 $('#arrivalM').val(date.substring(0, 2)); 
										 $('#arrivalD').val(date.substring(3, 5)); 
										 $('#arrivalY').val(date.substring(6, 10));
										 
										 // update day select for depart
										d = date.substring(3, 5);
										d =  d.replace(/(0)(\d{1})/, '$2');
										
										if (d < 8){ 
											d = Number(d) + Number(2);
											d = 0 + String(d);
										} 
										else if ( d < 29){
											d = Number(d) + Number(2);
										}
										
										 $('#departM').val(date.substring(0, 2)); 
										 $('#departD').val(d); 
										 $('#departY').val(date.substring(6, 10));
										 
									} 
									 
									// Prevent selection of invalid dates through the select controls 
									function checkLinkedDays() { 
										 var daysInMonth = 32 - new Date($('#arrivalY').val(), 
											$('#arrivalM').val() - 1, 32).getDate(); 
											$('#arrivalD option').attr('disabled', ''); 
											$('#arrivalD option:gt(' + (daysInMonth - 1) +')').attr('disabled', 'disabled'); 
										 if ($('#arrivalD').val() > daysInMonth) { 
											  $('#arrivalD').val(daysInMonth); 
										 } 
									} 
									
									// Depart Pullowns //////////////////////////////////
									$('#linkedDepart').datepicker($.extend({}, 
														$.datepicker.regional[langLabels],
															{ 
																maxDate: 365,
																beforeShow: updateBefore,
																onSelect: updateDepart,
																showOn: 'both',
																buttonImage: '/travelmate/images/datepicker/calendar.gif',
																buttonImageOnly: true,
																dateFormat: 'mm/dd/yy'
															}
															));
															
									
									function updateBefore(input) { 
										/*
										var d = $('#arrivalD').val();
										var m = $('#arrivalM').val();
										var y = $('#arrivalY').val();
										
										d = d.substring(3, 5);
										d = d.replace(/(0)(\d{1})/, '$2');
										if (d < 8){ 
											d = Number(d) + Number(2);
											d = 0 + String(d);
										} 
										else if ( d < 29){ d = Number(d) + Number(2); }
										
										//  return {minDate: (new Date( y + '/' + m + '/' + d)) }; 
										 */
										 return {minDate: (new Date($('#departY').val() + '/' + $('#departM').val() + '/' + $('#departD').val())) }; 
									} 

									function updateDepart(date) { 
										 $('#departM').val(date.substring(0, 2)); 
										 $('#departD').val(date.substring(3, 5)); 
										 $('#departY').val(date.substring(6, 10)); 
									} 
									 
									function checkLinkedDays2() { 
										 var daysInMonth = 32 - new Date($('#departY').val(), 
											$('#departM').val() - 1, 32).getDate(); 
											$('#departD option').attr('disabled', ''); 
											$('#departD option:gt(' + (daysInMonth - 1) +')').attr('disabled', 'disabled'); 
										 if ($('#departD').val() > daysInMonth) { 
											  $('#departD').val(daysInMonth); 
										 } 
									} 
							
									
							
				// Remember Search Values and populate action box ////////////////////////////////////////////////////
				
						$("select[name='arrivalD']").change(function () {
								 var t = $("select[name='arrivalD'] option:selected").val();
								 $("select[name='departD']").val([t]);
							}).trigger('change');
						
						$("select[name='arrivalM']").change(function () {
							  var t = $("select[name='arrivalM'] option:selected").val();
							  $("select[name='departM']").val([t]);
						  }).trigger('change');
						  
						  $("select[name='arrivalY']").change(function () {
							  var t = $("select[name='arrivalY'] option:selected").val();
							  $("select[name='departY']").val([t]);
						  }).trigger('change');
				
						$("form").submit(function () {
								var aD = $("select[name='arrivalD'] option:selected").val();
								var aM = $("select[name='arrivalM'] option:selected").val();
								var aY = $("select[name='arrivalY'] option:selected").val();
								var dD = $("select[name='departD'] option:selected").val();
								var dM = $("select[name='departM'] option:selected").val();
								var dY = $("select[name='departY'] option:selected").val();
								var type = $("select[name='type'] option:selected").val();
								var city = $("select[name='city'] option:selected").val();
								var persons = $("input[name='persons']:text").val();
				
								searchVal = aY + '-' + aM + '-' + aD + ';' + dY + '-' + dM + '-' + dD + ';' + type + ';' + city + ';' + persons;
								$.cookie('TraveData', searchVal, { expires: 2, path: '/', domain: window.location.host });
								
								  });	  
				
								if($.cookie('TraveData')){
									var data = $.cookie('TraveData').split(';');
									var arrival = data[0].split('-');
									var aD = arrival[2];
									var aM = arrival[1];
									var aY = arrival[0];
									
									var depart = data[1].split('-');
									var dD = depart[2];
									var dM = depart[1];
									var dY = depart[0];
									
									var type = data[2];
									var city = data[3];
									var persons = data[4];
									
									$("input[name='persons']").val([persons]);
									$("select[name='type']").val([type]);
									$("select[name='city']").val([city]);
									$("select[name='arrivalD']").val(aD);
									$("select[name='arrivalM']").val(aM);
									$("select[name='arrivalY']").val(aY);
									$("select[name='departD']").val(dD);
									$("select[name='departM']").val(dM);
									$("select[name='departY']").val(dY);
								} 


	
});


/*
Bookmark site script- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
*/
function bookmarksite(title, url){
	if (document.all)
	window.external.AddFavorite(url, title);
	else if (window.sidebar)
	window.sidebar.addPanel(title, url, "")
}

/**
 * Cookie plugin
 *
 * Copyright (c) 2006 Klaus Hartl (stilbuero.de)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */

/**
 * Create a cookie with the given name and value and other optional parameters.
 *
 * @example $.cookie('the_cookie', 'the_value');
 * @desc Set the value of a cookie.
 * @example $.cookie('the_cookie', 'the_value', { expires: 7, path: '/', domain: 'jquery.com', secure: true });
 * @desc Create a cookie with all available options.
 * @example $.cookie('the_cookie', 'the_value');
 * @desc Create a session cookie.
 * @example $.cookie('the_cookie', null);
 * @desc Delete a cookie by passing null as value. Keep in mind that you have to use the same path and domain
 *       used when the cookie was set.
 *
 * @param String name The name of the cookie.
 * @param String value The value of the cookie.
 * @param Object options An object literal containing key/value pairs to provide optional cookie attributes.
 * @option Number|Date expires Either an integer specifying the expiration date from now on in days or a Date object.
 *                             If a negative value is specified (e.g. a date in the past), the cookie will be deleted.
 *                             If set to null or omitted, the cookie will be a session cookie and will not be retained
 *                             when the the browser exits.
 * @option String path The value of the path atribute of the cookie (default: path of page that created the cookie).
 * @option String domain The value of the domain attribute of the cookie (default: domain of page that created the cookie).
 * @option Boolean secure If true, the secure attribute of the cookie will be set and the cookie transmission will
 *                        require a secure protocol (like HTTPS).
 * @type undefined
 *
 * @name $.cookie
 * @cat Plugins/Cookie
 * @author Klaus Hartl/klaus.hartl@stilbuero.de
 */

/**
 * Get the value of a cookie with the given name.
 *
 * @example $.cookie('the_cookie');
 * @desc Get the value of a cookie.
 *
 * @param String name The name of the cookie.
 * @return The value of the cookie.
 * @type String
 *
 * @name $.cookie
 * @cat Plugins/Cookie
 * @author Klaus Hartl/klaus.hartl@stilbuero.de
 */
jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};