$.fn.delay = function(time, name) {
    return this.queue((name || 'fx'), function() {
        var self = this;
        setTimeout(function() { $.dequeue(self); } , time);
    });
};

function loadSlideShow(groupID) {
	$('a.slideshow-' + groupID).colorbox({
		slideshow: true,
		transition: 'elastic'
	});
}


function SearchSwitcher(type) {
    $('#qs_main_search > .qs-module').hide();
    
	switch (type) {
		case 0:
			$('#dep_dest_container').show();
			$('#arr_dest_container').show();
			$('#dep_flight_dates').show();
			$('#ret_flight_dates').show();
			odin.qs.settings.urls = 'Flights|Hotels|Passengers|Services|Payment|Receipt';
			odin.qs.settings.searchType = 'Departures';
			break;
		case 1:
			$('#dep_dest_container').show();
			$('#arr_dest_container').show();
			$('#dep_flight_dates').show();
			$('#ret_flight_dates').show();
			odin.qs.settings.urls = 'Flights|Passengers|Payment|Receipt';
			odin.qs.settings.searchType = 'Departures';
			odin.qs.isFlightOnly = true;
			break;
		case 2:
		    $('#pkg_cats').show();
		    $('pkg_items').show();
			odin.qs.settings.searchType = 'Packages';
			odin.qs.settings.urls = 'Package|Passengers|Payment|Receipt';
	}
}

HotelSelection = function() {
	this.init();
}

/*	HOTEL IMAGE THINGY:
	--------------------------------------------------------------------------------------------------------------------- */
HotelImageMovers = function() {
  this.init();
};
jQuery.extend(HotelImageMovers.prototype, {
	init: function() {
		this.UL = $('div.thumbnails ul');
		
		this.Positions = new Array();
		this.CurrentPosition = 0;		
		var containingClass = this;
		
		$('img.hotel-thumb').each( function(i, element) {
				$(element).click( function(ev) {
				$('#hotel_image_loader').show();
				
				$('#main_image').fadeTo("slow",0, function(){
					var engine = $(element).attr('engine');
					if( engine == "getfile") {
						var procID = $(element).attr('procID');
						var imageID = $(element).attr('imgID');
						$('#main_image').attr('src', "/modules/odin/getfile.aspx?fileid=" + imageID + "&processid=" + procID);
											
						$('#main_image').fadeTo("def",1,(function() {	
							$('#hotel_image_loader').hide();
						}));
					} else {
						var hotel_id = $(element).attr("hotel_id");
						var filename = $(element).attr("filename");
						$('#main_image').attr('src', "/Modules/Odin/ImageHost.aspx?MaxHeight=270&MaxWidth=310&Quality=60&HotelID=" + hotel_id + "&FileName=" + filename );
											
						$('#main_image').fadeTo("slow",1,(function() {	
							$('#hotel_image_loader').hide();
						}));	
					}
				});
				
			});		
			containingClass.Positions[containingClass.Positions.length] = parseInt($(element).height()) + 8;
		});

		if ($('#move_up')) {
			$('#move_up').bind('click', function(event) {
				if (containingClass.CurrentPosition > 0) {					
					containingClass.CurrentPosition = containingClass.CurrentPosition - 1;
					var currentPosY = 0;
					
					for (var i = 0; i < containingClass.CurrentPosition; i++) {
						currentPosY = currentPosY + containingClass.Positions[i];
					}
					
					containingClass.UL.animate({ 
				        top: (currentPosY * -1) + 'px'
				      }, 350 );
				}
			});
		}

		if ($('#move_down')) {
			$('#move_down').bind('click', function(event) {
				if (containingClass.CurrentPosition < (containingClass.Positions.length - 1)) {
					containingClass.CurrentPosition = containingClass.CurrentPosition + 1;
					var currentPosY = 0;
					
					for (var i = 0; i < containingClass.CurrentPosition; i++) {
						currentPosY = currentPosY + containingClass.Positions[i];
					}
					containingClass.UL.animate({ 
				        top: (currentPosY * -1) + 'px'
				      }, 350 );
				}
			});
		}
	}
});

