// eSKY Destinator
// (c) eSKY 2010, www.esky.pl

(function($) {

	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
	};

	$.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'));
				var winWidth = $(window).width();
				var iOffset = $input.offset();
				var dLeft = iOffset.left;
				if (iOffset.left > winWidth/2) {
					dLeft = iOffset.left + $input.width() - 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"><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></div>')
					).bgIframe()
				);
				swfobject.embedSWF("qsf_new_22_02_2010/MAPA/swf/destinatorloader.swf", "destinatorContent", "750", "500", "9.0", "", opts);
				return false;
			});
		});
	};

	$.esky = {
		closeDestinator: function() {
			$destinator = $('#eskydestinator');
			if ($destinator.length > 0) {
				$destinator.empty().remove();
			}
		},
		setDestination: function(target, value) {
			$('#' + target).attr('value', value);
			$.esky.closeDestinator();
		}
	}

})(jQuery);

$(document).ready(function(){
	$('a.eskydestinator').eskydestinator(destinatorOptions);
	$(this).click(function(){
		$.esky.closeDestinator();
	});
});