/***********************************\
* Autor:  PAV                       *
* E-mail: proalex85@gmail.com       *
* Created: 2009.09.12 09:30 (UTC+3) *
* Updated: 2009.11.11 06:48 (UTC+3) *
\***********************************/

jQuery.fn.Banners = function() {
	var container = jQuery(this);
	container.find('p.loading').remove();
	var BannerCount = container.find('li.Banner').size();

	container.find('li.Banner').css('display', 'none');
	container.find('ul.BannersContainer').css({'display' : 'block', 'visibility' : 'visible'});

	container.find('li.Banner').each(function(x) {
		jQuery(this).addClass('banner'+(x+1));
	});

	setCookie = function (name, value, expiredays) {
		var exdate = new Date();
		exdate.setDate(exdate.getDate() + expiredays);
		document.cookie = name + '=' + escape(value) + ((expiredays==null) ? '' : ';expires=' + exdate.toGMTString()) + ' path=/; domain=' + window.location.hostname;
	};
	getCookie = function (name) {
		if (document.cookie.length > 0) {
			c_start = document.cookie.indexOf(name + '=');
			if (c_start != -1) {
				c_start = c_start + name.length + 1;
				c_end = document.cookie.indexOf(';', c_start);
				if (c_end == -1) c_end = document.cookie.length;
				return unescape(document.cookie.substring(c_start, c_end));
			}
		}
		return null;
	};

	if (BannerCount > 1) {
		var cnt = 0;
		var cid = 0;
		if (parseInt(getCookie('bn_cnt')) != 'NaN') cnt = parseInt(getCookie('bn_cnt'));
		if (parseInt(getCookie('bn_cid')) != 'NaN') cid = parseInt(getCookie('bn_cid'));

		if (cnt != BannerCount) {
			setCookie('bn_cnt', BannerCount, 365);
			setCookie('bn_cid', 0, 365);
			cid = 0;
		}

		jQuery('ul.BannersContainer li.Banner:eq('+cid+')').css({'display' : 'block'});

		cid++;
		if (cid>=BannerCount) cid = 0;

		setCookie('bn_cid', cid, 365);
	} else jQuery('ul.BannersContainer li.Banner:eq(0)').css({'display' : 'block'});

/*	if (BannerCount>1) {
		var idx = 0;
		var timeshow = 0;
		var timer = 0;
		var isPause = false;
		var inProgress = false;

        container.each(function(i) {
			jQuery(this).mouseover(function() {
				isPause = true;
			}).mouseout(function() {
                isPause = false;
			});
        });

		var bloader = jQuery('div.banner-loading');
		start = function() {
			if (inProgress || isPause) return;
			inProgress = true;
			if (timer>=timeshow) {
				bloader.css({'display' : 'none'});
				jQuery('ul.BannersContainer li.Banner:eq(' + idx + ')').css({'display' : 'block'});

				if (idx >= BannerCount) {
					idx = 0;
					jQuery('ul.BannersContainer li').not(jQuery('ul.BannersContainer li.Banner:eq(0)')).css({'display' : 'none'});
				}

				if (idx == 0) timeshow = 15;
				else if (idx == 1) timeshow = 10;
				else timeshow = 5;

				idx++;

				timer = 0;
			}
			inProgress = false;
			timer++;
			if (timer==(timeshow-2)) bloader.css({'display' : 'block'});
		};

		var timerid = setInterval('start()',1000);
		jQuery('ul.BannersContainer li.Banner:eq(0)').css({'display' : 'block'});
	} else jQuery('ul.BannersContainer li.Banner:eq(0)').css({'display' : 'block'});*/
};