/* login.js */

 /**
  * vytvoření třídy přes ClassMaker
  * @class Profile
  */
var Profile = SZN.ClassMaker.makeClass({
	NAME: "Profile",
	VERSION: "1.0",
	CLASS: "static"
});

Profile.init = function(data, defaultProfile) {
	this.ec = {};
	this.data = data;
	this.defaultProfile = defaultProfile;
	this.profileIdSelect = SZN.gEl('profileIdSelect');
	this.linkCreateNew = SZN.gEl('linkCreateNew');
	this.formSelectProfiles = SZN.gEl('formSelectProfiles');
	this.textLink = "zrušit a vrátit se k výchozímu?";
	this.newProfile = false;
	this.tmpProfile = {};
	this.defaultProfile = 0; /* index defaultniho profilu */
	/* hidden pro new profile */
	this.newHidden = SZN.cEl('input','newProfile');
	this.newHidden.type = 'hidden';
	this.newHidden.value = '1';
    this.newHidden.name = 'newProfile'
	this.changed = SZN.gEl('changed');
	this.hiddenableElem = SZN.gEl('hiddenable');

	this.licenceLink = SZN.gEl('licence-link');
	this.licence = SZN.gEl('licence');
	this.licence.style.display = 'none';
	this.hiddenableElem.style.display = 'none';

	this.HideLink = SZN.cEl('a');
	this.HideLink.id = 'show-fowm-link';
	this.HideLink.href = '#';
	this.HideLink.innerHTML = 'Zobrazit detaily profilu &raquo;';
	this.hiddenableElem.parentNode.insertBefore(this.HideLink, this.hiddenableElem);
	SZN.Events.addListener(this.HideLink, 'click', this, this.showForm, false, false);
	this.textLink2 = 'Skrýt detaily profilu &raquo;';


	this.elems = {
		profileId:		SZN.gEl('profileId'),
		name: 			SZN.gEl('name'),
		nick: 			SZN.gEl('nick'),
		fullname: 		SZN.gEl('fullname'),
		sex1: 			SZN.gEl('sex1'),
		sex2: 			SZN.gEl('sex2'),
		birthDay:		SZN.gEl('birthDay'),
		birthMonth:		SZN.gEl('birthMonth'),
		year:			SZN.gEl('year'),
		email:			SZN.gEl('email'),
		country:		SZN.gEl('country'),
		language:		SZN.gEl('language'),
		timezone:		SZN.gEl('timezone'),
        zip:            SZN.gEl('zip')
	};

	SZN.Events.addListener(this.profileIdSelect, 'change', this, this.changeProfile, false, false);
	if (this.licenceLink != null) {
		SZN.Events.addListener(this.licenceLink, 'click', this, this.showLicence, false, false);
	}
	this.ec.link = SZN.Events.addListener(this.linkCreateNew, 'click', this, this.createNew, false, true);
}

Profile.showForm = function(e, elm) {
	if (typeof e == 'object') {
		SZN.Events.cancelDef(e);

		if (this.hiddenableElem.style.display == 'none') {
			this.hiddenableElem.style.display = 'block';
		} else {
			this.hiddenableElem.style.display = 'none';
		}
	} else {
		/* volano z metody v e je hodnota co se ma udelat*/
		if (e == 'show') {
			this.hiddenableElem.style.display = 'block';
		} else {
			this.hiddenableElem.style.display = 'none';
		}

	}

	var tmp = this.HideLink.innerHTML;
	this.HideLink.innerHTML = this.textLink2;
	this.textLink2 = tmp;
}

Profile._changeDisable = function() {
	for (var e in this.elems) {
		this.elems[e].disabled = (this.elems[e].disabled == true ? false : true);
	}
}

Profile.changeProfile = function(e, elm) {
	this._loadProfileData(elm.selectedIndex);
};

