jQuery(document).ready(function() {
    // forms
	var form = jQuery('form'),
		fields = jQuery('textarea, input:text', form);
	fields.bind('focus.highlighter', function() {
		jQuery(this).addClass('focused').parents().addClass('focused');
	}).bind('blur.highlighter', function() {
		jQuery(this).removeClass('focused').parents().removeClass('focused');
	});
    jQuery('input:checkbox, input:radio', form).customInput();

	// upload field
	var field = jQuery('.upload input', form);
    if (field.length) {
        field.before('<div class="upload_field_underlay button"><div class="left"><div class="right"><div class="bg">Прикрепить файл</div></div></div></div>');
        var underlay = field.siblings('.upload_field_underlay');
        underlay.after('<a class="upload_field_remove" style="display: none">| убрать</a>');
        underlay.after('<span class="upload_field_file_name"></span>');
        var file_name = underlay.siblings('.upload_field_file_name'),
            remove_link = underlay.siblings('.upload_field_remove');
        field.bind('change.upload', function() {
            file_name.text(this.value);
            if (!jQuery.browser.msie) {
                remove_link.show();
            }
        });
        if (!jQuery.browser.msie) {
            remove_link.bind('click.upload', function() {
                field[0].value = null;
                file_name.text('');
                remove_link.hide();
            });
        }
        if (jQuery.browser.msie) {
            // Fix unclickable text part of file input in IE.
            field.after('<div class="ie_file_field_fix"></div>');
            field.siblings('.ie_file_field_fix').css({
                position: 'absolute', top: 0, left: 0,
                width: field.outerWidth(), height: field.outerHeight(),
                'z-index': 10, 'cursor': 'pointer'
            }).bind('click.upload', function() { field.click(); });
        }
        field.add(field.siblings('.ie_file_field_fix'))
            .bind('mouseover', function() { underlay.addClass('hover'); })
            .bind('mouseout', function() { underlay.removeClass('hover'); })
            .bind('mousedown', function() { underlay.addClass('active'); })
            .bind('mouseup', function() { underlay.removeClass('active'); });
    }

	// payment
	var payment_radios = jQuery('input:radio[value=charge], input:radio[value=cash], input:radio[value=free]', form);
    if (payment_radios.length) {
		var textarea = payment_radios.parents('li.cf-box-group').next(),
		    note = textarea.next(),
            upload = note.next(),
            extra = textarea.add(upload).add(note);
        payment_radios.bind('change', function() {
            if (this.value == 'charge') {
                extra.slideDown();
            } else {
                extra.slideUp();
            }
        });
        var checked = payment_radios.filter(':checked');
        extra.toggle(checked && checked.val() == 'charge');
    }

    // member type
    (function() {
        var memberTypeRadios = jQuery('input:radio[value=participant], input:radio[value=speaker], input:radio[value=volunteer]', form);
        if (memberTypeRadios.length) {
            var f = function(b) {
                payment_radios.closest('li').show();
                if (!b) {
                    payment_radios.filter('[value=free]').removeAttr('checked')
                        .closest('li').hide();
                } else {
                    payment_radios.filter('[value=free]').attr('checked', true);
                    payment_radios.not('[value=free]').removeAttr('checked')
                        .closest('li').hide();
                }
                payment_radios.trigger('updateState').filter(':checked').trigger('change');
            };
            memberTypeRadios.bind('change', function() {
                f(!(this.value == 'participant'));
            });

            if (document.location.hash) {
                var m = document.location.hash.substring(1, document.location.hash.length);
                document.location.hash = '';
                memberTypeRadios.filter('[value=' + m + ']')
                    .attr('checked', true).trigger('updateState').trigger('change');
            }

            var checked = memberTypeRadios.filter(':checked');
            if (checked.length) f(checked.val() != 'participant');
        }
    })();

    // comments (yeah, i know)
    jQuery('.post span').each(function() {
        if (this.id.indexOf('more') == 0) {
            jQuery(this).parent().remove();
        }
    });

    // hacks
    (function() {
        var iphone = navigator.userAgent.match(/iPhone/i),
            ipad = navigator.userAgent.match(/iPad/i),
            ipod = navigator.userAgent.match(/iPod/i),
            safari = navigator.userAgent.match(/AppleWebKit/) && !navigator.userAgent.match(/Chrome/i),
            win = navigator.platform.match(/Win/),
            mac = navigator.platform.match(/Mac/),
            ie = navigator.userAgent.match(/Internet Explorer/i);
        var f = function() {
            if (safari && win) {
                jQuery('button').addClass('safari_win_fix');
            }
            if (iphone || ipad || ipod) {
                var matches = /Version\/[\d.]+/.exec(navigator.userAgent);
                if (matches.length) {
                    var version = matches[0].split('/')[1],
                        major = parseInt(version[0]);
                    if (major == 4) {
                        jQuery('button').addClass('apple_devices_fix');
                    }
                }
            }
        }
        f();
    })();

});
