$(function(){

	position_window()
	$(window)
		.resize(position_window)
	
	// Close content
	$("#link_close_content, #link_logo").click(function () {
		$("#inner_window_shadow").slideUp(250, function() {
			hide_content("#logo_menu", function() {
				$("#logo_menu").animate({bottom:"50%"}, 250)
			});
		})
		return false;
	})
	
	// Hide all content
	function hide_content(p_next_content, p_callback) {
		$("#content_yacht_charter").not(p_next_content).hide();
		$("#content_yacht_brokerage").not(p_next_content).hide();
		$("#content_yacht_training").not(p_next_content).hide();
		$("#content_yacht_crew").not(p_next_content).hide();
		$("#content_yacht_management").not(p_next_content).hide();

		if (typeof p_callback == "function") p_callback();
	}
	
	// Move logo
	function move_logo_bottom(p_callback) {
		$("#logo_menu").animate({bottom:"164px"}, 250, function() {
			if (typeof p_callback == "function") p_callback(); 
		})
	}
	
	// Yacht charter
	$("#link_yacht_charter").click(function () {
		hide_content("#content_yacht_charter", function () {
			move_logo_bottom(function() {
				$("#yacht_charter_search_form select").each(function(){
					$(this).val($('option:first', this).val());
				});
				$("#content_yacht_charter").show();
				refresh_search_charter_yachts("");
				$("#inner_window_shadow").slideDown(250)
			});
		})
		return false;
	})			
	$("#yacht_charter_search_form select").change(function () {
		refresh_search_charter_yachts($("#yacht_charter_search_form").serialize())
	})
	$("#yacht_charter_search_form input").change(function () {
		refresh_search_charter_yachts($("#yacht_charter_search_form").serialize())
	})
	function refresh_search_charter_yachts(p_params) {
		$("#charter_yachts_search_result").html("<p style=\"padding:20px;\"><img src=\"http://www.bluewateryachting.com/_images/_internet/loading.gif\"></p>");
		$("#charter_yachts_search_link").html("");
		$.get("index_charter_yacht_search.php", p_params, function(data) {
			if (data != "") {
				$("#charter_yachts_search_result").html(data);
			}
		}, "html");	
	}
	
	// Yacht brokerage
	$("#link_yacht_brokerage").click(function () {
		hide_content("#content_yacht_brokerage", function () {
			move_logo_bottom(function() {
				$("#yacht_brokerage_search_form select").each(function(){
					$(this).val($('option:first', this).val());
				});
				$("#content_yacht_brokerage").show();
				refresh_search_brokerage_yachts("");
				$("#inner_window_shadow").slideDown(250)
			})
		})
		return false;
	})			
	$("#yacht_brokerage_search_form select").change(function () {
		refresh_search_brokerage_yachts($("#yacht_brokerage_search_form").serialize())
	})
	$("#yacht_brokerage_search_form input").change(function () {
		refresh_search_brokerage_yachts($("#yacht_brokerage_search_form").serialize())
	})
	function refresh_search_brokerage_yachts(p_params) {
		$("#brokerage_yachts_search_result").html("<p style=\"padding:20px;\"><img src=\"http://www.bluewateryachting.com/_images/_internet/loading.gif\"></p>");
		$("#brokerage_yachts_search_link").html("");
		$.get("index_brokerage_yacht_search.php", p_params, function(data) {
			if (data != "") {
				$("#brokerage_yachts_search_result").html(data);
			}
		}, "html");	
	}
	
	// Crew training
	$("#link_crew_training").click(function () {
		hide_content("#content_yacht_training", function () {
			move_logo_bottom(function() {
			
				$("#select_training_course_id").hide();
				$("#select_training_class_id").hide();
				$("#select_training_book_btn").hide();
				
				$("#yacht_training_search_form select").each(function(){
					$(this).val($('option:first', this).val());
				});
				$("#content_yacht_training").show();
				refresh_search_training("");
				$("#inner_window_shadow").slideDown(250)
			})
		})
		return false;
	})	
	$("#yacht_training_search_form select").live('change', function () {
		refresh_search_training($("#yacht_training_search_form").serialize())
	})
	function refresh_search_training(p_params) {
		$("#training_courses_search_result").html("<p style=\"padding:20px;\"><img src=\"http://www.bluewateryachting.com/_images/_internet/loading.gif\"></p>");
		$.get("index_training_course_search.php", p_params, function(data) {
			if (data != "") {
				$("#training_courses_search_result").html(data);
			}
		}, "html");	
	}

	// Yacht management
	$("#link_yacht_management").click(function () {
		hide_content("#content_yacht_management", function () {
			move_logo_bottom(function() {
				$("#content_yacht_management").show();
				refresh_search_management("");
				$("#inner_window_shadow").slideDown(250)
			})
		})
		return false;
	})	
	function refresh_search_management(p_params) {
		$("#yacht_management_search_result").html("<p style=\"padding:20px;\"><img src=\"http://www.bluewateryachting.com/_images/_internet/loading.gif\"></p>");
		$.get("index_yacht_management_search.php", p_params, function(data) {
			if (data != "") {
				$("#yacht_management_search_result").html(data);
			}
		}, "html");	
	}
	
	// Yacht crew
	$("#link_yacht_crew").click(function () {
		hide_content("#content_yacht_crew", function () {
			move_logo_bottom(function() {
				$("#content_yacht_crew").show();
				refresh_search_crew("");
				$("#inner_window_shadow").slideDown(250)
			})
		})
		return false;
	})	
	function refresh_search_crew(p_params) {
		$("#yacht_crew_search_result").html("<p style=\"padding:20px;\"><img src=\"http://www.bluewateryachting.com/_images/_internet/loading.gif\"></p>");
		$.get("index_yacht_crew_search.php", p_params, function(data) {
			if (data != "") {
				$("#yacht_crew_search_result").html(data);
			}
		}, "html");	
	}
	

	// Position window
	function position_window() {
	
		var document_width = $(document).width()
		var document_height = $(document).height()
		
		if (document_width > 962) {
			
			$("#inner_window_shadow").css("width", "" + (document_width - 85) + "px") 
			$("#inner_window").css("width", "" + (document_width - 85) + "px") 
			
			$("#charter_yachts_search_result").css("width", "" + (document_width - 599 - 85) + "px") 
			$("#brokerage_yachts_search_result").css("width", "" + (document_width - 599 - 85) + "px") 
			
			$("#training_courses_search_result").css("width", "" + (document_width - 599 - 85) + "px") 
			$("#yacht_management_search_result").css("width", "" + (document_width - 599 - 85) + "px") 
			$("#yacht_crew_search_result").css("width", "" + (document_width - 599 - 85) + "px") 

		} else {
			$("#inner_window").css("width", "879px") 
			$("#inner_window_shadow").css("width", "879px") 
			$("#charter_yachts_search_result").css("width", "280px") 
			$("#brokerage_yachts_search_result").css("width", "280px") 
			$("#training_courses_search_result").css("width", "280px") 
			$("#yacht_management_search_result").css("width", "280px") 
			$("#yacht_crew_search_result").css("width", "280px") 
		}
		
		if (document_height < 677) {
			$("#inner_window_shadow").css("top", "" + (document_height - 400 - 140 - 10 + 205) + "px") 
		} else {
			$("#inner_window_shadow").css("top", "50%") 
		}		
	}
	

	var yacht_id = 0;
	var ajax_get;

	$("#charter_yachts_search_result .thumbnail_yacht").live("mouseenter", function() {
		
		var yacht_details_position_horizontal = "";
		var yacht_details_position_vertical = "";
		var yacht_details_slide_horizontal = "";
		
		if (yacht_id != $(this).attr("yacht_id")) {
		
			yacht_id = $(this).attr("yacht_id");
			
			var position_thumbnail = $(this).offset();
			var position_search_result = $("#charter_yachts_search_result").offset();
		
			$("#yacht_details").css("top", position_search_result.top - 15 + "px")
	
			if ((position_thumbnail.left + $(this).width() + $("#yacht_details").width()) > $(document).width()) {
				$("#yacht_details").css("left", (position_thumbnail.left - 2 - $("#yacht_details").width()) + "px")
				yacht_details_position_horizontal = "left";
				yacht_details_slide_horizontal = "right";
			} else {
				$("#yacht_details").css("left", (position_thumbnail.left + $(this).width()) + "px")
				yacht_details_position_horizontal = "right";
				yacht_details_slide_horizontal = "left";
			}
			yacht_details_position_vertical = position_thumbnail.top - position_search_result.top + 15;
			
			$("#yacht_details").html("<p style=\"padding:20px;\"><img src=\"http://www.bluewateryachting.com/_images/_internet/loading.gif\"></p>");
			
			if (ajax_get) {
				ajax_get.abort();
			}
			
			ajax_get = $.get("index_charter_yacht_details.php", "yacht_id=" + yacht_id + "&position_horizontal=" + yacht_details_position_horizontal + "&position_vertical=" + yacht_details_position_vertical, function(data) {
				if (data != "") {
					$("#yacht_details").html(data);
					ajax_get = null;
				}
			}, "html");	
		}	
		
		$("#charter_yachts_search_result .thumbnail_yacht").each(function(){
			if ($(this).attr("yacht_id") != yacht_id) {
				$(this).css({ "opacity" : 0.45 })
			} else {
				$(this).css({ "opacity" : 1 })
			}
		});

		$("#yacht_details").show();
	});
	
	$("#charter_yachts_search_result .thumbnail_yacht").live("mouseleave", function() {
		$("#yacht_details").hide();
		$("#charter_yachts_search_result .thumbnail_yacht").each(function(){
			$(this).css({ "opacity" : 1 })
		});
	});
	
	$("#brokerage_yachts_search_result .thumbnail_yacht").live("mouseenter", function() {
		
		var yacht_details_position_horizontal = "";
		var yacht_details_position_vertical = "";
		var yacht_details_slide_horizontal = "";
		
		if (yacht_id != $(this).attr("yacht_id")) {
		
			yacht_id = $(this).attr("yacht_id");
			
			var position_thumbnail = $(this).offset();
			var position_search_result = $("#brokerage_yachts_search_result").offset();
		
			$("#yacht_details").css("top", position_search_result.top - 15 + "px")
	
			if ((position_thumbnail.left + $(this).width() + $("#yacht_details").width()) > $(document).width()) {
				$("#yacht_details").css("left", (position_thumbnail.left - 2 - $("#yacht_details").width()) + "px")
				yacht_details_position_horizontal = "left";
				yacht_details_slide_horizontal = "right";
			} else {
				$("#yacht_details").css("left", (position_thumbnail.left + $(this).width()) + "px")
				yacht_details_position_horizontal = "right";
				yacht_details_slide_horizontal = "left";
			}
			yacht_details_position_vertical = position_thumbnail.top - position_search_result.top + 15;
			
			$("#yacht_details").html("<p style=\"padding:20px;\"><img src=\"http://www.bluewateryachting.com/_images/_internet/loading.gif\"></p>");
			
			if (ajax_get) {
				ajax_get.abort();
			}
			
			ajax_get = $.get("index_brokerage_yacht_details.php", "yacht_id=" + yacht_id + "&position_horizontal=" + yacht_details_position_horizontal + "&position_vertical=" + yacht_details_position_vertical, function(data) {
				if (data != "") {
					$("#yacht_details").html(data);
					ajax_get = null;
				}
			}, "html");	

		}	
		
		$("#brokerage_yachts_search_result .thumbnail_yacht").each(function(){
			if ($(this).attr("yacht_id") != yacht_id) {
				$(this).css({ "opacity" : 0.45 })
			} else {
				$(this).css({ "opacity" : 1 })
			}
		});

		$("#yacht_details").show();
	});
	
	$("#brokerage_yachts_search_result .thumbnail_yacht").live("mouseleave", function() {
		$("#yacht_details").hide();
		$("#brokerage_yachts_search_result .thumbnail_yacht").each(function(){
			$(this).css({ "opacity" : 1 })
		});
	});
	
	$("#link_close_yacht_details").live("click", function() {
		$("#yacht_details").hide();
		$("#brokerage_yachts_search_result .thumbnail_yacht").each(function(){
			$(this).css({ "opacity" : 1 })
		});
		$("#charter_yachts_search_result .thumbnail_yacht").each(function(){
			$(this).css({ "opacity" : 1 })
		});
	});
	
	$("#yacht_details").live("mouseenter", function() {
		$("#yacht_details").show();
		$("#charter_yachts_search_result .thumbnail_yacht").each(function(){
			if ($(this).attr("yacht_id") != yacht_id) {
				$(this).css({ "opacity" : 0.45 })
			} else {
				$(this).css({ "opacity" : 1 })
			}
		});
		$("#brokerage_yachts_search_result .thumbnail_yacht").each(function(){
			if ($(this).attr("yacht_id") != yacht_id) {
				$(this).css({ "opacity" : 0.45 })
			} else {
				$(this).css({ "opacity" : 1 })
			}
		});
	});
	
	$("#yacht_details").live("mouseleave", function() {
		$("#yacht_details").hide();
		$("#charter_yachts_search_result .thumbnail_yacht").each(function(){
			$(this).css({ "opacity" : 1 })
		});
		$("#brokerage_yachts_search_result .thumbnail_yacht").each(function(){
			$(this).css({ "opacity" : 1 })
		});
	});

	// Tooltips
	if (!isiPad()) {
		$('#link_yacht_charter').tipsy({trigger: 'hover', gravity: 'nw', title: 'tooltip', offset: -23, opacity: 1, fade: true});
		$('#link_yacht_brokerage').tipsy({trigger: 'hover', gravity: 'n', title: 'tooltip', offset: -23, opacity: 1, fade: true});
		$('#link_crew_training').tipsy({trigger: 'hover', gravity: 'n', title: 'tooltip', offset: -23, opacity: 1, fade: true});
		$('#link_yacht_crew').tipsy({trigger: 'hover', gravity: 'n', title: 'tooltip', offset: -23, opacity: 1, fade: true});
		$('#link_yacht_management').tipsy({trigger: 'hover', gravity: 'ne', title: 'tooltip', offset: -23, opacity: 1, fade: true});
	}
	
});


