
var img_fadein_state = [];
var img_fadein_counter = 0;

function elementInViewport2(el)
{
  var top = el.offsetTop;
  var left = el.offsetLeft;
  var width = el.offsetWidth;
  var height = el.offsetHeight;

  while(el.offsetParent) {
    el = el.offsetParent;
    top += el.offsetTop;
    left += el.offsetLeft;
  }

  return (
    top < (window.pageYOffset + window.innerHeight) &&
    left < (window.pageXOffset + window.innerWidth) &&
    (top + height) > window.pageYOffset &&
    (left + width) > window.pageXOffset
  );
}

function is_iphone()
{
	var deviceAgent = navigator.userAgent.toLowerCase();
	return deviceAgent.match(/(iphone)/);
}

function img_fadein()
{
	$('img.fadein').each(
		function()
		{
			var id = $(this).attr('id');

			if (!id)
			{
				id = 'img_fadein_' + (img_fadein_counter++);

				$(this).attr('id', id);
			}

			if (is_iphone() || elementInViewport2(this))
			{
				if (!img_fadein_state[id])
				{
					img_fadein_state[id] = true;

					$(this).animate({
						'opacity': 1
					});
				}
			}

			//$(this).css('opacity', 1);
		}
	);
}

function onresize()
{
	var w = ($(window).width() - 770) / 2;
	
	w = Math.round(w);

	$('.logoblock').css(
		{
			left: w
		}
	);
}

