/*
*
* @name functions-portal.js
* @package phpBB3 Portal
* @version $Id: functions-portal.js,v 1.0 16/06/2011 $
*
* @copyright (c) 2011 CaniDev
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

// HighSlide Initialize
hs.align			= 'center';
hs.transitions		= ['expand', 'crossfade'];
hs.fadeInOut		= true;
hs.dimmingOpacity	= 0.8;
hs.wrapperClassName	= 'borderless floating-caption';
hs.outlineType		= 'rounded-white';
hs.continuePreloading = false;

// Add the simple close button
hs.registerOverlay({
	html: '<div class="closebutton" onclick="return hs.close(this)" title="' + l_highslide_closetitle + '"></div>',
	position: 'top right',
	fade: 2
});

//Initialize common functions
var rsz_timer = null;

function rsz_refresh_tables()
{
	$('table').each(function(i) {
		this.refresh();
	});
}

function rsz_img_loaded(obj)
{
	size = $(obj).attr('width');

	if(rsz_go && rsz_max_width && Number(rsz_max_width) > 0)
	{
		if(Number(size) > Number(rsz_max_width))
		{
			el = '<a class="highslide" href="' + $(obj).attr('src') + '" onclick="return hs.expand(this);">' +
				'<img class="resized" src="' + $(obj).attr('src') + '" title="' + l_highslide_enlarge + '" alt="' + $(obj).attr('alt') + '" width="' + rsz_max_width + '" />' +
				'</a>';

			if($(obj).parent().is('a'))
			{
				$(this)
					.attr('width', rsz_max_width)
					.addClass('resized');
			}
			else
			{
				$(obj).replaceWith(el);
			}
		}

		if (window.ActiveXObject) // IE on Mac and Windows
		{
			window.clearTimeout(rsz_timer);
			rsz_timer = window.setTimeout('rsz_refresh_tables()', 10000);
		}
	}
}

function setBlockCookies()
{
	var cookie_data = '';

	$('.portal-block').each(function() {
		el = $(this).find('.block-content');

		if(el.css('display') == 'none')
		{
			if(cookie_data != '') cookie_data += ':';
			parent_id = $(this).attr('id');
			cookie_data += parent_id.replace('block-', '');
		}
	});

	var days = 90;
	var date = new Date();
	date.setTime(date.getTime()+(days*24*60*60*1000));
	expires = "; expires="+date.toGMTString();
	document.cookie = cookie_name + "=" + cookie_data + expires + "; path=/; domain=" + cookie_domain;
}

function collapse_stored_blocks()
{
	cookie_data = document.cookie.split(';');
	c_data = '';

	for(i = 0; i < cookie_data.length; i++)
	{
		d = cookie_data[i];

		while (d.charAt(0) == ' ')
		{
			d = d.substring(1, d.length);
		}

		c = d.split('=', 2);

		if (c[0] == cookie_name)
		{
			c_data = c[1];
			break;
		}
	}

	if(c_data)
	{
		block_ids = c_data.split(':');
		for(i = 0; i < block_ids.length; i++)
		{
			$('#block-' + block_ids[i]).find('.block-content').hide();
			$('#block-' + block_ids[i]).find('.b-side').addClass('collapsed');;
		}
	}
}

$(function() {
	$( ".accordion" ).accordion({
		autoHeight: false,
		navigation: true
	});

	$('.b-side').click(function(e) {
		d = $('#' + $(this).attr('rel') + ' .block-content');

		if(d.css("display") == "block")
		{
			d.slideUp('slow', function() { setBlockCookies(); });
			$(this).addClass('collapsed');
		}
		else
		{
			d.slideDown('slow', function() { setBlockCookies(); });
			$(this).removeClass('collapsed');
		}
	});

	if(allow_collapse)
	{
		collapse_stored_blocks();
	}
});

