jQuery(document).ready(function($){
	initMeta();
	initTGnav();
	jQuery('div.news-teaser div.teaser:odd').addClass("right");
	
/*--- select open --*/
	jQuery("#boxbdropdown").change(function(){
		var pos = this.selectedIndex;
		var obj = jQuery("#boxbdropdown option:eq("+pos+")");
		var target = obj.attr("value");
		var how = jQuery.trim(obj.attr("class"));
		var size = obj.attr("title")+",scrollbars=yes";
		if (target!=null){
			if (how =="o-new"){
				window.open(target, "Popupname", size);
			}else if(how=="o-self"){
				top.location.href=target;
			}
			this.selectedIndex = 0;
		}
	});
});


/*--- menu function ---*/
function initMeta(){
	jQuery('#metanav li').hover(function(){
		jQuery(this).addClass('hover');
	}, function(){
		jQuery(this).removeClass('hover');
	});
	jQuery('#metanav li:has("div")').each(function(){
		var box_h = jQuery(this).children('div');
		var _box = box_h.children('div');
		var _h;
		jQuery(this).mouseenter(function(){
			if(box_h.is(':hidden')){
				box_h.css({height: 0, display: 'block'});
				_h = _box.outerHeight();
				_box.css({marginTop: -_h});
			}
			box_h.animate({height: _h}, {queue:false, duration:200});
			_box.stop().animate({marginTop: 0}, 200, function(){
				hideSelectBoxes(box_h);
			});
		}).mouseleave(function(){
			box_h.animate({height: 0}, {queue:false, duration:200});
			_box.stop().animate({marginTop: -_h}, 200, function(){
				box_h.css({display: 'none', height: 'auto'});
				_box.css({marginTop: 0});
				showSelectBoxes(box_h);
			});
		});
	});
}


/*--- menu function ---*/
function initTGnav(){
	jQuery('#tgnav li').hover(function(){
		jQuery(this).addClass('hover');
	}, function(){
		jQuery(this).removeClass('hover');
	});
	jQuery('#tgnav li:has("div")').each(function(){
		var box_h = jQuery(this).children('div');
		var _box = box_h.children('div');
		var _h;
		jQuery(this).mouseenter(function(){
			if(box_h.is(':hidden')){
				box_h.css({height: 0, display: 'block'});
				_h = _box.outerHeight();
				_box.css({marginTop: -_h});
			}
			box_h.animate({height: _h}, {queue:false, duration:200});
			_box.stop().animate({marginTop: 0}, 200, function(){
				hideSelectBoxes(box_h);
			});
		}).mouseleave(function(){
			box_h.animate({height: 0}, {queue:false, duration:200});
			_box.stop().animate({marginTop: -_h}, 200, function(){
				box_h.css({display: 'none', height: 'auto'});
				_box.css({marginTop: 0});
				showSelectBoxes(box_h);
			});
		});
	});
}


/*--- hide selects function ---*/
function hideSelectBoxes(object) {
	if (jQuery.browser.msie && jQuery.browser.version < 7) {
		var selects_list = jQuery('select');
		object.each(function() {
			var _el = jQuery(this);
			var t = _el.offset().top;
			var l = _el.offset().left;
			var w = _el.outerWidth();
			var h = _el.outerHeight();
			var el_selects = [];
			selects_list.filter(':visible').not(_el.find('select')).each(function(){
				var _select = jQuery(this);
				var s_t = _select.offset().top;
				var s_l = _select.offset().left;
				var s_w = _select.outerWidth();
				var s_h = _select.outerHeight();
				var _ver = false, _hor = false;
				if((t - s_t > 0) ? (t - s_t < s_h) : (t - s_t + h > 0)) _ver = true;
				if((l - s_l > 0) ? (l - s_l < s_w) : (l - s_l + w > 0)) _hor = true;
				if(_ver && _hor) {
					_select.css('visibility', 'hidden');
					el_selects.push(this);
				}
			});
			this.sboxes = el_selects;
		});
	}
}
function showSelectBoxes(object) {
	if (jQuery.browser.msie && jQuery.browser.version < 7) {
		object.each(function() {
			if(this.sboxes.length > 0) {
				jQuery(this.sboxes).css('visibility','visible');
			}
		});
	}	
}
