var countries = null;
function changeCountryCode() {
	var value = $(this).val();
	if (value != '@') return;
	var select = $(this);
	var options = select[0].options;
	select.unbind('change keyup', changeCountryCode );
	function initCountries() {
		options.length = 0;
		$.each( countries, function(index,country) {
			options[ index ] = new Option( country[1], country[0] );
		});
	}
	if (countries != null) initCountries();
	else $.getScript('/javascripts/dynamic_countries.js', initCountries );
}
function changePaymentMethod() {
	var checked = $("#order_group_payment_method_id_2").is(':checked');
	$("#order_group_payment_method_subtype").css({display:(checked)?'block':'none'});
}
function changeCustomerType() {
	var value = $('select#order_group_customer_type').val();
	$('#frm-order p.nip').css({display:(value == 'F')?'block':'none'});
	$('#frm-order p.company_name').css({display:(value == 'F')?'block':'none'});
}
function changeBillingAddressCheckbox() {
	var check = $('input#order_group_has_billing_address').is(':checked');
	$('#frm-order fieldset.adr2').css({display:(check)?'block':'none'});
}
function changeContactAddressCheckbox() {
	var check = $('input#order_group_has_contact_address').is(':checked');
	$('#frm-order fieldset.adr3').css({display:(check)?'block':'none'});
}
function changeFirstLastName() {
	var fn = $("input#order_group_first_name").val();
	var ln = $("input#order_group_last_name").val();
	$("input#order_group_default_address_attributes_address1, \
	   input#order_group_billing_address_attributes_address1, \
	   input#order_group_contact_address_attributes_address1").each( function( idx, input ) {
		var re = /^\s+|\s+$/g;
		$(input).val( (fn + ' ' + ln).replace(re,""));
		var label = $(input).prev();
		if ($.browser.msie) label.children("span").css({display: ($(input).val() != "") ? "none" : "block"});
		else label.children("span").animate({opacity: ($(input).val() != "") ? 0.0 : 1.0}, 200);
	});
}
function changeSetPassword() {
	var is_new = ($('input#order_group_set_password').length > 0);
	if (!is_new && $('input#order_group_email').val() != "") {
		//$('input#order_group_email').attr({disabled:'disabled'});
	}
	var check = $('input#order_group_set_password').is(':checked');
	$('#frm-order p.password').css({display:(check)?'block':'none'});
	$('#frm-order p.password_confirmation').css({display:(check)?'block':'none'});
}
function submitOrderProcessForm() {
	$('#frm-order-process').submit();
}
function submitForgotPasswordForm() {
	$.ajax({
		type: $(this).attr('method'), 
		url: $(this).attr('action')+'.js',
		data: $.param($(this).serializeArray()), 
		dataType: "script"
	});	 
	return false;
}
function initForms() {
  var textboxes = $("form.pretty input.text");
  textboxes.each( function(index, input) {
    var label = $(input).prev();
    label.html( label.html().replace(/:$/,""));
    label.wrapInner("<span></span>");
    if ($.browser.msie) label.children("span").css({display: ($(input).val() != "") ? "none" : "block"});
    else label.children("span").animate({opacity: ($(input).val() != "") ? 0.0 : 1.0}, 0);
    $(input).bind('focus', function() {
      if ($.browser.msie) label.children("span").css({display:"none"});
      else label.children("span").animate({opacity: 0.0}, 200);
    });
    $(input).bind('blur', function() {
      if ($.browser.msie) label.children("span").css({display: ($(input).val() != "") ? "none" : "block"});
      else label.children("span").animate({opacity: ($(input).val() != "") ? 0.0 : 1.0}, 200);
    });
  });
  var comboboxes = $("form.pretty select");
  comboboxes.each( function(index, input) {
    var label = $(input).prev();
    label.html( label.html().replace(/:$/,""));
    label.wrapInner("<span></span>");
    if ($.browser.msie) label.children("span").css({display: ($(input).val() != "") ? "none" : "block"});
    else label.children("span").animate({opacity: ($(input).val() != "") ? 0.0 : 1.0}, 0);
  });
}
function initOrders() {
	initForms();
	$("#frm-forgot").bind('submit', submitForgotPasswordForm );
	if ($('#frm-order-process').length == 1) {
		$('#frm-order-process input.button').hide();
		setTimeout( submitOrderProcessForm, 1000 );
	}
	if ($('#frm-order').length == 0) return;
	$('select#order_group_customer_type').bind( 'change keyup', changeCustomerType );
	if ($('input#order_group_first_name').val() == "" && $('input#order_group_last_name').val() == "") {
		$('input#order_group_first_name').bind( 'change keyup', changeFirstLastName );
		$('input#order_group_last_name').bind( 'change keyup', changeFirstLastName );
	}
	$('input#order_group_set_password').bind( 'click', changeSetPassword );
	$('input#order_group_has_billing_address').bind( 'change click', changeBillingAddressCheckbox );
	$('input#order_group_has_contact_address').bind( 'change click', changeContactAddressCheckbox );
	$('p.country select').bind('change keyup', changeCountryCode );
	$("ul#payment-method input").bind('click', changePaymentMethod );
	changeCustomerType();
	changeBillingAddressCheckbox();
	changeContactAddressCheckbox();
	changeFirstLastName();
	changeSetPassword();
	changePaymentMethod();
	$('a[rel=qtip]').qtip({
		position: {
			corner: { target: 'topMiddle', tooltip:'bottomLeft' },
			adjust: { x: -50 }
		},
		style: {
		  width: 640, padding: 10,
		  height: 380,
		  border: { width: 5, radius: 5, color: '#237eb5' },
		  tip: 'bottomLeft', name: 'light'
		},
		adjust: {
		  scroll: true
		},
		content: {
			title: {
				text: "Warunki czarteru jachtu",
				button: "Zamknij"
			},
		  url: '/pl/warunki-czarteru',
		  data: { qtip: true },
		  method: 'get'
		},
		show: 'click',
		hide: 'unfocus',
		api: {
			onRender: function () { 
				this.elements.tip.css({marginLeft: "50px" });
				this.elements.content.css({ overflow: "auto" });
				//$("div.qtip-content").css({ overflow: "auto" });
			}
		}
	}).click( function() { return false });
}
$(document).ready( initOrders );