/*
 * @author Marcin Bielecki <santanachia@gmail.com>
 */

var fancy_szablony_opts = {
		prevEffect: 'none',
		nextEffect: 'none',
		closeBtn: false,
		helpers	: {
			title: {
				type: 'inside'
			},
			thumbs: {
				width: 84,
				height: 63
			},
			buttons: {}
		}
	};

function show(element,cel){
	if (typeof(pageTracker) != 'undefined')
		pageTracker._trackPageview("/?page="+element);

	if (element == 'kontaktform') {
		if ($('#kontaktform').length)
			$('#kontaktform').load(baseUrl+element+".php?base="+baseUrl+"&cel="+cel);
		else {
			$("#middle").append("<div id='kontaktform'></div>");
			$('#kontaktform').load(baseUrl+element+".php?base="+baseUrl+"&cel="+cel);
		}
	} else if ($("#"+element).length)
		slide(element);
	else {
		$("#middle").append("<div id='"+element+"'></div>");
		$("#"+element).load(baseUrl+element+".php?base="+baseUrl);
	}
}

function slide(element) {
	if ($('.left:not(":hidden")').length)
		$(".left:not(':hidden')").slideUp("fast",function(){
			$("#"+element+" .left").slideDown("fast",'easeInExpo');
		});
	else
		$("#"+element+" .left").slideDown("fast",'easeInExpo');

	if ($(".center:not(':hidden')").length)
		$(".center:not(':hidden')").slideUp("fast",function(){
			$("#"+element+" .center").slideDown("slow",'easeInExpo');
		});
	else
		$("#"+element+" .center").slideDown("slow",'easeInExpo');

	$("#top_menu a.naviButton img").css("background-image","");
	$("#top_menu a.naviButton."+element+" img").css("background-image","url('"+baseUrl.replace('ajax/','')+"gfx/"+element+"_small_bg.png')");

	switch(element) {
		case "zbuduj_system":
			$("body").animate({borderTopColor:'#F90'},'slow');
			break;
		case "o_nas":
			$("body").animate({borderTopColor:'#09C'},'slow');
			break;
		case "portfolio":
			$("body").animate({borderTopColor:'#9C0'},'slow');
			break;
		case "demo":
			$("body").animate({borderTopColor:'#969'},'slow');
			break;
		case "dolacz":
			$("body").animate({borderTopColor:'#06C'},'slow');
			break;
		case "kontaktform":
			$("body").animate({borderTopColor:'#900'},'slow');
			break;
	}
	$("#loading").hide();
	$.fancybox.hideLoading();
}

/**
 * zamienia wskazany element na ładnego selecta
 *
 * @param selector string string po którym jQuery odnajdzie elementy do wymiany
 * @param handler funkcja do wykonania po kliknięciu
 */
function selects(selector, handler) {
	if (typeof(selector) == 'undefined')
		selector = "select";
	$(selector).each(function(){
		if ($(this).hasClass("mySelect") || !$(this).is('select'))
			return false;

		div = $("<div>").addClass('mySelect').click(function(){
			$(this).find('.opts').slideToggle();
		}).hover(function(){
			if (typeof(selectTimeout) != 'undefined')
				clearTimeout(selectTimeout);
		}, function(){
			selectTimeout = setTimeout(function(){
				$('.opts').slideUp();
			}, 1000);
		}).insertAfter($(this));

		dSel = $("<div>").text($(this).find("option:selected").text()).addClass('selected').appendTo(div);

		dOpts = $("<div>").addClass('opts').prependTo(div);

		$(this).find('option').each(function(){
			dOpt = $("<div>").text($(this).text()).addClass($(this).val()).click(function(){
				$(this).parent().parent().next().val($(this).attr('class'));
				$(this).parent().next().text($(this).text());
				if (typeof(handler) == 'function')
					handler($(this).attr('class'));
			}).appendTo(dOpts);
		});

		input = $("<input>").attr({
			name: $(this).attr('name'),
			type: 'hidden',
			id: $(this).attr('id')
		}).val($(this).find('option:selected').val()).insertAfter(div);

		$(this).remove();
	});
}

