/*--- 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;
	}
}
(function($) {
	$.fn.gallery = function(options) { return new Gallery(this.get(0), options); };
	
	function Gallery(context, options) { this.init(context, options); };
	
	Gallery.prototype = {
		options:{},
		init: function (context, options){
			this.options = $.extend({
				duration: 700,
				slideElement: 1,
				autoRotation: false,
				effect: false,
				listOfSlides: 'ul > li',
				switcher: false,
				disableBtn: false,
				topLink: '.back-to-top',
				nextBtn: 'a.link-next, a.btn-next, a.next',
				prevBtn: 'a.link-prev, a.btn-prev, a.prev',
				circle: true,
				slider:false,
				direction: false,
				event: 'click',
				IE: false
			}, options || {});
			var _el = $(context).find(this.options.listOfSlides);
			this.els = _el;
			this.links = $(context).find(this.options.topLink);
			if (this.options.effect) this.list = _el;
			else this.list = _el.parent();
			if (this.options.switcher) this.switcher = $(context).find(this.options.switcher);
			this.nextBtn = $(context).find(this.options.nextBtn);
			this.prevBtn = $(context).find(this.options.prevBtn);
			this.count = _el.index(_el.filter(':last'));
			
			if (this.options.switcher) this.active = this.switcher.index(this.switcher.filter('.active:eq(0)'));
			else this.active = _el.index(_el.filter('.active:eq(0)'));
			if (this.active < 0) this.active = 0;
			this.last = this.active;
			
			this.els.removeClass('active').eq(this.active).addClass('active');
			
			this.woh = _el.outerWidth(true);
			if (!this.options.direction) this.installDirections(this.list.parent().width());
			else {
				this.woh = _el.outerHeight(true);
				this.installDirections(this.list.parent().height());
			}
			this.end = this.els.length - this.wrapHolderW;
			if (!this.options.effect) {
				this.rew = this.count - this.wrapHolderW + 1;
				if (!this.options.direction) this.list.css({marginLeft: -(this.woh * this.active)});
				else this.list.css({marginTop: -(this.woh * this.active)});
			}
			else {
				this.rew = this.count;
				this.list.css({opacity: 0}).removeClass('active').eq(this.active).addClass('active').css({opacity: 1}).css('opacity', 'auto');
				if (this.options.switcher) this.switcher.removeClass('active').eq(this.active).addClass('active');
			}
			
			if (this.options.disableBtn) {
				if (this.count < this.wrapHolderW) this.nextBtn.addClass(this.options.disableBtn);
				if (this.active == 0) this.prevBtn.addClass(this.options.disableBtn);
			}
			
			this.initEvent(this, this.nextBtn, this.prevBtn, true);
			this.initEvent(this, this.prevBtn, this.nextBtn, false);
			
			if (this.options.autoRotation) this.runTimer(this);
			
			if (this.options.switcher) this.initEventSwitcher(this, this.switcher);
			this.hoverEls(this);
			if (this.links.length) this.backTop(this);
			
			this.summ = 0;
			this.finish = 0;
			if (this.options.slider) {
				var _this = this;
				this.els.each(function(){
					_this.summ += $(this).outerWidth();
				});
				this.finish = this.summ - this.list.parent().outerWidth();
				this.eventSlide(this,this.nextBtn,true);
				this.eventSlide(this,this.prevBtn,false);
				this.list.css({
					marginLeft: -_this.finish/2
				});
			}
		},
		eventSlide: function($this,$btn,_dir){
			$btn.mouseenter(function(){
				var _marg = Math.abs(parseInt($this.list.css('marginLeft')));
				var _left = _dir ? $this.finish - _marg : _marg;

				if (_dir) $this.prevBtn.show();
				else $this.nextBtn.show();

				$this.initSlide($this,_dir ? -$this.finish : 0,_left,_dir);
			}).mouseleave(function(){
				$this.list.stop();
			});
		},
		initSlide: function($this, _ind,_dur,_dir){
			$this.list.animate({
				marginLeft: _ind
			},{
				queue:false,
				duration: $this.options.duration * _dur / $this.finish,
				complete:function(){
					if (_dir) $this.nextBtn.hide();
					else $this.prevBtn.hide();
				}
			});
		},
		backTop: function($this){
			$this.links.click(function(){
				if ($this.switcher.length) $this.switcher.eq(0).trigger('click');
				return false;
			});
		},
		hoverEls: function($this){
			$this.els.mouseenter(function(){
				$(this).addClass('hover');
			}).mouseleave(function(){
				$(this).removeClass('hover');
			});
		},
		installDirections: function(temp){
			this.wrapHolderW = Math.floor(temp / this.woh);
			if (((this.wrapHolderW - 1) * this.woh + this.woh / 2) > temp) this.wrapHolderWwrapHolderW--;
		},
		fadeElement: function(){
			if ($.browser.msie && this.options.IE){
				this.list.eq(this.last).css({opacity:0});
				this.list.removeClass('active').eq(this.active).addClass('active').css({opacity:'auto'});
			}
			else{
				this.list.eq(this.last).animate({opacity:0}, {queue:false, duration: this.options.duration});
				this.list.removeClass('active').eq(this.active).addClass('active').animate({
					opacity:1
				}, {queue:false, duration: this.options.duration, complete: function(){
					$(this).css('opacity','auto');
				}});
			}
			if (this.options.switcher) this.switcher.removeClass('active').eq(this.active).addClass('active');
			this.last = this.active;
		},
		scrollElement: function(_end){
			if (_end) {
				if (!this.options.direction) this.list.animate({marginLeft: -(this.woh * _end)}, {queue:false, duration: this.options.duration});
				else this.list.animate({marginTop: -(this.woh * _end)}, {queue:false, duration: this.options.duration});
			} else {
				if (!this.options.direction) this.list.animate({marginLeft: -(this.woh * this.active)}, {queue:false, duration: this.options.duration});
				else this.list.animate({marginTop: -(this.woh * this.active)}, {queue:false, duration: this.options.duration});
			}
			if (this.options.switcher) this.switcher.removeClass('active').eq(this.active).addClass('active');
		},
		runTimer: function($this){
			if($this._t) clearTimeout($this._t);
			$this._t = setInterval(function(){
				$this.toPrepare($this, true);
			}, this.options.autoRotation);
		},
		initEventSwitcher: function($this, el){
			el.bind($this.options.event, function(){
				$this.active = $this.switcher.index($(this));
				if($this._t) clearTimeout($this._t);
				if ($this.options.autoRotation) $this.runTimer($this);
				$this.els.removeClass('active').eq($this.active).addClass('active');
				if ($this.active <= $this.end) {
					if (!$this.options.effect) $this.scrollElement();
					else $this.fadeElement();
				} else {
					if (!$this.options.effect) $this.scrollElement($this.end);
					else $this.fadeElement();
				}
				return false;
			});
		},
		initEvent: function($this, addEventEl, addDisClass, dir){
			addEventEl.bind($this.options.event, function(){
				if (!$this.options.slider) {
					if($this._t) clearTimeout($this._t);
					if ($this.options.disableBtn &&($this.count > $this.wrapHolderW)) addDisClass.removeClass($this.options.disableBtn);
					$this.toPrepare($this, dir);
					if ($this.options.autoRotation) $this.runTimer($this);
				}
				return false;
			});
		},
		toPrepare: function($this, side){
			if (($this.active == $this.rew) && $this.options.circle && side) $this.active = -$this.options.slideElement;
			if (($this.active == 0) && $this.options.circle && !side) $this.active = $this.rew + $this.options.slideElement;
			for (var i = 0; i < $this.options.slideElement; i++){
				if (side) {
					if ($this.active + 1 > $this.rew) {
						if ($this.options.disableBtn && ($this.count > $this.wrapHolderW)) $this.nextBtn.addClass($this.options.disableBtn);
					}
					else $this.active++;
				}
				else{
					if ($this.active - 1 < 0) {
						if ($this.options.disableBtn && ($this.count > $this.wrapHolderW)) $this.prevBtn.addClass($this.options.disableBtn);
					}
					else $this.active--;
				}
			};
			if ($this.active == $this.rew && side) if ($this.options.disableBtn &&($this.count > $this.wrapHolderW)) $this.nextBtn.addClass($this.options.disableBtn);
			if ($this.active == 0 && !side) if ($this.options.disableBtn &&($this.count > $this.wrapHolderW)) $this.prevBtn.addClass($this.options.disableBtn);
			if (!$this.options.effect) $this.scrollElement();
			else $this.fadeElement();
		},
		stop: function(){
			if (this._t) clearTimeout(this._t);
		},
		play: function(){
			if (this._t) clearTimeout(this._t);
			if (this.options.autoRotation) this.runTimer(this);
		}
	}
}(jQuery));
$(document).ready(function(){
	initMenu2();
	initMenu();
	quickLinks();
	subLinks();
	initPopups();
	initBuzz();
	initToggle();
	$('div.long').each(function(){
		$(this).css({width: $(this).children().width()})
	});
	$('div.container-slider').gallery({
		duration: 500,
		listOfSlides: '.post',
		switcher: '.add-nav a',
		direction: true
	});
	$('div.scroll-block').gallery({
		duration: 2000,
		listOfSlides: '.scroll-block-area a',
		nextBtn: 'div.next',
		prevBtn: 'div.prev',
		circle:false,
		slider:true
	});
	// hide the pop-up
	$('#gi-popup').css('opacity', 0);
	
	// on mouseover
	$('#gi-popup-container').hover(function() {
		$('#gi-popup').animate({opacity: 1}, 200);
	},function() {
		$('#gi-popup').animate({opacity:0},200);
	});	
	
});
