$(document).ready(function(){
	initMenu();
	initTabs();
	fadeGall();
	clearInputs();
	initBtnHover();
	initInfo();
	InfoPos();
	//initInfoText();
});

_curlink = 0;
_linkheight = 0;

/*--- info poition function ---*/
function InfoPos(){
	var _box = $('#ministry-info').hide();
	var box_h = _box.parent();

	$(window).scroll(function(){
		if(_box.is(':visible')){
			var t_top = $(window).scrollTop() - box_h.offset().top - 20;
			if(t_top > 0){
				/*if($(window).scrollTop() - box_h.offset().top < _curlink - _linkheight - 20) {
					_box.css('margin-top',_curlink - (_box.height() / 2) + 'px');
				}
				else {
					if(t_top + 30 + _box.outerHeight() < box_h.height()) _box.css('margin-top', t_top + 30);
					else _box.css('margin-top', box_h.height() - _box.outerHeight());
				}*/
				_box.css('margin-top',_curlink -_linkheight/2 + 'px');
			}
		}
	});
}

/*--- ministries info function ---*/
function initInfo(){
	var _hold = $('#ministry-info');
	var t_top;
	$('.all-ministries .tabset a').click(function(){
		_hold.stop().animate({opacity: 0}, 300, function(){
			$(this).hide();
		});
	});
	$('.all-ministries .images-holder').each(function(){
		var _btn = $(this).find('li > a');
		_btn.parent().removeClass('active');
		var __this = $(this);
		_btn.each(function(){
			var _text = $(this).parent().children('.more-info').html();
			$(this).mouseenter(function(){
				_hold.stop().css({opacity:0, display: 'block', marginTop: 20});
				if(_text) _hold.html(_text).animate({opacity: 1}, 300);
				
				t_top = $(window).scrollTop() - _hold.parent().offset().top - 20;
				
				_linkheight = $(this).outerHeight();
				_curlink = $(this).offset().top - _hold.parent().offset().top + (_linkheight/2);
			
				/*if(t_top > 0){
					if(t_top + 30 + _hold.outerHeight() < _hold.parent().height()) _hold.css('margin-top', t_top + 50);
						else _hold.css('margin-top', _hold.parent().height() - _hold.outerHeight());
				}*/
				//if(_curlink - _hold.outerHeight()/2 > 0) {
					$('#ministry-info').css('margin-top',_curlink -_linkheight/2 + 'px');
				//}

				_btn.parent().removeClass('active');
				$(this).parent().addClass('active');
			}).mouseleave(function(){
				//_hold.stop().animate({opacity: 0}, 300);
			});
		});
		
	});
}

/*--- button hover function ---*/
function initBtnHover(){
	$('input.submit').hover(
		function(){
			 this.src = this.src.replace(".gif", "-hover.gif");
		},
		function(){
			this.src = this.src.replace("-hover.gif", ".gif");
		}
	);
}

/*---- clear inputs function ---*/
function clearInputs(){
	$('input:text, input:password, textarea').each(function(){
		if(!this.val) this.val = this.value;
		this.onfocus = function(){
			if(this.value == this.val) this.value = '';
		}
		this.onblur = function(){
			if(this.value == '') this.value = this.val;
		}
	});
}

