(function($){
	
	function getUrlVars(href)
	{
	    var vars = [], hash;
	    var hashes = href.slice(href.indexOf('?') + 1).split('&');
	    for(var i = 0; i < hashes.length; i++)
	    {
	        hash = hashes[i].split('=');
	        vars.push(hash[0]);
	        vars[hash[0]] = hash[1];
	    }
	    return vars;
	};
	
	function getBarCodeImg(tobiekt1Id, typ, zoom)  {
        return $("<img src='/poi/PoiPub?q=kartBarcodeObiekt&tobiekt1Id=" + tobiekt1Id + "&lang=" + cmsLang  + "&typ=" + typ + "&zoom=" + zoom + "'/>");
    };
	
    function getVCardContainer() {
        var container = $("#vcardContainer");
        if (container.length) 
            return container;

        var container = $('\
        <div id="vcardContainer" class="modal">\
            <div class="ui-widget-header">\
                Pobierz fotokod\
            </div>\
            <div class="modal-content">\
                <div id="barcodeImageContainer" />\
                <div class="modal-buttons">\
                    <label for="barcodeDetailsLevel">\
                        Poziom szczegółów:\
                    </label>\
                    <select id="barcodeDetailsLevel">\
                        <option value="vcard1">telefon, e-mail</option>\
                        <option value="vcard2">pełen kontakt</option>\
                        <option value="directUrl">łącze do karty</option>\
                    </select>\
                    <label for="barcodeSize">\
                        Rozmiar fotokodu:\
                    </label>\
                    <select id="barcodeSize">\
                        <option value="200">Mały</option>\
                        <option value="300">Średni</option>\
                        <option value="400">Duży</option>\
                    </select>\
					<input type="hidden" id="barcodeTobiekt1Id" />\
                    <button id="vcardContainerClose">\
                        Zamknij\
                    </button>\
                </div>\
            </div>\
        </div>');
        $(document.body).append(container);
        return container;
    };
	
	$.extend({
        photocode: function(tobiekt1id, options){
		
        	var defaults = {
                barcodeSize: "300", //1,2,3
                barcodeDetails: "vcard2", //1 - Podstawowe, 2 - szczegółowe
                lang: "pl"
            };
        	
        	var options = $.extend(defaults, options);
        	
        	var container = getVCardContainer();
			
			$("#barcodeImageContainer").html(getBarCodeImg(tobiekt1id, options.barcodeDetails, options.barcodeSize));
			
			$("#barcodeSize option[value=" + options.barcodeSize + "]").attr('selected', 'selected');
			$("#barcodeDetailsLevel option[value=" + options.barcodeDetails + "]").each(function(){
				$(this).attr('selected', 'selected')
			});
			
			$("#barcodeSize").live("change", function(){
				options.barcodeSize = $("#barcodeSize").val();
				$("#barcodeImageContainer").html(getBarCodeImg(tobiekt1id, options.barcodeDetails, options.barcodeSize));
			});
			$("#barcodeDetailsLevel").live("change", function(){
				options.barcodeDetails = $("#barcodeDetailsLevel").val();
				$("#barcodeImageContainer").html(getBarCodeImg(tobiekt1id, options.barcodeDetails, options.barcodeSize));
			});
			
			$("#vcardContainerClose").button({
				icons: {
					primary: "ui-icon-close"
				}
			})
			
			$("#vcardContainer").lightbox_me({
				centered: true,
				closeSelector: "#vcardContainerClose"
			});
			
			
			
			
			
		}
    });
	
    $.fn.magickLinks = function(options){
        var defaults = {
            barcodeSize: "300", //1,2,3
            barcodeDetails: "vcard2", //1 - Podstawowe, 2 - szczegółowe
            lang: "pl"
        };
        var options = $.extend(defaults, options);
        
        var _this = this;
        this.regional = {
            en: {
                "Adres": "Address",
                "Strona WWW": "Website",
                "Telefon": "Phone",
                "Fax": "Fax",
                "vCard": "vCard",
                "Fotokod": "Photocode",
                "E-mail": "E-mail",
                "Wyślij vCard przez bluetooth": "Send vCard via Bluetooth"
            },
            de: {}
        }[options.lang];
        _this._ = $.i18n;
        
        $(".adres").attr("title", _this._("Adres"));
        $(".tel").attr("title", _this._("Telefon"));
        $(".fax").attr("title", _this._("Fax"));
        $(".vcard").attr("title", _this._("vCard"));
        $(".fotokod").attr("title", _this._("Fotokod"));
        if ($.fn.tipsy) 
            $(".www, .email, .tel, .fax, .vcard, .fotokod, .adres").tipsy({
                gravity: $.fn.tipsy.autoNS
            });
        
        
        
        
        $(this).each(function(){
            if ($(this).hasClass("email")) {
                $(this).append(function(){
                    var email = $(this).text();
                    email = email.replace(/\[dot\]/g, '.');
                    email = email.replace(/\[at\]/g, "@");
                    var link = $("<a title='" + _this._("E-Mail") + ": " + email + "' href='mailto:" + email + "'>" + email + "</a>");
                    if ($.fn.tipsy) {
                        link.tipsy({
                            gravity: $.fn.tipsy.autoNS,
                            live: true
                        });
                    }
                    
                    $(this).empty();
                    return link;
                });
            }
            
            if ($(this).hasClass("vcard")) {
					var link = $(this).attr("href");
					var vars = getUrlVars( link );
					
					var fotokodLink = $("<a href='#' class='fotokod'>Fotokod</a>").attr("title", _this._("Fotokod")).tipsy({
						gravity: $.fn.tipsy.autoNS,
						live: true
					}).click( function(){
						$.photocode(vars.tobiekt1Id);
					} );
					
				
                if (typeof infomatBtSendUrl != "undefined") {
                    var btLink = $("<a href='javascript:;' class='bluetooth'>Wyślij vCard przez bluetooth</a>").attr("title", _this._("Wyślij vCard przez bluetooth"));
                    btLink.click(function(){
                        window.open(infomatBtSendUrl + "?req=" + encodeURIComponent( baseUrl + link));
                    });
					$(this).after(fotokodLink);
					$(this).replaceWith(btLink);
                }  else{
					$(this).after(fotokodLink);
				}
				
				
				

                
                
                
            }
            
            if ($(this).hasClass("www")) {
                $(this).replaceWith(function(){
                    var link = $("<a class='www' title='" + $(this).text() + "' href='" + $(this).text() + "'>" + $(this).text().replace(/\./g, "<img src='" + resTplUrl + "/img/transparent.gif' alt='' width='0' height='0' />" + ".") + "</a>");
					
					if( $.fn.tipsy ) {
						link.tipsy({
	                        gravity: $.fn.tipsy.autoNS,
	                        live: true
	                    });						
					}

                    ;
                    return link;
                });
                
                
            }
            
            $("a[href^=http]").each(function(){
                if (this.href.indexOf(location.hostname) == -1) {
                    $(this).attr('target', '_blank');
                }
            })
            
            
            
            
        });
        

        

        
        
        
        
    }
})(jQuery);

