var ajaxTabsOn = false;
var interval = 0;

jQuery(function(){
	clearInputs();
	initLast();
	initOpenClose();
	initGallery();
	if(ajaxTabsOn) {
		initAjaxTabs();
	} else {
		initMinHeight();
	}
});

jQuery(function(){
	if(Shadowbox) Shadowbox.init();
});

jQuery(function(){
	$("#twitterFeed").getTwitter({
		userName: "adaptv",
		numTweets: 3,
		loaderText: "Loading tweets...",
		slideIn: true,
		slideDuration: 750,
		showHeading: true,
		headingText: "Latest Tweets",
		showProfileLink: true,
		showTimestamp: true
	});
})

// clear inputs
function clearInputs(){
	jQuery('input:text, input:password, textarea').each(function(){
		var _el = jQuery(this);
		var _val = _el.val();
		_el.bind('focus', function(){
			if(this.value == _val) this.value = '';
		}).bind('blur', function(){
			if(this.value == '') this.value = _val;
		});
	});
};

/////


function optout() {
	var img = new Image();
	img.src = "http://optout.adap.tv/optout";
	setTimeout(__reload,500);
}

// init Gallery
function initGallery(){
	jQuery('div.gallery').fadeGallery({
		switchTime:4000, //ms
		pauseOnHover:true,
		autoRotation:true
	});
};
// slideshow plugin
jQuery.fn.fadeGallery = function(_options){
	var _options = jQuery.extend({
		slideElements:'ul.fade-gallery > li',
		pagerGener: true,
		pagerHold: 'div.switcher-holder',
		pagerLinks:'div.switcher-holder li',
		btnNext:'a.link-next',
		btnPrev:'a.link-prev',
		btnPlayPause:'a.play-pause',
		btnPlay:'a.play',
		btnPause:'a.pause',
		pausedClass:'paused',
		disabledClass: 'disabled',
		playClass:'playing',
		activeClass:'active',
		currentNum:false,
		allNum:false,
		startSlide:null,
		noCircle:false,
		caption:false,
		pauseOnHover:false,
		autoRotation:false,
		autoHeight:false,
		onChange:false,
		switchTime:2000,
		duration:650,
		event:'click'
	},_options);

	return this.each(function(){
		// gallery options
		var _this = jQuery(this);
		var _slides = jQuery(_options.slideElements, _this);
		var _btnPrev = jQuery(_options.btnPrev, _this);
		var _btnNext = jQuery(_options.btnNext, _this);
		var _btnPlayPause = jQuery(_options.btnPlayPause, _this);
		var _btnPause = jQuery(_options.btnPause, _this);
		var _btnPlay = jQuery(_options.btnPlay, _this);
		var _pauseOnHover = _options.pauseOnHover;
		var _autoRotation = _options.autoRotation;
		var _activeClass = _options.activeClass;
		var _disabledClass = _options.disabledClass;
		var _pausedClass = _options.pausedClass;
		var _playClass = _options.playClass;
		var _autoHeight = _options.autoHeight;
		var _duration = _options.duration;
		var _switchTime = _options.switchTime;
		var _controlEvent = _options.event;
		var _currentNum = (_options.currentNum ? jQuery(_options.currentNum, _this) : false);
		var _allNum = (_options.allNum ? jQuery(_options.allNum, _this) : false);
		var _startSlide = _options.startSlide;
		var _noCycle = _options.noCircle;
		var _onChange = _options.onChange;
		var _pagerGener = _options.pagerGener;
		var _pagerHold = jQuery(_options.pagerHold,_this);
		var _caption = jQuery(_options.caption,_this);
		var _captions = jQuery('>a',_caption);
		var _paging = '';
		if(_pagerGener){
			for(var i=0; i< _slides.length; i++){
				_paging += '<li><a href="#">'+(i+1)+'</a></li>';
			}
			_pagerHold.html('<ul>'+_paging+'</ul>');
		}
		var _pagerLinks = jQuery(_options.pagerLinks, _this);
		// gallery init
		var _hover = false;
		var _prevIndex = 0;
		var _currentIndex = 0;
		var _slideCount = _slides.length;
		var _timer;
		if(_slideCount < 2) return;

		_prevIndex = _slides.index(_slides.filter('.'+_activeClass));
		if(_prevIndex < 0) _prevIndex = _currentIndex = 0;
		else _currentIndex = _prevIndex;
		if(_startSlide != null) {
			if(_startSlide == 'random') _prevIndex = _currentIndex = Math.floor(Math.random()*_slideCount);
			else _prevIndex = _currentIndex = parseInt(_startSlide);
		}
		_slides.hide().eq(_currentIndex).show();
		_captions.hide().eq(_currentIndex).show();
		if(_autoRotation) _this.removeClass(_pausedClass).addClass(_playClass);
		else _this.removeClass(_playClass).addClass(_pausedClass);

		// gallery control
		if(_btnPrev.length) {
			_btnPrev.bind(_controlEvent,function(){
				prevSlide();
				return false;
			});
		}
		if(_btnNext.length) {
			_btnNext.bind(_controlEvent,function(){
				nextSlide();
				return false;
			});
		}
		if(_pagerLinks.length) {
			_pagerLinks.each(function(_ind){
				jQuery(this).bind(_controlEvent,function(){
					if(_currentIndex != _ind) {
						_prevIndex = _currentIndex;
						_currentIndex = _ind;
						switchSlide();
					}
					return false;
				});
			});
		}

		// play pause section
		if(_btnPlayPause.length) {
			_btnPlayPause.bind(_controlEvent,function(){
				if(_this.hasClass(_pausedClass)) {
					_this.removeClass(_pausedClass).addClass(_playClass);
					_autoRotation = true;
					autoSlide();
				} else {
					_autoRotation = false;
					if(_timer) clearTimeout(_timer);
					_this.removeClass(_playClass).addClass(_pausedClass);
				}
				return false;
			});
		}
		if(_btnPlay.length) {
			_btnPlay.bind(_controlEvent,function(){
				_this.removeClass(_pausedClass).addClass(_playClass);
				_autoRotation = true;
				autoSlide();
				return false;
			});
		}
		if(_btnPause.length) {
			_btnPause.bind(_controlEvent,function(){
				_autoRotation = false;
				if(_timer) clearTimeout(_timer);
				_this.removeClass(_playClass).addClass(_pausedClass);
				return false;
			});
		}

		// gallery animation
		function prevSlide() {
			_prevIndex = _currentIndex;
			if(_currentIndex > 0) _currentIndex--;
			else {
				if(_noCycle) return;
				else _currentIndex = _slideCount-1;
			}
			switchSlide();
		}
		function nextSlide() {
			_prevIndex = _currentIndex;
			if(_currentIndex < _slideCount-1) _currentIndex++;
			else {
				if(_noCycle) return;
				else _currentIndex = 0;
			}
			switchSlide();
		}
		function refreshStatus() {
			if(_pagerLinks.length) _pagerLinks.removeClass(_activeClass).eq(_currentIndex).addClass(_activeClass);
			if(_currentNum) _currentNum.text(_currentIndex+1);
			if(_allNum) _allNum.text(_slideCount);
			_slides.eq(_prevIndex).removeClass(_activeClass);
			_slides.eq(_currentIndex).addClass(_activeClass);
			if(_noCycle) {
				if(_btnPrev.length) {
					if(_currentIndex == 0) _btnPrev.addClass(_disabledClass);
					else _btnPrev.removeClass(_disabledClass);
				}
				if(_btnNext.length) {
					if(_currentIndex == _slideCount-1) _btnNext.addClass(_disabledClass);
					else _btnNext.removeClass(_disabledClass);
				}
			}
			if(typeof _onChange === 'function') {
				_onChange(_this, _currentIndex);
			}
		}
		function switchSlide() {
			_slides.eq(_prevIndex).fadeOut(_duration);
			_slides.eq(_currentIndex).fadeIn(_duration);
			_captions.eq(_prevIndex).hide();
			_captions.eq(_currentIndex).show();
			if(_autoHeight) _slides.eq(_currentIndex).parent().animate({height:_slides.eq(_currentIndex).outerHeight(true)},{duration:_duration,queue:false});
			refreshStatus();
			autoSlide();
		}

		// autoslide function
		function autoSlide() {
			if(!_autoRotation || _hover) return;
			if(_timer) clearTimeout(_timer);
			_timer = setTimeout(nextSlide,_switchTime+_duration);
		}
		if(_pauseOnHover) {
			_this.hover(function(){
				_hover = true;
				if(_timer) clearTimeout(_timer);
			},function(){
				_hover = false;
				autoSlide();
			});
		}
		refreshStatus();
		autoSlide();
	});
};
// init Ajax Tabs
function initAjaxTabs(){
	jQuery('div.ajax-tabs').ajaxTabs({
		afterLoad:function(tab){
			tab.show();
			initMinHeight();
			tab.hide();
		}
	});
	jQuery('div.ajax-tabs2').ajaxTabs({
		tabsHolder:'div.tab-holder',
		tabset:'div.control-tabs > ul',
		afterLoad:function(tab){
			initOpenClose();
		}
	});
};
jQuery.fn.ajaxTabs = function(options){
	var options = jQuery.extend({
		tabset:'ul.tabset',
		tabsHolder:'div.ajax-content',
		afterFirstLoad:null,
		fadeSpeed:false,
		afterLoad:null
	},options);

	return this.each(function(){
		var holder = jQuery(this);
		var tabset = jQuery(options.tabset, holder);
		var tabsHolder = jQuery(options.tabsHolder,holder);
		var links = jQuery('a',tabset);
		var hrefArray = [];
		var tabArray = [];
		var firstLoad = [];
		var active = 0;
		var ajaxInAction = false;
		links.each(function(i){
			hrefArray[i] = jQuery(this).attr('href');
			firstLoad[i] = true;
			if (jQuery(this).parent().hasClass('active')) active = i;
		});

		links.each(function(i){
			jQuery(this).click(function(){
				if (i != active && !ajaxInAction) loadTab(i);
				return false;
			});
		});
		
		loadTab(active);
		
		function loadTab(ind) {
			if (typeof tabArray[ind] != 'undefined') {
				showTab(ind);
			} else {
				ajaxInAction = true;
				jQuery.ajax({
					url: hrefArray[ind],
					error:function(){
						alert('ajax error');
					},
					success:function(msg){
						ajaxInAction = false;
						tabArray[ind] = jQuery(msg).appendTo(tabsHolder).hide();
						 if (typeof options.afterLoad == 'function') options.afterLoad(tabArray[ind]);
						showTab(ind);
					}
				});
				
			}
		}
		
		function showTab(ind){
			tabArray[active].css({display:'none'});
			links.eq(active).parent().removeClass('active');
			links.eq(ind).parent().addClass('active');
			if (options.fadeSpeed) {
				tabArray[ind].fadeIn(options.fadeSpeed,function(){
					if (typeof options.afterFirstLoad == 'function' && firstLoad[ind]) {
						options.afterFirstLoad(tabArray[ind]);
						firstLoad[ind] = false;
					}
				});
			} else {
				tabArray[ind].show();
				if (typeof options.afterFirstLoad == 'function' && firstLoad[ind]) {
					options.afterFirstLoad(tabArray[ind]);
					firstLoad[ind] = false;
				}
			}
			active = ind;
		}
	});
};
// init MinHeight
function initMinHeight() {
	var max = 0;
	var boxes = jQuery("div.heading-box");
	boxes.each(function(){
		var hold = jQuery(this);
		if(hold.height() > max) max = hold.height();
	});
	boxes.css({height: max});
};
// init OpenClose
function initOpenClose() {
	jQuery('li.slide-block').OpenClose({
		activeClass:'active',
		opener:'div.opener',
		slider:'div.slide',
		effect:'slide',
		animSpeed:500
	});
}
jQuery.fn.OpenClose = function(_options){
	// default options
	var _options = jQuery.extend({
		activeClass:'active',
		opener:'.opener',
		slider:'.slide',
		animSpeed: 400,
		animStart:false,
		animEnd:false,
		effect:'fade',
		event:'click'
	},_options);

	return this.each(function(){
		// options
		var _holder = jQuery(this);
		var _slideSpeed = _options.animSpeed;
		var _activeClass = _options.activeClass;
		var _opener = jQuery(_options.opener, _holder);
		var _slider = jQuery(_options.slider, _holder);
		var _animStart = _options.animStart;
		var _animEnd = _options.animEnd;
		var _effect = _options.effect;
		var _event = _options.event;
		if(_slider.length) {
			_opener.bind(_event,function(){
				if(!_slider.is(':animated')) {
					if(typeof _animStart === 'function') _animStart();
					if(_holder.hasClass(_activeClass)) {
						_slider[_effect=='fade' ? 'fadeOut' : 'slideUp'](_slideSpeed,function(){
							if(typeof _animEnd === 'function') _animEnd();
						});
						_holder.removeClass(_activeClass);
					} else {
						_holder.addClass(_activeClass);
						_slider[_effect=='fade' ? 'fadeIn' : 'slideDown'](_slideSpeed,function(){
							if(typeof _animEnd === 'function') _animEnd();
						});
					}
				}
				return false;
			});
			if(_holder.hasClass(_activeClass)) _slider.show();
			else _slider.hide();
		}
	});
}
function initLast() {jQuery(".partners-list li:last-child").addClass("last");};

