function ajaxSearch(selector,formId){
    $(formId+" .ui-button-submit").click(function(){
        return loadList($(formId).attr('action'),selector,formId);
    });
}
function ajaxList(selector,formId){
    var list = $(selector);
    $(".pager",list)
    .find("a").button().click(function(){
        return loadList(this.href,selector,formId);
    }).end()
    .find("span.current").addClass('ui-button-text').wrap('<span class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only ui-state-active" />').end()
    .find(".bt-prev a").button({icons:{primary:'ui-icon-seek-prev'},text:false}).end()
    .find(".bt-next a").button({icons:{primary:'ui-icon-seek-next'},text:false}).end()
    .find(".bt-prev-disabled").button({icons:{primary:'ui-icon-seek-prev'},text:false}).button('disable').end()
    .find(".bt-next-disabled").button({icons:{primary:'ui-icon-seek-next'},text:false}).button('disable')
    ;
    return list;
}
function loadList(url,selector,formId){
    showSpinner();
    $.get(url,$(formId).serialize(),function(html){
        $(selector).replaceWith(html);
        hideSpinner();
        //scroll(0,0);
        $(".pager-counter",selector).effect("pulsate");
    });
    return false;
}
function ajaxSubmit(formId,scrollOnTop){
   $(formId+" .ui-button-submit").click(function(){
        showSpinner();
        $.post($(formId).attr('action'),$(formId+" :input").serialize(),function(responce){
            if(responce.debug){
                $("#debug").html(responce.debug);
            }
            if(responce.status<0) {
                $(formId+" .ui-error-message").addClass("ui-widget ui-state-error ui-corner-all").html(responce.msg).effect("pulsate");
                $(formId+" em").empty();
                if(responce.data){
                    $.each(responce.data,function(key,value){
                        $("#err_"+key,formId).html($.isArray(value) ? value[0]: value);
                    });
                }
            }
            else{
                $(formId).clearForm();
                $(formId).replaceWith('<div class="ui-widget ui-state-highlight ui-corner-all ui-thanks-message">'+responce.msg+'</div>');
            }
            hideSpinner();
            if(scrollOnTop){scroll(0,0);}
        },"json");
        return false;
    });
}
function showSpinner(){$("#main_spinner").show();}
function hideSpinner(){$("#main_spinner").hide();}
function setCharsLimiter(){
    $(".ui-chars-limiter").each( function(index){
        var obj = $(this);
        var limit = obj.attr("maxlength");
        obj.after('<div class="ui-note ui-chars-counter">'+$.charslimiter.charsCount.replace('%d',limit)+'</div>').keyup(function(){
            var text = obj.val();
            if(text.length > limit) {
                obj.next().removeClass('ui-note').addClass("ui-state-error").html($.charslimiter.maxChars.replace('%d',limit));
                obj.val(text.substr(0,limit));
                return false;
            } else {
                obj.next().removeClass("ui-state-error").addClass('ui-note').html($.charslimiter.charsCount.replace('%d',(limit - text.length)));
                return true;
            }
        });
    });
}
function setDateRange(fromDateId,toDateID){
    $(".ui-date-range").datepicker({beforeShow:function(input){return{minDate:(input.id == toDateID ? $("#"+fromDateId).datepicker("getDate") : null),maxDate: (input.id == fromDateId ? $("#"+toDateID).datepicker("getDate") : null)};},showOn:"both",buttonImage:"/img/calendar.gif",buttonImageOnly:true,changeMonth:true,changeYear:true,yearRange:"c-4:c+4",dateFormat:"dd-mm-yy"});
}
function setDatePicker(fromDateId,toDateID){
    $(".ui-date").datepicker({showOn:"both",buttonImage:"/img/calendar.gif",buttonImageOnly:true,changeMonth:true,changeYear:true,yearRange:"c-100:c",dateFormat:"dd-mm-yy"});
}
function setNumeric(){
    $(".ui-numeric").keypress(function(e){if(e.which!=8 && e.which!=0 && (e.which<48 || e.which>57)){return false;}});
}
$.fn.clearForm = function() {
    return this.each(function() {
        var type = this.type, tag = this.tagName.toLowerCase();
        if (tag == "form") return $(":input",this).clearForm();
        if (type == "text" || type == "password" || tag == "textarea")
            this.value = "";
        else if (type == "checkbox" || type == "radio")
            this.checked = false;
        else if (tag == "select")
            this.selectedIndex = -1;
    });
};
function MM_openBrWindow(theURL,winName,features){window.open(theURL,winName,features);}

