var base_url = 'http://www.netsikker.dk/';

$ (function()
{
	$ ('#screenshots').jCarouselLite ({
		btnNext: 'img#next',
		btnPrev: 'img#prev',
		scroll: 4,
		visible: 4
	});

	$ ('a#showCategories').click (function()
	{
		$offset = $ (this).offset();
		$ ('div#categories').css ({ top: ($offset.top + 45) + 'px', left: ($offset.left) + 'px' }).slideToggle();
	});
	
	$ ('div#categories').hover (function()
	{
	
	},
	function()
	{
		$a = { backgroundColor: '', backgroundImage: '', backgroundRepeat: '', borderLeft: '', borderRight: '' };
		$ ('a#showCategories').css ($a);
		$ (this).fadeOut();
	});
	
	$ ('img#img_submit').click (function()
	{
		$ (this).parent ('form').submit();
	});
	
	$ ('input[name=website]').focus (function()
	{
		if ($ (this).val() === 'www.ditnavnher.dk')
			$ (this).css ('color', '#000').val ('');
	}).blur (function()
	{
		if ($ (this).val() === '')
			$ (this).css ('color', '#A1A1A1').val ('www.ditnavnher.dk');
	});
	
	var mouseover = false;
	$ ('div#right_content img.screenshot').hover (function()
	{
		$ ('div#right_content img.screenshot').fadeTo ('fast', .5);
		$ (this).stop().fadeTo ('fast', 1);
		
		mouseover = true;
	}, function()
	{
		mouseover = false;
	}).mouseleave (function()
	{
		window.setTimeout (function()
		{
			if (mouseover === false)
				$ ('div#right_content img.screenshot').stop().fadeTo ('fast', 1);
		}, 800);
	});
	
	$ ('a.give_vote').fancybox ({
		transitionIn: 'elastic',
		transitionOut: 'elastic'
	});
	
	$ ('input#question').keyup (function (e)
	{
		if (e.keyCode === 13)
			$ ('img#ask').trigger ('click');
	});
	
	$ ('img#ask').click (function()
	{
		if ($ ('#question').val() !== 'Har du et spørgsmål til Netbetjenten?')
		{
			$ ('#answer').html ('Jeg tænker..');
			$.post (base_url + 'ajax/get_answer', { question: $ ('#question').val() }, function (answer)
			{
				$ ('#answer').html (answer).slideDown();
			});
		}
	});
	
	$ ('a#popUpLogin').fancybox ({
		transitionIn: 'elastic',
		transitionOut: 'elastic'
	});
	
	$ ('a#popupCase').fancybox ({
		transitionIn: 'elastic',
		transitionOut: 'elastic'
	});
	
	$ ('#complainbtn').fancybox ({
		transitionIn: 'elastic',
		transitionOut: 'elastic'
	});

	mouseOver = false;
	
	$ ('img.mouseoverEffect').hover (function()
	{
		$this = $ (this);
		$myOffset = $ (this).offset();
		$myHeight = ($ (this).height() + 4);
		$myWidth = ($ (this).width() + 4);
		
		if ($ ('#overlayImage').size() < 1 && $ ('#overlayImageLink').size() < 1)
		{
			$ ('body').prepend ('<div id="overlayImage" style="position: absolute; background-color: #A1A1A1; opacity: .5; -moz-opacity: .5; -webkit-opacity: .5; width: ' + $myWidth + 'px; height: ' + $myHeight + 'px; top: ' + $myOffset.top + 'px; left: ' + $myOffset.left + 'px;" />');
			$ ('body').prepend ('<a href="' + base_url + 'webshops/' + $this.attr ('data-link') + '" id="overlayImageLink" style="background-color: #FFF; padding: 10px 25px; text-decoration: none; color: #555; text-shadow: #F2F2F2 1px 1px 0; font-weight: bold; display: block; position: absolute; top: ' + ($myOffset.top + 70) + 'px; left: ' + ($myOffset.left + 2) + 'px; width: 204px; text-align: center; z-index: 10000;">' + $this.attr ('title') + '</a>');
		}
	});
	
	$ ('#overlayImageLink').live ('mouseover', function()
	{
		mouseOver = true;
	}).live ('mouseout', function()
	{
		mouseOver = false;
		
		window.setTimeout (function()
		{
			if ( ! mouseOver)
			{
				$ ('#overlayImage').remove();
				$ ('#overlayImageLink').remove();
			}
		});
	});
	
	$ ('#overlayImage').live ('mouseover', function()
	{
		mouseOver = true;
	}).live ('mouseout', function()
	{
		mouseOver = false;
		
		window.setTimeout (function()
		{
			if ( ! mouseOver)
			{
				$ ('#overlayImage').remove();
				$ ('#overlayImageLink').remove();
			}
		});
	});
	
	$ ('#question').focus (function()
	{
		if ($ (this).val() === 'Har du et spørgsmål til Netbetjenten?')
			$ (this).val ('');
	}).blur (function()
	{
		if ($ (this).val() === '')
			$ (this).val ('Har du et spørgsmål til Netbetjenten?');
	});
	
	if ($ ('.chzn-select').size() > 0)
	{
		$ ('.chzn-select').chosen();
	}
});
