/** i-Press Ajax Library: Module Tools
*
* @author Piotr Pielach
* @version 1.0.1 / 2008-11-09 / 2009-06-03
* @copyright Wszelkie prawa zastrzeżone. Rozpowszechnianie bez pozwolenia zabronione.
*/

var DOM = (document.createElement && document.getElementsByTagName);
var OP = (navigator.userAgent.indexOf('Opera') != -1) ? true : false;
var IE = (navigator.userAgent.indexOf('MSIE') != -1 && !OP) ? true : false;

var Tools = function() {
	var name = 'i-Press Ajax Library';
	var toStart = function(){};
};
Tools.prototype.addEvent = function(el,event,myFunc) {
	if (el.addEventListener) {
		el.addEventListener (event,myFunc,false);
	} else if (el.attachEvent) {
		el.attachEvent ('on'+event,myFunc);
	}
}
Tools.prototype.getElementsByClassName = function(className,node) {
	node = node || document;
	var arr = new Array();
	var tags = node.getElementsByTagName("*");
	var exp = new RegExp("^(.* )?"+className+"( .*)?$", "g");
	for (var i = 0; i < tags.length; i++){
		if (exp.test(tags[i].className)){
			arr.push(tags[i]);
		}
	}
	return arr;
}
Tools.prototype.addExternalCSS = function (file,media){
	var link = document.createElement("link");
	link.setAttribute("type","text/css");
	link.setAttribute("rel","stylesheet");
	link.setAttribute("href",file);
	link.setAttribute("media",media);
	document.getElementsByTagName("head")[0].appendChild(link);
};
Tools.prototype.moveNodes = function (sourceNode,targetNode,newWrapperTagName) {
	if (!DOM) return;
	var nodeRecipient = (targetNode) ? targetNode:document.createElement(newWrapperTagName);
	while (sourceNode.childNodes.length)
		nodeRecipient.appendChild(sourceNode.childNodes[0]);	
	if (newWrapperTagName)
		sourceNode.appendChild(nodeRecipient);
};
Tools.prototype.listToTable = function(idName) {
	if (!DOM) return;
	
	var menu = document.getElementById(idName);
	var menu_li = menu.getElementsByTagName('li');
	var table = document.createElement('table');
	var table_tbody = document.createElement('tbody');
	var table_row = document.createElement('tr');
	
	for (var i=0; i<menu_li.length; i++){
		var table_cell = document.createElement('td');
		moveNodes(menu_li[i],table_cell,'');
		table_row.appendChild(table_cell);
	}
	table_tbody.appendChild(table_row);
	table.appendChild(table_tbody);
	table.id = "IE";
	menu.insertBefore(table,menu.getElementsByTagName('ul')[0]);
	menu.removeChild(menu.getElementsByTagName('ul')[0]);
};
Tools.prototype.divToTable = function(idName) {
	if (!DOM) return;
	if (!document.getElementById('home')) return;
	
	var div = document.getElementById(idName);
	var div_rows = div.childNodes;
	var table = document.createElement('table');
	var table_tbody = document.createElement('tbody');
	
	for (var i=0; i<div_rows.length; i++){
		var table_row = document.createElement('tr');
		if (div_rows[i].className == 'row') {
			div_cols = div_rows[i].childNodes;
			for (var j=0;j<div_cols.length;j++) {
				if (div_cols[j].nodeType == 1) {
					var table_cell = document.createElement('td');
					var id = div_cols[j].getAttribute('id');
					this.moveNodes(div_cols[j],table_cell,'');
					table_cell.id = id;
					table_row.appendChild(table_cell);
				}
			}
			table_tbody.appendChild(table_row);
		}
	}
	table.appendChild(table_tbody);
	table.id = idName;
	div.parentNode.insertBefore(table,div);
	div.parentNode.removeChild(div);
};
Tools.prototype.startIfDOMReady = function() { //IE only
	var self = this;
	document.write('<script type="text/javascript" id="contentloadtag" defer="defer" src="javascript:void(0)"><\/script>');
	var contentloadtag = document.getElementById("contentloadtag");
	contentloadtag.onreadystatechange = function() {
		if (this.readyState=="complete")
			self.toStart();
	}
};
Tools.prototype.loginEvents = function() {
	var login = document.getElementById('inlogin');
	var pass = document.getElementById('inpassword');
	if (login) {
		this.addEvent(login,'focus',function() {
			if (IE)
				login.style.setAttribute('cssText','background-image:none;');
			else
				login.setAttribute('style','background-image:none;');
		});
	}
	if (pass) {
		this.addEvent(pass,'focus',function() {
			if (IE)
				pass.style.setAttribute('cssText','background-image:none;');
			else
				pass.setAttribute('style','background-image:none;');
		});
	}
}
Tools.prototype.gotoHome = function() {
	var self = this;
	self.goHome = true;
	var logo = document.getElementById('logo');
	if (logo) {
		logo.style.cursor = 'pointer';
		this.addEvent(logo,'click',function() {
			if (self.goHome)
				window.location.href = 'http://histmag.org/';
		});
	}
	var login = document.getElementById('login');
	if (login) {
		login.style.cursor = 'default';
		this.addEvent(login,'mouseover',function() {
			self.goHome = false;
		});
		this.addEvent(login,'mouseout',function() {
			self.goHome = true;
		});
	}
	var links = document.getElementById('toplinks');
	links.style.cursor = 'default';
}
Tools.prototype.footip = function() {
	var sups = document.getElementsByTagName('sup');
	for (var i = 0;i < sups.length;i++) {
		if (sups[i].className == 'footnote') {
			this.addEvent(sups[i],'mouseover',function(e) {
				if (IE) {
					if (window.event.srcElement.attributes[0].name == 'href')
						var ahref = window.event.srcElement.attributes[0].value;
					var dx = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
					var dy = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
				} else {
					if (e.target.attributes[0].name == 'href')
						var ahref = e.target.attributes[0].value;
					var dx = e.pageX;
					var dy = e.pageY;
				}
				if (ahref) {
					if (ahref.length > 1) ahref = ahref.substring(1,ahref.length);
					var fnt = document.getElementById(ahref);
					var p = document.createElement('p');
					p.id = 'tooltip';
					p.innerHTML = fnt.innerHTML;
					p.style.top = (dy - 10) + "px";
					p.style.left = (dx + 20) + "px";
					p.style.display = "block";
					document.getElementsByTagName('body')[0].appendChild(p);
				}
			});
			this.addEvent(sups[i],'mouseout',function() {
				var tip = document.getElementById('tooltip');
				if (tip) {
					tip.parentNode.removeChild(tip);
				}
			});
		}
	}
}
Tools.prototype.printme = function() {
	var o = document.getElementById('print');
	if (o) {
		o.style.display = 'block';
		this.addEvent(o,'click',function() {
			window.print();
			return false;
		});
	}
}
Tools.prototype.showme = function(id,link) {
	var o = document.getElementById(id);
	if (o) {
		o.style.display = 'block';
		this.addEvent(o,'click',function() {
			window.location.href = link;
			return false;
		});
	}
}
Tools.prototype.shareme = function() {
	var shr = document.getElementById('share');
	if (shr) {
		var url = window.location.href;
		var host =  window.location.hostname;
		
		var twit = 'http://twitter.com/home?status='+url;
		var fcbk = 'http://www.facebook.com/sharer.php?u='+url;

		var tbar = '<span id="print" title="Drukuj"></span>';
		tbar += '<span id="twitter" title="Dodaj do Twittera"></span>';
		tbar += '<span id="facebook" title="Dodaj do Facebooka"></span>';
		
		shr.innerHTML = tbar;
		this.printme();
		//this.showme('twitter',twit);
		//this.showme('facebook',fcbk);
	}
}
Tools.prototype.cartCalculate = function() {
	var sm = document.getElementById('sendmethod'); //metoda wysylki
	var pm = document.getElementById('paymethod'); //metoda platnosci
	var ism = document.getElementById('insm'); //koszty wysylki
	var iesum = document.getElementById('inendsum'); //suma końcowa
	var izsum = document.getElementById('insum'); //suma czesciowa książki ZN
	var izcnt = document.getElementById('incount'); //ilosc książek ZN
	var imsum = document.getElementById('inmbsum'); //suma czesciowa książki ZN
	var imcnt = document.getElementById('inmbcount'); //ilosc książek ZN
	var itsum = document.getElementById('intssum'); //suma czesciowa koszulek
	var itcnt = document.getElementById('intscount'); //ilosc koszulek
	var isp = document.getElementById('insp'); //priorytet
	
	var sendcost = 0.0;
	var count = new Array();
	var cond = new Array(2);cond[0] = new Array();cond[1] = new Array();cond[2] = new Array();cond[3] = new Array();
	cond[0]['max'] = 30;cond[1]['max'] = 5;cond[2]['max'] = 5;
	cond[0]['weight'] = 0.7;cond[1]['weight'] = 0.4;cond[2]['weight'] = 0.2;
	
	count[0] = parseInt(izcnt.value);
	count[1] = parseInt(imcnt.value);
	count[2] = parseInt(itcnt.value);
	
	if (count[0] < 0 || isNaN(count[0])) {
		count[0] = 0;
		izcnt.value = 0;
	}
	if (count[0] > cond[0]['max']) {
		count[0] = cond[0]['max'];
		izcnt.value = cond[0]['max'];
	}
	if (count[0] != izcnt.value) {
		izcnt.value = count[0];
	}
	if (count[1] < 0 || isNaN(count[1])) {
		count[1] = 0;
		imcnt.value = 0;
	}
	if (count[1] > cond[1]['max']) {
		count[1] = cond[1]['max'];
		imcnt.value = cond[1]['max'];
	}
	if (count[1] != imcnt.value) {
		imcnt.value = count[1];
	}
	if (count[2] < 0 || isNaN(count[2])) {
		count[2] = 0;
		itcnt.value = 0;
	}
	if (count[2] > cond[2]['max']) {
		count[2] = cond[2]['max'];
		itcnt.value = cond[2]['max'];
	}
	if (count[2] != itcnt.value) {
		itcnt.value = count[2];
	}
	
	var weight = count[0]*cond[0]['weight'] + count[1]*cond[1]['weight'] + count[2]*cond[2]['weight'];
	
	if (weight > 0 && weight < 0.35) {
		if (pm.value=='pm_prepaid')
			sendcost = 4.1;
		else
			sendcost = 10.5;
	} else if (weight >= 0.35 && weight < 0.5) {
		if (pm.value=='pm_prepaid')
			sendcost = 5.1;
		else
			sendcost = 10.5;
	} else if (weight >= 0.5 && weight < 1) {
		if (pm.value=='pm_prepaid')
			sendcost = 7.5;
		else
			sendcost = 12.5;
	} else if (weight >= 1 && weight < 2) {
		if (pm.value=='pm_prepaid')
			sendcost = 11.00;
		else
			sendcost = 14.0;
	} else if (weight >= 2 && weight < 5) {
		if (pm.value=='pm_prepaid')
			sendcost = 13.0;
		else
			sendcost = 16.0;
	}

	var zsum = count[0] * 35.0;
	var msum = count[1] * 30.0;
	var tsum = count[2] * 25.0;
	
	if (isp.checked && pm.value=='pm_prepaid' && weight > 0) sendcost += 2.0;
	if (isp.checked && pm.value=='pm_postpaid' && weight > 0) sendcost += 3.0;
	if (sm.value == 'sm_personal' || (zsum + msum + tsum) > 150.0) sendcost = 0.0;
	
	var endsum = zsum + msum + tsum + sendcost;
	
	izsum.setAttribute('value',zsum.toFixed(2));
	imsum.setAttribute('value',msum.toFixed(2));
	itsum.setAttribute('value',tsum.toFixed(2));
	iesum.setAttribute('value',endsum.toFixed(2));
	ism.setAttribute('value',sendcost.toFixed(2));
	
	this.cartSubmit(count[0],count[1],count[2]);
}
Tools.prototype.cartSubmit = function(zcount,mcount,tcount) {
	var submit = document.getElementById('insubmit');
	
	if (zcount > 0 || mcount > 0 || tcount > 0) {
		submit.disabled = false;
	} else {
		submit.disabled = true;
	}
}
Tools.prototype.cartEvents = function() {
	var sm = document.getElementById('sendmethod'); //metoda wysylki
	var pm = document.getElementById('paymethod'); //metoda platnosci
	var isp = document.getElementById('insp'); //priorytet
	var izcnt = document.getElementById('incount'); //ilosc książek ZN
	var imcnt = document.getElementById('inmbcount'); //ilosc książek MB
	var itcnt = document.getElementById('intscount'); //ilosc koszulek
	var iesum = document.getElementById('inendsum'); //suma końcowa
	var self = this;
	if (sm) {
		this.addEvent(sm,'change',function() {
			self.cartCalculate();
		});
	}
	if (pm) {
		this.addEvent(pm,'change',function() {
			self.cartCalculate();
		});
	}
	if (isp) {
		this.addEvent(isp,'change',function() {
			self.cartCalculate();
		});
	}
	if (izcnt) {
		this.addEvent(izcnt,'change',function() {
			self.cartCalculate();
		});
	}
	if (imcnt) {
		this.addEvent(imcnt,'change',function() {
			self.cartCalculate();
		});
	}
	if (itcnt) {
		this.addEvent(itcnt,'change',function() {
			self.cartCalculate();
		});
	}
	if (iesum) {
		this.addEvent(iesum,'change',function() {
			self.cartCalculate();
		});
	}
	self.cartCalculate();
}
Tools.prototype.cartCalculate2 = function() {
	var iesum = document.getElementById('inendsum'); //suma końcowa
	var irsum = document.getElementById('inrsum'); //suma czesciowa AWR
	var iawr = document.getElementById('awr'); //ilosc AWR

	var wsum = rsum = 0.0;
	if (iawr.checked)
		rsum = 100.0;
	var endsum = wsum + rsum;
	
	irsum.setAttribute('value',rsum.toFixed(2));
	iesum.setAttribute('value',endsum.toFixed(2));
	
	this.cartSubmit(wsum,rsum,0,0);
}
Tools.prototype.cartEvents2 = function() {
	var pm = document.getElementById('paymethod'); //metoda platnosci
	var iawr = document.getElementById('awr'); //ilosc AWR
	var iesum = document.getElementById('inendsum'); //suma końcowa
	var self = this;
	if (pm) {
		this.addEvent(pm,'change',function() {
			self.cartCalculate2();
		});
	}
	if (iawr) {
		this.addEvent(iawr,'change',function() {
			self.cartCalculate2();
		});
	}
	if (iesum) {
		this.addEvent(iesum,'change',function() {
			self.cartCalculate2();
		});
	}
	self.cartCalculate2();
}
Tools.prototype.menuHover = function() {//IE
	var mEls = document.getElementById("navigation").getElementsByTagName("li");
	for (var i=0; i<mEls.length; i++) {
		mEls[i].onmouseover=function() {
			this.className+=" menuHover";
		}
		mEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" menuHover\\b"), "");
		}
	}
}

/* 
 * Jedziemy... rozpisać na skrypty
 */
var tools = new Tools();
if (IE) { //let's do the magic
	tools.toStart = function() { tools.divToTable('content'); tools.menuHover(); };
	tools.startIfDOMReady();
}
window.onload = function() {
	tools.loginEvents();
	tools.gotoHome();
	tools.footip();
	tools.shareme();
}