$(function(){
	initClearForm();
	initIEhover();
	initDrop();
	customSlideGallery();
	
	/* overlay height */
	$('.overlay').css("height", $(document).height());
});

$(window).load(function(){
	initFadeGallery();
});

function vaMiddleCount(el){
	$(el).css('margin-top',($(el).parent().parent().height() - $(el).height())/2);
	$(el).css('padding-top', 0);
}

/* hover for IE6 */
function ieHover(h_list, h_class){
	if(jQuery.browser.msie && jQuery.browser.version < 7){
		if(!h_class) var h_class = 'hover';
		jQuery(h_list).mouseenter(function(){
			jQuery(this).addClass(h_class);
		}).mouseleave(function(){
			jQuery(this).removeClass(h_class);
		});
	};
};
// initIEhover
function initIEhover(){
	ieHover(".search-bl .drop li, .search-bl .drop li, .news-bl li, .compare-section .values td", "hover");
	ieHover("#nav .has-drop", "has-drop-hover");
	ieHover(".search-bl", "search-bl-hover");
	ieHover(".select-type-odds .drop-holder", "drop-holder-hover");
	ieHover(".sport-drop .matchs .row", "row-hover");
	ieHover(".results-table .tr", "li-hover");
	ieHover("#wrapper .sport-drop .col li", "tr-hover");
	ieHover(".best-odds .row-i", "row-i-hover");
	ieHover(".best-odds strong", "hover");
	ieHover(".team-results .row", "row-hover");
	ieHover(".team-results .row-title", "row-title-hover");
	ieHover(".sites-table .row", "row-hover");
	ieHover(".sites-table .row-title", "row-title-hover");
	ieHover(".match-info .compare-section .values tr", "tr-hover");
	ieHover(".match-form label", "label-hover");
	ieHover(".add-info", "add-info-hover");
	ieHover(".block-club", "block-club-hover");
	ieHover(".bonus-bl .box", "box-hover");
	ieHover(".block-club", "block-club-hover");
	ieHover(".match-info .cotes-block tr", "tr-hover");
}

/* initClearForm */
function initClearForm()
{
	clearFormFields({
		clearInputs: true,
		clearTextareas: true,
		passwordFieldText: true,
		addClassFocus: "focus",
		filterClass: "default"
	});
}

/* clearFormFields */
function clearFormFields(o)
{
	if (o.clearInputs == null) o.clearInputs = true;
	if (o.clearTextareas == null) o.clearTextareas = true;
	if (o.passwordFieldText == null) o.passwordFieldText = false;
	if (o.addClassFocus == null) o.addClassFocus = false;
	if (!o.filterClass) o.filterClass = "default";
	if(o.clearInputs) {
		var inputs = document.getElementsByTagName("input");
		for (var i = 0; i < inputs.length; i++ ) {
			if((inputs[i].type == "text" || inputs[i].type == "password") && inputs[i].className.indexOf(o.filterClass)) {
				inputs[i].valueHtml = inputs[i].value;
				inputs[i].onfocus = function ()	{
					if(this.valueHtml == this.value) this.value = "";
					if(this.fake) {
						inputsSwap(this, this.previousSibling);
						this.previousSibling.focus();
					}
					if(o.addClassFocus && !this.fake) {
						this.className += " " + o.addClassFocus;
						this.parentNode.className += " parent-" + o.addClassFocus;
					}
				}
				inputs[i].onblur = function () {
					if(this.value == "") {
						this.value = this.valueHtml;
						if(o.passwordFieldText && this.type == "password") inputsSwap(this, this.nextSibling);
					}
					if(o.addClassFocus) {
						this.className = this.className.replace(o.addClassFocus, "");
						this.parentNode.className = this.parentNode.className.replace("parent-"+o.addClassFocus, "");
					}
				}
				if(o.passwordFieldText && inputs[i].type == "password") {
					var fakeInput = document.createElement("input");
					fakeInput.type = "text";
					fakeInput.value = inputs[i].value;
					fakeInput.className = inputs[i].className;
					fakeInput.fake = true;
					inputs[i].parentNode.insertBefore(fakeInput, inputs[i].nextSibling);
					inputsSwap(inputs[i], null);
				}
			}
		}
	}
	if(o.clearTextareas) {
		var textareas = document.getElementsByTagName("textarea");
		for(var i=0; i<textareas.length; i++) {
			if(textareas[i].className.indexOf(o.filterClass)) {
				textareas[i].valueHtml = textareas[i].value;
				textareas[i].onfocus = function() {
					if(this.value == this.valueHtml) this.value = "";
					if(o.addClassFocus) {
						this.className += " " + o.addClassFocus;
						this.parentNode.className += " parent-" + o.addClassFocus;
					}
				}
				textareas[i].onblur = function() {
					if(this.value == "") this.value = this.valueHtml;
					if(o.addClassFocus) {
						this.className = this.className.replace(o.addClassFocus, "");
						this.parentNode.className = this.parentNode.className.replace("parent-"+o.addClassFocus, "");
					}
				}
			}
		}
	}
	function inputsSwap(el, el2) {
		if(el) el.style.display = "none";
		if(el2) el2.style.display = "inline";
	}
}
// initDrop
function initDrop(){
	var _holder = $('.search-bl');
	var _opener = _holder.find('>a');
	var _drop = _holder.find('>div');
	_opener.click(function(){
		if(_holder.hasClass('active')){
			_holder.removeClass('active');
			_drop.hide();
		}
		else{
			_holder.addClass('active');
			_drop.show();
		}
		return false;
	});
	$('body').click(function(e){
		var _f = false;
		$(e.target).parents().each(function(){
			if($(this).hasClass('search-bl')) _f =true;
		});
		if(!_f){
			$('.search-bl').removeClass('active').find('>div').hide();
		}
	});
}


