/* javascript */
$(document).ready(function() {
	/* toggle text from input fields */
	$('input[type=text]').bind('focus', function(){
		this.value == this.title ? this.value = '' : null;
	}).bind('blur', function(){
		this.value.replace(/\s/g, '').length ? null : this.value = this.title;
	});

	$('.accents').each(function() {
		var $accents = $(this),
			$parent = $accents.parent();

		if($accents.height() < $parent.height()) {
			$accents.css('height', $parent.height());
		};
	});

	/* position submenu */
	$('#mainmenu td').children('div').hover(function() {
		var $this = $(this),
			submenu = $this.find('div.submenu_inner');

		if ($this.offset().left > ($(document).width() / 2)) {
			submenu.css({'left' : 'auto', 'right' : '0'})
		};
	}, function() {
		var $this = $(this),
			submenu = $this.find('div.submenu_inner');

		if ($this.offset().left < ($(document).width() / 2)) {
			submenu.css({'left' : '0', 'right' : 'auto'})
		};
	});

	$('#main_carousel').carousel({
		btn_prev : $('.carousel').find('.prev_page'),
		btn_next : $('.carousel').find('.next_page'),
		navigation : $('.carousel').find('.navigation'),
		slide_per_seconds: 3,
		loop: true
	});

	$('#news_carousel').carousel({
		btn_prev : $('.actual_news').find('.prev_page'),
		btn_next : $('.actual_news').find('.next_page'),
		navigation : $('.actual_news').find('.navigation'),
		effect : 1,
		visible_items : 2
	});

	$('#page_gallery').carousel({
		btn_prev : $('.page_gallery').find('.prev_images'),
		btn_next : $('.page_gallery').find('.next_images'),
		item_margin : 10,
		visible_items : 3
	});

	$('#page_gallery_big').carousel({
		btn_prev : $('.page_gallery').find('.prev_images'),
		btn_next : $('.page_gallery').find('.next_images'),
		item_margin : 16,
		visible_items : 4
	});

	$('#related_gallery').carousel({
		btn_prev : $('.page_gallery').find('.prev_images'),
		btn_next : $('.page_gallery').find('.next_images'),
		item_margin : 35,
		visible_items : 5
	});

	/* events tabs */
	$('.forthcoming_events').find('div.tabs').tabs();

	if($('#increase_font')) {
		$('#increase_font').bind('click', function() {
			changeFont('content', '1');
		});
	}

	if($('#decrease_font')) {
		$('#decrease_font').bind('click', function() {
			changeFont('content', '-1');
		});
	}

	/* page gallery lightbox */
	if($('#page_gallery').length > 0) $('#page_gallery').find('a[rel^=prettyPhoto]').prettyPhoto();
	if($('#related_gallery').length > 0) $('#related_gallery').find('a[rel^=prettyPhoto]').prettyPhoto();
	if($('#page_gallery_big').length > 0) $('#page_gallery_big').find('a[rel^=prettyPhoto]').prettyPhoto();

	/* photo gallery */
	if($('.gallery').length > 0) $('.gallery').find('a[rel^=prettyPhoto]').prettyPhoto();

	if($('.accents').length > 0) $('.accents').find('a[rel^=prettyPhoto]').prettyPhoto();

	if($('.logo_r').length > 0) $('.logo_r').prettyPhoto();

	if($('#accordion').length > 0) {
		$('#accordion a.accordion_button').click(function() {
			var $content = $(this).parent().next();
			if($content.hasClass('opened')) {
				$content.slideUp('normal').removeClass('opened');
			}
			else {
				$('#accordion .accordion_content').slideUp('normal').removeClass('opened');
				$(this).parent().next().slideDown('normal').addClass('opened');
			}
		});

		$("#accordion .accordion_content").hide();
	}

});



