var HostingFirmowy_widget = {
	unique_id  : 0,
	partner_id : 0,
	theme : 'main',
	ad_host : "http://partner.hostingfirmowy.pl/?mod=mod_affiliate&th=hosting_widget&display_self=1",
	init: function() {
		this.partner_id = hf_partner_id;
		if(window.hf_theme) {
			this.theme = hf_theme;
		}
      if(this.generateUniqueId()) {
			this.render();
		}
	},
	generateUniqueId: function() {
		if(document.getElementById('HostingFirmowy_widget')) {
			alert('Masz już wstawiony jeden widget! Usuń jeden z nich!');
			return false;
		}
	   this.unique_id = 'HostingFirmowy_widget';
		return true;
	},
	render: function() {
		this.renderLinkedStyles();
		var div = document.createElement("div");
		div.id = this.unique_id;
//		div.style.width = '100%';
		div.className = 'hf_widget';
		var elements = document.getElementsByTagName("*");
		var position = elements[elements.length - 1];
		position.parentNode.appendChild(div);
		var url = this.ad_host;
		var script = document.createElement("script");
		script.setAttribute("language", "javascript");
      script.setAttribute("type", "text/javascript");
      script.setAttribute("src", url);
      script.setAttribute("charset", "utf-8");
      div.appendChild(script);
	},
	renderLinkedStyles: function() {
		
		var href = "http://partner.hostingfirmowy.pl/templates/default/css/widget/"+this.theme+".css";
		try { //IE
			var link = document.createStyleSheet(href);
		} catch (e) { //W3C
			var link = document.createElement("LINK");
			link.setAttribute("href", href);
			link.setAttribute("rel", "stylesheet");
			link.setAttribute("type", "text/css");
			link.setAttribute("charset", "utf-8");
			var head = document.getElementsByTagName('head')[0];
			head.appendChild(link);
		}
	},
	updateWidgetContentHeight : function() {
		document.getElementById('WidgetContent').style.height = document.getElementById('WidgetHeight').offsetHeight + 'px';
	},
	showWidgetProgress : function() {
		document.getElementById('WidgetProgress').style.display = 'block';
		document.getElementById('WidgetProgressOverlay').style.display = 'block';
		document.getElementById('WidgetProgressOverlay').style.height = (document.getElementById('WidgetHeight').offsetHeight + 20) + 'px';
	},
	hideWidgetProgress : function() {
		document.getElementById('WidgetProgress').style.display = 'none';
		document.getElementById('WidgetProgressOverlay').style.display = 'none';
	},
	checkDomain : function() {
		var check_domain = document.getElementById('HostingFirmowy_domain').value;
		if(check_domain!='') {
			this.clearDomainsAvailabilityList();
			this.showWidgetProgress();
			url = 'http://hostingfirmowy.pl/?mod=mod_domains&a=checkDomain&th=widget_check&display_self=1&domain=' + check_domain;
			var script = document.createElement("script");
			script.setAttribute("language", "javascript");
	      script.setAttribute("type", "text/javascript");
	      script.setAttribute("src", url);
	      script.setAttribute("charset", "utf-8");
	      document.getElementById('HostingFirmowy_widget').appendChild(script);
		} else {
			this.hideWidgetProgress();
			alert('Podaj nazwę domeny!');
		}
	},
	hasClassName: function(element, className) {
		if ( typeof element == "string" )
			element = document.getElementById(element);
		if (!(element)) return;
		var elementClassName = element.className;
		return (elementClassName.length > 0 && (elementClassName == className ||
      new RegExp("(^|\\s)" + className + "(\\s|$)").test(elementClassName)));
	},
	addClassName: function(element, className) {
		if ( typeof element == "string" )
			element = document.getElementById(element);
		if (!(element)) return;
		if (!this.hasClassName( element , className))
      	element.className += (element.className ? ' ' : '') + className;
    	return element;
  	},
  	removeClassName: function(element, className) {
		if ( typeof element == "string" )
			element = document.getElementById(element);
		if (!(element)) return;
		element.className = element.className.replace( new RegExp("(^|\\s+)" + className + "(\\s+|$)"), ' ').replace(/^\s+/, '').replace(/\s+$/, '');
		return element;
  	},
	addDomain : function(domain_name, availability, price) {
		//sprawdzenie czy jest kontener na domeny
		this.createDomainsAvailabilityList();
		table = document.getElementById('HostingFirmowy_domainsAvailabilityList');
		var row = table.insertRow(-1);
		var cell;
		if(availability==1) {
			row.className = 'available';
			text = 'dostępna';
			domain_price_unit_text = 'zł';
		}else {
			row.className = 'no_available';
			text = 'zajęta';
			price = '';
			domain_price_unit_text = '';
		}
		row.id="HF_av_domain_"+domain_name;
		cell = row.insertCell(-1);
		cell.className = 'domain_name';
		cell.appendChild(document.createTextNode(domain_name));
		cell = row.insertCell(-1);
		cell.className = 'domain_status';
		cell.appendChild(document.createTextNode(text));
		cell = row.insertCell(-1);
		cell.className = 'domain_price';
		cell.appendChild(document.createTextNode(price));
		cell = row.insertCell(-1);
		cell.className = 'domain_price_unit';
		cell.appendChild(document.createTextNode(domain_price_unit_text));
		
		cell = row.insertCell(-1);
		cell.className = 'add_remove';
		if(availability==1) {
			add_remove_text = '<a href="#" onclick="HostingFirmowy_widget.addDelDomainToRegister(\''+domain_name+'\'); return false;">dodaj</a>';
		} else {
			add_remove_text = '&nbsp;';
		}
		cell.innerHTML = add_remove_text;
		this.updateWidgetContentHeight();
	},
	createDomainsAvailabilityList : function() {
		table = document.getElementById('HostingFirmowy_domainsAvailabilityListTable');
		if(document.getElementById('HostingFirmowy_domainsAvailabilityListTable')) {
			table.style.display = '';
			return;
		}
		html = '<div id="HostingFirmowy_domainsAvailabilityListTable"><table cellspacing="0" class="HF_tableList"><thead><tr><th colspan="4">Sprawdzone domeny</th></tr><tr><td class="domain_name">Domena</td><td class="domain_status">Dostępność</td><td class="domain_price" colspan="2">Cena za rok</td><td class="add_remove">&nbsp;</td></tr></thead><tbody id="HostingFirmowy_domainsAvailabilityList"></tbody></table><span class="HF_span"></span></div>';
		document.getElementById('WidgetHeight').innerHTML = document.getElementById('WidgetHeight').innerHTML + html;
	},
	clearDomainsAvailabilityList : function() {
		tbody = document.getElementById('HostingFirmowy_domainsAvailabilityList');
		if(!tbody) {
			return;
		}
		rows = tbody.childNodes;
		
		for(i=rows.length-1; i>=0; --i) {
			rows[i].parentNode.removeChild(rows[i])
		}
		tbody.parentNode.parentNode.style.display = 'none';
		this.updateWidgetContentHeight();
	},
	addDelDomainToRegister : function(domain_name) {
		if(document.getElementById('HF_reg_domain_'+domain_name)) {
			this.delDomainFromRegister(domain_name);
		} else {
			this.addDomainToRegister(domain_name);
		}
	},
	delDomainFromRegister : function(domain_name) {
		document.getElementById('HF_reg_domain_'+domain_name).parentNode.removeChild(document.getElementById('HF_reg_domain_'+domain_name));
		row = document.getElementById('HF_av_domain_'+domain_name);
		if(row) {
			this.removeClassName(row, 'domain_added');
			row.childNodes[4].childNodes[0].innerHTML = 'dodaj';
		}
		if(document.getElementById('HostingFirmowy_domainsRegistryList').childNodes.length==0) {
			document.getElementById('HostingFirmowy_domainsRegistryListTable').style.display = 'none';
			document.getElementById('HostingFirmowy_hosting_plans').style.display = 'none';
		}
		this.updateDomainsTotal();
		this.updateWidgetContentHeight();
	},
	addDomainToRegister : function(domain_name) {
		this.createRegisterTable();
		rowdom = document.getElementById('HF_av_domain_'+domain_name);
		rowdom.childNodes[4].childNodes[0].innerHTML = 'usuń';
		table = document.getElementById('HostingFirmowy_domainsRegistryList');
		var row = table.insertRow(-1);
		row.id="HF_reg_domain_"+domain_name;
		cell = row.insertCell(-1);
		cell.className = 'domain_name';
		cell.appendChild(document.createTextNode(domain_name));
		cell = row.insertCell(-1);
		cell.className = 'domain_price';
		price = rowdom.childNodes[2].innerHTML;
		cell.appendChild(document.createTextNode(price));
		cell = row.insertCell(-1);
		cell.className = 'domain_price_unit';
		cell.appendChild(document.createTextNode('zł'));
		cell = row.insertCell(-1);
		cell.className = 'add_remove';
		remove_text = '<a href="#" onclick="HostingFirmowy_widget.delDomainFromRegister(\''+domain_name+'\'); return false;">usuń</a>';
		cell.innerHTML = remove_text;
		this.addClassName('HF_av_domain_'+domain_name, 'domain_added');
		//xshow(row.childNodes);
		this.updateDomainsTotal();
		this.showSelectHostingPlan();
		this.updateWidgetContentHeight();
	},
	createRegisterTable : function() {
		if(document.getElementById('HostingFirmowy_domainsRegistryList')) {
			document.getElementById('HostingFirmowy_domainsRegistryListTable').style.display = '';
			return;
		}
		html = '<div id="HostingFirmowy_domainsRegistryListTable"><table class="HF_tableList" cellspacing="0"><thead><tr><th>Domeny które chcesz zarejestrować</th></tr><tr><td>Domena</td><td class="domain_price" colspan="2">Cena</td><td>&nbsp;</td></tr></thead><tbody id="HostingFirmowy_domainsRegistryList"></tbody><tfoot><tr><td>RAZEM ZA DOMENY:</td><td id="HostingFirmowy_domains_total" colspan="2">0zł</td><td>&nbsp;</td></tr><tr class="total_gross"><td>brutto:</td><td id="HostingFirmowy_domains_total_gross" colspan="2">0zł</td><td>&nbsp;</td></tr></tfoot></table></div>';
		document.getElementById('WidgetHeight').innerHTML = document.getElementById('WidgetHeight').innerHTML + html;
	},
	updateDomainsTotal : function() {
		domains_list = document.getElementById('HostingFirmowy_domainsRegistryList').childNodes;
		total = 0;
		for(i=0; i<domains_list.length; ++i) {
			price = parseFloat(domains_list[i].childNodes[1].innerHTML);
			total += price;
		}
		document.getElementById('HostingFirmowy_domains_total').innerHTML = ( total.toFixed(2) + ' zł' );
		document.getElementById('HostingFirmowy_domains_total_gross').innerHTML = ( (total*1.22).toFixed(2) + ' zł' );
	},
	showSelectHostingPlan : function() {
		if(document.getElementById('HostingFirmowy_hosting_plans')) {
			document.getElementById('HostingFirmowy_hosting_plans').style.display = '';
			this.updateWidgetContentHeight();
			return;
		}
		var div = document.createElement("div");
		div.id = 'HostingFirmowy_hosting_plans';
		div.className = 'HF_hosting_plans';
		document.getElementById('WidgetHeight').appendChild(div);
		html = '<table cellspacing="0" class="HF_tableList"><thead><tr><th>Potrzebuję serwera dla</th></tr></thead></table>';
		html += '<div><label for="hosting_type_2" id="hosting_for_site_div" class="checkbox_unchecked">';
		html += '	<input type="checkbox" id="hosting_type_2" value="2" name="hosting_type[]" onclick="HostingFirmowy_widget.updateHPP(this)" class=""/>';
		html += '	firmowej strony WWW';
		html += '</label></div>';
		html += '<div class="hf_right_div"><label class="hf_selected" for="hosting_type_1" id="hosting_for_email_div">';
		html += '	<input type="checkbox" id="hosting_type_1" value="1" name="hosting_type[]" checked="checked" onclick="HostingFirmowy_widget.updateHPP(this)" class=""/>';
		html += '	poczty e-mail';
		html += '</label></div>';
		html += '<div><label for="hosting_type_3" id="hosting_for_shop_div" class="">';
		html += '	<input type="checkbox" id="hosting_type_3" value="3" name="hosting_type[]" onclick="HostingFirmowy_widget.updateHPP(this)" class=""/>'
		html += '	sklepu internetowego';
		html += '</label></div>';
		html += '<div class="hf_right_div"><label for="hosting_type_4" id="hosting_for_many_shops_div" class="">';
		html += '	<input type="checkbox" id="hosting_type_4" value="4" name="hosting_type[]" onclick="HostingFirmowy_widget.updateHPP(this)" class=""/>';
		html += '	wielu stron i/lub sklepów';
		html += '</label></div>';
		for(i=1; i<=4; ++i) {
			html += '<div id="HF_hostingPlan_'+i+'" class="hosting_plan_data"></div>';
		}
		html += '<div class="hf_sub_area"><input type="submit" onclick="HostingFirmowy_widget.submitPartnerForm();" value="Wypróbuj przez 14 dni za darmo!"></div>';
		div.innerHTML = html;
		for(i=1; i<=4; ++i) {
			switch(i) {
				case 1:
					hp_id = 60; break;
				case 2 : 
					hp_id = 171; break;
				case 3 : 
					hp_id = 172; break;
				case 4 : 
					hp_id = 173; break;
			}
			url = 'http://hostingfirmowy.pl/?mod=mod_structure&t=iP&cPath=156_168_'+hp_id+'&display_self=1&div_no='+i+'&th=itemPage_serwer_js';
			var script = document.createElement("script");
			script.setAttribute("language", "javascript");
		   script.setAttribute("type", "text/javascript");
		   script.setAttribute("src", url);
		   script.setAttribute("charset", "utf-8");
		   document.getElementById('HostingFirmowy_widget').appendChild(script);
		}
		this.updateWidgetContentHeight();
	},
	updateHPP : function(elem) {
		if(elem) {
			if(elem.checked) {
				this.addClassName(elem.parentNode, 'hf_selected');
			} else {
				this.removeClassName(elem.parentNode, 'hf_selected');
			}
		}
		document.getElementById('HF_hostingPlan_1').style.display = 'none';
		document.getElementById('HF_hostingPlan_4').style.display = 'none';
		document.getElementById('HF_hostingPlan_3').style.display = 'none';
		document.getElementById('HF_hostingPlan_2').style.display = 'none';
		if(document.getElementById('hosting_type_4').checked) {
			plan_id = 4;
		} else if(document.getElementById('hosting_type_3').checked) {
			plan_id = 3;
		} else if(document.getElementById('hosting_type_2').checked) {
			plan_id = 1;
		} else {
			plan_id = 2;
		}
		document.getElementById('HF_hostingPlan_'+plan_id).style.display = '';
		this.updateWidgetContentHeight();
	},
	submitPartnerForm : function() {
		partner_id = this.partner_id;
		new_form = '<form target="_top" action="http://hostingfirmowy.pl/#step_3_a" method="post" id="HostingFirmowy_refererForm">';
		new_form += '<input type="hidden" name="_aID" value="'+partner_id+'">';
		domains = document.getElementById('HostingFirmowy_domainsRegistryList').childNodes;
		for(i=0; i<domains.length; ++i) {
			dm = domains[i].childNodes[0].innerHTML;
			new_form += '<input type="hidden" name="domains[]" value="'+dm+'">';
		}
		for(i=1; i<=4; ++i) {
			if(document.getElementById('hosting_type_'+i).checked) {
				new_form += '<input type="hidden" name="hosting_type['+i+']" value="'+i+'">';
			}
		}
		new_form += '</form>';
		document.getElementById('WidgetHeight').innerHTML += new_form;
		document.getElementById('HostingFirmowy_refererForm').submit();
	}
}
HostingFirmowy_widget.init();