function ShowHotelImage() {
	$('#hotel_image_loader').hide();
	if (HIS != null) {
		$('#main_image').fadeTo("def",1);			
	}
}



	/*	GOOGLE MAPS - FIND HOTELS:
	--------------------------------------------------------------------------------------------------------------------- */    
function Hotel (id, name, lng, lat, stars, minprice){
  this.id = id;
  this.name = name;
  this.lng = lng;
  this.lat = lat;
  this.stars = stars;        
  this.minprice = minprice;       
  this.GLatLng = new GLatLng(lat, lng);
}

function Location(id, name, lng, lat, icon, typename) {
  this.id = id;
  this.name = name;
  this.long = lng;
  this.lat = lat;
  this.icon = icon;
  this.typename = typename;      
  
  this.GLatLng = new GLatLng(lat, lng);
}

GMapFindHotels = function(element, ownerInfo) {
  this.init(element, ownerInfo);
};
jQuery.extend(GMapFindHotels.prototype, {	                               
  init: function(element, ownerInfo){                                                
		this.hotels = new Array();
    this.locations = new Array();
    this.map = null; 
    this.mgr = null;      
    this.element = element;
    this.ownerInfo = ownerInfo;
    
		this.currentLocations = null;
		this.currentHotel = null;
		this.bounds = new GLatLngBounds();
	},
  
  InitMap: function( ) {  
  		if( this.map === null ) {
	      var defaultIcon = new GIcon(G_DEFAULT_ICON);
	      defaultIcon.image = "http://google-maps-icons.googlecode.com/files/hotel.png";
	      defaultIcon.iconAnchor = new GPoint(14,28);    
	      defaultIcon.iconSize = new GSize(28, 28);
				                                                                 
	      this.map = new GMap2(document.getElementById(this.element));
	      //map.enableGoogleBar();
	      this.map.addControl(new GLargeMapControl3D()); 
	      this.map.addControl(new GOverviewMapControl());
	      this.map.addControl(new GHierarchicalMapTypeControl());
	      //map.addControl(new GNavLabelControl(), new GControlPosition(G_ANCHOR_BOTTOM_LEFT, new GSize(10,10) ) );
	      //map.enableScrollWheelZoom();            
	      //this.map.addMapType(G_SATELLITE_3D_MAP);
	      this.map.setCenter( this.bounds.getCenter(), this.map.getBoundsZoomLevel(this.bounds) -1 );
	      
	      var mgrOptions = { borderPadding: 50, maxZoom: 25, trackMarkers: false };
	      this.mgr = new MarkerManager(this.map, mgrOptions);
	      
	      // Add the hotel markers
	      for( var i = 0; i < this.hotels.length; i++) {
	        tmpMarker = new GMarker( this.hotels[i].GLatLng, {icon:defaultIcon});   	        
	        GEvent.addListener(tmpMarker, 'click', this.mouseOverHotel);
					this.mgr.addMarker( tmpMarker, 0, 25); 
	      }         
	             
	      // Add the location markers
	      for( var i = 0; i < this.locations.length; i++) {
	        icon = new GIcon(defaultIcon, this.locations[i].icon);
	        tmpMarker = new GMarker( this.locations[i].GLatLng, {icon:icon});
	        tmpMarker.image = this.locations[i].icon;    
	        GEvent.addListener(tmpMarker, 'click', this.mouseOverLocation);
	        this.mgr.addMarker( tmpMarker, 0, 25);
	      }                   
	      
	      this.mgr.refresh();
	      
	      window.onbeforeunload = GUnload;
	    }
  },
  
  addHotel: function(id, name, lng, lat, stars, minprice) {
		if(lng != '' && lng != 0 && lat != '' && lat != 0) {              
			this.hotels.push(new Hotel(id, name, lng, lat, stars, minprice ));
			this.bounds.extend(new GLatLng(lat, lng));
		}
	},
	
	addLocation: function(id, name, lng, lat, icon, typename){                
		if(lng != '' && lng != 0 && lat != '' && lat != 0) {              
			this.locations.push( new Location(id, name, lng, lat, icon, typename) );
			this.bounds.extend(new GLatLng(lat, lng));	
		}
	},
  
  getHotelByLatLng: function(LatLng) {
    for(var i = 0; i < this.hotels.length; i++ ) {
      if( this.hotels[i].GLatLng === LatLng )
        return this.hotels[i];
    } 
  },
  
  getLocationByLatLng: function(LatLng) {
    for(var i = 0; i < this.locations.length; i++ ) {
      if( this.locations[i].GLatLng === LatLng )
        return this.locations[i];
    }
  },
  
  mouseOverHotel: function(LatLngPos){
    var hotel = __gmap.getHotelByLatLng(LatLngPos);
    __gmap.currentHotel = hotel;
    
    __gmap.map.openInfoWindowTabsHtml(
				LatLngPos, [
					new GInfoWindowTab( 'Info', $('#hotel_info_' + hotel.id).html())
					//,new GInfoWindowTab( 'Attractions', '<div id="hotel_info_locations"><img src="/templates/common/images/image-loader.gif" /></div>') 
		], {
				maxContent: '<div class="hotel_info">' + $('#hotel_item_' + hotel.id).html() + '</div>',
				maxTitle: '<div class="hotel_title">' + hotel.name + ' <span class="einkunn stars' + hotel.stars + '"> </span></div>',
				onCloseFn: function(){ if(__gmap.polyline != null){__gmap.map.removeOverlay(__gmap.polyline); __gmap.polyline = null; }}
		});
    __gmap.map.getInfoWindow().enableMaximize();
    
    setTimeout('__gmap.refreshLocations('+hotel.id+')', 200);
  }, 
	
	refreshLocations: function(hotelID){          
    //Zeus.Odin.DisillModules.API.OdinAPI.HotelLocations( __gmap.ownerInfo, hotelID, function(r) {
		//		div = [];
		//		while(div.length == 0) div = $('#hotel_info_locations');
		//		div.html('');
		//		__gmap.currentLocations = r;
		//		for(var i = 0; i < Math.min(r.length, 9); i++){
		//			lng = r[i].Coordinates.Longitude;
		//			lat = r[i].Coordinates.Latitude;
		//			div.append('<div class="attraction"><span class="location_name">' + r[i].Name + '</span><span class="show_line" onclick="__gmap.mouseOverLocationDistance(' + i + ');">(show)</span><span class="distance">' + double2str(r[i].Distance) + ' km.</span></div>');
		//		}			
		//	}, Error);           
	},                                        
  
  mouseOverLocation: function(LatLngPos){
    var location = __gmap.getLocationByLatLng(LatLngPos);
    if(location != null) {
	    __gmap.map.openInfoWindowHtml(LatLngPos, $('#location_desc_' + location.id).html() );        
	    __gmap.map.getInfoWindow().disableMaximize();
	  }
  },
	
	mouseOverLocationDistance: function(index) {
		if(__gmap.polyline != null) {
			__gmap.map.removeOverlay(__gmap.polyline);
		}
		pt = new GLatLng( __gmap.currentLocations[index].Coordinates.Latitude, __gmap.currentLocations[index].Coordinates.Longitude );
		this.polyline = new GPolyline([
		  __gmap.currentHotel.GLatLng, pt
		], '#0000ff', 5);
		__gmap.map.addOverlay(__gmap.polyline);	
	}    
});

var __gmap = null;

function NewGMap(element, ownerInfo) {
	__gmap = new GMapFindHotels(element, ownerInfo);
	return __gmap;
}

function double2str(X) {
    with (new Object(Math.round(100 * X) + "")) {
        return substring(0, length - 2) + "," + substring(length - 2, length);
    }
}  
