/*
 * 	Easy Slider - jQuery plugin
 *	written by Alen Grakalic	
 *	http://cssglobe.com/post/3783/jquery-plugin-easy-image-or-content-slider
 *
 *	Copyright (c) 2009 Alen Grakalic (http://cssglobe.com)
 *	Dual licensed under the MIT (MIT-LICENSE.txt)
 *	and GPL (GPL-LICENSE.txt) licenses.
 *
 *	Built for jQuery library
 *	http://jquery.com
 *
 */
 
/*
 *	markup example for $("#images").easySlider();
 *	
 * 	<div id="images">
 *		<ul>
 *			<li><img src="images/01.jpg" alt="" /></li>
 *			<li><img src="images/02.jpg" alt="" /></li>
 *			<li><img src="images/03.jpg" alt="" /></li>
 *			<li><img src="images/04.jpg" alt="" /></li>
 *			<li><img src="images/05.jpg" alt="" /></li>
 *		</ul>
 *	</div>
 *
 */
(function($) {

	$.fn.easySlider = function(options){
	  
		// default configuration properties
		var defaults = {
			prevId: 		'prevBtn',
			prevText: 		'Previous',
			nextId: 		'nextBtn',
			nextText: 		'Next',
			orientation:	'', //  'vertical' is optional;
			speed: 			800
		}; 
		
		var options = $.extend(defaults, options);
		
		return this.each(function() {
			obj = $(this);
			var s = $("li", obj).length;
			var w = obj.width();
			var h = obj.height();
			var ts = s-1;
			var t = 0;
			var vertical = (options.orientation == 'vertical');
			//$("ul", obj).css('width',s*w);
			if(!vertical) $("li", obj).css('float','left');
			$(obj).after('<div id="prevBtn"><a href="javascript:void(0);"><img src="http://pix.ero.ru/images3/slide_prev.png"></a></div><div id="nextBtn"><a href="javascript:void(0);"><img src="http://pix.ero.ru/images3/slide_next.png"></a></div>');

			$("a","#"+options.prevId).hide();
			$("a","#"+options.nextId).hide();

			$("a","#"+options.nextId).click(function(){
				animate("next");
				if (t>=ts) $(this).fadeOut();
				$("a","#"+options.prevId).fadeIn();
			});
			
			$("a","#"+options.prevId).click(function(){
				animate("prev");
				if (t<=0) $(this).fadeOut();
				$("a","#"+options.nextId).fadeIn();
			});

			function animate(dir){
				if(dir == "next"){
					getAttr(t+1);
					t = (t>=ts) ? ts : t+1;
				} else {
					getAttr(t-1);
					t = (t<=0) ? 0 : t-1;
				};
				if(!vertical) {
					p = (t*w*-1);
					$("ul",obj).animate(
						{ marginLeft: p }, 
						options.speed
					);
				} else {
					p = (t*h*-1);
					$("ul",obj).animate(
						{ marginTop: p }, 
						options.speed
					);
				}
				$("#featured_text").text(featured_texts[t]);
				$("#featured_name").text(featured_girls[t]);
			};
			if(s>1) $("a","#"+options.nextId).fadeIn();

			/********* вывод кнопки *************/
			$('img', obj).attr('name', function(index){return index;});
			outBtn ($('img', obj).attr('id'), $('img', obj).attr('readonly'));
			outStars (t);
			
			function getAttr (t) {
				id			= $('img[name='+t+']', obj).attr('id');
				collection	= $('img[name='+t+']', obj).attr('readonly');
				outBtn(id, collection);
				outStars (t);
			}

			function outBtn(id, collection) {
				if (typeof(collection) != 'undefined' && collection == '1') {
					$('#btnBuyGerl').html('<img src="http://pix.ero.ru/images3/btn_in_coll.png" width="232" height="62" alt="Шоу этой девушки уже есть в вашей коллекции">');
				} else {
					$('#btnBuyGerl').html('<a href="http://install.ero.ru/inst/'+advId+'/'+id+'/'+advTrack+'/eg.exe"><img src="http://pix.ero.ru/images3/btn_buy_gerl.png" width="232" height="62" alt="Загрузить бесплатное шоу этой девушки"></a>');
				}
			}
			
			/************ конец *****************/

			function outStars (t) {
                $("#featured_rate").attr('title', featured_girls[t] + " / " + featured_names[t] + 
				                                  " имеет средний балл " + featured_rates[t] + " из 5");
				                                  
				intRate = parseInt(featured_rates[t]);
				rest = featured_rates[t] - intRate;
				halfRate = 0;
				if (rest >= 0.8) intRate++;
				if (rest > 0.3 && rest < 0.8) halfRate = intRate + 1;
				
				outImg = '';
				for (m = 1; m < 6; m++) {					
					if (m <= intRate) nameImg = "star_red.gif";
					if (m == halfRate) nameImg = "star_half.gif";
					if (m > intRate && m != halfRate) nameImg = "star_grey.gif";
					outImg += '<img src="http://pix.ero.ru/images3/'+nameImg+'" width="24" height="23">';
				}
				
				$('#featured_rate').html(outImg);
			}
			
		});
	};

})(jQuery);
