var $jnc = jQuery.noConflict();
/*--- IE6 hover ---*/
function ieHover(h_list){
	if ($jnc.browser.msie && $jnc.browser.version < 7){
		$jnc(h_list).hover(function(){
			$jnc(this).addClass('hover');
		}, function(){
			$jnc(this).removeClass('hover');
		});
	}
}
/*---- clear inputs ---*/
function clearInputs(){
	$jnc('input:text, input:password, textarea').not('.must-select').each(function(){
		if(!this.val) this.val = this.value;
		this.onfocus = function(){
			if(this.value == this.val) this.value = '';
		}
		this.onblur = function(){
			if(this.value == '') this.value = this.val;
		}
	});
}
/*--- tooltip's function ---*/
function initTooltip(){
	var tooltip_l = $jnc('#tooltip-1');
	var tooltip_r = $jnc('#tooltip-2');
	var tooltip_l_c = tooltip_l.find('div.tooltip');
	var tooltip_r_c = tooltip_r.find('div.tooltip');
	
	tooltip_l.hide();
	tooltip_r.hide();
	var _t1, _t2;
	$jnc('div.games-list div.image').each(function(){
		var _f = true;
		if($jnc(this).parents('div.games-list:eq(0)').outerWidth()/2 <= $jnc(this).parent().position().left + $jnc(this).parent().outerWidth()/2) _f = false;
		var _text = $jnc(this).find('.tooltip-content');
		if(_text.length){
			$jnc(this).mouseenter(function(){
				if(_f){
					if(_t1) clearTimeout(_t1);
					tooltip_r_c.html(_text.html());
					tooltip_r.css({top: $jnc(this).offset().top, left: $jnc(this).offset().left}).show();
				}
				else{
					if(_t2) clearTimeout(_t2);
					tooltip_l_c.html(_text.html());
					tooltip_l.css({top: $jnc(this).offset().top, left: $jnc(this).offset().left}).show();
				}
			}).mouseleave(function(){
				tooltip_l.hide();
				tooltip_r.hide();
				/*
				if(_f){
					if(_t1) clearTimeout(_t1);
					_t1 = setTimeout(function(){
						tooltip_r.hide();
					}, 100);
				}
				else{
					if(_t2) clearTimeout(_t2);
					_t2 = setTimeout(function(){
						tooltip_l.hide();
					}, 100);
				}
				*/
			});
		}
	});
	/*
	tooltip_l.mouseenter(function(){
		if(_t2) clearTimeout(_t2);
	}).mouseleave(function(){
		if(_t2) clearTimeout(_t2);
		_t2 = setTimeout(function(){
			tooltip_l.hide();
		}, 100);
	});
	tooltip_r.mouseenter(function(){
		if(_t1) clearTimeout(_t1);
	}).mouseleave(function(){
		if(_t1) clearTimeout(_t1);
		_t1 = setTimeout(function(){
			tooltip_r.hide();
		}, 100);
	});
	*/
}
/*--- gallery function ---*/
function initGall(){
	$jnc('div.carousel').each(function(){
		var list_h = $jnc(this).find('ul:eq(0)');
		var btn_prev = $jnc(this).find('a.btn-back');
		var btn_next = $jnc(this).find('a.btn-next');
		var _w = list_h.children().outerWidth()*list_h.children().length;
		var _step = list_h.parent().width();
		var _timer = false;
		var _clone = list_h.children().clone();
		list_h.append(_clone);
		var _duration = _w * 10;
		var _over = false;
		
		btn_prev.mouseenter(function(){
			var _curMargin = parseInt(list_h.css('left'));
			var k = -_curMargin/_w;
			if (!k) k = 1;
		
			clearInterval(_timer);
			list_h.animate({left: 0}, {easing:'linear',queue:false, duration:_duration*k, complete:function(){
				list_h.css({left:-_w});
				btn_prev.trigger('mouseenter');
			}});
			return false;
		}).mouseout(function(){
			list_h.stop();
			_over = true;
			_timer = setInterval(function(){autoScroll();}, 8000);
		});
		btn_next.mouseenter(function(){
			var _curMargin = parseInt(list_h.css('left'));
			var k = (_w + _curMargin)/_w;
			if (!k) k = 1;
			
			clearInterval(_timer);
			list_h.animate({left: -_w}, {easing:'linear',queue:false, duration:_duration*k, complete:function(){
				list_h.css({left:0});
				btn_next.trigger('mouseenter');
			}});
			return false;
		}).mouseout(function(){
			list_h.stop();
			_over = true;
			_timer = setInterval(function(){autoScroll();}, 8000);
		});
		
		var _m = 0;
		_timer = setInterval(function(){autoScroll();}, 8000);
		function autoScroll(){
			var _curMargin = parseInt(list_h.css('left'));
			var _dif = _curMargin%_step;
			if (_dif && _over) {
				_dif = _curMargin*(-1);
				_over = false;
				do {
					_dif -= _step;
				} while (_dif > 0)
				_dif = _curMargin+_dif;
				list_h.animate({left:_dif}, {queue:false, duration:1200});
			} else {
				if(_m == _w) {
					list_h.css({left: 0});
					_m = _step;
				} else _m = _m + _step;
				list_h.animate({left: -_m}, {queue:false, duration:1500});
			}
		}
	});
	$jnc('.must-select').click(function(e){
		$jnc(this).select();
	});
}
function initPage(){
	ieHover('#navigation > li, div.bottom-box, div.games-list li, .between-box .image, .main-block .item, .item .image, .item-box .image , div.collapce, div.expand');
	clearInputs();
	initTooltip();
	initGall();
	$jnc('div.game-holder.slide-block').each(function(){
		var btn_open = $jnc(this).find('div.expand a');
		var btn_close = $jnc(this).find('div.collapce a');
		var _box = $jnc(this).find('div.slide-el');
		var box_w = _box.width();
		var box_h = _box.height();
		var _w = _box.children().outerWidth();
		var _h = _box.children().outerHeight();
		btn_open.click(function(){
			_box.stop().animate({width: _w, height: _h}, {
				duration:300,
				step: function(_r, _s){
					if(_s.prop == 'width') _box.css('left', -(_r-box_w));
				},
				complete: function(){
					
				}
			});
			return false;
		});
		btn_close.click(function(){
			_box.stop().animate({width: box_w, height: box_h}, {
				duration:300,
				step: function(_r, _s){
					if(_s.prop == 'width') _box.css('left', -(_r-box_w));
				},
				complete: function(){
					
				}
			});
			return false;
		});
	});
}
if (window.addEventListener) window.addEventListener("load", initPage, false);
else if (window.attachEvent && !window.opera) window.attachEvent("onload", initPage);
