$(document).ready(function (){
	ajaxTab();
	initScrollbars();
	ieHover('ul.nav > li, div.popup-box');
	initHScrollbars();
	initShedule();
});

// init custom scrollbars
function initScrollbars() {
	$('div.vscrollable').jScrollPane({
		scrollbarWidth: 18,
		dragMaxHeight: 55
	});
}

// initShedule
function initShedule(_obj){
	if(_obj){
		_holder = $('.content-schedule', _obj);
	}else{
		var _holder = $('.content-schedule');
	}
	
	_holder.each(function(){
		var _this = $(this),
			scrollContent = $('>.frame-holder', _this);
		
		if(scrollContent.outerWidth() > _this.width() && !$('.panetrack', _this).length){
			_this.css('overflow','hidden');
			_this.append('<div class="panetrack"><div class="holder-panetrack"><div class="arrowLeft"></div><div class="scrollbar"></div><div class="arrowRight"></div></div></div>');
			
			$('.scrollbar', _this).slider({
				min: 0,
				max: $('>.frame', scrollContent).length-1,
				slide:function(e, ui){
					 scrollContent.animate({'margin-left': - (ui.value * $('>.frame', scrollContent).outerWidth()) + 'px'}, 400);
				}
			});
			$('.arrowRight', _this).click(function(){
				var value = $('.scrollbar', _this).slider("option", "value" );
				$('.scrollbar', _this).slider({
					value: value + 1
				});
				value += 1
				scrollContent.animate({'margin-left': - ((value) * $('>.frame', scrollContent).outerWidth()) + 'px'}, 400);
			});
			$('.arrowLeft', _this).click(function(){
				var value = $('.scrollbar', _this).slider("option", "value" );
				$('.scrollbar', _this).slider({
					value: value - 1
				});
				value -= 1
				scrollContent.animate({'margin-left': - ((value) * $('>.frame', scrollContent).outerWidth()) + 'px'}, 400);
			})
		}
	})
}

// initHScrollbars
function initHScrollbars(_obj){
	if(_obj){
		var scrollPane = $('.hscrollable', _obj);
	}else{
		var scrollPane = $('.hscrollable');
	}
	
	scrollPane.each(function(){
		var _this = $(this),
			scrollContent = $('>ul', _this);
		
		if(scrollContent.outerWidth() > _this.width() && !$('.panetrack', _this).length){
			_this.css('overflow','hidden');
			_this.append('<div class="panetrack"><div class="holder-panetrack"><div class="scrollbar"></div></div></div>');
			
			$('.scrollbar', _this).slider({
				slide:function(e, ui){
					scrollContent.css('margin-left', Math.round( ui.value / 100 * ( _this.width() - scrollContent.width() )) + 'px')
				}
			});
			var slider_btn = slider_holder.find(".ui-slider-handle");
			slider_btn.mousedown(function(){
				return false
			});
			slider_btn.click(function(){
				return false
			});
		}
	})
}

// ieHover
function ieHover(h_list){
	if ($.browser.msie && $.browser.version < 7){
		$(h_list).mouseenter(function(){
			$(this).addClass('hover');
		}).mouseleave(function(){
			$(this).removeClass('hover');
		});
	}
}