$(document).ready(
	function()
	{
		img_fadein();

		$(document).scroll(img_fadein);

		onresize();

		$(window).resize(
			function()
			{
				onresize();
			}
		);

		$('.logo, .logoblock')
			.css('cursor', 'pointer')
			.click(
				function()
				{
					document.location = '/blog/';
				}
			);
		$('div.love')
			.show()
			.click(
				function()
				{
					if (!$(this).hasClass('loved'))
					{
						$(this).find('div.symbol, div.count').hide();
						$(this).find('div.loader').show();

						var uri = $(this).attr('luri') + '?a=l';
						var element = this;

						jQuery.ajax(
							uri,
							{
								complete: function(xhr, text)
								{
									$(element).addClass('loved');
									$(element).find('div.count').html(xhr.responseText).show();
									$(element).find('div.symbol').show();
									$(element).find('div.loader').hide();
								}
							}
						);
					}
				}
			);

	var snd_player_queued_file = null;
	var snd_player_isready = false;
	var snd_player_instance = null;
	var snd_player_status = '';
	var snd_player_active_element = null;
	var snd_player_elements_count = 0;

	function snd_player_event_play()
	{
		snd_player_status = 'playing';
		snd_player_updateActiveElement();
	}

	function snd_player_event_ended()
	{
		snd_player_status = '';
		snd_player_updateActiveElement();
		snd_player_active_element = null;
	}

	function snd_player_event_paused()
	{
		snd_player_status = 'paused';
		snd_player_updateActiveElement();
	}

	function snd_player_updateActiveElement()
	{
		if (!snd_player_active_element)
		{
			return;
		}

		if (snd_player_status == 'playing')
		{
			snd_player_active_element.addClass('isplaying');
		}
		else
		{
			snd_player_active_element.removeClass('isplaying');
		}
	}

	function snd_player_isloaded()
	{
		snd_player_instance = $("div.audioplayer");
		snd_player_instance.bind($.jPlayer.event.play, snd_player_event_play);
		snd_player_instance.bind($.jPlayer.event.ended, snd_player_event_ended);

		snd_player_isready = true;

		snd_player_loadfromqueue();
	}

	function snd_player_stop()
	{
		snd_player_instance.jPlayer("stop");
		snd_player_event_ended();
	}

	function snd_player_pause()
	{
		snd_player_instance.jPlayer("pause");
		snd_player_event_paused();
	}

	function snd_player_resume()
	{
		snd_player_instance.jPlayer("play");
	}

	function snd_player_playfile(forElement, file)
	{
		if (snd_player_active_element)
		{
			if (snd_player_status == 'paused')
			{
				snd_player_resume();

				return;
			}
			else
			{
				var wasSame = forElement.attr('id') == snd_player_active_element.attr('id');

				if (wasSame)
				{
					snd_player_pause();

					return;
				}
				
				snd_player_stop();
			}
		}

		snd_player_elements_count++;

		forElement.attr('id', 'snd_player_element_' + snd_player_elements_count);

		snd_player_active_element = forElement;
		snd_player_queued_file = file;

		if (snd_player_isready)
		{
			snd_player_loadfromqueue();
		}
	}

	function snd_player_loadfromqueue()
	{
		if (snd_player_queued_file)
		{
			snd_player_instance.jPlayer("setMedia", {
				mp3: snd_player_queued_file
			}).jPlayer("play");

			snd_player_queued_file = null;
		}
	}

	$("<div class='audioplayer'></div>").prependTo($('body'));

	$("div.audioplayer").jPlayer(
		{
			ready: snd_player_isloaded,
			backgroundColor: '#d0d0d0',
			swfPath: "/js",
			solution: "flash, html",
			supplied: "mp3"
		}
	);

		//var html = $('div.page_header').html();
		//html = html.replace(/\+31 35 6244154/, '<span class="phone">+31 35 6244154</span>');
		//$('div.page_header').html(html);

		$('div.page_header .phone').css('cursor', 'default').click(
			function()
			{
				return false;
			}
		);
	
		$(document).find('div.blog .item img').mousedown(
			function()
			{
				return false;
			}
		);
			
		$(document).find('div.audio').each(
			function()
			{
				var moi = $(this);
				var url = moi.find('a').attr('href');
				moi.attr('href', url);

				// audio_img_height_540
				var classList = moi.attr('class').split(/\s+/);

				var imageWidth = 720;
				var buttonSize = 83 + 40;
				
				var x = parseInt((imageWidth - buttonSize) / 2);
				moi.css('left', x + 'px');

				$.each(
					classList,
					function(index, item)
					{
						if (item.match(/audio_img_height_/))
						{
							item = item.replace(/audio_img_height_/, '');

							var imageHeight = parseInt(item);
							var y = parseInt((imageHeight - buttonSize) / 2);
							moi.css('top', y + 'px');
						}
					}
				);

				moi.html('<div class="icon"></div>').show().mousedown(
					function()
					{
						return false;
					}
				).click(
					function()
					{
						var url = moi.attr('href');
						snd_player_playfile($(this), url);
					}
				);
			}
		);

	}
)

var tag_state = [];
var tag_currentlyOpen = null;
var tag_height = [];

function tags_toggle(id)
{
	var state = !tag_state[id];
	var height = tag_height[id];

	if (!height)
	{
		height = $('#' + id).height();

		$('#' + id).css(
			{
				'display': 'block',
				'overflow': 'hidden',
				'padding-left': 0,
				'opacity': 0,
				'height': 0
			}
		);

		tag_height[id] = height;
	}

	tag_state[id] = state;
	$('#' + id).stop(true);

	if (tag_currentlyOpen)
	{
		tag_state[tag_currentlyOpen] = false;
		$('#' + tag_currentlyOpen).stop(true);
		$('#' + tag_currentlyOpen).css(
			{
			}
		).animate(
			{
				'height': 0,
				'padding-left': 0,
				'opacity': 0
			}
		);
	}

	tag_currentlyOpen = null;

	if (state)
	{
		tag_currentlyOpen = id;

		$('#' + id).animate(
			{
				'height': height,
				'padding-left': 25,
				'opacity': 1
			}
		);

	}
	else
	{
		$('#' + id).css(
			{
			}
		).animate(
			{
				'height': 0,
				'padding-left': 0,
				'opacity': 0
			}
		);

	}

	return false;
}