function startScript(element) {
	switch (element){
		case 'kontaktform':
			$("#strona_glowna, #slogan").remove();
			$("#top_menu").show();
			slide('kontaktform');

			selects("#kontaktForm select");

			$('.kontaktForm input[type="image"]').hover(function(){
				$(this).attr('src', $(this).attr('src').replace('.png','_h.png'));
			}, function(){
				$(this).attr('src', $(this).attr('src').replace('_h.png','.png'));
			}).click(function(){$('#kontaktForm').submit()});

			$('#kontaktFTel,#kontaktFData,#kontaktFCzas').attr('disabled','disabled');

			$('#kontaktFForma').prev().css('z-index',1).find('.opts div').click(function(){
				if ($(this).hasClass('telefon')) {
					$('#kontaktFTel,#kontaktFData,#kontaktFCzas').removeAttr('disabled');
					$('#kontaktForm .kontaktTermin,#kontaktFTel').show();
					$('#kontaktFMail').attr('disabled','disabled').hide();
					$('#kontaktForm label[for="kontaktFMail"]').attr('for',"kontaktFTel").text('Nr telefonu:');
				} else {
					$('#kontaktFTel,#kontaktFData,#kontaktFCzas').attr('disabled','disabled');
					$('#kontaktForm .kontaktTermin,#kontaktFTel').hide();
					$('#kontaktFMail').removeAttr('disabled').show();
					$('#kontaktForm label[for="kontaktFTel"]').attr('for',"kontaktFMail").text('E-mail:');
				}
			});

			$.datepicker.setDefaults($.datepicker.regional["pl"]);
			$('#kontaktFData').datepicker({
				minDate: 0,
				beforeShowDay: $.datepicker.noWeekends
			});

			$('#kontaktForm').submit(function(eventObject){
				if (typeof(eventObject.originalEvent) == 'undefined')
					return false;
				$('.kontaktForm .loading').css('display', 'block');
				$.ajax({
					url: baseUrl+'kontaktform_send.php',
					data: $("#kontaktForm").serialize(),
					type: 'POST',
					success: function(data){
						if(data.success) $('#kontaktFReset').click();
						$('<div>'+data.msg+'</div>').dialog({
							modal:true,
							buttons: {
								Ok: function() {
									$(this).dialog("close");
								}
							}
						});
					},
					error: function(){
						$('<div>Nie można wysłać wiadomości.\nSpróbuj ponownie.</div>').dialog({
							modal:true,
							buttons: {
								Ok: function() {
									$(this).dialog("close");
								}
							}
						});
					},
					complete: function(){
						$('.kontaktForm .loading').css('display', 'none');
					}
				});
				return false;
			});
		break;
	}
}

$(document).ready(function(){
	$(".totop").click(function(){
		$(window).scrollTop(0);
	});

	$('#celKontaktu a').click(function(){
		show('kontaktform',$(this).attr('rel'));
		$('#celKontaktu').dialog('close');
		return false;
	});

	$("#top_menu a.naviButton").click(function(){
		if ($(this).hasClass('zbuduj_system'))
			return true;
		else if ($(this).hasClass('kontaktform'))
			$('#celKontaktu').dialog({
				modal: true,
				resizable: false,
				title: 'Cel kontaktu:',
				width: $('#celKontaktu a').length*120+24
			});
		else if ($(this).hasClass('portfolio')) {
			$('#fancy_potrfolio a').fancybox({
				prevEffect	: 'none',
				nextEffect	: 'none',
				closeBtn	: false,
				helpers		: {
					title	: {
						type: 'inside'
					},
					overlay	: {
						opacity : 0.8,
						css		: {
							'background-color' : '#000'
						}
					},
					thumbs	: {
						width	: 100,
						height	: 100
					},
					buttons	: {}
				}
			});
			$('#fancy_potrfolio a:first').next().next().next().next().click();
		} else
			show($(this).attr('rel'));
		return false;
	});
});
