// JQUERY ON DOM READY

$(document).ready(function() { 

	var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.userAgent,
			subString: "Chrome",
			identity: "Chrome"
		},
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari",
			versionSearch: "Version"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			   string: navigator.userAgent,
			   subString: "iPhone",
			   identity: "iPhone/iPod"
	    },
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();

	//On Hover Over
	function megaHoverOver(){
	
        if (BrowserDetect.browser == 'Explorer') {
			
			$(this).find(".sub").stop().show(); //Find sub and fade it in
    		(function($) {
        	
        	//Function to calculate total width of all ul's
        	jQuery.fn.calcSubWidth = function() {
        	    rowWidth = 0;
        	    
        	    //Calculate row
        	    $(this).find(".level2_ul").each(function() { //for each ul...
        	        
        	        rowWidth += $(this).width(); //Add each ul's width together
        	    
        	    });
        	    
        	};
    		})(jQuery); 
    	
    	} else {
    		
    		$(this).find(".sub").stop().fadeTo('fast', 1).show(); //Find sub and fade it in
    		(function($) {
        	
        	//Function to calculate total width of all ul's
        	jQuery.fn.calcSubWidth = function() {
        	    rowWidth = 0;
        	    
        	    //Calculate row
        	    $(this).find(".level2_ul").each(function() { //for each ul...
        	        
        	        rowWidth += $(this).width(); //Add each ul's width together
        	    
        	    });
        	    
        	};
    		})(jQuery);
    	
    	}

    	if ( $(this).find(".column").length > 0 ) { //If row exists...
			
        	var subWidth = $(this).find(".column").length * 180;
        	
        	$(this).find(".sub").css({'width' :subWidth}); //Set width
        	//$(this).find(".row:last").css({'margin':'0'});  //Kill last row's margin

   		} else { //If row does not exist...
   		
   			$(this).calcSubWidth();  //Call function to calculate width of all ul's
        	$(this).find(".sub").css({'width' : '180'}); //Set Width

    	}
	}

	//On Hover Out
	function megaHoverOut(){
  		
  		if (BrowserDetect.browser == 'Explorer') {
  			
  			$(this).find(".sub").stop().hide();
  		
  		} else {
  		
  			$(this).find(".sub").stop().fadeTo('fast', 0, function() { //Fade to 0 opactiy
      		
      			$(this).hide();  //after fading, hide it
  			
  			});
  		
  		}

	}

	//Set custom configurations
	var config = {
     	sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)
     	interval: 100, // number = milliseconds for onMouseOver polling interval
     	over: megaHoverOver, // function = onMouseOver callback (REQUIRED)
     	timeout: 500, // number = milliseconds delay before onMouseOut
     	out: megaHoverOut // function = onMouseOut callback (REQUIRED)
	};

	//$("ul#topnav li .sub").css({'opacity':'0'}); //Fade sub nav to 0 opacity on default
	$("ul#topnav li").hoverIntent(config); //Trigger Hover intent with custom configurations
	
	
	//++++++++++JQUERY CAROUSEL++++++++++//
	
	jQuery('#mycarousel').jcarousel({
		vertical: true,
		scroll: 1
	}); 
	
	jQuery('#mycarousel_horizontal').jcarousel({
		scroll: 1
	});
	
	jQuery('.mycarousel_horizontal2').jcarousel({
		vertical: true,
		scroll: 1
	});
	
	//++++++++++JQUERY UI DATEPICKER+++++++++//
	
	jQuery('#date_start, #date_end').datepicker({ 
		monthNames: ['Januar','Februar','März','April','Mai','Juni','Juli','August','September','Oktober','November','Dezember'],
		dayNamesMin: ['SO','MO','DI','MI','DO','FR','SA'],
		firstDay: 1,
		beforeShow: customRange,
		dateFormat: 'dd.mm.yy'
	});
	
	function customRange(input) {
	
		var min = new Date(2009, 1, 1);
		var dateMin = min;
		var dateMax = null;
		var tempDate = null;
	
		if (input.id == 'date_start') {
		
			if (jQuery('#date_end').datepicker('getDate') != null && jQuery('#date_end').datepicker('getDate') != 'TT.MM.JJJJ') {
			
				tempDate = jQuery('#date_end').datepicker('getDate');
				dateMax = new Date(tempDate.getFullYear(), tempDate.getMonth(), tempDate.getDate());
			
			} 
		
		} else if (input.id == 'date_end') {
		
			if (jQuery('#date_start').datepicker('getDate') != null && jQuery('#date_start').datepicker('getDate') != 'TT.MM.JJJJ') {
			
				tempDate = jQuery('#date_start').datepicker('getDate');
				dateMin = new Date(tempDate.getFullYear(), tempDate.getMonth(), tempDate.getDate());
			
			}
		
		}
	
		return {
		
			minDate: dateMin,
			maxDate: dateMax
		
		};
	
	}
		
	//NEWSLETTER INPUT
	$('#newsletter_input').focus(function() {
	
		if ($(this).val() == "Please insert your email" || $(this).val() == "Geben Sie Ihre E-Mail-Adresse ein") {
		
			$(this).val('');
		
		}
		
	});
	
	//SEARCHBOX INPUT
	$('#search_input').focus(function() {
	
		if ($(this).val() == "Please insert your searchword" || $(this).val() == "Geben Sie Ihren Suchbegriff ein") {
		
			$(this).val('');
		
		}
		
	});
	
	//SELECTBOX Tauchreisen
	$('#uf_djt_type_con, #uf_djt_country_con').change(function() {
		
		//$('#uf_djt_form').attr('action', 'index.php?id=2').submit();
		$('#uf_djt_form').submit();
	
	});
	
	if (BrowserDetect.browser != 'Explorer') {	
	
		$('#navi_innerwrap').css('width', function() {
			
			var textWidth = 0;
			
			
			$('.level1_link').each(function(index, el) {
				
				var value = $(this).css("width").replace('/px/g', "");
				
				textWidth += (parseInt(value) + 11);
			
			});
			
			return textWidth;	
			
		});
	
	} else {
	
		$('#navi_innerwrap').css('width','900px');
	
	}
	
	//GOOGLEMAP EINBINDEN
	if ($("#map").length > 0) {
		
		$.post('../index.php?id=182', {
			
			viewtype: "ajaxrequest",
			country: $('.country_map').attr('id'),
			build_google_map: 1
			
		}, function (data) {
			
			if (GBrowserIsCompatible()) { 
				
				// Display the map, with some controls and set the initial location 
      			var map = new GMap2(document.getElementById("map"));
      			map.setCenter(new GLatLng(parseFloat(data.cLat),parseFloat(data.cLong)),parseInt(data.zoom));
      			map.addControl(new GSmallMapControl());
      			
      			var regionsArr = data.regions;
      			
      			//for each (var region in regionsArr) {
      			for (var i = 0; i < regionsArr.length; i++) {	
      				
      				var region = regionsArr[i];
      				var regDetArr = region.split("|");
      				
      				var latIng = new GLatLng(parseFloat(regDetArr[2]),parseFloat(regDetArr[3]));
      				
      				var regioIcon = new GIcon(G_DEFAULT_ICON);
					regioIcon.image = "http://www.aquaactive.de/" + regDetArr[5];
					regioIcon.iconSize = new GSize(regDetArr[7], 22);
					regioIcon.iconAnchor = new GPoint(0, 40);
					regioIcon.imageMap = [0,0, regDetArr[7],0, regDetArr[7],22, 0,22, 0,0];
					regioIcon.shadowSize = new GSize(0, 0);


					// Set up our GMarkerOptions object
					markerOptions = { icon:regioIcon };
      				
      				var marker = new GMarker(latIng, markerOptions);
      				marker.name = regDetArr[1];
      				marker.zoom = parseInt(regDetArr[4]);
      				marker.region = regDetArr[0];
      				marker.direct = regDetArr[6];
      				
      				var f = function(marker, latIng, map) {
    
    					return function() {
        					
        					if (marker.direct == '') {
        					
        						map.setCenter(latIng, marker.zoom);
        						map.removeOverlay(marker);
        						
        						$.post('../index.php?id=182', {
								
								    viewtype: "ajaxrequest",
								    region: marker.region,
								    build_google_map_region: 1
								
								}, function (data) {
								
								    var hotelsArr = data.hotels;
      							
      							    //for each (var hotel in hotelsArr) {
      							    for (var j = 0; j < hotelsArr.length; j++) {
      									
      									var hotel = hotelsArr[j];
      							    	var hotelDetArr = hotel.split("|");
      							
      							    	var latIng = new GLatLng(parseFloat(hotelDetArr[2]),parseFloat(hotelDetArr[3]));
      							    	
      							    	var hotelIcon = new GIcon(G_DEFAULT_ICON);
      							    	
								    	hotelIcon.image = "http://www.aquaactive.de/" + hotelDetArr[5];
								    	hotelIcon.iconSize = new GSize(hotelDetArr[8], 22);
										hotelIcon.imageMap = [0,0, hotelDetArr[8],0, hotelDetArr[8],22, 0,22, 0,0];
								    	hotelIcon.iconAnchor = new GPoint(7, 20);
								    	hotelIcon.shadowSize = new GSize(0, 0);
								    	
								    	// Set up our GMarkerOptions object
								    	markerOptions = { icon:hotelIcon };
      							
      							    	var marker = new GMarker(latIng, markerOptions);
      							    	//var marker = new GMarker(latIng);
      							    	marker.hotellink = hotelDetArr[4];
      							    	marker.hotelname = hotelDetArr[1];
      							    	marker.hotelimage = hotelDetArr[6];
      							    	marker.hotelcat = hotelDetArr[7];
      							    	
      							    	var g = function(marker, latIng, map) {
        						    		
        						    		return function () {
        						    		
        						    			var myHtml = '<div style="padding-bottom:2px;">' + marker.hotelcat + '</div><div style="padding-bottom:5px;"><a href="http://www.aquaactive.de/' + marker.hotellink + '"><b>' + marker.hotelname + '</b></a></div><div><img src="' + marker.hotelimage + '" alt="' + marker.hotelname + '" /></div>';
        						    			map.openInfoWindowHtml(latIng, myHtml);
        						    			
        						    		}
        						    	
        						    	}
      							    	
      							    	
      							    	GEvent.addListener(marker, "click", g(marker, latIng, map));
        						    	
        						    	map.addOverlay(marker);
        						    	
        						    }			
								
								}, "json");
								
							} else {
							
								window.location.replace("http://www.aquaactive.de/" + marker.direct);
							
							}
    
    					}
					
					}
					
					GEvent.addListener(marker, "click", f(marker, latIng, map));
      			
      				map.addOverlay(marker);
      			
      			}


    		} else {
      			
      			alert("Sorry, the Google Maps API is not compatible with this browser");
      			
    		}
		
		
		}, "json");
		
	}

	//TABS SHIP DETAILS
	$("ul.tabs").tabs("div.panes > div");
        
});  
