$(document).ready(function(){


	$('a.delete').css('color','red').click(function(){
		if (confirm('Czy napewno usunąć ten element ?'))
		{
			return true;
		}
		
		return false; 
	});
	
	$('.zobacz-na-mapie').click(function() {
		$('#gMapsFrame').animate({
			height: '410px'
		}, 800);
		$('#gMapsFrame iframe').css({ height: '410px'});
	});
	
	$(".colorbox").colorbox();
	
	$('.ask').colorbox({
		width: 	'500px',
		height: '450px',
		iframe: true
	});
	
	$('.askJazda').colorbox({
		width: 	'500px',
		height: '380px',
		iframe: true
	});
	
	$('.askFinanse').colorbox({
		width: 	'600px',
		height: '600px',
		iframe: true
	});
	
	$('.workRozwin').toggle(
		function(){
			$(this).html('Zwiń');
			$(this).prev().show('slow');
		},
		function(){
			$(this).html('Rozwiń');
			$(this).prev().hide('slow');
		}
	);
	
	/* Przelaczanie obrazkow w galerii */
	if($('.salonFOT').find('img').length > 0){
		var imgPaths = [];
		$('.salonFOT').find('img').each(function(){
			var src = $(this).attr('src').replace(/small/g,'orginal');
			var title = $(this).attr('alt');
			imgPaths.push('<a title="'+title+'" rel="imgGroup" href="'+src+'"></a>');
		});
		$("#mainImageLink").attr('rel','imgGroup').after(imgPaths.join('\n'));
		$('a[rel=imgGroup]').colorbox();
	}
	
	if ( window.location.href.indexOf('/ubezpieczenia') == -1 ) {
		$("#rightSlider").hover(function(){
			$("#rightSlider").stop(true,false).animate({right:"0"},"medium");
		},
		function(){
			$("#rightSlider").stop(true,false).animate({right:"-274px"},"medium")
		});

		$("#leftSlider").hover(function(){
			$("#leftSlider").stop(true,false).animate({left:"0"},"medium");
		},
		function(){
			$("#leftSlider").stop(true,false).animate({left:"-278px"},"medium")
		});
		
		$('#rightSlider, #leftSlider').click(function(){
			window.location = "/ubezpieczenia.html";
		});
	}
	else {
		$("#rightSlider, #leftSlider").remove();
	}
});

swfobject.registerObject("myId", "9.0.0", "/var/js/system/expressInstall.swf");

/**
* Wojażer
*/

Array.prototype.getUnique = function () {
	var o = new Object();
	var i, e;
	for (i = 0; e = this[i]; i++) {o[e] = 1};
	var a = new Array();
	for (e in o) {a.push (e)};
	return a;
} 

$(function(){
	var $q = $('#quiz');
	$('#quizTrigger').fancybox();
	$('.date').datePicker({clickInput:true});
	$('#insured .date').datePicker({
		startDate: '01-01-1900',
		endDate: (new Date()).asString(),
		clickInput: true
	});
	$('.dp-choose-date').remove();
	$input = $('.low.txt input');
	$input.each(function(){
		var txt = $(this).val();
		$(this).attr('data-default-txt', txt);
	});
	$input.click(function(){
		if ( $(this).val() == $(this).attr('data-default-txt') ) {
			$(this).val('');
		}
	});
	$input.focusout(function(){
		if ( $(this).val() == '')  {
			$(this).val($(this).attr('data-default-txt'));
		}
	});
	$('select#quantity').change(function(){
		var val  = $(this).val();
		var $insured = $('#insured');
		$insured.html('');
		for ( i = 1; i < parseInt(val) + 1; i++ ) {
			$insured.append('<p>- ' + i + ' osoba <input class="date"></p>')
		}
		$('#insured .date').datePicker({
			startDate: '01-01-1900',
			endDate: (new Date()).asString(),
			clickInput: true
		});
		$('.dp-choose-date').remove();
	});
	$('input[value=Anuluj]').click($.fancybox.close);
	$q.submit(function(){
		$err = $q.find('#errors');
		$err.empty();
		var arr = [];
		$q.find('input.date').each(function(){
			if ( !$(this).val().length ) {
				arr.push('<p>Proszę wypełnić wszystkie pola z datą.</p>');
			}
		});
		if ( $q.find('select:first').val() == 0 ) {
			arr.push('<p>Proszę wybrać kraj wyjazdu.</p>');
		};
		if ( !$q.find('.tripPurpose').is(':checked') ) {
			arr.push('<p>Proszę podać przynajmniej jeden cel wyjazdu.</p>');
		};
		$q.find('.person').each(function(){
			if ( $(this).val() == $(this).attr('data-default-txt') ) {
				arr.push('<p>Proszę podać dane osoby zgłaszającej.</p>');
			}
		});
		if ( !$q.find('input[name=agree]').is(':checked') ) {
			arr.push('<p>Wyrażenie zgody jest wymagane.</p>');
		};
		if ( arr.length ) {
			$err.html(arr.getUnique().join('\n')).slideDown('slow');
		}
		else {
			var arr = [];
			var $term = $('#term');
			arr.push($term.find('b').text() + " od " + $term.find('input:first').val() + " do " + $term.find('input:eq(1)').val());
			var $country = $('#country');
			arr.push($country.find('b').text() + " " + $country.find('select').val());
			arr.push($('#tripPurpose').find('b').text());
			$('.tripPurpose').each(function(){
			if ( $(this).is(':checked') ) arr.push('-' + $(this).parent().text());
			});
			var $quantity = $('#quantity');
			arr.push($quantity.find('b').text() + " " + $quantity.find('select').val());
			$('#insured p').each(function(){
			arr.push($.trim($(this).text()) + " " +$(this).children('input').val());
			});
			arr.push($('#moreOptions').find('b').text());
			$('.moreOptions').each(function(){
			if ( $(this).is(':checked') ) arr.push('-' + $(this).parent().text());
			});
			arr.push($('#person').find('b').text());
			$('.person').each(function(){
			arr.push($(this).val());
			});
			arr.push($.trim($('#agree').text()));
			$.ajax({
				data: 'data=' + arr.join('|'),
				type: $q.attr('method'),
				url: $q.attr('action'),
				success: function(html) {
					if ( html ) {	
						$q.html(
							'<h1><br>Twoje zapytanie zostało wysłane.</h1>' +
							'<p><b>Nasz konsultant skontaktuje się z Tobą w godzinach pracy działu: Pn.-Pt. 8-18, Sob. 9-15</b></p>'
						);
						$.fancybox.resize();
					}
				}
			});
		}
		return false;
	});
});