/*********************************/
/* Ajax                          */
/*********************************/
function ajax(p_target_div, p_url, p_params, p_loading_message, p_scroll_anchor) { 
	
	var target_div = "#" + p_target_div
	
	if (typeof p_loading_message == 'undefined') { 
        loading_message = "Loading ..."; 
	} else if (p_loading_message == '') {
		loading_message = "Loading ..."; 
    }  else {
		loading_message = p_loading_message; 
	}
	
	loading_message = "<div style=\"float:none; display:table; padding-top:10px; margin-bottom:22px;\"><img src=\"../_images/_bluespace/loading.gif\" align=\"absmiddle\" border=\"0\" width=\"16\" height=\"16\">&nbsp;&nbsp;" + loading_message + "</div>";
	
	if (typeof p_scroll_anchor == 'undefined') { 
        scroll_anchor = ""; 
	}  else {
		scroll_anchor = p_scroll_anchor; 
	}

	// Hide target > fadeTo 
	$(target_div).fadeOut(30, function(){

		// Display loading message
		$(target_div).html(loading_message);
		$(target_div).fadeIn(30, function(){
			
			// Execute Ajax query
			$.get(p_url, p_params, function(data) {
				
				// Hide loading message
				$(target_div).fadeOut(30, function(){
				
					if (data != "") {
						// Load results
						$(target_div).html(data);
						
						// Display result
						$(target_div).slideDown(100, function(){
						
							if (scroll_anchor != "") {
								var target_top = $("#" + scroll_anchor).offset().top - 120;  
								$('html, body').animate( { scrollTop:target_top } , 200);  
							}
						});
					}

				});
			}, "html");
		}); 
	});
}

function isiPad() {
	return navigator.platform.indexOf("iPad") != -1 ;
}