Profile.createNew = function (e, elm) {
	SZN.Events.cancelDef(e);

	if (this.newProfile == false) {
		this.newProfile = true;
		this.showForm('show');
		this._changeDisable();
		this.elems.name.value = "Nový profil";
		this.formSelectProfiles.appendChild(this.newHidden);
		this.changed.value = 1;
	} else {
		this.newProfile = false;
		this.showForm('hide');
		this._changeDisable();
		this.newHidden = this.formSelectProfiles.removeChild(this.newHidden);
		this.profileIdSelect.options[this.profileIdSelect.selectedIndex].selected = false;
		this.profileIdSelect.options[0].selected = true;
		this.changed.value = 0;
		/*nacteni defaultniho profilu */
		this._loadProfileData(this.defaultProfile);
	}

	/* zmena odkazu */
	var tmptext = this.linkCreateNew.innerHTML;
	this.linkCreateNew.innerHTML = this.textLink;
	this.textLink = tmptext;

	/*ulozeni hodnot*/
	this._saveActualData();

};

Profile.showLicence = function (e, elm) {
	SZN.Events.cancelDef(e);

	this.licence.style.display = (this.licence.style.display == 'block' ? 'none' : 'block');
};

/**
 * ulozi data napsana uzivatelem - kdyby to bylo potreba
*/
Profile._saveActualData = function() {
	for (var e in this.elems) {
		/* detekce radii a checku */
		if (this.elems[e].checked == true) {
		 this.tmpProfile[e] =  this.elems[e].value;
		 /* detekce selectu */
		} else if (this.elems[e].selectedIndex) {
			this.tmpProfile[e] =  this.elems[e].options[this.elems[e].selectedIndex].value;

		} else {
			this.tmpProfile[e] = this.elems[e].value;

		}
	}
};

/**
 * nacte data profilu
 * @param {Number} profile ID profilu (index v poli data)
*/
Profile._loadProfileData = function(profile) {
	for (var e in this.elems) {
		/* nastaveni radii a checkboxu */
		if (this.elems[e].type == 'radio' || this.elems[e].type == 'checkbox') {
			if (this.data[profile][this.elems[e].name] == this.elems[e].value) {
				this.elems[e].checked = true;
			}
		/* nastaveni selectu*/
		} else if (this.elems[e].nodeName.toLowerCase() == 'select') {
			var selectedItem = this.elems[e].options[this.elems[e].selectedIndex];
			var items = this.elems[e].options;
			var value = this.data[profile][this.elems[e].name];

			selectedItem.selected = false;
			for (var i = 0; i < items.length; i++) {
				if (items[i].value == value) {
					items[i].selected = true;
					break;
				}
			}

		/* inputy typu text a hidden */
		} else {
			this.elems[e].value = this.data[profile][this.elems[e].name];
		}
	}
};

/*- IE Hack - no active ActiveX Component*/
function activateActiveX () {
	if(document.attachEvent && !window.opera) {
		var embed = document.getElementsByTagName('embed');
		for (i=0;i<embed.length;i++) {
			embed[i].outerHTML = embed[i].outerHTML;
		}
	}
}

// minimalizovana verze BubleHelp (ze settings) pouze na bublinu na strance weak_password
SZN.BubleHelp = SZN.ClassMaker.makeClass({
	NAME: "BubleHelp",
	VERSION: "1.0",
	CLASS: "class"
});

SZN.BubleHelp.prototype.$constructor = function(inp, helpField) {
	this.hf = helpField;
	SZN.Events.addListener(SZN.gEl(inp), 'focus', this, '_show');
	SZN.Events.addListener(SZN.gEl(inp), 'blur', this, '_hide');
}

SZN.BubleHelp.prototype._show = function (e, elm) {
	SZN.gEl(this.hf).style.display = 'block';
};

SZN.BubleHelp.prototype._hide = function (e, elm) {
	SZN.gEl(this.hf).style.display = 'none';
};

SZN.Util = SZN.ClassMaker.makeClass({
	NAME: "SZN.Util",
	VERSION: "1.0",
	CLASS: "static"
});

SZN.Util.initHider = function (error) {
	this.form = SZN.gEl('wp-change-form');
	this.link = SZN.gEl('wp-open-link');
	this.input = SZN.gEl('wp_password1');
	SZN.Events.addListener(this.link, 'click', this, this._hider);
	if (error == 0) {
		this.form.style.display = 'none';
	} else {
		this.input.focus();
	}
};

SZN.Util._hider = function (e, elm) {
	SZN.Events.cancelDef(e);
	if (this.form.style.display == 'none') {
		this.form.style.display = 'block';
		this.input.focus();
	} else {
		this.form.style.display = 'none';
	}
}

