MAP = {

	init: function()
	{
		if(GBrowserIsCompatible())
		{
			var map 		= new GMap2(document.getElementById('map_canvas'));
			var geocoder 	= new GClientGeocoder();
			var address		= 'AB Laholms Lito, Skallebackav&auml;gen 31, 300 07 Halmstad, Sweden';

			map.setUIToDefault();

			geocoder.getLatLng(
			address,
				function(point)
				{
					if(!point)
					{
						 alert(address + ' not found');
					}
					else
					{
						map.setCenter(point, 13);
						var marker = new GMarker(point);
						map.addOverlay(marker);
						marker.openInfoWindowHtml(address);
					}
				}
			);
		}
	}
}

CONTACT = {

	init: function(lang)
	{
		var ts = Math.round(new Date().getTime() / 1000);
		var xmlPath = 'xml/' + lang + '/kontakt.xml#' + ts;

		swfobject.embedSWF('swf/flshow-carousel.swf#' + ts, 'carousel1', '100%', '100%', '9.0.0', false, { xmlfile: xmlPath, loaderColor: '0xFFFFFF' }, { wmode: 'transparent', allowScriptAccess: 'always' });

		return true;
	},

	show: function(id)
	{
		$('.contact-info').hide();
		$('#' + id).fadeIn();
	}
}

REFERRERS = {

	labels: '',

	init: function()
	{
		$('#referrers a').fancybox(
		{
			'overlayShow':		true,
			'overlayOpacity':	0.8,
			'overlayColor':		'#000000',
			'padding': 		10,
			'margin':		0,
			'titleShow':		true,
			'titlePosition':	'inside',
			'transitionIn':		'elastic',
			'transitionOut':	'elastic'
		});

		return false;
	}
}

EMAIL = {

	init: function()
	{
		$('a.email').each(function()
		{
			if($(this).attr('rel'))
				$(this).attr('href', 'mailto:' + EMAIL.reverse($(this).attr('rel')));

			$(this).bind('mouseover', function()
			{
				$(this).css('background-image', 'url(images/mail_icon_orange.png)');
				return false;
			});

			$(this).bind('mouseout', function()
			{
				$(this).css('background-image', 'url(images/mail_icon.png)');
				return false;
			});
		});

		return true;
	},

	reverse: function(str)
	{
		if(!str)
			return false;

		var revstr = '';

		for(i = str.length-1; i>=0; i--)
			revstr += str.charAt(i);

		return revstr;
	}

};

SWAP = {

	speed: 	600,
	n: 		1,

	init: function()
	{
		$('#retcontent').onImagesLoad(
		{
			selectorCallback: selectorImagesLoaded
		});

		function selectorImagesLoaded($selector)
		{
			SCROLLER.init();
			$('#retcontent').removeClass('loading');
			SWAP.image(1);
		}

		return true;
	},

	rebind: function()
	{
		$('#retcontent img').unbind('mouseover').unbind('mouseout');

		$('#ret' + SWAP.n).bind('mouseover', function()
		{
			$('#foto' + SWAP.n).show();
			$('#ret' + SWAP.n).fadeOut(SWAP.speed / 3, function(){});

			$('#foto' + SWAP.n).bind('mouseout', function()
			{
				$('#ret' + SWAP.n).fadeIn(SWAP.speed / 3, function(){});
			});
		});
	},

	image: function(n)
	{
		SWAP.n = n;
		SWAP.rebind();
		$('#images div').removeClass('clear');
		$('#frame' + SWAP.n).addClass('clear');
		$('#retcontent img').hide();
		$('#ret' + SWAP.n).fadeIn(SWAP.speed, function(){});

		return false;
	}
}

FLVPLAYER = {

	url:	'/resources/flowplayer/flowplayer.swf',

	load: function(obj, src, id)
	{
		$('#swfcontent').remove();
		$('.temp').prepend('<div id="swfcontent" style="width: 934px; height: 430px; visibility: visible!important; display: block;"></div>');
		$('#swfcontent').addClass('loading');

		$f("swfcontent", FLVPLAYER.url,
		{
			clip:
			{
				url: 				src,
				autoPlay: 			true,
				autoBuffering: 		true,
				scaling:			'fit'
			},
			canvas:
			{
				backgroundColor: 		'#ffffff',
				backgroundGradient: 	'none'
			},

			plugins:
			{
				controls: 			null
			},
			onLoad: function()
			{
				$('#images').css('left', SCROLLER.pos);
				$('#swfcontent').removeClass('loading');
				return false;
			}
		});

		return false;
	}
};

var SWFOBJECT = {

	load: function(obj, src, id, flvfile, width, height)
	{
		if(!width)
			width = 934;

		if(!height)
			height = 430;

		if(flvfile != '')
			var flashvars = { 'flvfile': flvfile };
		else
			var flashvars = {};

		var attributes = {};

		var params =
		{
			scale:	"default",
			menu: 	"false",
			wmode:	"transparent"
		};

		swfobject.embedSWF(src, id, width, height, "9.0.0", null, flashvars, params, attributes, SWFOBJECT.done);

		return false;
	},

	done: function(e)
	{
		$('#' + e.id).show().addClass('loading');
		return false;
	}

};

