/*--- main menu ---*/
function ShowHide(){
	$("#extra-info").animate({"height": "toggle"}, { duration: 100 });
}

 function OpenClose(obj) {
   if (document.getElementById(obj).style.display=='block') {
	 //obj.style.display='none';
	 document.getElementById(obj).style.display='none';
   } else {
	 //obj.style.display='block';
	 document.getElementById(obj).style.display='block';
   }
 }

function popUp(URL) {
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=750,height=600,left = 470,top = 200');");
}
function popUpVideo(URL) {
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=320,height=255,left = 470,top = 200');");
}
function popUpVideo2(URL) {
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=400,height=300,left = 470,top = 200');");
}

/*--- main menu ---*/
function initToggle() {

	//Hide (Collapse) the toggle containers on load
	$(".toggle_container").hide(); 

	//Switch the "Open" and "Close" state per click
	$("h2.trigger").toggle(function(){
		$(this).addClass("active");
		}, function () {
		$(this).removeClass("active");
	});

	//Slide up and down on click
	$("h2.trigger").click(function(){
		$(this).next(".toggle_container").slideToggle("slow");
	});
}
function initMenu(){
	var _speed = 400; // in ms
	var _hold = $('#header ul.nav');
	var _t = null;
	if(_hold.length){
		var _list = _hold.children();
		_list.each(function(){
			var _el = $(this);
			var _box = _el.children('div.nav-w');
			var _sub = _box.children();
			_box.show().width(_sub.outerWidth()).hide();
			_el.mouseenter(function(){
			var _this = this;
				_t = setTimeout(function(){
					$(_this).css('zIndex', 10);
					_el.addClass('hover');
					if(_box.is(':hidden')){
						_box.show();
						_sub.css('marginTop', -_sub.outerHeight());
					}
					_sub.stop().animate({marginTop: 0}, _speed);
				},300)
			}).mouseleave(function(){
				if(_t) clearTimeout(_t);
				var _this = $(this);
				$(this).css('zIndex', 5);
				if(_sub.length){
					_sub.stop().animate({marginTop: -_sub.outerHeight()}, _speed, function(){
						_box.hide();
						_el.removeClass('hover');
						_this.css('zIndex', 1);
					});
				}else{
					_el.removeClass('hover');
				}
			});
		});
	}
}
/*--- side menu ---*/
function initMenu2(){
	var _speed = 400; // in ms
	var _hold = $('.video-box ul.nav-item');
	var _t = null;
	if(_hold.length){
		var _list = _hold.children();
		_list.each(function(){
			var _el = $(this);
			var _box = _el.children('div.nav-w');
			var _sub = _box.find('ul:eq(0)');
			_box.show().width(_sub.outerWidth()).hide();
			_sub.find('li').css('float','right');
			_el.mouseenter(function(){
				var _this = this;
				_t = setTimeout(function(){
					$(_this).css('zIndex', 10);
					_el.addClass('hover');
					if(_box.is(':hidden')){
						_box.show();
						_box.css('opacity', 0);
					}
					_box.stop().animate({opacity: 1}, _speed, function(){
						$(this).css('opacity', 'auto');
					});
				},100);
			}).mouseleave(function(){
				var _this = $(this);
				if(_t) clearTimeout(_t);
					_t2 = setTimeout(function(){
						_this.css('zIndex', 5);
						if(_box.length){
							_box.stop().animate({opacity: 0}, _speed, function(){
								_box.hide();
								_el.removeClass('hover');
								_this.css('zIndex', 1);
							});
						}else{
							_el.removeClass('hover');
						}
					},70);
			});
		});
	}
}
/*--- quick links ---*/
function quickLinks(){
	var _speed = 300; // in ms
	var _hold = $('#content div.quick-links ul.datalist:eq(0) > li:eq(0)');
	if(_hold.length){
		var _btn = _hold.children('a.title');
		var _box = _hold.children('div.links-list');
		var _sub = _box.children();
		
		/*--- with fade ---*/
		_box.css({opacity:0, display:'none'});
		_hold.mouseenter(function(){
			_hold.addClass('hover');
			_box.stop().show().animate({opacity: 1}, _speed);
		}).mouseleave(function(){
			_box.stop().animate({opacity: 0}, _speed, function(){
				$(this).hide();
				_hold.removeClass('hover');
			});
		});
		/* slide like main menu
		_hold.mouseenter(function(){
			_hold.addClass('hover');
			if(_box.is(':hidden')){
				_box.show();
				_sub.css('marginTop', -_sub.outerHeight());
			}
			_sub.stop().animate({marginTop: 0}, _speed);
		}).mouseleave(function(){
			_sub.stop().animate({marginTop: -_sub.outerHeight()}, _speed, function(){
				_box.hide();
				_hold.removeClass('hover');
			});
		});
		*/
	}
}

