﻿(function( $ ){
	$.fn.bogyok = function() {
		var themap = $(this);
		
		var browserMsie = jQuery.browser.msie;
		
		if (browserMsie == true) {
			var cities = $("#thecities", themap);
			var boubles = $("#theboubles", themap);
			
			$(".m_boubles", boubles).css({"display" : "none", "opacity" : "0"});
			$(".m_agency", boubles).hide();
			
			$(".m_aclose", boubles).each(function(m) {
				$(this).bind("click", function () {
					closeOpenedBigBogyo(m);
				});
			});
			
			$(".m_blackpoint", cities).each(function(z) {
				$(this).css({"opacity" : "0.1"});
				var boubleId = $(this).attr("id").replace("_b_", "_a_");
				$(this).attr({ "title" : $("#"+boubleId+" span", boubles).html(), "alt" : $("#"+boubleId+" span", boubles).html() });
				if ($("#"+boubleId).hasClass("m_b_right")) {
					var modLeft = $(this).css("left").replace("px", "");
					$("#"+boubleId).css({"top" : $(this).css("top"), "left" : (modLeft-356)+"px"});
				} else {
					$("#"+boubleId).css({"top" : $(this).css("top"), "left" : $(this).css("left")});
				}
				$(this).bind("click", function () {
					if (($(this).css("opacity") != "1") && ($("#"+boubleId, boubles).css("display") == "none")) {
						closeOpenedBigBogyo(100);
						$(this).animate({"opacity" : "1"}, 1000, "easeInOutExpo");
						$("#"+boubleId, boubles).css({"display" : "block"});
						$("#"+boubleId, boubles).animate({"opacity" : "1"}, 1000, "easeInOutExpo", function() {
							$(".m_agency", "#"+boubleId).each(function(i){
								$(this).delay(i*1000).slideDown("slow", function() { // here is the difference for MSIE
									$(this).css("height","75px");
								});
							});
						});
					}
				});
			});
			function closeOpenedBigBogyo(o) {
				var g;
				$(".m_boubles", boubles).each(function(b) {
					if (o == 100) {
						g = b;
					} else {
						g = o;
					}
					if (g == b) {
						var cityId = $(this).attr("id").replace("_a_", "_b_");
						if (($(this).css("display") != "none") && ($("#"+cityId, cities).css("opacity") == "1")) {
							var delayI = 0;
							$(".m_agency", "#"+$(this).attr("id")).each(function(i) {
								$(this).delay(i*500).slideUp();
								delayI += 1;
							});
							$(this).delay(delayI*500).animate({"opacity" : "0"}, 500, "easeInOutExpo", function() {
								$(this).css({"display" : "none"});
								$("#"+cityId, cities).css({"opacity" : "0.1"});
							});
						}
					}
				});
			}
		} else {
			var cities = $("#thecities", themap);
			var boubles = $("#theboubles", themap);
			
			$(".m_boubles", boubles).css({"display" : "none", "opacity" : "0"});
			$(".m_agency", boubles).hide();
			
			$(".m_aclose", boubles).each(function(m) {
				$(this).bind("click", function () {
					closeOpenedBigBogyo(m);
				});
			});
			
			$(".m_blackpoint", cities).each(function(z) {
				$(this).css({"opacity" : "0.1"});
				var boubleId = $(this).attr("id").replace("_b_", "_a_");
				$(this).attr({ "title" : $("#"+boubleId+" span", boubles).html(), "alt" : $("#"+boubleId+" span", boubles).html() });
				if ($("#"+boubleId).hasClass("m_b_right")) {
					var modLeft = $(this).css("left").replace("px", "");
					$("#"+boubleId).css({"top" : $(this).css("top"), "left" : (modLeft-356)+"px"});
				} else {
					$("#"+boubleId).css({"top" : $(this).css("top"), "left" : $(this).css("left")});
				}
				$(this).bind("click", function () {
					if (($(this).css("opacity") != "1") && ($("#"+boubleId, boubles).css("display") == "none")) {
						closeOpenedBigBogyo(100);
						$(this).animate({"opacity" : "1"}, 1000, "easeInOutExpo");
						$("#"+boubleId, boubles).css({"display" : "block"});
						$("#"+boubleId, boubles).animate({"opacity" : "1"}, 1000, "easeInOutExpo", function() {
							$(".m_agency", "#"+boubleId).each(function(i){
								$(this).delay(i*1000).slideDown("slow");
							});
						});
					}
				});
			});
			function closeOpenedBigBogyo(o) {
				var g;
				$(".m_boubles", boubles).each(function(b) {
					if (o == 100) {
						g = b;
					} else {
						g = o;
					}
					if (g == b) {
						var cityId = $(this).attr("id").replace("_a_", "_b_");
						if (($(this).css("display") != "none") && ($("#"+cityId, cities).css("opacity") == "1")) {
							var delayI = 0;
							$(".m_agency", "#"+$(this).attr("id")).each(function(i) {
								$(this).delay(i*500).slideUp();
								delayI += 1;
							});
							$(this).delay(delayI*500).animate({"opacity" : "0"}, 500, "easeInOutExpo", function() {
								$(this).css({"display" : "none"});
								$("#"+cityId, cities).css({"opacity" : "0.1"});
							});
						}
					}
				});
			}
		}
	};
})( jQuery );
