var slideshowIntervalID;

function ie6png_init()
{
	var spaceball_src = 'http://l.yimg.com/g/images/spaceball.gif'; // Replace with absolute URL of spaceball on production server
	
	$("*[src$='.png']:not('.png-fixed')").each(function()
	{
		$(this)
			.css('width', $(this).attr('width')+'px')
			.css('height', $(this).attr('height')+'px')
			.css("filter", 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="'+$(this).attr('src')+'")')
			.attr('src', spaceball_src)
			.addClass('png-fixed');
	});
}

function arrow_buttons_init()
{
	$("*[src*='/button-']").each(function()
	{
		$(this).hover(
			function()
			{
				var src = $(this).attr('src');
				if (src.indexOf('-hover') == -1)
				{
					src = src.replace(/.(gif|png)$/, "-hover.$1");
					$(this).attr('src', src);
				}
			},
			function()
			{
				var src = $(this).attr('src');
				if (src.indexOf('-hover') > -1)
				{
					src = src.replace('-hover.', '.');
					$(this).attr('src', src);
				}
			}
		);
	});
}

function slideshow_init()
{
	if (!$('#home-slides').length) return false;
	
	$('#home-slides img').hide();	
	$('#home-slides').prepend($('#home-slides').prev('img'));
	
	$('#home-slides').show();
	var w = $('#home-slides img:first').attr('width');
	var h = $('#home-slides img:first').attr('height');
	$('#home-slides').css('width', w+'px').css('height', h+'px');
	
	$('.panel-events .panel-content').css('margin-top', '30px');
	$('#home-slides').before('<a id="home-slides-control"></a>');
	$('#home-slides-control').click(function()
	{
		if (!$(this).hasClass('play'))
		{
			$(this).addClass('play');
			clearInterval(slideshowIntervalID);
		}
		else
		{
			$(this).removeClass('play');
			slideshowIntervalID = setInterval(slideshow_next, 6000);
		}
	});
	
	slideshowIntervalID = setInterval(slideshow_next, 6000);
}

function slideshow_next()
{
	var current = $('#home-slides img:visible');
	var next = current.next('img');
	if (!next.length) next = $('#home-slides img:eq(0)');
	current.fadeOut(1000);
	next.fadeIn(1000);
}

function load_inbox(pageNumber)
{
    // show ajax indicator
    $('div#inbox-container').hide();
    $('div.indicator').show();

    pageNumber = typeof(pageNumber) != 'undefined' ? pageNumber : 1;

    // load inbox via a symfony component, used to help with paging
    // same URL as API except its /inbox not /api.
    $.ajax(
    {
        url: apiURL.replace('api/', 'inbox'),
        data: "page=" + pageNumber,
        cache: false,
        success: function(html)
        {
            $("div#inbox-container").html(html);
            inbox_init();
        }
    });
}

function inbox_init()
{
	$('.msg-profile').hide();
	
	var hash = location.hash.replace("#","");
	if (hash)
	{
		if ($('#'+hash).hasClass('unread'))
		{
			var id = hash.replace('msg-', '');
			$.post(apiURL+'message-read', {message_id: id});
		}
		
		$('.msg:not(#'+hash+') .msg-body').hide();
		$('#'+hash).removeClass('unread');
		$('#'+hash+' .msg-newlabel').remove();
		
		window.scrollTo(0, $('#'+hash).offset().top);
	}
	else
	{
		$('.msg-body').hide();
	}
	
	$('.msg-sender .profile').click(function()
	{
		$(this).parents('.msg-wrapper').children('.msg-body').hide();
		$(this).parents('.msg-sender').nextAll('.msg-profile').show();
	});
	
	$('a[rel="delete"]').click(function()
	{
	   var id = parseInt($(this).attr('href').replace(/.*#msg-/, ''));
	   $.post(apiURL + 'message-delete', { message_id: id }, function(data)
	   {
	       if(data.status == 'ok')
	       {
	           load_inbox();
	       }
	       else
	       {
	           alert('Sorry but there was a problem deleting this message');
	       }
	   }, "json");
	   
	   return false;
	});
	
	$('.msg-subject .msg-action').click(function()
	{
		if ($(this).parents('.msg').hasClass('unread'))
		{
			var id = parseInt($(this).parents('.msg').attr('id').replace('msg-', ''));
			$.post(apiURL+'message-read', {message_id: id});
		}
		
		$(this).parents('.msg-wrapper').children('.msg-profile').hide();
		$(this).parent().nextAll('.msg-body').show();
		$(this).parents('.msg').removeClass('unread');
		$(this).parents('.msg-wrapper').children('.msg-newlabel').remove();
		
		// decrement unread count on the page when a message is opened
		unreadCount = $("#unread-count").html();
		unreadCount = (unreadCount > 0) ? unreadCount - 1 : 0;
		$("#unread-count").html(unreadCount);
		
		window.scrollTo(0, $(this).parents('.msg').offset().top);
		return false;
	});
	
	$('.msg-profile .close, .msg-body .close').click(function()
	{
		$(this).parent().hide();
		return false;
	});
	
	$('a[rel="pager"]').click(function()
	{
	   var pageNumber = parseInt($(this).attr('id').replace('page-', ''));
	   load_inbox(pageNumber);
	   return false;
	});
	
    $('div.indicator').hide();
    $('div#inbox-container').show();

	arrow_buttons_init(); // initialize arrow button rollovers for new HTML
	if ($.browser.msie && parseInt($.browser.version) == 6) ie6png_init(); // Fix PNGs for new HTML
}

function personalise_avatar_init()
{
	$('#personalise-appearance li').click(function()
	{
		if ($(this).hasClass('active')) return false;
		$('#personalise-appearance li').removeClass('active')
		$('#personalise-appearance li img').each(function()
		{
			var image_src = $(this).attr('src').replace('-active.gif', '.gif');
			$(this).attr('src', image_src);
		});
		$(this).addClass('active');
		var image_src = $(this).children('img').attr('src').replace('.gif', '-active.gif');
		$(this).children('img').attr('src', image_src);
		$(this).find('input').attr('checked', 'checked'); 
	}).css('cursor', 'pointer');
}

$(document).ready(function()
{
	if ($.browser.msie && parseInt($.browser.version) == 6)
	{
		ie6png_init();
	}
	arrow_buttons_init();
	slideshow_init();
	load_inbox();
	//inbox_init();
	personalise_avatar_init();
});