// initFadeGallery
function initFadeGallery(){
	$('.match-box').fadeGallery({
		slideElements:'.gal-item',
		activeClass:'active-gal-item',
		pagerLinks:'div.thumbs li',
		pagerGener: false,
		autoRotation: true,
		switchTime: 4000,
		noCirclePlus1: true
	});
	$('.gallery1').fadeGallery({
		slideElements:'ul.slides > li',
		pagerHold: 'ul.pager',
		pagerLinks:'ul.pager li',
		autoRotation: true,
		switchTime: 2500,
		noCirclePlus1: true
	});
}

// slideshow plugin
jQuery.fn.fadeGallery = function(_options){
	var _options = jQuery.extend({
		slideElements:'ul.fade-gal > li',
		pagerGener: true,
		pagerHold: 'div.switcher',
		pagerLinks:'div.switcher 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:true,
		autoRotation:false,
		autoHeight:false,
		onChange:false,
		switchTime:3000,
		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 _noCyclePlus1 = _options.noCirclePlus1;
		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(_paging);
		}
		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() {
			if(_noCyclePlus1 && typeof(_this._noCyclePlus1_init) != 'undefined' && _this._noCyclePlus1_init) {
				return;
			}
			_prevIndex = _currentIndex;
			if(_currentIndex > 0) _currentIndex--;
			else {
				if (_noCyclePlus1 && typeof(_this._noCyclePlus1_init) == 'undefined') {
					_this._noCyclePlus1_init = true;
					_currentIndex = _slideCount-1;
				} else if(_noCycle) {
					return;
				} else {
					_currentIndex = _slideCount-1;
				}
			}
			switchSlide();
		}
		function nextSlide() {
			_prevIndex = _currentIndex;
			if(_noCyclePlus1 && typeof(_this._noCyclePlus1_init) != 'undefined' && _this._noCyclePlus1_init) {
				return;
			}
			if(_currentIndex < _slideCount-1) _currentIndex++;
			else {
				if (_noCyclePlus1 && typeof(_this._noCyclePlus1_init) == 'undefined') {
					_this._noCyclePlus1_init = true;
					_currentIndex = 0;
				} 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();
	});
}


function customSlideGallery(){
	var _wrapper = $('.gallery-block');
	var speed = 500;
	$('.gallery-hold').height($('.gallery-block .scroll-holder').height() - 28);
	$('.cotes-block .btn-scroll').height($('.gallery-block .scroll-holder').height());
	$('.cotes-block .btn-scroll span').css('margin-top', ($('.cotes-block .btn-scroll').height() - 251)/2 );
	_wrapper.each(function(){
		var _this = $(this);
		var _holder = _this.find('.gallery-hold');
		var _table = _holder.find('table');
		var _blockW = _holder.width();
		var _scrollHolder = _this.find('.scroll-holder');
		var _blockH = _table.outerHeight();
		var _btn = _this.find('.btn-scroll');
		var _leftOffset = _table.find('.t1').outerWidth();
		var _offset = _leftOffset;
		var _curStep = 0;
		_scrollHolder.css({
			width:_leftOffset
		});
		var _block = $('<div class="table-wrap"><div class="table-hold"></div></div>');
		_holder.append(_block);
		var _hold = _this.find('.table-wrap');
		_hold.css({
			width:_blockW - _leftOffset + 3,
			height:_blockH,
			left:_leftOffset -3
		});
		var _stepCount = Math.ceil(_table.outerWidth()/(_blockW - _leftOffset));
		var _stepWidth = _hold.outerWidth();
		var _sumWidth = _table.outerWidth();
		
		var _slider = _this.find('.table-hold');
		_slider.append(_table.clone()).css({
			marginLeft: -_leftOffset + 3
		});
		
		// recalc 
		function recalc(){
			_stepWidth = _hold.outerWidth();
			if(_curStep != 0) _offset = -_stepWidth*_curStep + 11;
			else _offset = -_leftOffset + 3; 
			if(_offset < _stepWidth-_sumWidth) _offset = _stepWidth-_sumWidth+1;
		}
		
		// btnClick
		_btn.click(function(){
			nextSlide();
			return false;
		});
		
		
		// nextSlide
		
		function nextSlide() {
			recalc();
			if(_curStep < _stepCount-1) _curStep++;
			else _curStep = 0;
			switchSlide();
		}
		
		// switchSlide
		function switchSlide() {
			recalc();
			_slider.animate({marginLeft:_offset},{duration:speed,queue:false,easing:'swing'});
		}
	});
}

function initTabNav(params) {
	$(params.mainSelector).each(function() {
										 
		frameParent = $(this).find('.frame');
		frameParentWidth = frameParent.outerWidth(true);
		frame = frameParent.find('ul');
		frameOffset = frame.offset();
										 
		frameWidth = 0;
		frameDays = frame.find('li.day');
		frameDays.each(function() {
			frameWidth += $(this).outerWidth(true);
		});
		frame.width(frameWidth);
		
		frameElWidth = $(this).find('li.day:first').outerWidth(true);
		frameMaxOffset = -(frameDays.length * frameElWidth) + frameParentWidth;
		frameTotalSteps = Math.ceil((frameWidth-frameParentWidth)/(5*frameElWidth));
		frameLeft = 0;
		
		frameLeftZeroInit = true;
		frameLeftMaxInit = true;
		
		$(this).find(params.buttonPrev).click(function(){
			frameLeft += 5 * frameElWidth;
			if (frameLeft > 0) {
				if (frameLeftZeroInit) {
					frameLeft = frameMaxOffset;
					frameLeftZeroInit = false;
				} else {
					frameLeftZeroInit = true;
					frameLeft = 0;
				}
				frameLeftMaxInit = true;
			} else {
				frameLeftZeroInit = false;
			}
			frameAnimate();
		});
		
		$(this).find(params.buttonNext).click(function(){
			frameLeft -= 5 * frameElWidth;
			if (frameLeft < frameMaxOffset) {
				if (frameLeftMaxInit) {
					frameLeft = 0;
					frameLeftMaxInit = false;
				} else {
					frameLeftMaxInit = true;
					frameLeft = frameMaxOffset;
				}
				frameLeftZeroInit = true;
			} else {
				frameLeftMaxInit = false;
			}
			frameAnimate();
		});
		
		frameAnimate = function() {
			frame.animate({left: frameLeft+'px'}, 500);
		};
		frameClick = function(el, check_pos, check_callback) {
			frame.find('.active').removeClass('active');
			el.addClass('active');
			if (check_pos) {
				var link_id = el.index();
				var link_step = Math.ceil(link_id * frameTotalSteps / frameDays.length);
				frameLeft = -5 * link_step * frameElWidth;
				frameAnimate();
			}
			if (check_callback) {
				if (typeof(switchTabCallback) != 'undefined') {
					switchTabCallback.call(this, el);
				}
			}
		};
		
		$(this).find('li.day').click(function() {
			frameClick($(this), false, true);
		});
		$(this).find('li.active').each(function() {
			frameClick($(this), true, false);
		});
	});
}
