$(function() {
	Cufon.replace('.cufon-font', {textShadow: 'none', hover: true});

	$('.lb').lightBox();
	$("input:radio, select, input:file").uniform({fileBtnText: '', fileDefaultText: ''});

	/*
	 * Skynet Konfigurator
	 * Copyright 2011, Siewruk Kamil
	 * Email: ks@4k.pl
	 */

	var Skynet = {
		domLokalizacjaId: 2,
		currentLokalizacjaId: 0,

		init: function() {
			$('input[name="typ"]').change(function() {
				if(this.value == 'dom') {
					$('.konfigurator-lokalizacje').fadeOut();
					Skynet.currentLokalizacjaId = Skynet.domLokalizacjaId;

					Skynet.getOferta();
				}
				if(this.value == 'blok') {
					$('.konfigurator-oferty').html('<h2 class="cufon-font oferty-brak">Proszę wybrać swoją lokalizację</h2>');
					Cufon.replace('.cufon-font', {textShadow: 'none', hover: true});

					$.ajax({
						url: 'indywidualni/lokalizacje/',
						dataType: 'html',
						success: function(data) {
							$('.konfigurator-lokalizacje').html(data);

							$("input:radio, select, input:file").uniform({fileBtnText: '', fileDefaultText: ''});
							$('.konfigurator-lokalizacje').fadeIn();

							$('.konfigurator-lokalizacja').change(function() {
								if(this.value == 0) {
									$('.konfigurator-oferty').html('<h2 class="cufon-font oferty-brak">Proszę wybrać swoją lokalizację</h2>');
									Cufon.replace('.cufon-font', {textShadow: 'none', hover: true});
								} else {
									Skynet.currentLokalizacjaId = this.value;

									Skynet.getOferta();
								}
							});
						}
					});
				}
			});
		},

		getOferta: function() {
			$.ajax({
				url: 'indywidualni/oferta/' + Skynet.currentLokalizacjaId + '/',
				dataType: 'html',
				success: function(data) {
					$('.konfigurator-oferty').html(data);

					Cufon.replace('.cufon-font', {textShadow: 'none', hover: true});

					$.getJSON('indywidualni/oferta_json/' + Skynet.currentLokalizacjaId + '/', function(data) {



						$('.konfigurator-oferta').each(function(index) {

							$("input:radio, input:checkbox", $(this)).uniform({fileBtnText: '', fileDefaultText: ''});
							$("input:radio", $('.konfigurator-okres')).uniform({fileBtnText: '', fileDefaultText: ''});

							var oferta_box_obj = this;

							$('input:checkbox', $(this)).change(function() {
								temp = $(this).is(':checked');
								if(temp) {
									$('input[name="wartosci[' + (index+1) + ']"]').attr('on', 1);
									$('input[name="instalacje[' + (index+1) + ']"]').attr('on', 1);

									$('.oferta-inner', $(oferta_box_obj)).fadeIn();

									var suma = 0;

									$('.wartosci').each(function(index) {
										if(parseInt(this.value) !=  'NaN') {
											if($(this).attr('on') == 1) {
												suma += parseInt(this.value);
											}
										}
									});

									$('.suma').html(suma);

									var suma_instalacja = 0;

									$('.instalacje').each(function(index) {
										if(parseInt(this.value) !=  'NaN') {
											if($(this).attr('on') == 1) {
												suma_instalacja += parseInt(this.value);
											}
										}
									});

									$('.suma_instalacja').html(suma_instalacja);

								} else {
									$('input[name="wartosci[' + (index+1) + ']"]').attr('on', 0);
									$('input[name="instalacje[' + (index+1) + ']"]').attr('on', 0);

									$('.oferta-inner', $(oferta_box_obj)).fadeOut();

									var suma = 0;

									$('.wartosci').each(function(index) {
										if(parseInt(this.value) !=  'NaN') {
											if($(this).attr('on') == 1) {
												suma += parseInt(this.value);
											}
										}
									});

									$('.suma').html(suma);

									var suma_instalacja = 0;

									$('.instalacje').each(function(index) {
										if(parseInt(this.value) !=  'NaN') {
											if($(this).attr('on') == 1) {
												suma_instalacja += parseInt(this.value);
											}
										}
									});

									$('.suma_instalacja').html(suma_instalacja);
								}
							});

							$('input[name="konfigurator-czas"]').change(function() {
								$('input[name="konfigurator-czas-1"]').trigger('change', this.value);
								$('input[name="konfigurator-czas-2"]').trigger('change', this.value);
								$('input[name="konfigurator-czas-3"]').trigger('change', this.value);
								$('input[name="konfigurator-czas-4"]').trigger('change', this.value);

								$('input[name="konfigurator-czas-1"]').removeAttr('checked');
								$('input[name="konfigurator-czas-2"]').removeAttr('checked');
								$('input[name="konfigurator-czas-3"]').removeAttr('checked');
								$('input[name="konfigurator-czas-4"]').removeAttr('checked');
								
								$('.konfigurator-okres input[value="' + this.value + '"]').attr('checked', 'checked');
							});

							var qty = $('.konfigurator-oferta-item', $(this)).length;
							boxWidth = (757 / qty) - 1;

							$('.konfigurator-oferta-item', $(this)).css({width: boxWidth});
							$('.konfigurator-slider-inner', $(this)).css({width: ((boxWidth+1) * (qty))});

							var oferta_item = this;
							var pakiet_index = 0;

							$('.konfigurator-slider-inner', $(this)).slider({
								max: (qty-1),
								min: 0,
								animate: true,
								slide: function (ev, ui) {
									pakiet_index = ui.value;

									//console.log(data[(index)].oferta[ui.value]);

									var pakiet = data[(index)].oferta[ui.value];
									$('.pakiet-name', $(oferta_item)).html(pakiet.Pakiet.name);
									
									$('.short-1', $(oferta_item)).html(pakiet.Pakiet.short1);
									$('.short-2', $(oferta_item)).html(pakiet.Pakiet.short2);
									$('.short-3', $(oferta_item)).html(pakiet.Pakiet.short3);

									var okres = $('input[name="konfigurator-czas"]:checked').val();

									if(okres == 0) {
										$('.short-cena', $(oferta_item)).html(pakiet.cena12);
										$('.koszt-instalacji', $(oferta_item)).html(pakiet.instalacja_12);

									
										$('input[name="wartosci[' + (index+1) + ']"]').val((parseInt(pakiet.cena12)));
										$('input[name="instalacje[' + (index+1) + ']"]').val((parseInt(pakiet.instalacja_12)))
									}
									if(okres == 1) {
										$('.short-cena', $(oferta_item)).html(pakiet.cena24);
										$('.koszt-instalacji', $(oferta_item)).html(pakiet.instalacja_24);

										$('input[name="wartosci[' + (index+1) + ']"]').val((parseInt(pakiet.cena24)));
										$('input[name="instalacje[' + (index+1) + ']"]').val((parseInt(pakiet.instalacja_24)))
									}
									if(okres == 2) {
										$('.short-cena', $(oferta_item)).html(pakiet.cena_nie);
										$('.koszt-instalacji', $(oferta_item)).html(pakiet.instalacja_nie);

										$('input[name="wartosci[' + (index+1) + ']"]').val((parseInt(pakiet.cena_nie)));
										$('input[name="instalacje[' + (index+1) + ']"]').val((parseInt(pakiet.instalacja_nie)));
									}

									var suma = 0;

									$('.wartosci').each(function(index) {
										if(parseInt(this.value) !=  'NaN') {
											if($(this).attr('on') == 1) {
												suma += parseInt(this.value);
											}
										}
									});

									$('.suma').html(suma);

									var suma_instalacja = 0;

									$('.instalacje').each(function(index) {
										if(parseInt(this.value) !=  'NaN') {
											if($(this).attr('on') == 1) {
												suma_instalacja += parseInt(this.value);
											}
										}
									});

									$('.suma_instalacja').html(suma_instalacja);
								}
							});

							$('input[name="konfigurator-czas-' + (index+1) + '"]').change(function(event, param) {
								var pakiet = data[(index)].oferta[pakiet_index];
								var okres = param;

								if(okres == 0) {
									$('.short-cena', $(oferta_item)).html(pakiet.cena12);
									$('.koszt-instalacji', $(oferta_item)).html(pakiet.instalacja_12);
									$('input[name="wartosci[' + (index+1) + ']"]').val((parseInt(pakiet.cena12)));
									$('input[name="instalacje[' + (index+1) + ']"]').val((parseInt(pakiet.instalacja_12)));
								}
								if(okres == 1) {
									$('.short-cena', $(oferta_item)).html(pakiet.cena24);
									$('.koszt-instalacji', $(oferta_item)).html(pakiet.instalacja_24);
									$('input[name="wartosci[' + (index+1) + ']"]').val((parseInt(pakiet.cena24)));
									$('input[name="instalacje[' + (index+1) + ']"]').val((parseInt(pakiet.instalacja_24)));
								}
								if(okres == 2) {
									$('.short-cena', $(oferta_item)).html(pakiet.cena_nie);
									$('.koszt-instalacji', $(oferta_item)).html(pakiet.instalacja_nie);
									$('input[name="wartosci[' + (index+1) + ']"]').val((parseInt(pakiet.cena_nie)));
									$('input[name="instalacje[' + (index+1) + ']"]').val((parseInt(pakiet.instalacja_nie)));
								}

								var suma = 0;

								$('.wartosci').each(function(index) {
									if(parseInt(this.value) !=  'NaN') {
										if($(this).attr('on') == 1) {
											suma += parseInt(this.value);
										}
									}
								});

								$('.suma').html(suma);

								var suma_instalacja = 0;

								$('.instalacje').each(function(index) {
									if(parseInt(this.value) !=  'NaN') {
										if($(this).attr('on') == 1) {
											suma_instalacja += parseInt(this.value);
										}
									}
								});

								$('.suma_instalacja').html(suma_instalacja);
							});
						});


						$('.konfigurator-dalej').click(function() {
							$('.konfigurator-slider, .konfigurator-bar, .konfigurator-oferta-items, .konfigurator-dalej, .radio, .checker').hide();
							//$('.konfigurator-oferta').css({border: 'none'});
							$('.konfigurator-oferta h2').css({'margin-top': '0'});

							$('.radio').each(function(index) {
								child = $('span.checked', $(this));
								if(!child[0]) {
									$(this).parent('div').hide();
								}
							});

							$('.zadzwon').fadeIn();
						});

					});
				}
			});
		}
	};

	Skynet.init();

});