/*--- quick links ---*/
function subLinks(){
	var _speed = 300; // in ms
	var _hold = $('#content div.quick-links ul.datalist:eq(0) > li:eq(0)');
	if(_hold.length){
		var _btn = _hold.children('a.title');
		var _box = _hold.children('div.links-list');
		var _sub = _box.children();
		
		/*--- with fade ---*/
		_box.css({opacity:0, display:'none'});
		_hold.mouseenter(function(){
			_hold.addClass('hover');
			_box.stop().show().animate({opacity: 1}, _speed);
		}).mouseleave(function(){
			_box.stop().animate({opacity: 0}, _speed, function(){
				$(this).hide();
				_hold.removeClass('hover');
			});
		});
		/* slide like main menu
		_hold.mouseenter(function(){
			_hold.addClass('hover');
			if(_box.is(':hidden')){
				_box.show();
				_sub.css('marginTop', -_sub.outerHeight());
			}
			_sub.stop().animate({marginTop: 0}, _speed);
		}).mouseleave(function(){
			_sub.stop().animate({marginTop: -_sub.outerHeight()}, _speed, function(){
				_box.hide();
				_hold.removeClass('hover');
			});
		});
		*/
	}
}
/*--- buzz animations ---*/
function initBuzz(){
	var _speed = 300; // in ms
	var change_speed = 500; //in ms
	var _hold = $('#content div.buzz:eq(0)');
	if(_hold.length){
		var btn_view = _hold.find('a.view-all');
		var box_view = _hold.find('ul.datalist');
		var view_f = false;
		var view_t;
		box_view.css({display:'none', opacity: 0});
		btn_view.click(function(){
			if(view_f){
				view_f = false;
				box_view.stop().animate({opacity: 0}, _speed, function(){
					$(this).hide();
					btn_view.parent().removeClass('opened');
				});
			}
			else{
				view_f = true;
				btn_view.parent().addClass('opened');
				box_view.stop().show().animate({opacity: 1}, _speed);
			}
			return false;
		});
		_hold.mouseenter(function(){
			if(view_f){
				if(view_t) clearTimeout(view_t);
			}
		}).mouseleave(function(){
			if(view_f){
				if(view_t) clearTimeout(view_t);
				view_t = setTimeout(function(){
					view_f = false;
					box_view.stop().animate({opacity: 0}, _speed, function(){
						$(this).hide();
						btn_view.parent().removeClass('opened');
					});
				}, 1000);
				
			}
		});
		$(document).mousedown(function(e){
			e = e || event;
			var t = e.target || e.srcElement;
			t = $(t);
			if(view_f && t.parents('div.buzz').length == 0 && !t.hasClass('buzz')){
				if(view_t) clearTimeout(view_t);
				view_f = false;
				box_view.stop().animate({opacity: 0}, _speed, function(){
					$(this).hide();
					btn_view.parent().removeClass('opened');
				});
			}
		});
		
		var list_hold = _hold.find('div.buzz-list > ul');
		var _list = list_hold.children();
		var btn_prev = _hold.find('a.btn-prev');
		var btn_next = _hold.find('a.btn-next');
		var hold_w = list_hold.parent().width();
		var _autoRun = 7000;
		var _t = null;
		var _w = _list.length * _list.outerWidth(true);
		if(_w > hold_w){
			list_hold.append(_list.clone()).append(_list.clone());
			var _flag = true;
			var _m = _w;
			list_hold.css({left: -_m});

			btn_prev.click(function(){
				if(_t) clearTimeout(_t);
				moveList(false);
				return false;
			});
			btn_next.click(function(){
				if(_t) clearTimeout(_t);
				moveList(true);
				return false;
			});
		}
		else{
			btn_prev.click(function(){
				return false;
			}).css({visibility:'hidden'});
			btn_next.click(function(){
				return false;
			}).css({visibility:'hidden'});
		}

		if(_autoRun){
			_t = setTimeout(function(){
				moveList(true);
			},_autoRun);
		}

		function moveList(_f){
			if(_flag){
				_flag = false;
				if(_f){
					if(_m + hold_w > _w*3 - hold_w){
						_m -= _w;
						list_hold.css('left', -_m);
					}
					_m += hold_w;
				}
				else{
					if(_m - hold_w < 0){
						_m += _w;
						list_hold.css('left', -_m);
					}
					_m -= hold_w;
				}
				list_hold.animate({left: -_m}, change_speed, function(){
					_flag = true;
				});
				if(_autoRun){
					_t = setTimeout(function(){
						moveList(true);
					},_autoRun);
				};
			}
		}
	}
}
/*--- popups function ---*/
function initPopups(){
	if($('#fader').length == 0) $('body').append('<div id="fader"></div>');
	var _fader = $('#fader');
	_fader.hide();
	var _popup = -1;
	
	$('a.with-popup').each(function(){
		var _el = this;
		if(_el.hash && _el.hash.length > 1){
			_el._popup = $(_el.hash);
			if(_el._popup.length){
				_el.onclick = function(){
					if(_el._popup){
						_popup = _el._popup;
						showPopup();
					}
					return false;
				}
				_el._popup.find('a.btn-close').click(function(){
					if($.browser.msie){
						_popup.hide();
						_fader.fadeOut(200);
						_popup = -1;
						if($.browser.version < 7) $('#page select').css('visibility', 'visible');
					}
					else{
						_popup.fadeOut(400, function(){
							_fader.fadeOut(200);
							_popup = -1;
						});
					}
					return false;
				});
			}
		}
	});
	_fader.click(function(){
		if(_popup != -1){
			if($.browser.msie){
				_popup.hide();
				_fader.fadeOut(200);
				_popup = -1;
				if($.browser.version < 7) $('#page select').css('visibility', 'visible');
			}
			else{
				_popup.fadeOut(400, function(){
					_fader.fadeOut(200);
					_popup = -1;
				});
			}
		}
		return false;
	});
	
	$(document).keydown(function(e){
		if(!e)evt = window.event;
		if(e.keyCode == 27 && _popup != -1){
			if($.browser.msie){
				_popup.hide();
				_fader.fadeOut(200);
				_popup = -1;
				if($.browser.version < 7) $('#page select').css('visibility', 'visible');
			}
			else{
				_popup.fadeOut(400, function(){
					_fader.fadeOut(200);
					_popup = -1;
				});
			}
		}
	});
	$(window).resize(function(){
		if(_popup != -1){
			_popup.css({top: $(window).scrollTop()+ ($(window).height() - _popup.outerHeight())/2});
			_fader.height(initH());
			if(initH() < _popup.outerHeight()+_popup.offset().top) _fader.height(_popup.outerHeight()+_popup.offset().top);
		}
	}).scroll(function(){
		if(_popup != -1){
			_popup.css({top: $(window).scrollTop()+ ($(window).height() - _popup.outerHeight())/2});
			_fader.height(initH());
			if(initH() < _popup.outerHeight()+_popup.offset().top) _fader.height(_popup.outerHeight()+_popup.offset().top);
		}
	});
	function showPopup(){
		_fader.css({
			opacity: 0,
			height: initH(),
			display:'block'
		});
		_popup.css({top: $(window).scrollTop()});
		if($.browser.msie && $.browser.version < 7) $('#page select').css('visibility', 'hidden');
		_fader.fadeTo(400, 0.5, function(){
			if($.browser.msie) _popup.show();
			else _popup.fadeIn(200, function(){
				if(initH() < _popup.outerHeight()+_popup.offset().top) _fader.height(_popup.outerHeight()+_popup.offset().top);
			});
			_popup.css({top: $(window).scrollTop()+ ($(window).height() - _popup.outerHeight())/2});
			if(initH() < _popup.outerHeight()+_popup.offset().top) _fader.height(_popup.outerHeight()+_popup.offset().top);
		});
	}
	function initH(){
		var _h = $('#page').outerHeight();
		if(_h < $(window).height()) _h = $(window).height();
		if(_h < $('body').height()) _h = $('body').height();
		return _h;
	}
}
$(document).ready(function(){
	initMenu2();
	initMenu();
	quickLinks();
	subLinks();
	initPopups();
	initBuzz();
	initToggle();
	$('div.long').each(function(){
		$(this).css({width: $(this).children().width()})
	});
});