Ext.onReady(function(){
    Ext.QuickTips.init();
    var inps = Ext.DomQuery.select('input[type="text"]');
	Ext.each(inps,function(i){
		if(Ext.get(i).hasClass('inp-calendar')) {
            var t = new Ext.form.DateField({
				applyTo: i,
				format: 'Y-m-d'
			});
		}
		else if(i.id != 'txt_search') {
			var t = new Ext.form.TextField({
				applyTo: i
			});
		}
		if(Ext.get(i).hasClass('required')) {
			t.allowBlank = false;
		}
		if(i.id == 'id_email') {
			t.vtype = 'email'
		}
		else if(i.id == 'id_homepage' || i.id == 'id_blog_feed_url' || i.id == 'id_blog_url' || i.id.indexOf('url') != -1) {
			t.vtype = 'url'
		}
	});

	var txts = Ext.DomQuery.select('textarea');
    Ext.each(txts, function(i){
        var ta = new Ext.form.TextArea({
			applyTo: i
		});
        if(Ext.get(i).hasClass('required')) {
			ta.allowBlank = false;
		}
	});

    var dds = Ext.DomQuery.select('select.dropdown');
    Ext.each(dds, function(i){
		var c = new Ext.form.ComboBox({
			typeAhead: true,
		    triggerAction: 'all',
		    transform:i,
		    forceSelection:true
		});

	});
	
	// add functionality of delete/remove subform
	
	var subforms = Ext.DomQuery.select("fieldset.subform");
	Ext.each(subforms, function(i){
		var subform_id = i.id;
		
		Ext.get("formset_" + subform_id).setVisibilityMode(Ext.Element.DISPLAY);
		Ext.get("id_" + subform_id + "-DELETE").setVisibilityMode(Ext.Element.DISPLAY);
		
		// hide original checkbox
		Ext.get("id_" + subform_id + "-DELETE").parent().hide();
		
		addClossButton(subform_id);
		showHideInlineFormset(subform_id);
	});

	// add functionality to add button
	var add_links = Ext.DomQuery.select("p.add-subform a");
	Ext.each(add_links, function(i){
		Ext.get(i).on('click', function(evt, t, o) {
			var formset_prefix = t.getAttribute('name');
			var formset = Ext.DomQuery.select("." + formset_prefix + ":last")[0];
			var clone = formset.cloneNode(true);
			var totalForms = parseInt(Ext.get("id_" + formset_prefix + "-TOTAL_FORMS").getValue());
			Ext.get("id_" + formset_prefix + "-TOTAL_FORMS").set({
				'value' : (totalForms + 1)
			});
			clone.id = formset_prefix + "-" + totalForms;
			closeButtonEvent(Ext.get(clone).query("legend a")[0], clone.id);
			var inputs = Ext.get(clone).query("input");
			inputs = inputs.concat(Ext.get(clone).query("select"));
			inputs = inputs.concat(Ext.get(clone).query("textarea"));
			inputs = inputs.concat(Ext.get(clone).query("ul"));
			Ext.each(inputs,function(i){
				i.id = i.id.replace((totalForms - 1), totalForms);
				if(typeof i.name != "undefined") {
					i.name = i.name.replace((totalForms - 1), totalForms);
					Ext.get(i).prev("label").set({
						'for' : i.id
					});
				}
				if(typeof i.checked != "undefined") {
					i.checked = false;
				}
				i.value = "";
			});
			
			Ext.get(clone).insertAfter(formset);
			Ext.get("formset_" + clone.id).setVisibilityMode(Ext.Element.DISPLAY);
			Ext.get("id_" + clone.id + "-DELETE").setVisibilityMode(Ext.Element.DISPLAY);
			showHideInlineFormset(clone.id, true);
		});
	});
});

function showHideInlineFormset(subform_id, force_show) {
	var checked = Ext.get("id_" + subform_id + "-DELETE").dom.checked;
	if(typeof force_show == "undefined")
		force_show = false
	if(checked == false || force_show) {
		Ext.get(subform_id).child("legend a").dom.innerHTML = 'x';
		Ext.get("formset_" + subform_id).show(true);
		Ext.get(subform_id).setOpacity(1);
	}
	else {
		Ext.get(subform_id).child("legend a").dom.innerHTML = 'undo remove';
		Ext.get("formset_" + subform_id).hide(true);
		Ext.get(subform_id).setOpacity(0.5);
	}
}

function addClossButton(subform_id) {
	var remove_link = Ext.DomHelper.createDom({
		tag: 'legend',
		children: [
			{tag: 'a', html: 'x', href:'javascript:void(0);'}
		]
	});
	Ext.get(subform_id).insertFirst(remove_link);
	closeButtonEvent(remove_link, subform_id);
}

function closeButtonEvent(remove_link, subform_id) {
	Ext.get(remove_link).on('click',function(evt, t, o){
		Ext.get("id_" + this.subform_id + "-DELETE").dom.checked = Ext.get("id_" + this.subform_id + "-DELETE").dom.checked ? false : true;
		showHideInlineFormset(this.subform_id);
	},{subform_id:subform_id});
	return remove_link;
}