/*--- fade gallery function ---*/
function fadeGall(){
	var stay_time = 6000;
	$('.slideshow-box').each(function(){
		var t;
		var _f = true;
		var _list = $(this).find('.gallery > li');
		var btn_h = $(this).find('#carusel ul');
		var btn_h_w = 0;
		btn_h.children().each(function(){
			btn_h_w += $(this).outerWidth();
		});
		btn_h.width(btn_h_w);
		if(btn_h_w < btn_h.parent().width()) btn_h.parent().width(btn_h_w);
		_list.find('div.text').width($(this).find('.slideshow-main-holder').width() - $(this).find('#carusel').width() - 25);
		var _btn = btn_h.find('a');
		var btn_prev = $(this).find('#carusel .link-prev');
		var btn_next = $(this).find('#carusel .link-next');
		var _a = _btn.parent().index(_btn.parent().filter('.active:eq(0)'));
		if(_a == -1) _a = 0;
		_btn.parent().removeClass('active').eq(_a).addClass('active');
		if ($.browser.msie && $.browser.version <= 7) _list.removeClass('active').hide().eq(_a).addClass('active').show();
		else _list.removeClass('active').css('opacity', 0).eq(_a).addClass('active').css('opacity', 1);
		_btn.click(function(){
			changeEl(_btn.index(this));
			return false;
		});
		btn_prev.click(function(){
			if(_a == 0) changeEl(_btn.length - 1);
			else changeEl(_a - 1);
			return false;
		});
		btn_next.click(function(){
			if(_a == _btn.length - 1) changeEl(0);
			else changeEl(_a + 1);
			return false;
		});
		$(this).mouseenter(function(){
			_f = false;
			if(_t) clearTimeout(_t);
		}).mouseleave(function(){
			_f = true;
			if(_t) clearTimeout(_t);
			if(_f) _t = setTimeout(function(){
				if(_a == _list.length - 1) changeEl(0);
				else changeEl(_a + 1);
			}, stay_time);
		});
		var t_p;
		var t_m;
		function changeEl(_ind){
            _list.eq(_ind).show();
			if(_t) clearTimeout(_t);
			if(_ind != _a){
				t_p = _btn.eq(_ind).position().left;
				if(t_p >= btn_h.parent().width() || t_p<0){
					t_m = _ind*btn_h.children().outerWidth();
					if(btn_h_w - t_m < btn_h.parent().width()) btn_h.animate({marginLeft: -btn_h_w + btn_h.parent().width()}, { queue:false, duration:500});
					else btn_h.animate({marginLeft: -_ind*btn_h.children().outerWidth()}, { queue:false, duration:500});
				}
				if ($.browser.msie && $.browser.version <= 7){
					_list.eq(_a).removeClass('active').hide();
					_list.eq(_ind).addClass('active').show();
				}
				else{
					_list.eq(_a).removeClass('active').animate({opacity: 0}, { queue:false, duration:500});
					_list.eq(_ind).addClass('active').animate({opacity: 1}, { queue:false, duration:500});
				}
				_btn.eq(_a).parent().removeClass('active');
				_btn.eq(_ind).parent().addClass('active');
				_a = _ind;
			}
			if(_f) _t = setTimeout(function(){
				if(_a == _list.length - 1) changeEl(0);
				else changeEl(_a + 1);
			}, stay_time);
		}
		if(_f) _t = setTimeout(function(){
			if(_a == _list.length - 1) changeEl(0);
			else changeEl(_a + 1);
		}, stay_time);
	});
}


/*--- menu function ---*/
function initMenu(){
    $('.verse').removeClass('noJS');
    var main_verse = $('#main-verse');
    var active_verse = $('.active .verse');
    if (active_verse.length) {
        main_verse.hide();
        main_verse.attr('id', 'main-verse-old');
        active_verse.attr('id', 'main-verse');
        active_verse.remove().appendTo('strong.logo');
        var active_verse_pos;
    }
	$('#menu li').each(function(){
		var _el = $(this);
		var _t1;
		var _t2;
		var _pos;
		var _verse = _el.children('.verse');
		if (_verse.length) {
		    pos = _el.position();
		    offset = 60;
		    if ($.browser.msie) {
		        offset = 68;
		    }
		    _verse.css({position: "absolute",
		                marginTop:0,
		                top: '-70px',
		                left: (_verse.width() + offset) - pos.left + 'px'});
		    if (_verse.attr('id') == active_verse.attr('id')) {
		        _verse.css('left', 'auto');
		        _verse.fadeIn(function() {
		            active_verse_pos = $(this).position();
		        });
		    }
		}
		_el.mouseenter(function(e){
		    if (_verse.length) {
		        $('#main-logo').hide();
		    }
			var _window = window.innerWidth;
			if ($.browser.msie) _window = document.documentElement.clientWidth;
			_pos = e.clientX;
			if ((_pos+(_el.children("div.drop-menu").width()*2)) > _window ) {if ($(this).parent("#menu")) $(this).children("div.drop-menu").addClass("drop-right")}
			else {if ($(this).parent("#menu")) $(this).children("div.drop-menu").removeClass("drop-right")};
			if(_t1) {clearTimeout(_t1)};
			if(_t2) {clearTimeout(_t2)};
			_t2 = setTimeout(function(){
				if(_el.children('div').length){
					if ($.browser.msie) {
					    _el.addClass('hover').children('div').show();
					} else if (!_el.children('div').is(':animated')) { 
					    _el.addClass('hover').children('div').fadeIn(300);
					}
				}
			}, 100);
			if (_verse.length && active_verse.attr('id') != _verse.attr('id')) {
			    _verse.css({'z-index': '2'});
			}
		}).mouseleave(function(event){
			if(_t2) clearTimeout(_t2);
			if(_t1) clearTimeout(_t1);
			_t1 = setTimeout(function(){
				if(_el.children('div').length){
					if ($.browser.msie) {
					    _el.removeClass('hover').children('div').hide();
					    $('#main-verse').show();
					}
					else _el.children('div').stop().fadeOut(300, function(){
					    $(this).css('opacity', 1);
					    _el.removeClass('hover');
			            $('#main-verse').fadeIn();
					});
				}
			}, 100);
		});
	});
}

