var enable_text_gallery = 0;
$(document).ready(function() {
	changeTextSize(readCookie('saved_text_size'));
	if(enable_text_gallery == 1){   
		var options = { 
			insert : '#gallery_main_image',
			history : false,
			onImage : gallery_onImage,
			onThumb : gallery_onThumb
		};
		jQuery(function($) { $('ul.gallery').galleria(options); });
	}
});

/******************************************
** TEXT GALLERY ** BEGIN
*******************************************/
function gallery_onImage(image,caption,thumb) { // let's add some image effects for demonstration purposes
	// fade in the image & caption
	image.css('display','none').fadeIn(1000);
	caption.css('display','none').fadeIn(1000);
	
	image.VerticalAlign();
	// fetch the thumbnail container
	var _li = thumb.parents('li');
	
	// fade out inactive thumbnail
	_li.siblings().children('img.selected').fadeTo(500,0.3);
	
	// scroll to active thumbnail
	$('#gallery_thumbnails .thumbnail_scroll').scrollTo(thumb, 800, {axis:'x',offset:-120});
	
	// fade in active thumbnail
	thumb.fadeTo('slow',1).addClass('selected'); 
	
	
	// add a title for the clickable image
	//image.attr('title','Next image >>');
};

function gallery_onThumb(thumb) { // thumbnail effects goes here
	// fetch the thumbnail container
	var _li = thumb.parents('li');
	
	// if thumbnail is active, fade all the way.
	var _fadeTo = _li.is('.active') ? '1' : '0.3';
	
	// fade in the thumbnail when finnished loading
	thumb.css({display:'none',opacity:_fadeTo}).fadeIn(1500);
	
	// hover effects
	thumb.hover(
		function() { thumb.fadeTo('fast',1); },
		function() { _li.not('.active').children('img').fadeTo('fast',0.3); } // don't fade out if the parent is active
	)
};
function gallery_thumbnail_scroll(direction){
	if(direction > 0){
		$('#gallery_thumbnails .thumbnail_scroll').scrollTo('+=200px', 500, {axis:'x'});
	}
	else{
		$('#gallery_thumbnails .thumbnail_scroll').scrollTo('-=200px', 500, {axis:'x'});
	}
}
/******************************************
** TEXT GALLERY ** END
*******************************************/

(function($) {
    $.fn.VerticalAlign = function(opts) {
        return this.each(function() {
            var top = (($(this).parent().height() - $(this).height()) / 2);
            $(this).css('margin-top', top);
        });
    };
})(jQuery);


function openImgPopUp(imgUrl, imgName){
	MyWindow = window.open(imgUrl,"Picture","toolbar=0,location=0,status=0,menubar=0,scrollbars=0,resizable=1,width=400,height=400");
	MyWindow.document.write('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">');
	MyWindow.document.write('<html>');
	MyWindow.document.write('<head>');
	MyWindow.document.write('	<script type="text/javascript">');
	MyWindow.document.write('		function resizePopUp() {');
	MyWindow.document.write('			iWidth = document.documentElement.clientWidth;');
	MyWindow.document.write('			iHeight = window.innerHeight;');
	MyWindow.document.write('			if(iHeight == undefined) { iHeight = document.documentElement.clientHeight; }');
	MyWindow.document.write('			iWidth = document.images[0].width - iWidth;');
	MyWindow.document.write('			iHeight = document.images[0].height - iHeight;');
	MyWindow.document.write('			window.resizeBy(iWidth, iHeight);');
	MyWindow.document.write('		};');
	MyWindow.document.write('	</script>');
	MyWindow.document.write('	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />');
	MyWindow.document.write('	<title>'+imgName+'</title>');
	MyWindow.document.write('</head>');
	MyWindow.document.write('<body style="margin:0; padding:0;" onLoad="resizePopUp();">');
	MyWindow.document.write('	<img src="'+imgUrl+'" alt="'+imgName+'" />');
	MyWindow.document.write('</body>');
	MyWindow.document.write('</html>');
	MyWindow.document.close();
	return(false);
}

function open_gallery(gallery_id, pic_id){
	window.open(g_web_url+'gallery.php?gallery_id='+gallery_id+'&pic_id='+pic_id, "Gallery", "toolbar=0,location=0,status=0,menubar=0,scrollbars=0,resizable=0,width=640,height=545");
	return false;
}

function openPopUp(sUrl, iWidth, iHeight) {
	window.open(sUrl, 'NewWindow', 'width='+iWidth+',height='+iHeight+',resizable=1');
	return false;
}

/***************************************
** SYSTEM TOOLS
****************************************/
function Set_Cookie( name, value, expires, path, domain, secure ){
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );

	/*
	if the expires variable is set, make the correct
	expires time, the current script below will set
	it for x number of days, to make it for hours,
	delete * 24, for minutes, delete * 60 * 24
	*/
	if ( expires ){
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );

	document.cookie = name + "=" +escape( value ) +
	( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
	( ( path ) ? ";path=" + path : "" ) +
	( ( domain ) ? ";domain=" + domain : "" ) +
	( ( secure ) ? ";secure" : "" );
	return true;
}


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);
}


function changeTextSize(tSize){ 
	tSize = tSize*1;

	oMainColumn = document.getElementById('mainColumn');
	if(tSize == 0){
		oMainColumn.style.fontSize = '1em';
		createCookie('saved_text_size', tSize, 120);
	}
	else if (tSize == 1){
		oMainColumn.style.fontSize = '1.2em';
		createCookie('saved_text_size', tSize, 120);
	}
	else if (tSize == 2){
		oMainColumn.style.fontSize = '1.4em';
		createCookie('saved_text_size', tSize, 120);
	}
}


