$(document).ready(function(){
	if($(document.body).children('#video-popup').length == 1){
		initPopup();
	}
	else{
		if(typeof(initSifr) == 'function') initSifr();
		initTabs();
		fadeGall();
		slideGall2();
		initSlideBox();
		initTooltip();
		initMenu();
		videoPopup();
	}
});

/*--- function initMenu() ---*/
function initMenu(){
	$('#nav > li a').css('opacity', 0);
	$('#nav > li.active a').css('opacity', 1);
	$('#nav > li a').hover(function(){
		if(!$(this).parent().hasClass('active')) $(this).animate({opacity: 1}, { queue:false, duration: 400});
	}, function(){
		if(!$(this).parent().hasClass('active')) $(this).animate({opacity: 0}, { queue:false, duration: 400});
	});
}
/*--- function initTooltip ---*/
function initTooltip(){
	var _tooltip = $('<div id="tooltip"><div class="hold"></div><div class="arrow"></div></div>');
	var _text = _tooltip.find('.hold');
	$('.tooltip-info').each(function(_i, _el){
		$(this).parent().mouseover(function(e){
			$(document.body).append(_tooltip);
			_text.html($(_el).html());
			_tooltip.css({
				top: e.pageY,
				left: e.pageX,
				marginTop: - _tooltip.outerHeight() - 4,
				marginLeft: 4
			}).fadeIn(100);
		}).mousemove(function(e){
			_tooltip.css({
				top: e.pageY,
				left: e.pageX
			});
		}).mouseout(function(){
			_tooltip.fadeOut(100, function(){
				_tooltip.remove();
			});
		});
	});
}
/*--- function initAccordion ---*/
function initSlideBox(_cont){
	var _list = $('#services').children();
	_list.each(function(_i){
		var el_h = $(this);
		var _btn = el_h.children('.opener');
		var _box = el_h.children('.slide');
		if(el_h.hasClass('active')) _box.height('auto');
		else _box.height(0);
		_btn.click(function(){
			if(el_h.hasClass('active')){
				el_h.removeClass('active');
				_box.stop().animate({height: 0}, 500);
			}
			else{
				el_h.addClass('active');
				_box.stop().animate({height: _box.children().outerHeight()}, 500, function(){
					$(this).height('auto');
				});
			}
			return false;
		});
	});
}
/*--- function slide gallery ---*/
function slideGall2(){
	var _cont = $('#videos');
	var _a = 0;
	var list_hold = _cont.find('.videos-holder > ul');
	var _list = list_hold.children().each(function(_i){
		if($(this).hasClass('active')) _a = _i;
	});
	var _l = _list.length - 1;
	var _w = _list.eq(0).width();
	var _info = _cont.find('.pager .num');
	if(_cont.find('.pager ul').length == 0) _cont.find('.pager .prev').after('<ul></ul>');
	var _btn = _cont.find('.pager ul').empty();
	for(var i = 1; i <= _list.length; i++) _btn.append('<li><a href="#">'+ i +'</a></li>');
	_btn = _btn.find('a');
	_btn.each(function(_i){
		if(_i == _a) $(this).parent().addClass('active');
		$(this).click(function(){
			changeEl(_i);
			return false;
		});
	});
	_info.text('Page ' + (_a+1) + ' of ' + _list.length);
	_cont.find('.pager').css({
		opacity: 0,
		visibility: 'hidden'
	});
	_list.not(':first').children().children().children('a').css('visibility', 'visible');
	var t_el = _list.eq(0).children().children().children('a').css({
		opacity: 0,
		visibility: 'visible'
	}).addClass('must-show');
	showEl();
	function showEl(){
		t_el = t_el.filter('.must-show');
		if(t_el.length > 0){
			var _r = Math.floor(Math.random()*t_el.length);
			t_el.eq(_r).fadeTo(400, 1, function(){
				$(this).removeClass('must-show');
				setTimeout(function(){
					showEl(t_el);
				}, 200);
			});
		}
		else{
			_cont.find('.pager').css('visibility', 'visible').fadeTo(200, 1);
		}
	}
	_cont.find('.pager .prev a').click(function(){
		if(_a > 0) changeEl(_a - 1);
		return false;
	});
	_cont.find('.pager .next a').click(function(){
		if(_a < _list.length - 1) changeEl(_a + 1);
		return false;
	});
	function changeEl(_ind){
		if(_a != _ind){
			_info.text('Page ' + (_ind+1) + ' of ' + _list.length);
			list_hold.animate({marginLeft: -_w*_ind}, { queue:false, duration: 1000});
			_btn.eq(_a).parent().removeClass('active');
			_btn.eq(_ind).parent().addClass('active');
			_a = _ind;
		}
	}
}
/*--- function fade gallery ---*/
function fadeGall(){
	var _duration = 5000;
	$('.fade-gall').each(function(){
		var _hold = $(this);
		var _timer;
		var _a = 0;
		var _el = _hold.children().each(function(_i){
			if($(this).hasClass('active')) _a = _i;
		});
		 var _ind = _a;
		_el.css({
			position: 'absolute',
			zIndex: 1,
			opacity: 0
		}).eq(_a).css({
			position: 'relative',
			zIndex: 2,
			opacity: 1
		});
		
		function changeEl(){
			if (_timer) clearTimeout(_timer);
			if(_a < _el.length - 1) _ind++;
			else _ind = 0;
			_hold.stop().animate({height: _el.eq(_ind).outerHeight()}, 250);
			_el.eq(_a).css({zIndex: 1, position: 'absolute'}).stop().animate({opacity: 0}, 500).removeClass('active');
			_el.eq(_ind).css({zIndex: 2, position: 'relative'}).stop().animate({opacity: 1}, 500, function(){
				_hold.height('auto');
			}).addClass('active');
			_a = _ind;
			_timer = setTimeout(function(){ changeEl();}, _duration);
		}
		_timer = setTimeout(function(){ changeEl();}, _duration);
	});
}
/*--- tabs function ---*/
function initTabs(){
	$('#tabs-area').each(function(){
		var tab_btn = $(this).find('.tabset a').css('opacity', 0);
		var _a = tab_btn.index(tab_btn.filter('.active').eq(0));
		tab_btn.hover(function(){
			if(tab_btn.index(this) != _a) $(this).animate({opacity: 1}, { queue:false, duration: 400});
		}, function(){
			if(tab_btn.index(this) != _a) $(this).animate({opacity: 0}, { queue:false, duration: 400});
		}).click(function(){
			tab_btn.eq(_a).animate({opacity: 0}, { queue:false, duration: 400});
			$(this).animate({opacity: 1}, { queue:false, duration: 400});
			_a = tab_btn.index(this);
		});
	});
	
	
	$('.tabset').each(function(_ind, _el) {
		var btn_h = $(_el);
		var _btn = $(_el).find('a');
		var _a = 0;
		_btn.each(function(_ind, _el) {
			var _box = $('#'+_el.href.substr(_el.href.indexOf("#") + 1));
			if($(_el).hasClass('active')) {
				_box.show();
				_a = _ind;
			}
			else {
				_box.hide();
			}
			_el.onclick = function() {
				if(!$(this).hasClass('active')){
					$('#'+ _btn.get(_a).href.substr(_btn.get(_a).href.indexOf("#") + 1)).hide();
					_btn.eq(_a).removeClass('active');
					$('#'+ this.href.substr(this.href.indexOf("#") + 1)).show();
					$(this).addClass('active');
					_a = _ind;
					if(typeof(initSifr) == 'function') initSifr();
				}
				return false;
			}
		});
	});
}
/*--- slide gallery function ---*/
function slideGall1(){
	var _duration = 20000;
	$('#container .gallery').each(function(){
		var _hold = $(this).children('.hold').eq(0);
		var _list = _hold.children().children('ul');
		var _el = _list.children();
		var _flag = false;
		var _w = _list.width();
		var _d = 0;
		_duration = _duration/1000;
		if(_list.width() - _hold.width() > 0){
			_flag = true;
			_list.append(_el.clone(true));
		}
		/*--- show images ---*/
		_list.children().children('a').css({
			opacity: 0,
			visibility: 'visible'
		});
		var t_el = [];
		_list.children().each(function(){
			if($(this).position().left < _hold.width()) t_el.push(this);
			else $(this).children('a').css('opacity', '1');
		});
		t_el = $(t_el).children('a').addClass('must-show');
		showEl();
		function showEl(){
			t_el = t_el.filter('.must-show');
			if(t_el.length > 0){
				var _r = Math.floor(Math.random()*t_el.length);
				t_el.eq(_r).fadeTo(400, 1, function(){
					$(this).removeClass('must-show');
					setTimeout(function(){
						showEl(t_el);
					}, 200);
				});
			}
			else if(_flag){
				moveList();
			}
		}
		/*--- move function's ---*/
		function moveList(){
			_list.prepend(_el.clone(true)).css('left', -_w);
			_w = _w*2;
			var hold_p = 0;
			var _x = 0;
			var w_part = (_hold.width()/2)/10;
			
			function moveListL(){
				_d = (_w + parseInt(_list.css('left'))) * _duration;
				if(_x) _d = _d/_x;
				_list.animate({left: -_w}, _d, "linear", function() {
					_list.css('left', -_w/2);
					moveListL();
				});
			}
			function moveListR(){
				_d = -parseInt(_list.css('left')) * _duration;
				if(_x) _d = _d/_x;
				_list.animate({left: 0}, _d, "linear", function() {
					_list.css('left', -_w);
					moveListR();
				});
			}
			_hold.bind("mouseenter",function(e){
				hold_p = $(this).offset().left + $(this).width()/2;
			}).bind("mouseleave",function(){
				_x = 0;
				_list.stop();
				moveListL();
			}).mousemove(function(e){
				var _k = Math.floor((hold_p - e.pageX)/w_part);
				if(_k == 0) _list.stop();
				if(Math.abs(_k) != _x){
					_x = Math.abs(_k);
					_list.stop();
					if(_k > 0) moveListR();
					else if(_k < 0) moveListL();
				}
			});
			moveListL();
		}
	});
}
/*--- initFooter ---*/
function initFooterPre(){
	$('#page').height($('#page').height());
}
function initFooterAfter(){
	$('#page').animate({height: $('#content-popup_block').outerHeight()}, 300, function(){
		$(this).height('auto');
	});
}
/*--- video popup ---*/
function initPopup(){
	if(window._url){
		flowplayer('video', 'http://katabatic.tv/blog/wp-content/themes/katabatic/KatabaticDigital_js/swf/flowplayer.commercial-3.0.7.swf', {
			key: '$8068bec45585ab11498',
			clip: {
				url: window._url,
				autoPlay: true,
				scaling: 'fit'
			},
			plugins: {
				controls: null
			},
			play: {
				url: 'http://katabatic.tv/blog/wp-content/themes/katabatic/KatabaticDigital_js/images/btn-play.png', 
				width: 95, 
				height: 95 
			}
		}).setVolume('100').controls('controls').onStart(function(){
			this.setVolume(100);
		});;
	}
	var _popup = $('#video-popup');
	if(_popup.hasClass('hd-video')){
		$('#video').height(document.body.offsetHeight - 100);
		$(window).resize(function(){
			$('#video').height(document.body.offsetHeight - 100);
		});
	}
	_popup.find('.btn-hd').click(function(){
		_P2 = window.open($(this).attr('rel'), 'hd_popup_window', 'width='+screen.width+', height='+screen.height+', fullscreen=yes, top=0, left=0');
		_P2._url = this.href;
		window.close();
		/*--- HD CODE ---*/
		
		return false;
	});
	_popup.find('.credits .btn-close').click(function(){
		_popup.find('.credits').fadeOut(300, function(){
			_popup.find('#controls, .title-holder').fadeIn(300);
		});
		return false;
	});
	_popup.find('.cre-link').click(function(){
		_popup.find('#controls, .title-holder').fadeOut(300, function(){
			_popup.find('.credits').fadeIn(300);
		});
		return false;
	});
}
var _P1, _P2;
function videoPopup(){
	var text_title = $('#videos > .title-holder .title');
	var text_title_t = text_title.html();
	var text_time = $('#videos > .title-holder .time');
	var text_time_t = text_time.html();
	$('#videos .videos-holder li > a').hover(function(){
		text_title.html($(this).parent().find('.hidden-info .title-holder .title').html());
		text_time.html($(this).parent().find('.hidden-info .title-holder .time').html());
	}, function(){
		text_title.html(text_title_t);
		text_time.html(text_time_t);
	});
	$('a.btn-video').click(function(){
		if(_P1) _P1.close();
		_P1 = window.open($(this).attr('rel'), 'popup_window', 'top=200, left=200, height=640, width=860, resizable=no, status=no, toolbar=no, menubar=no, location=no');
		return false;
	});
}


function initPage(){
	if(typeof($) == 'function') slideGall1();
}
if (window.addEventListener)
	window.addEventListener("load", initPage, false);
else if (window.attachEvent)
	window.attachEvent("onload", initPage);
