//
function loading (form) {
	$(form).children('div.button_container').addClass('loading');
}
function unloading (form) {
	$(form).children('div.button_container').removeClass('loading');
}
function checkForm (form) {
	send = 1;
	fields = $(form).find('input.mandatory, select.mandatory, textarea.mandatory').serializeArray();
	$.each(fields, function(i, field) {
		if (field.value == '') {
			send = 0;
		};
	});
	return send;	
}
function toggleNavItem (element) {
	$(element).next('ul').slideToggle('fast');
}
function display_img (mID, sens, freq) {
	var total_images = $("#"+mID+" img").length - 1;
	var current_index = $("#"+mID+" img:visible").index();
	var index_to_display = current_index + sens;
	if (index_to_display < 0) {
		index_to_display = total_images;
	} else if (index_to_display > total_images) {
		index_to_display = 0;
	}
	$("#"+mID+" img:visible").fadeOut('slow', function () {
		$("#"+mID+" img:eq("+index_to_display+")").fadeIn('fast');
	});
	panorama(mID, freq);
}
function panorama (mID, freq) {	
	timer_panorama = setTimeout("display_img('"+mID+"', 1, '"+freq+"')", freq);
}
function open_gallery (mID) {
	$('#gc'+mID).slideToggle('slow', function (){
		$('#m'+mID+' .preview').toggle();
	});
//	panorama(mID);
}
function launchGalleries () {
	if ( $('#container .Gallery.slide').length > 0 ) {
		$('#container .Gallery.slide').each( function(){
			mID = $(this).attr('id');
			freq = $(this).attr('frequency');
			panorama(mID, freq);
		});
	};
	if ( $('#container .Gallery.random').length > 0 ) {
		l = gallery.length;
		r = Math.floor(Math.random()*l);
		$('#container .Gallery.random .img_container').html('<img src="'+img_script+gallery[r]['src']+'" alt="'+gallery[r]['alt']+'" />');
	} else if ( $('#container .Gallery.manual').length > 0 ) {
		$('#container .Gallery.manual .img_container img').click(function(){
			$(this).fadeOut('fast', function() {
				$(this).next().fadeIn();
				if ( $(this).next().length == 0) {
					$(this).parent().children('img').eq(0).fadeIn();
				};
			});
		});
	}
}
function initZIndex () {
	el = $('#side_content>div');
	if ( $('body.Website').length > 0) {
		el = $('body.Website #content>div');
	};
	el.mouseenter( function (){
		el.removeClass('over');
		$('#header').removeClass('over');
		$(this).addClass('over');
		$('#side_content').addClass('over')
	});
	el.mouseleave( function () {
		$('#header').addClass('over');
		$('#side_content').removeClass('over');
	})
}
function resizeElements () {
	if ( $('body.Subscribtion').length > 0 ) {
		if ($('#content').innerHeight() > $('#specific').innerHeight() - 60) {
			$('#specific').css({'height': $('#content').innerHeight() - 60});
		} else {
			$('#content').css({'height': $('#specific').innerHeight() - 60});
		}
		nh = $('#content').outerHeight() + 200;
		$('#container').css({'height': nh});
	} else if ( $('#content_follow').length > 0 ) {
		ch = $('#content').outerHeight();
		cfh = $('#content_follow').outerHeight();
		nh = ch - 60;
		if (cfh > ch) {
			nh = cfh -60;
		};
		$('#content_follow, #content').css({'height': nh});
		$('body').append('<br/>rsel // '+ch+' / '+cfh);
	} else {
		nh = $('#content').outerHeight() + 200;
		$('#container').css({'height': nh});
	}
}
function resizeGeneratedDoublePage () {
	cfH = $('#content_follow').outerHeight();
	cH = $('#content').outerHeight();
	if (cH > cfH) {
		nh = $('#content').outerHeight() - 60;
		$('#content_follow').css({'height': nh});					
	} else {
		nh = $('#content_follow').outerHeight() - 60;
		$('#content').css({'height': nh});					
	}
}
function doublePages () {
	maxpH = 800;
	cpH = $('#content').outerHeight();
	if (cpH > maxpH) {
		//pNbr = Math.ceil(cpH / maxpH);
		pNbr = 2;
		if ($('body.Page').length > 0) {
			h2 = $('#content>.styled_text.BODY h2').length;
			h3 = $('#content>.styled_text.BODY h3').length;
			h4 = $('#content>.styled_text.BODY h4').length;
			pn = $('#content>.styled_text.BODY p').length;
			n_pos = 2;
			if ( h2 > 1 ) {
				n_pos = Math.ceil(h2 / pNbr);
				el = 'h2;'
			} else if ( h3 > 1 ) {
				n_pos = Math.ceil(h3 / pNbr);
				el = 'h3;'
			} else if ( h4 > 1 ) {
				n_pos = Math.ceil(h4 / pNbr);
				el = 'h4;'
			} else if ( pn > 1 ) {
				n_pos = Math.ceil(pn / pNbr);
				el = 'p;'
			}
			elI = $('#content>.BODY').children(el).eq(n_pos).index() - 1;
			body = $('#content>.BODY').children().eq(elI).nextAll().detach();
			$('#content').after('<div class="content_follow" id="content_follow"><div class="styled_text BODY"></div></div>');
			body.appendTo('#content_follow>.BODY');
			nh = $('#content').outerHeight() - 60;
			$('#content_follow').css({'height': nh});		
		} else if (($('body.Trip').length > 0 || $('body.Masterclass').length > 0) && $('body.Subscribtion').length === 0) {
			dn = $('#content>div').length;
			n_pos = 0;
			if (dn > 1) {
				n_pos = Math.ceil(dn / pNbr);
				el = 'div';
			};
			elI = $('#content>div').eq(n_pos).index() - 1;
			body = $('#content').children().eq(elI).nextAll().detach();
			$('#content').after('<div class="content_follow" id="content_follow"></div>');
			body.appendTo('#content_follow');
			resizeGeneratedDoublePage();
			$('#side_content').addClass('postit');
		}
	};
}
function pageInit () {
	initZIndex();
	launchGalleries();
	resizeElements();
	doublePages();
}
$(window).load(function() {
	resizeElements();
	timerLoader = setTimeout("resizeElements()", 2000);
});

$(document).ready(pageInit);

