var nimbus = { src: 'frontend/flash/ne19046t.swf' };
sIFR.activate(nimbus);

var cmc = null;
var translate = [];

$().ready(function () {
	set_lang();
	cmc = new main();
	cmc.init();
    rotate_links();
});

var set_lang = function ()
{
	if (_t) translation = _t;
};

var _ = function (text)
{
	if (translation[text])
		return translation[text];
	else
		return text;
};

var _s = function ()
{
	if (arguments.length > 0)
	{
		args = Array.prototype.slice.call(arguments);
		var text = args.shift();
		if (translation[text])
			return $.vsprintf(translation[text], args);
		else
			return $.vsprintf(text, args);
	}
};

var main = function ()
{
	var $$ = this;
	var iboxtimr = null;
	var mappos = [[52.271793499999994, 20.942808499999998], [52.190235, 21.045039]]
	// galla anonima, pory
	this.init = function ()
	{
		var flash = {
			width: 950,
			height: 307,
			autoplay: true,
			src: 'frontend/flash/top.swf',
			params: { menu: false, wmode: 'transparent' }
		};
		
		if ($('#top-flash').size() > 0) $('#top-flash').media(flash);
		if ($('#content-ibox').size() > 0) this.ibox();
		if ($('h2.header, h2.header-doc, h2.header-pat, h2.header-spo').size() > 0) this.headers();
		if ($('#contact-map').size() > 0) this.map();
		
		$('.lightbox').lightbox({ fitToScreen: true, imageClickClose: false });
	};
	
	this.map = function ()
	{
		var _opts = { clickable: true, draggable: false };
		var _marker1 = new GMarker(new GLatLng(mappos[0][0], mappos[0][1]), _opts); //galla anonima
		var _marker2 = new GMarker(new GLatLng(mappos[1][0], mappos[1][1]), _opts); //pory
		GEvent.addListener(_marker1, 'click', function () { _marker1.openInfoWindowHtml('<strong>' + _('Carolina Medical Center') + '</strong><br />' + _('ul. Broniewskiego 89 (dojazd od ul. Galla Anonima)') + '<br />' + _('01-876 Warszawa')); })
		GEvent.addListener(_marker2, 'click', function () { _marker2.openInfoWindowHtml('<strong>' + _('Carolina Medical Center') + '</strong><br />' + _('ul. Pory 78') + '<br />' + _('02-757 Warszawa')); })
		
		$$.map = new google.maps.Map2($('#contact-map').get(0));
		$$.map.setCenter(new google.maps.LatLng(mappos[1][0], mappos[1][1]), 15);
		$$.map.addControl(new GSmallZoomControl3D());
		$$.map.addControl(new GMenuMapTypeControl());
		$$.map.addOverlay(_marker1);
		$$.map.addOverlay(_marker2);
		
		$$.mapdirs = new GDirections($$.map);
	};
	
	this.map_switch = function (i, e)
	{
		$$.map.panTo(new google.maps.LatLng(mappos[i - 1][0], mappos[i - 1][1]));
		$('#contact-map-tabs').find('a').removeClass('c');
		$(e).addClass('c');
		if ($$.mapdirs.getNumRoutes() > 0) $$.route();
		return false;
	};
	
	this.route = function ()
	{
		var _idx = $('#contact-map-tabs').find('a').index($('#contact-map-tabs').find('a.c'));
		$$.mapdirs.clear();
		//alert(_idx);
		_idx = Math.abs(1-_idx);
		$$.mapdirs.load('from: ' + $('#route-s').val() + ' to: ' + mappos[_idx][0] + ', ' + mappos[_idx][1]);
		return false;
	};
	
	this.headers = function ()
	{
		sIFR.replace(nimbus, { fixWrap: true, fitExactly: true, forceWidth: true, transparent: true, thickness: -50, forceSingleLine: true, selectable: false, selector: 'h2.header', css: '.sIFR-root { font-size: 16px; text-transform: uppercase; color: #a4d0d9; }' });
		sIFR.replace(nimbus, { fixWrap: true, fitExactly: true, forceWidth: true, transparent: true, thickness: -50, forceSingleLine: true, selectable: false, selector: 'h2.header-doc', css: '.sIFR-root { font-size: 16px; text-transform: uppercase; color: #00a984; }' });
		sIFR.replace(nimbus, { fixWrap: true, fitExactly: true, forceWidth: true, transparent: true, thickness: -50, forceSingleLine: true, selectable: false, selector: 'h2.header-pat', css: '.sIFR-root { font-size: 16px; text-transform: uppercase; color: #4a69b4; }' });
		sIFR.replace(nimbus, { fixWrap: true, fitExactly: true, forceWidth: true, transparent: true, thickness: -50, forceSingleLine: true, selectable: false, selector: 'h2.header-spo', css: '.sIFR-root { font-size: 16px; text-transform: uppercase; color: #ff0000; }' });
	};
	
	this.ibox = function (p)
	{
		if (p)
		{
			if (p == 'next')
			{
				var max = $('#content-ibox>ul>li').size();
				if (max > 1)
				{
					var cur = $('#content-ibox>ul>li').index($('#content-ibox>ul>li.current')) + 1;
					var nxt = cur < max ? cur : 0;
					$('#content-ibox>ul').find('li.current').removeClass('current').end().find('li:eq(' + nxt + ')').addClass('current');
					$('#content-ibox>div.ibox-item:visible').fadeOut('slow');
					$('#content-ibox>div.ibox-item:eq(' + nxt + ')').fadeIn('slow');
				}
			}
			else if (!$('#content-ibox>ul>li:eq(' + (p - 1) + ')').is('.current'))
			{
				$('#content-ibox>ul').find('li.current').removeClass('current').end().find('li:eq(' + (p - 1) + ')').addClass('current');
				$('#content-ibox>div.ibox-item:visible').fadeOut('slow');
				$('#content-ibox>div.ibox-item:eq(' + (p - 1) + ')').fadeIn('slow');
			}
		}
		else
		{
			$('#content-ibox').find('ul').empty();
			$('#content-ibox').find('div.ibox-item').each(function (x, y) {
				$('#content-ibox').find('ul').append('<li' + (x == 0 ? ' class="current"' : '') + '><a href="#" onclick="return cmc.ibox(' + (x + 1) + ')">' + (x + 1) + '</a></li>');
				if (x > 0) $(y).hide();
			});
			$('#content-ibox').mouseover(function () { if ($$.iboxtimr) { clearInterval($$.iboxtimr); $$.iboxtimr = null; } }).mouseout(function () { $$.iboxtimr = setInterval(function () { $$.ibox('next'); }, 5000); });
			$$.iboxtimr = setInterval(function () { $$.ibox('next'); }, 15000);
		}
		return false;
	};
};

