// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
function reposFooter() {
	mh = 0; ch = 0; rh = 0; th = 0; hh = 0; fh  = 0;
	wh = document.documentElement.clientHeight;

	if ($('menu')) 
	mh = $('menu').offsetHeight
	if ($('content'))
	ch = $('content').offsetHeight
	if ($('right'))
	rh = $('right').offsetHeight
	if ($('header'))
	hh = $('header').offsetHeight
	if ($('footer'))
	fh = $('footer').offsetHeight

	rch = Math.max(mh,ch);

	if (!$('content_blue')) {
		if ( (wh - hh - fh) > rch ) { tm = wh - hh - fh - rch }
		else if ( rch < rh ) { tm = rh + 40 - rch }
		else { tm = rch - ch}

		Element.setStyle( 'content', { height: rch + 'px' } );

		Element.setStyle( 'footer', { top: tm + 'px' } );
	} else {
		if ( wh > hh + fh + rch ) {
			Element.setStyle( 'content_blue_table', { height: (wh-hh-fh-rch)-2 + 'px' } );
		}
	}
}

function resizeDetailedItem() {
	if ($('connections')) {
		Element.setStyle( 'detailed_item', { minHeight: $('connections').offsetHeight + 'px' } );
	}
}
function resizeCenter() {
	Element.setStyle( 'center', { minHeight: $('right').offsetHeight + 'px' } );
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

/* Session handlers (called by flash)*/
var g_session_event_id = null;

function session_get_ticket() {
	return readCookie('_mediaserver_session');
}

function session_get_event_id() {
	return g_session_event_id;
}

function style_set_alter(style) {
	if ( style != '1' && style != '2' && style != '3') {
		style = readCookie('mediaserver_stylesheet') || 1;
	}
	var i, lnk;
	for( i = 0; (lnk = document.getElementsByTagName("link")[i]); i++ ) {
		if ( lnk.getAttribute('rel').indexOf('style') != -1 && lnk.getAttribute('title') ) {
			lnk.disabled = ( lnk.getAttribute('id') == 'font_' + style ) ? false : true;
			if ($('button_' + lnk.getAttribute('id'))) {
				$('button_' + lnk.getAttribute('id')).src = '/images/site/button_' + lnk.getAttribute('id') + '_' + ( ( lnk.getAttribute('id') == 'font_' + style ) ? 'on' : 'off' ) + '.gif';
			}
		}
	}
	createCookie('mediaserver_stylesheet', style);
}

function redirect_to_question(question_id) {
	document.location.href = '/question/show/'+question_id
}

function textCounter( field, maxlimit ) {
	if ( field.value.length > maxlimit ) {
		field.value = field.value.substring( 0, maxlimit );
		field.blur();
		field.focus();
		return false;
	}
}

function acceptPrivacy( chbox ) {
	if ( chbox.checked ) {
		$('reg_submit_text').hide();
		$('reg_submit_image').show();
	} else {
		$('reg_submit_image').hide();
		$('reg_submit_text').show();
	}
}

function showHidePrivacyLayer() {
	$('privacy_text').toggle();
}

function changeMni() {
	$('mni_' + active_mni).hide();
	active_mni = ( active_mni + 1 == mni_num ) ? 0 : active_mni + 1;
	new Effect.Appear('mni_' + active_mni);
}


var thumbnails = new Array();
var currentThumbnail = 0;
var maxThumbnail = 0;

function hideThumbnail() {
	$('big_thumbnail').hide()
}

function nextThumbnail() {
	if ( currentThumbnail == maxThumbnail ) {
		showThumbnail(0);
	} else {
		showThumbnail(currentThumbnail+1);
	}		
}
function prevThumbnail() {
	if ( currentThumbnail == 0 ) {
		showThumbnail(maxThumbnail);
	} else {
		showThumbnail(currentThumbnail-1);
	}	
}

function showThumbnail(i) {

	function f_clientHeight() {
		return f_filterResults (
			window.innerHeight ? window.innerHeight : 0,
			document.documentElement ? document.documentElement.clientHeight : 0,
			document.body ? document.body.clientHeight : 0
		);
	}

	function f_scrollTop() {
		return f_filterResults (
			window.pageYOffset ? window.pageYOffset : 0,
			document.documentElement ? document.documentElement.scrollTop : 0,
			document.body ? document.body.scrollTop : 0
		);
	}

	function f_filterResults(n_win, n_docel, n_body) {
		var n_result = n_win ? n_win : 0;
		if (n_docel && (!n_result || (n_result > n_docel)))
			n_result = n_docel;
		return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
	}

	currentThumbnail = i;
	act = thumbnails[ i ];
	$('big_thumbnail_img').src = act[0];
	$('big_thumbnail_download').href = act[1];
	$('big_thumbnail_title').innerHTML = act[2];
	Element.setStyle( 'big_thumbnail', { top: ( f_scrollTop() + f_clientHeight()/2 - 180 - 140 ) + 'px' } );
	$('big_thumbnail').show();

}