// ajaxTab
function ajaxTab(){
	var tabContent = $('.main'),
		tabLinks = $('.nav ul a'),
		tabLinkActiveIndex = tabLinks.parent('li').index(tabLinks.parent('li').filter('.sub-active'));
	
	if(tabLinkActiveIndex >= 0){
		$.ajax({
			dataType: "html",
			url: tabLinks.get(tabLinkActiveIndex).href,
			success: function(ajaxContent){
				tabLinks.get(tabLinkActiveIndex).content = $(ajaxContent);
				tabLinks.get(tabLinkActiveIndex).content.attr('rel', tabLinkActiveIndex);
				tabContent.append(tabLinks.get(tabLinkActiveIndex).content);
				tabLinks.get(tabLinkActiveIndex).content.find('div.vscrollable').jScrollPane({
					scrollbarWidth: 18,
					dragMaxHeight: 55
				});
				initHScrollbars(tabLinks.get(tabLinkActiveIndex).content);
				initShedule(tabLinks.get(tabLinkActiveIndex).content);
			}
		});
	}
	
	tabLinks.each(function (){
		var tabBtn = $(this);
		var i;
		tabBtn.click(function (){
			if (!tabBtn.parent('li').hasClass('sub-active')){
				if(!tabLinks.get(tabLinks.index(tabBtn)).content){
					$.ajax({
						dataType: "html",
						url: tabBtn.attr('href'),
						success: function(ajaxContent){
							tabLinkActiveIndex = tabLinks.index(tabBtn);
							i = $('.nav ul li').index($('.nav ul li').filter('.sub-active'));
							$('.nav ul li').removeClass('sub-active');
							tabBtn.parent('li').addClass('sub-active');
							tabLinks.get(tabLinkActiveIndex).content = $(ajaxContent);
							tabLinks.get(tabLinkActiveIndex).content.attr('rel', tabLinkActiveIndex);
							tabContent.append(tabLinks.get(tabLinkActiveIndex).content);
							_switch(tabLinkActiveIndex, i);
							
							tabLinks.get(tabLinkActiveIndex).content.find('div.vscrollable').jScrollPane({
								scrollbarWidth: 18,
								dragMaxHeight: 55
							});
							initHScrollbars(tabLinks.get(tabLinkActiveIndex).content);
							initShedule(tabLinks.get(tabLinkActiveIndex).content);
						}
					});
				} else {
					tabLinkActiveIndex = tabLinks.index(tabBtn);
					i = $('.nav ul li').index($('.nav ul li').filter('.sub-active'));
					$('.nav ul li').removeClass('sub-active');
					tabBtn.parent('li').addClass('sub-active');
					_switch(tabLinkActiveIndex, i);
				}
			}
			function _switch(_next,_curent){
				var _nextTab = $('.main .tab[rel='+_next+']'),
					_curentTab = $('.main .tab[rel='+_curent+']'),
					_speedAnimate = 800;
				
				tabContent.height(_curentTab.outerHeight());
				
				if(_next>_curent){
					_nextTab.css({
						'position':'absolute',
						'left':'1097px'
					})
					_curentTab.css({
						'position':'absolute'
					})
					_curentTab.animate({'left':-tabContent.outerWidth()+'px'}, {queue:false, duration:_speedAnimate, complete:function(){
						_curentTab.css({
							'left': tabContent.outerWidth()+'px'
						})
					}});
					_nextTab.animate({'left':0}, {queue:false, duration:_speedAnimate, complete:function(){
						_nextTab.css({
							'position':'relative'
						})
						tabContent.animate({'height':_nextTab.outerHeight()+'px'}, {queue:false, duration:_speedAnimate/2});
					}})
				} else {
					_nextTab.css({
						'position':'absolute',
						'left': -tabContent.outerWidth()+'px'
					})
					_curentTab.css({
						'position':'absolute'
					})
					_curentTab.animate({'left':tabContent.outerWidth()+'px'}, {queue:false, duration:_speedAnimate, complete:function(){
						_curentTab.css({
							'left': tabContent.outerWidth()+'px'
						})
					}});
					_nextTab.animate({'left':0}, {queue:false, duration:_speedAnimate, complete:function(){
						_nextTab.css({
							'position':'relative'
						})
						tabContent.animate({'height':_nextTab.outerHeight()+'px'}, {queue:false, duration:_speedAnimate/2});
					}})
				}
			}
			return false;
		});
	});
}


/* Copyright (c) 2009 Brandon Aaron (http://brandonaaron.net)
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 * Thanks to: http://adomas.org/javascript-mouse-wheel/ for some pointers.
 * Thanks to: Mathias Bank(http://www.mathias-bank.de) for a scope bug fix.
 *
 * Version: 3.0.2
 *
 * Requires: 1.2.2+
 */
