$(document).ready(function() {
	window.scrollTo(0, 0);
	$("#overlaybg").css({opacity:0.8});
	
	$('a[rel=wallpaperoverlay]').click(function() {
		if(document.getElementById('navigation')) {
			document.getElementById('navigation').disableNavigation();
		}
		$('.overlaycontent').css('width','750px');
		$('#overlay').css({ display: 'block', left: ($(window).width() - $('#overlay').width())/2, top: $(window).scrollTop() + 100 + 'px', position: 'absolute' });			
		$('#overlaybg').css({width: $(window).width(),height:$(document).height()});	
		$('#overlaybg').fadeIn('fast');	
		$('#overlay > div').hide();
		$('#wallpaper-overlay').show();
		wallpaperURL = $(this).attr('href');
		$('#wallpaper-media > img').remove();	
		$('<img src="' + wallpaperURL + '" width="318" height="238" />').appendTo('#wallpaper-media');
		return false;
	});
	
	$('a[rel=ssoverlay]').click(function() {
		if(document.getElementById('navigation')) {
			document.getElementById('navigation').disableNavigation();
		}
		$('.overlaycontent').css('width','750px');
		$('#overlay').css({ display: 'block', left: ($(window).width() - $('#overlay').width())/2, top: $(window).scrollTop() + 100 + 'px', position: 'absolute' });	
		$('#overlaybg').css({width: $(window).width(),height:$(document).height()});	
		$('#overlaybg').fadeIn('fast');	
		$('#overlay > div').hide();
		$('#screensaver-overlay').show();
		ssURL = $(this).attr('href');
		imgURL = $(this).attr('rev');	
		if($(this).attr('class') == 'pc') {
			$('#screensaver-overlay .mac').hide();
			$('#screensaver-overlay .windows').show();
		} else if ($(this).attr('class') == 'mac') {
			$('#screensaver-overlay .mac').show();
			$('#screensaver-overlay .windows').hide();
		}			
		$('#screensaver-media > a').remove();	
		$('<a href="' + ssURL + '"><img src="' + imgURL + '" width="320" height="240" /></a>').appendTo('#screensaver-media');		
		return false;
	});
	
	$('#buddyicons > li').click(function() {
		if(document.getElementById('navigation')) {
			document.getElementById('navigation').disableNavigation();
		}
		$('.overlaycontent').css('width','750px');
		$('#overlay').css({ display: 'block', left: ($(window).width() - $('#overlay').width())/2, top: $(window).scrollTop() + 100 + 'px', position: 'absolute' });		
		$('#overlaybg').css({width: $(window).width(),height:$(document).height()});	
		$('#overlaybg').fadeIn('fast');	
		$('#overlay > div').hide();
		$('#buddyicons-overlay').show();
		divid = $('ul',this).attr('class');
		$('#icons-wrapper > ul').hide();	
		$('#' + divid).show();
		return false;
	});
	
	$('#featured-buddyicons a').click(function() {
		if(document.getElementById('navigation')) {
			document.getElementById('navigation').disableNavigation();
		}
		$('.overlaycontent').css('width','750px');
		$('#overlay').css({ display: 'block', left: ($(window).width() - $('#overlay').width())/2, top: $(window).scrollTop() + 100 + 'px', position: 'absolute' });	
		$('#overlaybg').css({width: $(window).width(),height:$(document).height()});	
		$('#overlaybg').fadeIn('fast');	
		$('#overlay > div').hide();
		$('#buddyicons-overlay').show();
		divid = $(this).attr('class');
		$('#icons-wrapper > ul').hide();	
		$('#' + divid).show();
		return false;
	});
	
	$('.closelink, #closelink, #overlaybg').click(function() {
		overlayClose();
		return false;
	});	

	subnavigation('download-navigation');
	
	// Wallpaper Carousel
	$('#wallpapers').css('width', $('#wallpapers > li').length * 165 + 'px');								
	pagination('wallpapers', 'wallpapers-container', 'wallpapers-carousel-pages', 165, 5, 500, 0.5);				
	$('#wallpapers-container > .previous').click(function() {
		goPrev('wallpapers', 'wallpapers-container', 'wallpapers-carousel-pages', 165, 5, 500, 0.5);		
	}); 						
	$('#wallpapers-container > .next').click(function() {
		goNext('wallpapers', 'wallpapers-container', 'wallpapers-carousel-pages', 165, 5, 500, 0.5);
	});									
	$("#wallpapers-carousel-pages > .page").eq(0).addClass('current');			
	$('#wallpapers-carousel-pages').css( { width : 886 - (parseInt(($('#wallpapers-carousel-pages').width() - ($('#wallpapers-carousel-pages > li').length * 19 / 2))/2)), paddingLeft : parseInt(($('#wallpapers-carousel-pages').width() - ($('#wallpapers-carousel-pages > li').length * 19 / 2))/2) } );

	// Screensavers Carousel
	$('#screensavers').css('width', $('#screensavers > li').length * 165 + 'px');								
	pagination('screensavers', 'screensavers-container', 'screensavers-carousel-pages', 165, 5, 500, 0.5);				
	$('#screensavers-container > .previous').click(function() {
		goPrev('screensavers', 'screensavers-container', 'screensavers-carousel-pages', 165, 5, 500, 0.5);		
	}); 						
	$('#screensavers-container > .next').click(function() {
		goNext('screensavers', 'screensavers-container', 'screensavers-carousel-pages', 165, 5, 500, 0.5);
	});									
	$("#screensavers-carousel-pages > .page").eq(0).addClass('current');			
	$('#screensavers-carousel-pages').css( { width : 886 - (parseInt(($('#screensavers-carousel-pages').width() - ($('#screensavers-carousel-pages > li').length * 19 / 2))/2)), paddingLeft : parseInt(($('#screensavers-carousel-pages').width() - ($('#screensavers-carousel-pages > li').length * 19 / 2))/2) } );

	// Buddy Icons Carousel
	$('#buddyicons').css('width', $('#buddyicons > li').length * 366 + 'px');								
	pagination('buddyicons', 'buddyicons-container', 'buddy-icons-carousel-pages', 366, 2, 500, 0.5);				
	$('#buddyicons-container > .previous').click(function() {
		goPrev('buddyicons', 'buddyicons-container', 'buddy-icons-carousel-pages', 366, 2, 500, 0.5);		
	}); 						
	$('#buddyicons-container > .next').click(function() {
		goNext('buddyicons', 'buddyicons-container', 'buddy-icons-carousel-pages', 366, 2, 500, 0.5);
	});									
	$("#buddy-icons-carousel-pages > .page").eq(0).addClass('current');			
	$('#buddy-icons-carousel-pages').css( { width : 886 - (parseInt(($('#buddy-icons-carousel-pages').width() - ($('#buddy-icons-carousel-pages > li').length * 19 / 2))/2)), paddingLeft : parseInt(($('#buddy-icons-carousel-pages').width() - ($('#buddy-icons-carousel-pages > li').length * 19 / 2))/2) } );
	
	sectionLocation('wallpapers');
});