/***************************************
** NEWS ** BEGIN
****************************************/
function page_selector_show_year(obj, direction){
	var oUl = $(obj).parents('ul');
	var oLi = $(oUl).children('li:not(.pg_sel_right,.pg_sel_left)');
	var stage = 0;
	if(direction == 1){
		for(var i=0; i < oLi.length; i++ ){
			// first hide one
			if(stage == 0){
				if($(oLi[i]).is(':visible')){
					$(oLi[i]).hide();
					stage = 1;
					// make sure that "arrow back" is visible
					$(oUl).children('li:first').show();
				}
			}
			// set next visible
			else if(stage == 1){
				if($(oLi[i]).is(':hidden')){
					$(oLi[i]).show();
					stage = 2;
					// if this is last elemnt, then hide "arrow next"
					if(i+1 == oLi.length){
						$(oUl).children('li:last').hide();
					}
				}
			}
		}
	}
	if(direction == -1){
		var hide = 0;
		for(var i=0; i < oLi.length; i++ ){
			// show one more
			if(stage == 0){
				if($(oLi[i+1]).is(':visible')){
					$(oLi[i]).show();
					stage = 1;
					// hide "arrow back"
					if(i == 0){
						$(oUl).children('li:first').hide();
					}
				}
			}
			// hide one on back
			else if(stage == 1){
				if(i+1 == oLi.length){
					hide = 1;
				}
				else{
					if($(oLi[i+1]).is(':hidden')){
						hide = 1;
					}
				}
				if(hide == 1){
					$(oLi[i]).hide();
					// make sure that "arrow next" is visible
					$(oUl).children('li:last').show();
				}
			}
		}
	}
}

// comment_parameters[][0] - page
// comment_parameters[][1] - per_page
// comment_parameters[][2] - direction
var comment_parameters = [];

function news_gallery_show_pic(pic_id, direction){
	if(pic_id < 0){
		pic_id = news_pic_selected+direction;
	}
	if((pic_id >= 0) && (pic_id < news_pics.length)){
		news_pic_selected = pic_id;
		var oDiv = document.getElementById('news_gallery');
		oDiv.style.backgroundImage = 'url(' + news_pics[news_pic_selected][0] + ')';
		oDiv.title = news_pics[news_pic_selected][1];
		// remove active class
		$('#news_gallery_selector li').removeClass('active');
		$('#news_gallery_selector li:eq('+pic_id+')').addClass('active');
	}
}

function comment_post_click(e, comment_type_p){
	e.preventDefault();

	if($('#comment_text').val().length == 0){
		return;
	}

	// AJAX post
	$.post("?", {comment_post:'ajax', comment_type:comment_type_p, comment_type_id:$('#comment_type_id').val(), comment_text:$('#comment_text').val()}, comment_post_callback);
	// Hide post form
	$('#post_comment_form').hide();
	// Show waiting line
	$('#comment_post_block p.ajax_post_wait').show();
}

function comment_post_callback(xml){
	// hide waiting line
	$('#comment_post_block p.ajax_post_wait').hide();
	// show finish line
	$('#comment_post_block p.ajax_post_done').show();
	var parts = []
	parts = xml.split('{|}');
	if((parts.length == 4) && (parts[0] == 0)){
		// error free response. Must reload comment section box
		if(comment_parameters[parts[2]][2] == 'desc') {
			var comment_page_p = 0;
		}
		else{
			var comment_page_p = -1;
		}
		$.get('?', {comment_request:'ajax', comment_type:parts[1], comment_type_id:parts[2], comment_page:comment_page_p, comment_per_page:comment_parameters[parts[2]][1], comment_direction:comment_parameters[parts[2]][2]}, comment_show_callback);
	}
	$('#comment_post_block p.ajax_post_done').append(parts[3]);
}

function comment_show_callback(xml){
	var parts = [];
	parts = xml.split('{|}');
	if(parts.length == 2){
		$('#comment_container_'+parts[0]).html(parts[1]);
	}
}

function comment_show_page(comment_type_p, type_id, comment_page){
	comment_parameters[type_id][0] = comment_page;
	$.get('?', {comment_request:'ajax', comment_type:comment_type_p, comment_type_id:type_id, comment_page:comment_parameters[type_id][0], comment_per_page:comment_parameters[type_id][1], comment_direction:comment_parameters[type_id][2]}, comment_show_callback);
}

function comment_set_direction(obj, comment_type_p, type_id, direction){
	$('#comment_direction_select_'+type_id+' a').removeClass('inactive');
	$(obj).addClass('inactive');

	comment_parameters[type_id][2] = direction;
	Set_Cookie('comment_sort_order', direction, 90);
	comment_show_page(comment_type_p, type_id, 0);
}

function textarea_limit_length(obj, max_len, status_id){
	if(obj.value.length > max_len){
		obj.value = obj.value.substring(0, max_len);
	}
	if((status_id != undefined) && (status_id.length > 0)){
		$('#'+status_id).text(obj.value.length);
	}
}
/***************************************
** NEWS ** END
****************************************/

/***************************************
** CALENDAR ** BEGIN
****************************************/
function calendar_change_month(month_p, year_p){
	$.post("?", {calendar_change_month:'ajax', month:month_p, year:year_p}, calendar_change_month_callback);
	// Hide old calendar
	$('#small_calendar_container').slideUp('fast');
	// Show waiting line
	$('#small_calendar_wait').show();
}
function calendar_change_month_callback(xml){
	var parts = []
	parts = xml.split('{|}');
	if((parts.length == 2) && (parts[0] == '0')){
		// error free response

		// hide waiting line
		$('#small_calendar_wait').hide();
		// populate calendar container with new data
		$('#small_calendar_container').html(parts[1]);
		// show calendar block
		$('#small_calendar_container').slideDown('fast');
	}
}
/***************************************
** CALENDAR ** END
****************************************/
