(function($){
	$.fn.slider = function() {
		return this.each(function(){
			var obj  = $(this);
			var prev = $('.prev', obj);
			var next = $('.next', obj);
			var num  = 0;
			var ul   = $('ul', obj);
			var h = 0;
			
			obj.bind('preInit', function() {
				ul.children('li').each(function(){
					var t = $(this);
					var w = 0;
					w = $('img', t).width();
					if(w > 0)
						t.width(w);
				});
			});
			
			$('img', ul).one('load', function(){
				if($("li", obj).size() < 3)
						next.addClass('disabled disabled-next');
					obj.trigger('preInit');
					init();
					obj.trigger('check');
			}).each(function(){
				if(this.complete || ($.browser.msie && $.browser.version == 6))
					$(this).trigger("load");
			});
			
			function init() {
				num = Math.floor($('.inner', obj).width() / $('li', obj).outerWidth());
				check();
			}
			
			function check() {
				var w = 0;
				var c = 0;
				var p = 20;
				var wa = 0;
				
				$("li:visible", obj).each(function(){
					if(w < $(".inner", obj).width() - (wa + p*2))
					{
						w += $(this).width();
						c++;
						wa = w / c;
					}
					else
						return;
				});
				
				$(".photo-container, .video-container").trigger('check');
				
				prev.removeClass('disabled disabled-prev');
				next.removeClass('disabled disabled-next');
				
				if($("li:hidden", obj).size() <= 0 || $('li:visible', obj).size() == 0)
					prev.addClass('disabled disabled-prev');
					
				if($('li:visible', obj).size() > 0 && $('li:visible', obj).size() - c <= 0)
					next.addClass('disabled disabled-next');
					
			}
			
			$(window).bind('resize', function(){
				check();
			});
			$(window).bind('load', function(){
				check();
			});
			
			obj.bind('checkSlider', function(){
				check();
			});
			
			next.click(function(){
				if(!$(this).hasClass('disabled')) {
					ul.animate(
						{
							left : -$('li', obj).outerWidth()
						},
						200,
						function() {
							$('li:eq(' + $("li:hidden", obj).size() + ')', obj).hide();
							ul.css('left', 0);
							init();
						}
					);
				}
				return false;
			});
			
			prev.click(function(){
				if(!$(this).hasClass('disabled'))
				{
					$('li:eq(' + ($("li:hidden", obj).size()-1) + ')', obj).show();
					ul.css('left', -$('li', obj).outerWidth())
					ul.animate(
						{
							left : 0
						},
						200,
						init
					);
				}
				return false;
			});
			
			
		});
	};
})(jQuery);

(function($){
	$.fn.photo = function() {
		return this.each(function(){
			$(this).bind('check', function() {
				var w = 0;
				var c = 0;
				var p = 10;
				var wa = 0;
				var obj = $(this);

				$("li", obj).stop();
				
				$("li:visible:lt(6)", obj).each(function(){
					if(w + $(this).width() + p * (c + 2) < $(".inner", obj).width())
					{
						w += $(this).width();
						c++;
					}
					else
						return;
				});
				if(c == 0){
					wa = 1;
				}else{
					wa = w / c;
				}
				
				$("li:visible:lt(" + (c+2) + ")", obj).animate(
					{
						paddingLeft : Math.ceil(($(".inner", obj).width() - (c * wa)) / (c + 1)) + 1
					},
					100
				);
				$(this).trigger('photoInit');
			});
			
			$(this).bind('photoInit', function(){
				var obj = $(this);
				var inner = $(".inner", obj);
				var h = inner.height();
				
				$('li', inner).each(function(){
					h = Math.max(h, $(this).height());
				});
				
				if(h > 0)
					inner.height(h);
			});

			$(this).trigger('check');
		});
	};
})(jQuery);

(function( $ ){
	$.fn.photoreport = function(options){
		return this.each(function(){
			var obj  = $(this);
			var inner= $('.inner', obj);
			var prev = $('.prev', obj);
			var next = $('.next', obj);
			var ul   = $('ul', obj);
			var img  = $('.photo-report-img');
			var prevImg = $('.photo-report-controls .prev');
			var nextImg = $('.photo-report-controls .next');
			var pos = $('li.active', ul).index();
			
			if(options == 'with'){
				obj.append('<span class="new_slider_left_opa"></span><span class="new_slider_right_opa"></span>');
			}			

			checkScroll();
			checkPosition();
			
			if( $.browser.msie &&  parseInt($.browser.version) < 7 )
				inner.wrap('<div class="cont" />');
			
			function checkScroll() {
				var sl  = inner.scrollLeft();
				var w   = inner.width();
				var sw  = inner.attr('scrollWidth');

				if (sl + w < sw)
					next.removeClass('disabled disabled-next');
				else
					next.addClass('disabled disabled-next');
					
				if (sl > 0)
					prev.removeClass('disabled disabled-prev');
				else
					prev.addClass('disabled disabled-prev');
			}
			
			function checkActive() {
				pos = $('li.active', ul).index();
				if(pos < 1)
					prevImg.addClass('disabled');
				else
					prevImg.removeClass('disabled');
					
				if(pos >= $('li', ul).size() - 1)
					nextImg.addClass('disabled');
				else
					nextImg.removeClass('disabled');
			}
			
			
			function checkPosition() {
				var activeWidth = 0;
				
				$('.active', ul).prevUntil().each(function(){
					activeWidth += $(this).width();
				});
				
				var shiftWidth = Math.abs(inner.scrollLeft() - activeWidth) - inner.width()/2;
				
				if(Math.abs(shiftWidth) > inner.width()/4) {
					inner.animate(
						{ scrollLeft : activeWidth - inner.width()/4 },
						200
					);
				}
				checkActive();
			}
			
			function showImg(o) {
				var i = $('li', ul).index(o);
				if(img.eq(i).size() > 0)
				{
					img.hide();
					img.eq(i).show();
					$('li', ul).removeClass('active');
					$(o).addClass('active');
					checkScroll();
					return false;
				}
				return true;
			}
			
			inner.scroll(function(){
				checkScroll();
			});
			
			$('a', ul).click(function(){
				var result = showImg($(this).parent());
				checkPosition();
				return result;
			});
			
			next.click(function(){
				inner.animate(
					{ scrollLeft : inner.scrollLeft() + 500 },
					200
				);
				checkScroll();
				return false;
			});
			
			prev.click(function(){
				inner.animate(
					{ scrollLeft : inner.scrollLeft() - 500 },
					200
				);
				checkScroll();
				return false;
			});
			
			nextImg.click(function(){
				showImg($('li', ul).eq(pos+1));
				checkPosition();
				return false;
			});
			
			prevImg.click(function(){
				showImg($('li', ul).eq(pos-1));
				checkPosition();
				return false;
			});
		});
	};
})( jQuery );
