/*--- Make Favorite / Homepage ---*/
var addHomePage='http://www.populargirlsgames.com';

function makehome()
{
       theobj = document.anchors[0];  
       theobj.style.behavior='url(#default#homepage)';
       theobj.setHomePage(addHomePage);
}
/*--- IE6 hover ---*/
function ieHover(h_list){
	if ($.browser.msie && $.browser.version < 7){
		$(h_list).hover(function(){
			$(this).addClass('hover');
		}, function(){
			$(this).removeClass('hover');
		});
	}
}
/*---- clear inputs ---*/
function clearInputs(){
	$('input:text').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 = $('#tooltip-1');
	var tooltip_r = $('#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;
	$('div.games-list div.image').each(function(){
		var _f = true;
		if($(this).parents('div.games-list:eq(0)').outerWidth()/2 <= $(this).parent().position().left + $(this).parent().outerWidth()/2) _f = false;
		var _text = $(this).find('.tooltip-content');
		if(_text.length){
			$(this).mouseenter(function(){
				if(_f){
					if(_t1) clearTimeout(_t1);
					tooltip_r_c.html(_text.html());
					tooltip_r.css({top: $(this).offset().top, left: $(this).offset().left}).show();
				}
				else{
					if(_t2) clearTimeout(_t2);
					tooltip_l_c.html(_text.html());
					tooltip_l.css({top: $(this).offset().top, left: $(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(){
	$('div.carousel').each(function(){
		var list_h = $(this).find('ul:eq(0)');
		var btn_prev = $(this).find('a.btn-back');
		var btn_next = $(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 * 8;
		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});
			}
		}
	});
}
/*--- init menu function ---*/
function initMenu(){
	var _list = $('#navigation > li');
	var _a = _list.index(_list.filter('.active'));
	_list.children('div').hide();
	if(_a != -1) _list.eq(_a).children('div').show();
	
	_list.mouseenter(function(){
		if($(this).children('div').length){
			if(_a != -1) _list.eq(_a).removeClass('active').children('div').hide();
			$(this).addClass('active').children('div').show();
			_a = _list.index(this);
		}
	});
	
}
function initPage(){
	ieHover('div.bottom-box, div.games-list li');
	clearInputs();
	initTooltip();
	initGall();
	initMenu();
}
if (window.addEventListener) window.addEventListener("load", initPage, false);
else if (window.attachEvent && !window.opera) window.attachEvent("onload", initPage);