var SCROLLER = {

	speed: 	400,
	width:	0,
	step:		0,
	pos:		0,
	count:	0,
	limit:	0,
	cpos:		0,

	init: function()
	{
		SCROLLER.count	= $('.frame').length;
		SCROLLER.step 	= $('#images div').width() + 10;
		SCROLLER.width	= SCROLLER.count * SCROLLER.step;
		SCROLLER.pos 	= parseInt($('#images').css('left'));

		$('#images').css('width', SCROLLER.width + 'px');
		$('#sleft').css('background-image', 'url(..)').css('cursor','default');

		if(SCROLLER.width < $('#view').width())
		{
			$('#sright').hide();
		}
		else
		{
			$('#sleft').bind('click', function()
			{
				SCROLLER.left();
			});

			$('#sright').bind('click', function()
			{
				SCROLLER.right();
			});
		}
	},

	left: function()
	{
		$('#sright').show();

		if(SCROLLER.pos < 0)
		{
			$('#images').animate(
			{
				left: SCROLLER.pos + (SCROLLER.step * 3)
			}, SCROLLER.speed, function()
			{
				SCROLLER.pos = parseInt($('#images').css('left'));
				SCROLLER.cpos -= 3;

				if(SCROLLER.cpos == 0)
				{
					$('#sleft').css('background-image', 'url(..)').css('cursor','default');
				}
			});
		}
	},

	right: function()
	{
		$('#sleft').css('background-image', 'url(../../resources/gfx/arrow-left.png)').css('cursor','pointer');

		SCROLLER.limit = (SCROLLER.pos - SCROLLER.step) - (SCROLLER.width * -1);

		if(SCROLLER.limit > 680)
		{
			$('#images').animate(
			{
				left: SCROLLER.pos - (SCROLLER.step * 3)
			}, SCROLLER.speed, function()
			{
				SCROLLER.pos = parseInt($('#images').css('left'));
				SCROLLER.cpos += 3;

				if((SCROLLER.cpos + 5) >= SCROLLER.count)
				{
					$('#sright').hide();
				}
			});
		}
	}

};

var BOX = {

	speed: 	600,
	closed: 	0,
	wait:		1000,

	init: function()
	{
		$('.tabs .button').bind('click', function()
		{
			BOX.toggle();
		});

		BOX.timer = setInterval('BOX.toggle()', BOX.wait);
	},

	toggle: function()
	{
		clearInterval(BOX.timer);

		if(BOX.closed == 0)
			BOX.open();
		else
			BOX.close();

		return false;
	},

	open: function()
	{
		BOX.closed = 1;

		$('.box').show().animate(
		{
			top: 0
		}, BOX.speed, function()
		{
			$('.tabs .arrow').removeClass('down').addClass('up');
		});
	},

	close: function()
	{
		BOX.closed = 0;

		$('.box').animate(
		{
			top: -540
		}, BOX.speed, function()
		{
			$('.box').hide();
			$('.tabs .arrow').removeClass('up').addClass('down');
		});
	}
};

var SLIDESHOW = {

	count: 	0,
	length: 	0,
	interval:	3000,
	speed:	1000,
	timer:	'',
	debug:	false,

	init: function()
	{
		SLIDESHOW.length = $('#slideshow img').length;
		$('#slideshow').onImagesLoad(
		{
			selectorCallback: selectorImagesLoaded
		});

		function selectorImagesLoaded($selector)
		{
			SLIDESHOW.start();
		}

		return false;
	},

	start: function()
	{
		$('#slideshow').css('background', 'url()');
		$('#slideshow img:eq(' + SLIDESHOW.count + ')').fadeIn(SLIDESHOW.speed);

		if(SLIDESHOW.debug == true)
			alert('All ' + SLIDESHOW.length  + ' images loaded. Starting slideshow from frame ' + SLIDESHOW.count);

		SLIDESHOW.timer = setInterval('SLIDESHOW.next()', SLIDESHOW.interval);

		return true;
	},

	next: function()
	{
		$('#slideshow img:eq(' + SLIDESHOW.count + ')').fadeOut(SLIDESHOW.speed);

		SLIDESHOW.count++;
		SLIDESHOW.reset();
		$('#slideshow img:eq(' + SLIDESHOW.count + ')').fadeIn(SLIDESHOW.speed);

		return true;
	},

	reset: function()
	{
		if(SLIDESHOW.count >= SLIDESHOW.length)
		{
			SLIDESHOW.count = 0;
			return false;
		}

		return true;
	},

	stop: function()
	{
		clearInterval(SLIDESHOW.timer);

		return true;
	}

};

var CONFIG = {

	toggle: function(o)
	{
		$(o).parent().parent().find('textarea').toggle();
		return false;
	}
};

var POPUP = {

	show: function()
	{
		$.fancybox('<span style="color: #333;">' + $('#text').html() + '<br /><br /></span>',
		{
			'overlayShow':		true,
			'overlayOpacity':	0.8,
			'overlayColor':		'#000000',
			'padding': 		30,
			'margin':		20,
			'width':		600,
			'height':		'auto',
			'autoDimensions': 	false,
			'titleShow':		true,
			'titlePosition':	'inside',
			'transitionIn':		'elastic',
			'transitionOut':	'elastic'
		});
		
		return false;
	}

};