new function() {
	/* function carousel */
	$.fn.carousel = function(options){
		var $movable = $(this),
			$objects = $('> li', $movable),
			objects_length = $objects.length,
			$prev = options.btn_prev,
			$next = options.btn_next,
			$navigation = options.navigation ? options.navigation : false,
			item_margin = options.item_margin ? options.item_margin : 0,
			item_width = $objects.eq(0).width() + item_margin,
			effects = ['horizontal', 'fade'],
			effect = options.effect ? effects[options.effect] : effects[0],
			visible_items = options.visible_items ? options.visible_items : 1,
			moving = false,
			current = 0,
			auto_slide_per_seconds = options.slide_per_seconds ? options.slide_per_seconds : 0,
			loop = options.loop ? true : false,
			autoplay_interval;

		if (!$movable.length) {
			return;
		};

		if(objects_length <= visible_items) {
			$navigation && $navigation.hide();
			$prev && $prev.hide();
			$next && $next.hide();
			return;
		};

		if ($navigation) {
			for(var i = 0; i < objects_length; i = i + visible_items) {
				$navigation.children('span').append('<a href="javascript:;" title=""><span>'+ i +'</span></a>');
			};

			var $navigation_items = $navigation.find('span > a');
				$navigation_items.eq(0).addClass('active');
			
			if(!loop) {
				$navigation_items.live('click', function() {
					var pos = $(this).index();
					if (pos != current) {
						move(pos);
					};
				});
			} else {
				$navigation_items.css('cursor', 'default');
			};
		};
		
		autoplay();
		
		switch(effect) {
			case 'horizontal':
				$movable.css('width', objects_length * item_width);
				var move = move_horizontal;
			break;
			case 'fade':
				var move = fade_items;
			break;
		};

		$prev.click(function() {
			return move(current - 1);
		});

		$next.click(function() {
			return move(current + 1);
		});

		function fade_items(idx) {
			if (moving) {
				return;
			};
			
			if(autoplay_interval) {
				clearTimeout(autoplay_interval);
			};
			
			moving = true;

			if (idx < 0) {
				idx = Math.ceil(objects_length / visible_items) - 1;
			} else if (idx >= (objects_length / visible_items)) {
				idx = 0;
			};

			var start = idx * visible_items,
				end = start + visible_items,
				next_items = $objects.slice(start, end);
			
			$objects.filter('.visible').fadeOut(function() {
				$(this).removeClass('visible');
				next_items.fadeIn(function() {
					$(this).addClass('visible');
					current = idx;
					moving= false;
					autoplay();
				});
			});

			update_navigation(idx);
		}

		function move_horizontal(next) {
			if (moving) {
				return false;
			};
			
			if(autoplay_interval) {
				clearTimeout(autoplay_interval);
			};
			
			moving = true;		
			
			if (next < 0) {
				next = Math.ceil(objects_length / visible_items) - 1;
			} else if (next >= (objects_length / visible_items)) {
				next = 0;
			};

			update_navigation(next);

			var idx = next * visible_items,
				end = loop ? item_width * -1  : (idx * item_width) * -1;

			$movable.animate({
				left: end + 'px'
			}, 500, function() {
				current = next;
				$('.useful_menu li').removeClass('active');
				$('.useful_menu li:eq(' + current + ')').addClass('active');
				
				if(loop) {
					$movable.css('left', 0);
					$movable.find('li:first').appendTo($movable);
				};		
				moving = false;
				autoplay();
			});
		}

		function update_navigation(idx) {
			if($navigation) {
				$navigation_items.removeClass('active');
				$navigation_items.eq(idx).addClass('active');
			};
		}
		
		function autoplay() {
			if(auto_slide_per_seconds > 0) {
				autoplay_interval = setTimeout(function() {
					return move(current + 1);
				}, 4000);
			};
		}
	};

	/* function tabs */
	$.fn.tabs = function(options) {
		var object = $(this);

		if(!object.length) {
			return;
		};

		var elements = object.find('a');

		elements.click(function() {
			var next_list_id = $(this).attr('href').split('#')[1],
				current_list_id = object.find('a.active').attr('href').split('#')[1];

			object.find('a').removeClass('active');
			$('#' + current_list_id).hide();
			$('#' + next_list_id).show();
			$(this).addClass('active');
			return false;
		});
	};
};


function changeFont(element,step)
{
	var current_font_size = document.getElementById(element).style.fontSize;
	if(!current_font_size) {
		current_font_size = '14px;';
	}

    document.getElementById(element).style.fontSize =  (parseInt(current_font_size,10) + parseInt(step,10)) + 'px';
}