;(function(c){var a=["DOMMouseScroll","mousewheel"];c.event.special.mousewheel={setup:function(){if(this.addEventListener){for(var d=a.length;d;){this.addEventListener(a[--d],b,false)}}else{this.onmousewheel=b}},teardown:function(){if(this.removeEventListener){for(var d=a.length;d;){this.removeEventListener(a[--d],b,false)}}else{this.onmousewheel=null}}};c.fn.extend({mousewheel:function(d){return d?this.bind("mousewheel",d):this.trigger("mousewheel")},unmousewheel:function(d){return this.unbind("mousewheel",d)}});function b(f){var d=[].slice.call(arguments,1),g=0,e=true;f=c.event.fix(f||window.event);f.type="mousewheel";if(f.wheelDelta){g=f.wheelDelta/120}if(f.detail){g=-f.detail/3}d.unshift(f,g);return c.event.handle.apply(this,d)}})(jQuery);

/* Copyright (c) 2007 Paul Bakaus (paul.bakaus@googlemail.com) and Brandon Aaron (brandon.aaron@gmail.com || http://brandonaaron.net)
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 *
 * $LastChangedDate: 2007-12-20 08:43:48 -0600 (Thu, 20 Dec 2007) $
 * $Rev: 4257 $
 *
 * Version: 1.2
 *
 * Requires: jQuery 1.2+
 */
;(function($){$.dimensions={version:'1.2'};$.each(['Height','Width'],function(i,name){$.fn['inner'+name]=function(){if(!this[0])return;var torl=name=='Height'?'Top':'Left',borr=name=='Height'?'Bottom':'Right';return this.is(':visible')?this[0]['client'+name]:num(this,name.toLowerCase())+num(this,'padding'+torl)+num(this,'padding'+borr);};$.fn['outer'+name]=function(options){if(!this[0])return;var torl=name=='Height'?'Top':'Left',borr=name=='Height'?'Bottom':'Right';options=$.extend({margin:false},options||{});var val=this.is(':visible')?this[0]['offset'+name]:num(this,name.toLowerCase())+num(this,'border'+torl+'Width')+num(this,'border'+borr+'Width')+num(this,'padding'+torl)+num(this,'padding'+borr);return val+(options.margin?(num(this,'margin'+torl)+num(this,'margin'+borr)):0);};});$.each(['Left','Top'],function(i,name){$.fn['scroll'+name]=function(val){if(!this[0])return;return val!=undefined?this.each(function(){this==window||this==document?window.scrollTo(name=='Left'?val:$(window)['scrollLeft'](),name=='Top'?val:$(window)['scrollTop']()):this['scroll'+name]=val;}):this[0]==window||this[0]==document?self[(name=='Left'?'pageXOffset':'pageYOffset')]||$.boxModel&&document.documentElement['scroll'+name]||document.body['scroll'+name]:this[0]['scroll'+name];};});$.fn.extend({position:function(){var left=0,top=0,elem=this[0],offset,parentOffset,offsetParent,results;if(elem){offsetParent=this.offsetParent();offset=this.offset();parentOffset=offsetParent.offset();offset.top-=num(elem,'marginTop');offset.left-=num(elem,'marginLeft');parentOffset.top+=num(offsetParent,'borderTopWidth');parentOffset.left+=num(offsetParent,'borderLeftWidth');results={top:offset.top-parentOffset.top,left:offset.left-parentOffset.left};}return results;},offsetParent:function(){var offsetParent=this[0].offsetParent;while(offsetParent&&(!/^body|html$/i.test(offsetParent.tagName)&&$.css(offsetParent,'position')=='static'))offsetParent=offsetParent.offsetParent;return $(offsetParent);}});function num(el,prop){return parseInt($.curCSS(el.jquery?el[0]:el,prop,true))||0;};})(jQuery);

/* Copyright (c) 2006 Kelvin Luck (kelvin AT kelvinluck DOT com || http://www.kelvinluck.com)
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 *
 * See http://kelvinluck.com/assets/jquery/jScrollPane/
 * $Id: jScrollPane.js 33 2008-12-10 22:55:28Z kelvin.luck $
 */
