jQuery.easing['jswing'] = jQuery.easing['swing'];

jQuery.extend( jQuery.easing,
{
	def: 'easeOutQuad',
	swing: function (x, t, b, c, d) {
		return jQuery.easing[jQuery.easing.def](x, t, b, c, d);
	},
	easeInQuad: function (x, t, b, c, d) {
		return c*(t/=d)*t + b;
	},
	easeOutQuad: function (x, t, b, c, d) {
		return -c *(t/=d)*(t-2) + b;
	},
	easeOutBack: function (x, t, b, c, d, s) {
		if (s == undefined) s = 1.70158;
		return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;
	},
	easeInOutBack: function (x, t, b, c, d, s) {
		if (s == undefined) s = 1.70158;
		if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b;
		return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b;
	},
	easeInOutCubic: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t*t + b;
		return c/2*((t-=2)*t*t + 2) + b;
	}
});



if (window.jQuery) $(document).ready(function() {
	$('div.carousel').each(block_carousel);
	if ($('.promo-tabs').length) main_page();
	if ($('.faq').length) faq();
	if ($('.select_house').length) select_house();
	if ($('.netmap').length) $('.netmap').netmap();
	if ($('.super-menu .fast-links').length) fast_links();
	if ($('.fast-login').length) fast_login();
});


function fast_login()
{
	$('a#fast_login_link').bind('click', function(){
		$('.fast-login .form').slideToggle();
		return false;
	});
}

function main_page()
{
	$('.promo-tabs .tabs li a').bind('click', function() {
		if ($(this).hasClass('active')) return false;
		$('.promo-tabs .tabs li a').removeClass('active');
		$(this).addClass('active');
		$('#tab-container div.tab').hide();
		$('#tab-container div.'+this.rel).show();
		return false;
	});

	$('.tarify .select-who a').bind('click', function() {
		if ($(this).hasClass('active')) return false;
		$('.tarify .select-who a').removeClass('active');
		$(this).addClass('active');
		$('.show-tarif li').hide();
		$('.show-tarif li.'+this.rel).show();
		$('#send_order').attr('href', ((this.rel == 'fizik') ? '/order' : '/order/legal'));
		$('.tarify a.detail').attr('href', ((this.rel == 'fizik') ? '/tariffs' : '/tariffs/category/2'));
		return false;
	});

	$('#send_order').bind('click', function() {
		var house = $('#order_house').val();
		window.location = $(this).attr('href') + ((!house || house == '0') ? '' : '/house:'+house);
		return false;
	});
}


function faq()
{
	$('.faq ol li a.js').bind('click', function() {
		$(this).toggleClass('active');
		$('#answer_'+this.rel).slideToggle();
		return false;
	});
}


function select_house()
{
	$('#order_house_street').bind('change', function() {
		var sid, i, l, input;
		sid = this.value;
		l = houses.length;
		input = $('#order_house');
		input.empty();
		$('<option value="0">-</option>').appendTo(input);
		for (i=0; i<l; i++)
			if (houses[i][2] == sid) $('<option value="'+houses[i][0]+'">'+houses[i][1]+'</option>').appendTo(input);
	});
	$('#order_house_street option:first').attr('disabled', 'disabled');
}


function block_carousel()
{
	var carousel, carousel_ul, timer, n, n_max, w, m, z;

	carousel = $(this);
	carousel_ul = $('.mid ul', this);
	timer = undefined;

	w = $('li', carousel_ul)[0].clientWidth;
	if (!w) return;

	n = 1;
	m = Math.floor($('div.mid', this)[0].clientWidth / w);
	n_max = $('li', carousel_ul).size() - m;
	if (n_max < 0) n_max = 0;
	z = Math.round(n_max / m) + 1;

	$('a.prev', this).bind('click', function() {
		if (timer) { window.clearInterval(timer); timer = undefined; }
		if (n <= m)
		{
			carousel_ul.animate({left:"-="+(w*n_max)+"px"}, 1300, "easeInOutBack");
			n = n_max + 1;
		} else {
			carousel_ul.animate({left:"+="+(w*m)+"px"}, "slow", "easeInOutCubic");
			n -= m;
		}
		$('span.count', carousel).html(Math.round((n+m-1)/m)+' θη '+z);
		return false;
	});

	$('a.next', this).bind('click', function() {
		if (timer) { window.clearInterval(timer); timer = undefined; }
		if (n > n_max)
		{
			carousel_ul.animate({left:"0px"}, 1300, "easeInOutBack");
			n = 1;
		} else {
			carousel_ul.animate({left:"-="+(w*m)+"px"}, "slow", "easeInOutCubic");
			n += m;
		}
		$('span.count', carousel).html(Math.round((n+m-1)/m)+' θη '+z);
		return false;
	});

	timer = window.setInterval(function() {
		if (n > n_max)
		{
			carousel_ul.animate({left:"0px"}, 1300, "easeInOutBack");
			n = 1;
		} else {
			carousel_ul.animate({left:"-="+(w*m)+"px"}, "slow", "easeInOutCubic");
			n += m;
		}
		$('span.count', carousel).html(Math.round((n+m-1)/m)+' θη '+z);
		return false;
	}, 5000);

	return;
}

function fast_links()
{
		$('.super-menu .fast-links a.f').bind('click', function(){
			$('#fast_resurs').slideToggle();
		});
}