/*--- Tabs function ---*/
function initTabs(){
	var _active = window.location.toString();
	var _loader = $('<div id="loader"></div>').css({opacity: 0, display: 'block'});
	_active = _active.split('#')[1];
	if(_active != undefined){
		var a_btn = $('.tabset a.tab[rel="#'+_active+'"]');
		if(a_btn.length){
			a_btn.addClass('active').parents('.tabset:eq(0)').find('a.tab').not(a_btn).removeClass('active');
		}
	}
	$('.tabset').each(function(){
		var _btn = $(this).find('a.tab');
		var _a = _btn.index(_btn.filter('.active:eq(0)'));
		if(_a == -1) _a = 0;
		_btn.removeClass('active').eq(_a).addClass('active');
		_btn.each(function(_i){
			this._url = $(this).attr('href');
			this.onclick = function(){
				changeEl(_i);
				return false;
			}
		});
		$.ajax({
			url: _btn.get(_a)._url,
			cache: false,
			dataType: 'html',
			success: function(_html) {
				_btn.get(_a)._box = $(_html);
				$(_btn.eq(_a).attr('rel')).append(_btn.get(_a)._box);
				_btn.get(_a)._box = _btn.get(_a)._box.parent();
				_btn.get(_a)._box.show();
				initInfo();
			}
		});
		var _flag = true;
		function changeEl(_ind){
			if(_a != _ind){
				if(_flag){
					_flag = false;
					_btn.get(_a)._box.animate({opacity: 0}, 200, function(){
						_btn.eq(_a).removeClass('active');
						_btn.eq(_ind).addClass('active');
						if(_btn.get(_ind)._box){
							_btn.get(_a)._box.hide();
							_btn.get(_ind)._box.show().animate({opacity: 1}, 200, function(){
								_flag = true;
								_a = _ind;
							});
						}
						else{
							var p_h = _btn.get(_a)._box.parent().height();
							_btn.get(_a)._box.parent().height(p_h).append(_loader);
							_loader.css('top', p_h/2).animate({opacity: 1}, 100,function(){
								$.ajax({
									url: _btn.get(_ind)._url,
									cache: false,
									dataType: 'html',
									success: function(_html) {
										_btn.get(_ind)._box = $(_html);
										_loader.animate({opacity: 0}, 100,function(){
											$(this).remove();
											_btn.get(_a)._box.parent().height('auto');
											$(_btn.eq(_ind).attr('rel')).append(_btn.get(_ind)._box);
											_btn.get(_ind)._box = _btn.get(_ind)._box.parent().css('opacity', 0);
											_btn.get(_a)._box.hide();
											_btn.get(_ind)._box.show().animate({opacity: 1}, 200, function(){
												_flag = true;
												_a = _ind;
											});
											initInfo();
										});
									},
									error: function(){
										_loader.animate({opacity: 0}, 100,function(){
											$(this).remove();
											_btn.get(_a)._box.parent().height('auto');
											_flag = true;
											_btn.eq(_ind).removeClass('active');
											_btn.eq(_a).addClass('active');
											_btn.get(_a)._box.show().css('opacity', 1);
										});
									}
								});
							});
						}
					});
				}
			}
		}
	});
}
/*--- info text function ---*/
/*function initInfoText(){
	$('.nav-links').each(function(){
		var _btn = $(this).find('> li > a');
		var _a = _btn.index(_btn.filter('.active:eq(0)'));
		var _active = -1;
		_btn.removeClass('active').eq(_a).addClass('active');
		_btn.each(function(_i, _el){
			_el._box = $($(_el).attr('rel')).css({display:'block', opacity: 0});
			if(_i == _a){
				_active = _el;
				_a = _el;
				_el._box.css('opacity', 1).addClass('active');
			}
			else{
				_el._box.css('opacity', 0).removeClass('active');
			}
			$(_el).mouseenter(function(){
				if(this._box.length && this != _a){
					if(_a._box){
						_a._box.removeClass('active').animate({opacity:0}, {queue:false, duration:500});
						$(_a).removeClass('active');
					}
					this._box.addClass('active').animate({opacity:1}, {queue:false, duration:500});
					$(this).addClass('active');
					_a = this;
				}
			});
		});
		$(this).mouseleave(function(){
			if(_a._box){
				_a._box.removeClass('active').animate({opacity:0}, {queue:false, duration:500});
			}
			if(_active._box){
				_active._box.addClass('active').animate({opacity:1}, {queue:false, duration:500});
				_a = _active;
			}
		});
	});
}
*/