//  copyright Agachi Valentin

/**
 * Browser detect
 */

var brIE = ( /msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent) );



/**
 * Location functions
 */

function go(link, target) {
	if (target == '_blank') {
		popup(link, 700, 550, 'go_win');
	} else {
		if (!target) {
			target = document;
		}
		target.location = link;
	}
}

function win_new(link, width, height) {
	var settings = 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,width='+width+',height='+height;
	cms = window.open(link, 'cms_new', settings);
}



/**
 * Page onload functions handling
 */

var icd_onload_funcs=new Array();

function icd_onload_register(func) {
	if (func != null) {
		icd_onload_funcs[icd_onload_funcs.length] = func;
	}
}

function icd_onload() {
	for (i=0; i < icd_onload_funcs.length; i++) {
		icd_onload_funcs[i]();
	}
}
window.onload = icd_onload;



/* external links */

function linkExternalClick(o) {
	var p = icdTraficID.split('-');
	if (p.length < 2) p[1] = '0';
	o.href = icdUrl + 'inc/trafic/out.php?s=' + p[0] + '&w=' + p[1] + '&u=' + escape(o.href);
	o.onclick='';
	return true;
}

function getURLHost(sURL) {
	var Re = new RegExp('^http://([^/]+)/.*$');
	var s = sURL.replace(Re, '$1');
	return s;
}

function fixLinksExternal() {
	if (document.getElementById) {
		var aLinks = document.getElementsByTagName('a');
	} else if (document.all) {
		var aLinks = document.all.tags('A');	
	} else {
		return false; 
	}
	var sHost = getURLHost(new String(document.location));
	for (var i = 0; i < aLinks.length; i++) {
		s = getURLHost(aLinks[i].href);
		if ((s != sHost)) {
			aLinks[i].className += ' external';
			aLinks[i].setAttribute('target', '_blank');
//			aLinks[i].onclick = new Function('return linkExternalClick(this);');
		}
	}
}
icd_onload_register(fixLinksExternal);



function enableLiveCommentPreview() {
	if (!document.getElementById) return false;
	$('#commentPreview').removeClass('hide');
	$('#comm-dpoufou').bind('keyup', function(ev){ updateLiveCommentPreview(this, ev); });
	$('#comm-obnf').bind('keyup', function(ev){ updateLiveCommentPreview(this, ev); });
	$('#comm-vsm').bind('keyup', function(ev){ updateLiveCommentPreview(this, ev); });
}

function updateLiveCommentPreview(o, ev) {
	if (!ev) ev = window.event;
	if (o.name == 'dpoufou') {
		var t = $('#preview-content').html(o.value);
	} else if (o.name == 'obnf' || o.name == 'vsm') {
		var t = $('#preview-author').get(0);
		var mk = false;
		if (t.childNodes.length < 1) {
			mk = true;
		} else {
			var c = t.childNodes[0];
			if (o.name == 'obnf') {
				if (c.nodeName.toLowerCase() == 'a') c.innerHTML = o.value;
				else c.data = o.value;
			} else {
				if (c.nodeName.toLowerCase() == 'a') c.href = 'http://' + o.value;
				else mk = true;
			}
		}
		if (mk) {
			t.innerHTML = '';
			c = document.createElement('a');
			c.href = 'http://' + document.getElementById('comm-vsm').value;
			c.innerHTML = document.getElementById('comm-obnf').value;
			c.rel = 'nofollow';
			t.appendChild(c);
		}
	}
}



function popupImg(sPath, sPicURL, sTitle) { 
	window.open(sPath + "lib/image.popup.htm?" + sPicURL + '?' + sTitle, "", "resizable=1,height=200,width=200"); 
	return false;
}





/* API - created by Valentin Agachi - http://agachi.name/ */

var api = {
	ratings: {
		init: function () {
			$('td.rating').each(function(){
				var p = this.className.split(' ');
				for (var k = 0; k < p.length; k++) {
					if (regs = p[k].match(/rating-([0-9]+)/)) {
						$(this).empty();
						for (var j = 1; j <= regs[1]; j++) {
							var l = $.A({ href: '#', innerHTML: '&nbsp;', 'class': 's' + j, 'onclick': function(){ return false; } });
							this.appendChild(l);
						}
					}
				}
			});
		}
	},
	stripes: function() {
		if ($.alternate)
			$.alternate('#sites table', 'tr', 'row1,row2');
	}
};
icd_onload_register(api.stripes);
icd_onload_register(api.ratings.init);