;(function(A){A.jScrollPane={active:[]};A.fn.jScrollPane=function(C){C=A.extend({},A.fn.jScrollPane.defaults,C);var B=function(){return false};return this.each(function(){var O=A(this);O.css("overflow","hidden");var X=this;if(A(this).parent().is(".jScrollPaneContainer")){var Ac=C.maintainPosition?O.position().top:0;var L=A(this).parent();var d=L.innerWidth();var Ad=L.outerHeight();var M=Ad;A(">.jScrollPaneTrack, >.jScrollArrowUp, >.jScrollArrowDown",L).remove();O.css({top:0})}else{var Ac=0;this.originalPadding=O.css("paddingTop")+" "+O.css("paddingRight")+" "+O.css("paddingBottom")+" "+O.css("paddingLeft");this.originalSidePaddingTotal=(parseInt(O.css("paddingLeft"))||0)+(parseInt(O.css("paddingRight"))||0);var d=O.innerWidth();var Ad=O.innerHeight();var M=Ad;O.wrap(A("<div></div>").attr({className:"jScrollPaneContainer"}).css({height:Ad+"px",width:d+"px"}));A(document).bind("emchange",function(Ae,Af,p){O.jScrollPane(C)})}if(C.reinitialiseOnImageLoad){var N=A.data(X,"jScrollPaneImagesToLoad")||A("img",O);var G=[];if(N.length){N.each(function(p,Ae){A(this).bind("load",function(){if(A.inArray(p,G)==-1){G.push(Ae);N=A.grep(N,function(Ag,Af){return Ag!=Ae});A.data(X,"jScrollPaneImagesToLoad",N);C.reinitialiseOnImageLoad=false;O.jScrollPane(C)}}).each(function(Af,Ag){if(this.complete||this.complete===undefined){this.src=this.src}})})}}var o=this.originalSidePaddingTotal;var l={height:"auto",width:d-C.scrollbarWidth-C.scrollbarMargin-o+"px"};if(C.scrollbarOnLeft){l.paddingLeft=C.scrollbarMargin+C.scrollbarWidth+"px"}else{l.paddingRight=C.scrollbarMargin+"px"}O.css(l);var m=O.outerHeight();var i=Ad/m;if(i<0.99){var H=O.parent();H.append(A("<div></div>").attr({className:"jScrollPaneTrack"}).css({width:C.scrollbarWidth+"px"}).append(A("<div></div>").attr({className:"jScrollPaneDrag"}).css({width:C.scrollbarWidth+"px"}).append(A("<div></div>").attr({className:"jScrollPaneDragTop"}).css({width:C.scrollbarWidth+"px"}),A("<div></div>").attr({className:"jScrollPaneDragBottom"}).css({width:C.scrollbarWidth+"px"}))));var z=A(">.jScrollPaneTrack",H);var P=A(">.jScrollPaneTrack .jScrollPaneDrag",H);if(C.showArrows){var g;var Ab;var S;var r;var j=function(){if(r>4||r%4==0){y(u+Ab*b)}r++};var K=function(p){A("html").unbind("mouseup",K);g.removeClass("jScrollActiveArrowButton");clearInterval(S)};var Z=function(){A("html").bind("mouseup",K);g.addClass("jScrollActiveArrowButton");r=0;j();S=setInterval(j,100)};H.append(A("<a></a>").attr({href:"javascript:;",className:"jScrollArrowUp"}).css({width:C.scrollbarWidth+"px"}).html("Scroll up").bind("mousedown",function(){g=A(this);Ab=-1;Z();this.blur();return false}).bind("click",B),A("<a></a>").attr({href:"javascript:;",className:"jScrollArrowDown"}).css({width:C.scrollbarWidth+"px"}).html("Scroll down").bind("mousedown",function(){g=A(this);Ab=1;Z();this.blur();return false}).bind("click",B));var Q=A(">.jScrollArrowUp",H);var J=A(">.jScrollArrowDown",H);if(C.arrowSize){M=Ad-C.arrowSize-C.arrowSize;z.css({height:M+"px",top:C.arrowSize+"px"})}else{var s=Q.height();C.arrowSize=s;M=Ad-s-J.height();z.css({height:M+"px",top:s+"px"})}}var w=A(this).css({position:"absolute",overflow:"visible"});var D;var Y;var b;var u=0;var V=i*Ad/2;var a=function(Ae,Ag){var Af=Ag=="X"?"Left":"Top";return Ae["page"+Ag]||(Ae["client"+Ag]+(document.documentElement["scroll"+Af]||document.body["scroll"+Af]))||0};var f=function(){return false};var v=function(){n();D=P.offset(false);D.top-=u;Y=M-P[0].offsetHeight;b=2*C.wheelSpeed*Y/m};var E=function(p){v();V=a(p,"Y")-u-D.top;A("html").bind("mouseup",T).bind("mousemove",h);if(A.browser.msie){A("html").bind("dragstart",f).bind("selectstart",f)}return false};var T=function(){A("html").unbind("mouseup",T).unbind("mousemove",h);V=i*Ad/2;if(A.browser.msie){A("html").unbind("dragstart",f).unbind("selectstart",f)}};var y=function(Ae){Ae=Ae<0?0:(Ae>Y?Y:Ae);u=Ae;P.css({top:Ae+"px"});var Af=Ae/Y;w.css({top:((Ad-m)*Af)+"px"});O.trigger("scroll");if(C.showArrows){Q[Ae==0?"addClass":"removeClass"]("disabled");J[Ae==Y?"addClass":"removeClass"]("disabled")}};var h=function(p){y(a(p,"Y")-D.top-V)};var q=Math.max(Math.min(i*(Ad-C.arrowSize*2),C.dragMaxHeight),C.dragMinHeight);P.css({height:q+"px"}).bind("mousedown",E);var k;var R;var I;var t=function(){if(R>8||R%4==0){y((u-((u-I)/2)))}R++};var Aa=function(){clearInterval(k);A("html").unbind("mouseup",Aa).unbind("mousemove",e)};var e=function(p){I=a(p,"Y")-D.top-V};var U=function(p){v();e(p);R=0;A("html").bind("mouseup",Aa).bind("mousemove",e);k=setInterval(t,100);t()};z.bind("mousedown",U);H.bind("mousewheel",function(Ae,Ag){v();n();var Af=u;y(u-Ag*b);var p=Af!=u;return !p});var F;var W;function c(){var p=(F-u)/C.animateStep;if(p>1||p<-1){y(u+p)}else{y(F);n()}}var n=function(){if(W){clearInterval(W);delete F}};var x=function(Af,p){if(typeof Af=="string"){$e=A(Af,O);if(!$e.length){return}Af=$e.offset().top-O.offset().top}H.scrollTop(0);n();var Ae=-Af/(Ad-m)*Y;if(p||!C.animateTo){y(Ae)}else{F=Ae;W=setInterval(c,C.animateInterval)}};O[0].scrollTo=x;O[0].scrollBy=function(Ae){var p=-parseInt(w.css("top"))||0;x(p+Ae)};v();x(-Ac,true);A("*",this).bind("focus",function(Ah){var Ag=A(this);var Aj=0;while(Ag[0]!=O[0]){Aj+=Ag.position().top;Ag=Ag.offsetParent()}var p=-parseInt(w.css("top"))||0;var Ai=p+Ad;var Af=Aj>p&&Aj<Ai;if(!Af){var Ae=Aj-C.scrollbarMargin;if(Aj>p){Ae+=A(this).height()+15+C.scrollbarMargin-Ad}x(Ae)}});if(location.hash){x(location.hash)}A(document).bind("click",function(Ae){$target=A(Ae.target);if($target.is("a")){var p=$target.attr("href");if(p.substr(0,1)=="#"){x(p)}}});A.jScrollPane.active.push(O[0])}else{O.css({height:Ad+"px",width:d-this.originalSidePaddingTotal+"px",padding:this.originalPadding});O.parent().unbind("mousewheel")}})};A.fn.jScrollPane.defaults={scrollbarWidth:10,scrollbarMargin:5,wheelSpeed:18,showArrows:false,arrowSize:0,animateTo:false,dragMinHeight:1,dragMaxHeight:99999,animateInterval:100,animateStep:3,maintainPosition:true,scrollbarOnLeft:false,reinitialiseOnImageLoad:false};A(window).bind("unload",function(){var C=A.jScrollPane.active;for(var B=0;B<C.length;B++){C[B].scrollTo=C[B].scrollBy=null}})})(jQuery);
