jQuery.noConflict();
jQuery.fn.getNaturalSize = function(){
	var i = this.filter('img:first');
	if (i.length > 0)
	{
		var s = i.attr('style');
		try {
			i.css({
				width:'auto',
				height:'auto',
				display:'inline'
			});
			return {
				width:i.width(),
				height:i.height()
			};
		}
		finally {
			i.attr('style',s);
		}
	}
	//return null;
	return {width:0,height:0};
}

jQuery.preloadImages = function()
{
  for(var i = 0; i<arguments.length; i++)
  {
    jQuery("<img src='"+arguments[i]+"'>");
  }
}

jQuery(document).ready(function($){


	$.preloadImages(Lumisfera.globals.imagesdir+"/ng/end/photoreport/1m/1/large/1.jpg",
			Lumisfera.globals.imagesdir+"/ng/end/photoreport/1m/2/large/2.jpg",
			Lumisfera.globals.imagesdir+"/ng/end/photoreport/1m/3/large/3.jpg",
			Lumisfera.globals.imagesdir+"/ng/end/photoreport/1m/4/large/4.jpg",
			Lumisfera.globals.imagesdir+"/ng/end/photoreport/1m/5/large/5.jpg",
			Lumisfera.globals.imagesdir+"/ng/end/photoreport/1m/6/large/6.jpg");
    
	$(".photoVotes a").live("click", function(e) {
		
		var $locationPath = location.pathname;
		var $rating = $(this).text();
				
		if ($locationPath.indexOf('photoreport') > 0) {
			var $photoReportId = $(this).attr('rel');
			var $url = '/ajax/ng/report/rate';
			var $data = "photoReport=" + $photoReportId + "&rating=" +$rating;
		} else {
			var $photoId = $(this).attr('rel');
			var $url = '/ajax/ng/rate';
			var $data = "photo=" + $photoId + "&rating=" +$rating;			
		}
			
		
		$(this).css({'fontWeight': 'bolder', 'textDecoration': 'none'});
	
	    $.ajax({
	       type: "POST",
	       url: $url,
	       data: $data,
	       success: function(msg){
	         alert("Głos został zapisany");
	       }
	    });
	    
	    return false;
			
	});
	
	
	$(".photoFlags a").live("click", function(e) {
		
		var $locationPath = location.pathname;		
		var $flag = $(this).text();
				
		if ($locationPath.indexOf('photoreport') > 0) {
			var $photoReportId = $(this).attr('rel');
			var $url = '/ajax/ng/report/mark';
			var $data = "photoReport=" + $photoReportId + "&flag=" +$flag;
		} else {
			var $photoId = $(this).attr('rel');
			var $url = '/ajax/ng/mark';
			var $data = "photo=" + $photoId + "&flag=" +$flag;			
		}
		
		$(this).css({'fontWeight': 'bolder', 'textDecoration': 'none'});
		
        $.ajax({
           type: "POST",
           url: $url,
           data: $data,
           success: function(msg){
             alert("Ustawiono flagę");
           }
        });
        
        return false;
		
	});
	
	$("#ratingMenu select").change(function() {
		var $selectedRating = $(this).val();
		var $locationPath = location.pathname;
		
		if ($locationPath.indexOf('photoreport') > 0) {
			var $newLocationUrl = '?rating=' + $selectedRating;
		} else {
			var $selectedCategory = $('.panelLongPhotos .menu b').html();
			var $newLocationUrl = '?category=' + $selectedCategory + '&rating=' + $selectedRating;
		}
		
		location.href = $newLocationUrl;
	});
	
	$("#flagMenu select").change(function() {
		var $selectedFlag = $(this).val();
		var $locationPath = location.pathname;
		
		if ($locationPath.indexOf('photoreport') > 0) {
			var $newLocationUrl = '?flag=' + $selectedFlag;
		} else {
			var $selectedCategory = $('.panelLongPhotos .menu b').html();		
			var $newLocationUrl = '?category=' + $selectedCategory + '&flag=' + $selectedFlag;
		}
		
		location.href = $newLocationUrl;
	});
	
	$("#clearFilter").live("click", function() {
		location.href = location.pathname;		
	});
	
	window.onload = function() {
		
		var $locationPath = location.pathname;
		
		if ($locationPath.indexOf('photoreport') > 0) {
			
			var $allPhotosListBoxes = $('.photosListBox');
			
			$.each($allPhotosListBoxes, function($number, $value) {
				
				var $reportNumber = $number;
				
				var $photosList = $($value).find('.photo');
				
				$.each($photosList, function($numberPhoto, $list) {
					
					if($($list).find('img').getNaturalSize().height < 200 && $($list).find('img').height() != null) {

						//$($list).remove();
					}
				});
				
				var $newPhotosList = $($value).find('.photo');
				
				if($newPhotosList.length == 0) {
					
			//		$($value).parents('.reportBox').remove();
					
				} else {
				
					$.each($newPhotosList, function($numberPhoto, $list) {
						
						if(($numberPhoto+1) % 3 == 1) {
							
							$($list).addClass('clb');
						}
						
						$($list).find('img').attr('style', '');
					});  
					
			//		$($value).find(".photo").remove();
					$($value).append($newPhotosList);
					
				}
			
			});
			
			
		} else {
		/*
			var $allLists = $("#NGPhotosList .photo");
			
			$.each($allLists, function($number, $list) {
				
				if($($list).find('img').getNaturalSize().height < 200 && $($list).find('img').height() != null) {
					$($list).remove();
				} 
					
			});
				
			var $newLists = $("#NGPhotosList .photo");
			
			$.each($newLists, function($number, $list) {
				if(($number+1) % 3 == 1) {
					$($list).addClass('clb');
				}
				$($list).find('img').attr('style', '');
			});  
			
			$("#NGPhotosList .photo").remove();
			$("#NGPhotosList").append($newLists);
			*/
		}
		
        $('.slider a.previous').bind('click',function(e){
            $('.slider  li[class!="next"]:visible:last').addClass('hidden');
            $('.slider li[class!="next"]:last').insertAfter('.slider li.previous').removeClass('hidden');
            e.preventDefault();
        });
        $('.slider a.next').bind('click',function(e){
        	$('.slider li[class!="next"]:visible:last').next().removeClass('hidden');
        	$('.slider li[class!="previous"]:visible:first').addClass('hidden').insertBefore('.slider li.next');
            e.preventDefault();
        });
        
        $('.slider2 a.previous').bind('click',function(e){
            $('.slider2  li[class!="next"]:visible:last').addClass('hidden');
            $('.slider2 li[class!="next"]:last').insertAfter('.slider2 li.previous').removeClass('hidden');
            e.preventDefault();
        });
        $('.slider2 a.next').bind('click',function(e){
        	$('.slider2 li[class!="next"]:visible:last').next().removeClass('hidden');
        	$('.slider2 li[class!="previous"]:visible:first').addClass('hidden').insertBefore('.slider2 li.next');
            e.preventDefault();
        });
        
        $('#mainPhotoNavigation a.previous').bind('click',function(e){
            $('.slider3 li[class!="next"]:visible:last').addClass('hidden');
            $('.slider3 li[class!="next"]:last').insertAfter('.slider3 li.previous').removeClass('hidden');
            e.preventDefault();
        });
        $('#mainPhotoNavigation a.next').bind('click',function(e){
        	$('.slider3 li[class!="next"]:visible:last').next().removeClass('hidden');
        	$('.slider3 li[class!="previous"]:visible:first').addClass('hidden').insertBefore('.slider3 li.next');
            e.preventDefault();
        });

        $('.slider4 a.previous').bind('click',function(e){
            $('.slider4  li[class!="next"]:visible:last').addClass('hidden');
            $('.slider4 li[class!="next"]:last').insertAfter('.slider4 li.previous').removeClass('hidden');
            e.preventDefault();
        });
        $('.slider4 a.next').bind('click',function(e){
        	$('.slider4 li[class!="next"]:visible:last').next().removeClass('hidden');
        	$('.slider4 li[class!="previous"]:visible:first').addClass('hidden').insertBefore('.slider4 li.next');
            e.preventDefault();
        });
        
	};
		
});


