$(document).ready(function(){
	initHover();
	initGallery();
	initSlider();
});
function initHover(){
	
	$('#header ul.menu li').hover(
		function(){$(this).addClass('hover');},
		function(){$(this).removeClass('hover');}	
	);
}

function initFader(){
    var documentHeight = $(document).height();
    var documentWidth = $(window).width();
    var windowWidth = $('#page').width();
    if (windowWidth > documentWidth)  documentWidth = windowWidth;
    $('#fader').css({
        height : documentHeight+"px",
        width : documentWidth+"px"
    });
    $('body').css({
        width : documentWidth+"px"
    });
}
$(window).scroll(function(){
	initTopPopup();
	initFader()
});
$(window).resize(function(){
	initTopPopup();
	initFader()
});
function initTopPopup(){
	var windowH = $(window).height();
	var windowTop =$(window).scrollTop()
	var top_offset =parseInt(windowH/2)+windowTop;
	if ($('#popup').get(0)) {
		var imgH = parseInt($('#popup img').height() / 2);
		if (imgH < 1) imgH = 340;
		top_offset = top_offset -imgH; 
		$('#popup').css('top',top_offset);
	}
}
function initGallery(){
	if ($('#main ul.gallery').get(0)) {
		$('#main ul.gallery a').lightBox({
			imageLoading: null
		});
	}
	if ($('#main #sidebar div.image').get(0)) {
		$('#main #sidebar div.image a').click(function(){
			var src=$(this).attr('href');
			$('#popup').html('<img src="'+src+'" alt="" />');
			$('#fader').show();
               initFader();initTopPopup();
			$('#popup').show();
			initClose();
			return false;
		});
	}
	if ($('#content ul.games div.image').get(0)) {
		$('#content ul.games div.image a').click(function(){
			var src=$(this).attr('href');
			$('#popup').html('<img src="'+src+'" alt="" />');
			$('#fader').show();
			initFader();initTopPopup();
			$('#popup').show();
			initClose();
			return false;
		});
	}
}
function initClose(){
	$('#fader').click(function(){
		$('#fader').hide();
		$('#popup').hide();
	});
	$('#popup img').click(function(){
		$('#fader').hide();
		$('#popup').hide();
	});
}
function initSlider(){
	if ($('#main #slider').get(0)) {
		$('#main #slider .bottom-boxes').jCarouselLite({
			btnNext: "#next",
		     btnPrev: "#prev",
		     visible: 1,
		     speed: 500,
			btnGo:[".boxes-pager .1", ".boxes-pager .2",
				    ".boxes-pager .3", ".boxes-pager .4"],
			afterEnd: function(a) {
		        	var index=$('.bottom-boxes ul li').index(a);
		        	if (index==5) index=1;
		        	if (index==0) index=4;
		        	var cur=index-1;
		        	$('ul.boxes-pager li').removeClass('active');
		        	$('ul.boxes-pager li:eq('+cur+')').addClass('active');
		    }
	
		});
		
	}	
	if ($('#ad-slider').get(0)) { 
		$('#ad-slider').cycle({ 
		    fx:      'fade', 
		    speed:    1000,
		    pause: 1,
		    next: '#next-ad',
		    prev:'#prev-ad',
		    timeout:  5000,
		    pager:  '#ad-pager .pager-links' 
		});
	}
$('#ad-pager .pager-links a').click(function(){
		$('#ad-slider').cycle('pause');
})
		
}
