// eSKY Destinator
// (c) eSKY 2010, www.esky.pl
(function($) {
	var current_target;
	var defaults = {
		partner: '', //ID partnera (wymagane)
		language: 'PL', //wersja językowa
		selectedCountry: '', //domyślnie wybrany kraj
		selectedContinent: '', //domyślnie wybrany kontynent
		configXML: '', //plik XML z konfiguracją mapy
		swfLocation: 'swf/', //lokalizacja pliku swf
		rOffset: 0, //przesunięcie mapy przy wyrównaniu do prawej strony
		getFlashText: '<p>Do działania mapy niezbędny jest plugin <a href="http://get.adobe.com/flashplayer/" target="_blank">Adobe Flash</a> - prosimy o jego zainstalowanie.</p>'
	};

	$.fn.eskydestinator = function(options){
		$(this).each(function(){
			$(this).unbind().click(function(){
				$this = $(this);
				if ($('#eskydestinator').length > 0) {
					$.esky.closeDestinator();
					return false;
				}
				opts = $.extend({}, defaults, options);
				opts.input = $this.attr('rel');
				var $input = $('#' + $this.attr('rel'));
				current_target = $this.attr('rel');
				var winWidth = $(window).width();
				var iOffset = $input.offset();
				var dLeft = iOffset.left;
				if (iOffset.left > winWidth / 2) {
					dLeft = iOffset.left + $input.width() + Number(opts.rOffset) - 750;
				}

				$('body').append(
					$('<div id="eskydestinator" style="position: absolute;width: 750px;height: 500px;border: 1px solid #aaa;background-color: #fff;z-index: 9999;"></div>')
					.css({top:iOffset.top + $input.height() + 10,left:dLeft})
					.append($('<div id="destinatorContent">' + opts.getFlashText + '</div>')).bgIframe()
				);
				swfobject.embedSWF(opts.swfLocation + "destinatorloader.swf", "destinatorContent", "750", "500", "9.0", "", opts, { wmode: "transparent" });
				return false;
			});
		});
	};

	$.esky = {
		closeDestinator: function() {
			$destinator = $('#eskydestinator');
			if ($destinator.length > 0) {
				$destinator.empty().remove();
			}
		},
		setDestination: function(target, value) {
			if (!target) {
				target = current_target;
			}
			$('#' + target).focus().attr('value', value).change();
			$.esky.closeDestinator();
		}
	}
})(jQuery);

$(document).ready(function(){
	$('a.eskydestinator').eskydestinator(destinatorOptions);
	$(this).click(function(e){
		if (e.target.id !== 'destinatorContent'){
			$.esky.closeDestinator();
		}
	});
});
