$().ready(function () {
	apteki.init();
});

var apteki = {
	map: null,
	me: null,
	ib: null,
	obj: [],
	
	init: function ()
	{
		if ($('#where').size() > 0) 
		{
			$('#where')
				.data('placeholder', $('#where').attr('title'))
				.focus(function () { 
					if ($('#where').val() == $('#where').data('placeholder')) 
						$('#where').removeClass('na').val('');
				})
				.blur(function () {
					if ($(this).val() == '') $(this).addClass('na').val($('#where').data('placeholder'));
				})
				.parents('form')
					.submit(function () {
						if ($('#where').val() == $('#where').data('placeholder') || $('#where').val().length < 3) return false;
					});
			if ($('#where').val() == $('#where').data('placeholder')) $('#where').addClass('na');
		}
		
		if ($('#map').size() > 0)
		{
			this.map = new google.maps.Map($('#map').get(0), { zoom: 13, center: new google.maps.LatLng(52.228405, 21.012446), mapTypeId: google.maps.MapTypeId.ROADMAP, mapTypeControlOptions: { style: google.maps.MapTypeControlStyle.DROPDOWN_MENU }});
			google.maps.event.addListener(this.map, 'click', function () { if (apteki.ib != null) apteki.ib.close(); });
			
			if (typeof mp != 'undefined' && mp)
			{
				var _mps = new google.maps.LatLng(mp[0], mp[1]);
				var _mii = new google.maps.MarkerImage(cp + '/frontend/images/phmm.png', new google.maps.Size(28, 30), new google.maps.Point(0, 0), new google.maps.Point(13, 27));
				var _mis = new google.maps.MarkerImage(cp + '/frontend/images/phms.png', new google.maps.Size(28, 30), new google.maps.Point(0, 0), new google.maps.Point(13, 27));
				
				this.me = new google.maps.Marker({ position: _mps, map: this.map, shadow: _mis, icon: _mii, clickable: false, draggable: false });
				this.map.setCenter(_mps);
			}
			
			if (typeof md != 'undefined' && md)
			{
				var _b = new google.maps.LatLngBounds();
				$.each(md, function (x, y) 
				{
					var _pps = new google.maps.LatLng(y.la, y.lo);
					var _pii = new google.maps.MarkerImage(cp + '/frontend/images/phm' + (y.ft && y.ft == 1 ? 'f' : '') + '.png', new google.maps.Size(28, 30), new google.maps.Point(0, 0), new google.maps.Point(13, 27));
					var _pis = new google.maps.MarkerImage(cp + '/frontend/images/phms.png', new google.maps.Size(28, 30), new google.maps.Point(0, 0), new google.maps.Point(13, 27));
					
					apteki.obj[y.id] = new google.maps.Marker({ position: _pps, map: apteki.map, shadow: _pis, icon: _pii, clickable: true, draggable: false });
					google.maps.event.addListener(apteki.obj[y.id], 'click', function () { apteki.show(y.id); });
					_b.extend(_pps);
				});
				if (md.length > 1 && mc === false)
					this.map.fitBounds(_b);
				else
				{
					this.map.setCenter(new google.maps.LatLng(md[0].la, md[0].lo));
					this.map.setZoom(15);
				}
			}
		}
		
		if ($('ul.pharms').size() > 0) this.hovers();
		if ($('a[href^="mailto:"]').size() > 0) this.mails();
		if ($('a[href^="http"]').size() > 0) this.links();
		if ($('a.permalink').size() > 0) $('a.permalink').click(function () { $(this).get(0).target = '_blank'; });
	},
	
	tab: function (i, e)
	{
		$('#tabs').find('div.tab').hide().end().find('div.tab-' + i).show();
		$(e).parents('ul').find('li').removeClass('c').end();
		$(e).parents('li').addClass('c');
		return false;
	},
	
	show: function (i)
	{
		if (apteki.ib != null)
			apteki.ib.close();
		apteki.ib = new google.maps.InfoWindow({ content: '<div class="infowindow">' + $('#p' + i).html() + '</div>', pixelOffset: new google.maps.Size(0, 5) });
		apteki.ib.open(this.map, apteki.obj[i]);
		return false;
	},
	
	hovers: function ()
	{
		$('ul.pharms').find('li').hover(function () { $(this).addClass('hv'); }, function () { $(this).removeClass('hv') });
		$('ul.pharms').find('li').not('a').click(function (e) { if (e.target.nodeName != 'A') { apteki.show($(this).attr('id').substring(1)); return false; }});
	},
	
	mails: function ()
	{
		$('a[href^="mailto:"]').click(function () { $(this).attr('href', $(this).attr('href').replace(/-?usun_to-?/g, '')); })
	},
	
	links: function ()
	{
		$('a[href^="http"]').not('[href^="http://apteki"]').click(function () { $(this).attr('target', '_blank'); });
	},
	
	page: function (p, t)
	{
		$('div.pages-' + t).find('li').removeClass('c').end().find('li:eq(' + (p - 1) + ')').addClass('c');
		$('div.tab-' + t).find('li.opr').addClass('phid').end().find('li.ppg' + (p - 1)).removeClass('phid');
		return false;
	}
};
