$(function() {
	function myLightBox()
	{
		$('.thumbnails a').lightBox();
		$('a[rel^=lightbox]').lightBox();
	}
	
	function mySlideshow()
	{
		$("#news-bar a.slides").fancybox({
			'hideOnContentClick': false,
			'autoDimensions': true,
			'scrolling': 'no',
			'padding': 6,
			'overlayShow': true,
			'overlayColor': '#000',
			'overlayOpacity': '0.8',
			'showCloseButton' : false,
			'onComplete': function() {
				$('.slideshow').cycle({
					fx: 'fade',
					speed: 2500
				});
			}
		});
	}
	
	function checkCollection()
	{
	
		if ($('#collections').length) 
		{
			$('#collections li a').unbind('click').bind('click',function() {
			//$('#collections li a').click(function() {
				var myTitle = jQuery(this).attr('title');
				_gaq.push(['_trackPageview', '/collections/'+myTitle+'/']);
				var cHeight = $('#content').height();
						   $("#collections li a").removeClass('sel');
				   $(this).addClass('sel');
					Cufon.refresh();
					$.ajax({  
					   type: "POST",  
					   url: "/resources/ajax/collection_pieces.php",  
					   data: "cID="+$(this).attr('id').split('_')[1],  
					   success: function(msg){  
					     $('#col2').fadeOut("slow", function(){
					    	 $(this).html(msg).fadeIn("slow");
								var cHeight2 = $('#content').height();
								if (cHeight > cHeight2)
									{
									$('#content').height(cHeight);
									}
					    	 myLightBox();
					     });
					   }  
					 });  
	
			return false;
		});
		}

	}
	
	myLightBox();
	checkCollection();
	mySlideshow();
	
	$('.slides_wrapper').suSlider({ 
		mode: 'fade',
	 	speed: 500,
	 	select: 0,		
		pause: 500,
	 	auto:false,
	 	//height: 290,
		wrapper_class: 'slider_container'
	});
	
	function news()
	{
	if ($('.news').length) {
		$('#news-bar').data('num', 0);
	$(window).scroll(function(){
		//console.log( $('#news-bar').data('num') );
		//console.log( $(".postitem:last").attr("id").split('_')[1] );
	    if($(window).scrollTop() == $(document).height() - $(window).height()){
	    	if ($('#news-bar').data('num') == 0)
	    		{
	    		$('#news-bar').data('num', 1);
	    	$('div#loadmoreajaxloader').show();
			$.ajax({
				url: "/resources/ajax/news.php?lastid=" + $(".postitem:last").attr("id").split('_')[1],
				success: function(html){
					if(html){
						$("#postswrapper").append(html);
						$('div#loadmoreajaxloader').hide();
						myLightBox();
			    		$('#news-bar').data('num', 0);
			    		news();
					}else{
						$('div#loadmoreajaxloader').html('<p>No more posts to show.</p>');
			    		$('#news-bar').data('num', 0);
					}
				}
			});
	    }
	    }
	});

	}
	}
	news();
});
