/* Copyright (C) YOOtheme GmbH, YOOtheme Proprietary Use License (http://www.yootheme.com/license) */

jQuery(function(a){a('select[name="settings[style]"]').bind("change",function(){a("#form").trigger("submit")});a("button.action.add").bind("click",function(){a.post(widgetkitajax+"&task=item_map",{id:a('input[name="id"]').val()},function(b){a(b).appendTo("#items");a("#order").trigger("update")})});0==a('input[name="id"]').val()&&a("button.action.add").trigger("click");a("#items").delegate(".box","delete",function(){a(this).fadeOut(300,function(){a(this).remove();a("#order").trigger("update")})});
a("#items").delegate("input.title","update",function(){var b=a(this).closest(".item"),c=a(this).val()?a(this).val():"untitled",e=b.find("h3.title");b.is(".item:first-child")?e.html(c+'<span class="info">center</span>'):e.html(c);a('#order li[rel="'+b.attr("id")+'"]').html(c)});a("#order").sortable({axis:"y",start:function(b,c){a("#"+c.item.attr("rel")).addClass("sortactive")},stop:function(b,c){setTimeout(function(){a("#"+c.item.attr("rel")).removeClass("sortactive")},800)},update:function(b,c){var e=
a("#"+c.item.attr("rel")),d=c.item.next(),f=c.item.prev();e.find(".html-editor").trigger("editor-action-start");d.length?e.insertBefore(a("#"+d.attr("rel"))):e.insertAfter(a("#"+f.attr("rel")));e.find(".html-editor").trigger("editor-action-stop");a("#order").trigger("update")}}).bind("update",function(){var b=a(this);a("li",this).each(function(){a("#"+a(this).attr("rel")).length||a(this).remove()});a("#items > .item").each(function(){var c=a(this).attr("id");b.find("[rel='"+c+"']").length||b.append('<li rel="'+
c+'"></li>');a("input.title",this).trigger("update")})}).trigger("update");var h=a("#addresslocator"),k=h.find("input.address"),d=h.find("input.lat"),f=h.find("input.lng"),g=h.find("div.map"),j=new google.maps.Map(g.get(0),{zoom:4,streetViewControl:!1,center:new google.maps.LatLng(-34.397,150.644),mapTypeId:google.maps.MapTypeId.ROADMAP});""!=d.val()&&""!=f.val()?g=new google.maps.LatLng(d.val(),f.val()):(g=new google.maps.LatLng(51.151786,10.415039),d.val(51.151786),f.val(10.415039));j.setCenter(g);
var i=new google.maps.Marker({position:g,draggable:!0,map:j});google.maps.event.addListener(i,"dragend",function(){d.val(i.position.lat());f.val(i.position.lng())});a("#items").delegate(".loc-data","click",function(){var b=a(this),c=b.find("span:first"),e=b.find('input[name$="[lat]"]'),g=b.find('input[name$="[lng]"]');k.autocomplete({delay:500,minLength:3,source:function(b,c){a.getJSON(widgetkitajax+"&task=locate_map",{address:k.val()},function(b){c(a.map(b.results,function(a){return{label:a.formatted_address,
value:a.formatted_address,lat:a.geometry.location.lat,lng:a.geometry.location.lng}}))})},select:function(a,b){d.val(b.item.lat);f.val(b.item.lng);var c=new google.maps.LatLng(d.val(),f.val());i.setPosition(c);j.setCenter(c)}});k.val("");d.val(e.val());f.val(g.val());b=new google.maps.LatLng(e.val(),g.val());i.setPosition(b);j.setCenter(b);h.find("button").unbind("click").bind("click",function(){e.val(d.val());g.val(f.val());c.html("(Lat: "+d.val()+", Lng: "+f.val()+")");a.modalwin.close()});a.modalwin(h)})});
