// Inicjalizacja frameworka
Enforce.Init({
	path : '/library/javascript/enforce/',
	libraries : [
		'Banners',
		'Gallery',
		'ViewState',
		'GoogleMap'
	]
});

$(document).ready(function()
{
	$('input[@type=radio]').css('border', 'none');
	$('input[@type=checkbox]').css('border', 'none');
});

var Tools = new Object();

// Kreski
Tools.VerticalLines = function()
{
	var maxheight = 0;
	var delta = $("#side-bottom").height();
	var elements = new Array($("#col-left"), $("#col-right"), $("#side-left"), $("#side-right"));
	
	$(elements).each(function()
	{
		if(this.length == 0) return;
		
		var height = this.get(0).offsetHeight; // offsetHeight jest znacznie szybsze niz jQuery.height()
		
		if(height > maxheight)
			maxheight = height;
	});
	
	$(elements).each(function()
	{
		if(this.length == 0) return;
		
		if(this.attr('id') == "side-left" || this.attr('id') == "side-right")
			var height = maxheight - parseInt(delta);
		else
			var height = maxheight;
		
		if(jQuery.browser.msie)
			this.height(height);
		else
			this.css('min-height', height);
	});
}
$(document).ready(Tools.VerticalLines);

// Przyciski Aqua
Tools.AquaButtons = function()
{
	$(".btn-orange").each(function()
	{
		$(this).removeClass("btn-orange");
		$(this).addClass("btn-orange-aqua");
		$(this).html("<span><span>" + $(this).html() + "</span></span>");
	});
	
	$("input[@type=submit]").each(function()
	{
		$(this).after("<a href=\"/\" class=\"btn-orange-aqua\"  onclick=\"return Enforce.Submit(this)\"><span><span>" + this.value + "</span></span></a>");
		$(this).remove();
	});
}
$(document).ready(Tools.AquaButtons);

// Galeria
$(document).ready(function()
{
	Enforce.Gallery.init();
});

// Sklep
var Shop = new Object();

Shop.Basket = function(e)
{
	var e = e || window.event;
	
	$("#task").val("basket");
	
	return Enforce.Submit(e);
}

Shop.Compare = function(e, id)
{
	var e = e || window.event;
	
	$("#task").val("compare," + id);
	
	return Enforce.Submit(e);
}

Shop.Remove = function(e, id)
{
	var e = e || window.event;
	
	$("#task").val("remove," + id);
	
	return Enforce.Submit(e);
}

Shop.Recount = function(e)
{
	var e = e || window.event;
	
	$("#task").val("recount");
	
	return Enforce.Submit(e);
}

Shop.ClientType = function(value)
{
	var form = $('#dane_kontaktowe');
	if(value == 'osoba')
	{
		form.find('input[@name=typ][@value=osoba]').attr('checked', 1);
		form.find('tr[@rel=osoba]').css('display', '');
		form.find('tr[@rel=firma]').css('display', 'none');
	}
	else
	{
		form.find('input[@name=typ][@value=firma]').attr('checked', 1);
		form.find('tr[@rel=osoba]').css('display', 'none');
		form.find('tr[@rel=firma]').css('display', '');
	}
}

Shop.CopyAddress = function(value)
{
	var form = $('#adres_dostawy');
	
	if(value)
		form.find('.required').css('display', 'none');
	else
		form.find('.required').css('display', '');
}

Shop.Checkout = function(e, value)
{
	var e = e || window.event;
	
	$("#task").val(value);
	
	return Enforce.Submit(e);
}

// Ocena
var Rating = function(sender)
{
	this.sender = sender;
	this.uservote = 0;
	
	this.sender.onmousemove = this.MouseMove;
	this.sender.onmouseout = this.MouseOut;
	this.rating = parseFloat(this.sender.getAttribute('rating'));
	
	this.setRating(this.rating);
}

Rating.prototype.MouseMove = function(e)
{
	var e = e || window.event;
	
	if(typeof(this.position) == 'undefined')
		this.position = Enforce.ElementPosition(this);
	
	var mouse = Enforce.MousePosition(e);
	var rating = 1 + parseInt((mouse.left - this.position.left) / 24);
	
	this.handler.setRating(rating);
}

Rating.prototype.MouseOut = function()
{
	this.handler.setRating(this.handler.rating);
}

Rating.prototype.setRating = function(rating)
{
	this.uservote = rating;
	
	rating = -120 + parseInt(rating * 24);
	
	this.sender.style.backgroundPosition = rating + 'px 0px';
}

Rating.prototype.Vote = function(link)
{
	link = link + '&vote=' + this.uservote;
	
	$.post(link, function(data)
	{
		var status = $('status', data).text();
		var message = $('message', data).text();
		var msg = $('#stars-msg');
		
		msg.html(message)
		   .slideDown('slow')
		   .animate({opacity: 1.0}, 3000)
		   .slideUp('slow');
	});
	
	this.sender.style.cursor = 'default';
	
	this.sender.onclick = function() {};
	this.sender.onmousemove = function() {};
	this.sender.onmouseout();
}

Rating.Init = function()
{
	$('img.rating').each(function()
	{
		this.handler = new Rating(this);
	});
}

Rating.Vote = function(sender, link)
{
	sender.handler.Vote(link);
}

$(document).ready(function()
{
	Rating.Init();
});

// Ankieta
var Ankieta = new Object();
Ankieta.linki = new Object();

Ankieta.Init = function(id, url)
{
	Ankieta.linki[id] = url;
	
	$.post(url, function(data)
	{
		$('#ankieta-' + id).html(data);
		Tools.AquaButtons();
		Tools.VerticalLines();
	});
}

Ankieta.Glosuj = function(id)
{
	var url = Ankieta.linki[id];
	var ans = $('#ankieta-' + id).find(':radio').filter(':checked').val();
	
	$.post(url, { answer: ans }, function(data)
	{
		$('#ankieta-' + id).html(data);
		Tools.AquaButtons();
	});
	
	return false;
}

// Zakladki
var Tabs = new Object();

Tabs.Init = function(id)
{
	var formularz = $('#'+id);
	var zakladki = formularz.find('.tab');
	var panele = formularz.find('.tab-panel');
	var loading = formularz.find('.loading');
	
	zakladki.each(function(i)
	{
		$(this).bind('click', function()
		{
			Tabs.Click(id, i);
		});
	});
	
	panele.appendTo(formularz);
	Tabs.Click(id, 0);
}

Tabs.Click = function(id, position)
{
	var formularz = $('#'+id);
	var zakladki = formularz.find('.tab');
	var panele = formularz.find('.tab-panel');
	
	for(i = 0; i < zakladki.length; i++)
	{
		var zakladka = zakladki.get(i);
		var panel = panele.get(i);
		
		if(position == i)
		{
			$(zakladka).attr('class', 'tab');
			$(panel).css('display', 'block');
		}
		else
		{
			$(zakladka).attr('class', 'tab off');
			$(panel).css('display', 'none');
		}
	}
}


// Bloczek wyszukiwania
var Search = new Object();

Search.Focus = function(sender)
{
	if($(sender).val() == $(sender).attr('rel'))
	{
		$(sender).val('');
	}
}

Search.Blur = function(sender)
{
	if($(sender).val() == '')
	{
		$(sender).val($(sender).attr('rel'));
	}
}