/* wyswietlanie flash banera */
$(function(){
    var top_layer = $('div#top_layer');

    if (top_layer.length > 0)
    {
        top_layer.width($(document).width());
        top_layer.height($(document).height());

        var content = $('div#top_layer_content');
        var flash = $('div#top_layer_content object:first');

        var width = flash.attr('width');
        var height = flash.attr('height');

        var top = (($(window).height() / 2) - (height / 2)) + $(window).scrollTop();
        var left = (($(window).width() / 2) - (width / 2));

        content.height(height);
        content.width(width);
        content.css('top', top + 'px');
        content.css('left', left + 'px');

        content.click(function(event){
            event.stopPropagation();
        });

        top_layer.click(function(){
            top_layer.remove();
        });

        $('div#top_layer_content img#close_button').click(function(){
            top_layer.remove();
        });

        top_layer.show();
    }
});

/* rotacja linkow-logo */
var rotate_links = function()
{
    var links_list = $('div#links ul');

    if (links_list.find('li').length > 0)
    {
        var first = links_list.find('li').eq(0);

        first.animate({marginLeft: -(parseInt(first.width()) + 10)}, 1000, function(){
            first.remove();
            links_list.append(first);
            first.css('margin', 0);
        });

        var timeout = setTimeout('rotate_links()', 5000);
    }
}