MapUtil = {
    options:[],
    loadMap:function(mapId,options) {
        var gm;
        MapUtil.options = options;
        if (GBrowserIsCompatible()){
            gm = new GMap2(document.getElementById(mapId));
            gm.enableDoubleClickZoom();
            gm.addControl(new GLargeMapControl());
            gm.addControl(new GMapTypeControl());
            gm.setCenter(new GLatLng(options.lat,options.lng),options.zoom);
            for(var i = 0; i<options.data.length; ++i){
                gm.addOverlay(MapUtil.createMarker(options.data[i],options.iconW,options.iconH));
            }
            return gm;
        }
    },
    createMarker:function(item,iconW,iconH) {
        var item = $.extend({'Struct':{'lat':0,'lng':0,'name':'','city':'','zip_code':'','address':'','page_url':''},'Province':{'name':'','abbr':''}},item||{}),
            m=new GMarker(new GLatLng(item['Struct']['lat'],item['Struct']['lng']),MapUtil.createIcon('/img/maps/icons/'+iconW+'x'+iconH+'/'+item['StructTipology']['icon'],iconW,iconH))
            //m=new GMarker(new GLatLng(item['Struct']['lat'],item['Struct']['lng']))
            ;
        var openInfoWindow = function(){
            m.openInfoWindowHtml(
            '<div class="gmap-info-window" style="text-align:center;padding:10px 0;">'+
                '<h4 style="font-size:1.2em;font-weight:bold;">'+item['Struct']['name']+'</h4>'+
                '<div>'+
                    item['Struct']['address']+'<br />'+
                    item['Struct']['zip_code']+' '+item['Struct']['city']+' ('+item['Province']['abbr']+')<br />'+
                    $.coreTrans.tel+': '+item['Struct']['tel']+'<br />'+
                    (item['Struct']['page'] == '1' ? '<a title="'+$.coreTrans.scheda+'" style="margin-top:5px;" class="bt-scheda ui-button ui-widget ui-state-active ui-corner-all ui-button-text-only" href="'+item['Struct']['page_url']+'"><span class="ui-button-text uppercase" style="padding:0 1em">'+$.coreTrans.scheda+'</span></a>' : '')+
                '</div>'+
            '</div>');
        };
        GEvent.addListener(m, "click",openInfoWindow);
        if(MapUtil['options']['autoOpen'] && MapUtil['options']['autoOpen']==true){
            openInfoWindow();
        }
        return m;
    },
    createIcon:function(iconUrl,iconW,iconH){
        var ico=new GIcon();
        ico.image=iconUrl;
        ico.iconSize=new GSize(iconW,iconH);
        ico.iconAnchor=new GPoint(iconW/2,iconH/2);
        ico.infoWindowAnchor=new GPoint(iconW/2,iconH/2);
        return ico;
    }
};
/*
 * jQuery UI Tooltip
 *
 * Copyright 2011, AUTHORS.txt (http://jqueryui.com/about)
 * Dual licensed under the MIT or GPL Version 2 licenses.
 * http://jquery.org/license
 *
 * http://docs.jquery.com/UI/Tooltip
 *
 * Depends:
 *    jquery.ui.core.js
 *    jquery.ui.widget.js
 *    jquery.ui.position.js
 */
(function($){var f=0;$.widget("ui.tooltip",{options:{width:'auto',items:"[title]",content:function(){return $(this).attr("title")},position:{my:"left center",at:"right center",offset:"15 0"}},_create:function(){var b=this;this.tooltip=$("<div></div>").attr("id","ui-tooltip-"+f++).attr("role","tooltip").attr("aria-hidden","true").addClass("ui-tooltip ui-widget ui-corner-all ui-widget-content").appendTo(document.body).hide();this.tooltipContent=$("<div></div>").addClass("ui-tooltip-content").appendTo(this.tooltip);this.opacity=this.tooltip.css("opacity");this.element.bind("focus.tooltip mouseover.tooltip",function(a){b.open(a)}).bind("blur.tooltip mouseout.tooltip",function(a){b.close(a)})},enable:function(){this.options.disabled=false},disable:function(){this.options.disabled=true},_destroy:function(){this.tooltip.remove()},widget:function(){return this.element.pushStack(this.tooltip.get())},open:function(b){var c=$(b&&b.target||this.element).closest(this.options.items);if(this.current&&this.current[0]==c[0])return;var d=this;this.current=c;this.currentTitle=c.attr("title");var e=this.options.content.call(c[0],function(a){setTimeout(function(){if(d.current==c)d._show(b,c,a)},13)});if(e){d._show(b,c,e)}},_show:function(a,b,c){if(!c)return;b.attr("title","");if(this.options.disabled)return;this.tooltipContent.html(c);this.tooltip.css({top:0,left:0,'max-width':this.options.width}).show().position($.extend({of:b},this.options.position)).hide();this.tooltip.attr("aria-hidden","false");b.attr("aria-describedby",this.tooltip.attr("id"));this.tooltip.stop(false,true).fadeIn();this._trigger("open",a)},close:function(a){if(!this.current)return;var b=this.current;this.current=null;b.attr("title",this.currentTitle);if(this.options.disabled)return;b.removeAttr("aria-describedby");this.tooltip.attr("aria-hidden","true");this.tooltip.stop(false,true).fadeOut();this._trigger("close",a)}});$.ui.tooltip.version="@VERSION"})(jQuery);
