/* init & load */
String.prototype.startsWith = function(s) {
	return (s == null) ? false : (this.substring(0, s.length) == s);
}

hasClass = function(elm, cls) {
	if (!elm || !elm.className) return null;
	return (' ' + elm.className + ' ').indexOf(' ' + cls + ' ') + 1;
}
changeClass = function(elm, oldcls, newcls) {
	if (!elm) return null;
	var c = elm.className.split(' ');
	var n = [newcls];
	for (var i = 0; i < c.length; i++) {
		if (c[i]!=oldcls && c[i]!=newcls && c[i]!='') {
			n.push(c[i]);
		}
	}
	elm.className = n.join(' ');
}
removeClass = function(elm, cls) {changeClass(elm,cls,'');}
addClass = function(elm, cls) {changeClass(elm,'',cls);}

__ebt = function(tag, elm) {
	return (elm ? elm : document).getElementsByTagName(tag);
}
__ebi = function(id) {
	var elm = document.getElementById(id);
	if (arguments.length > 1) {
		var elms = {};
		elms[id] = elm;
		for (var i = 1; i < arguments.length; i++) elms[arguments[i]] = __ebi(arguments[i]);
		return elms;
	}
	return elm;
}

switchframes = function(linkElement) {
	var frame = 'weather1';
	if (linkElement.href.indexOf('weather1') >= 0) frame = 'weather1';
	else if (linkElement.href.indexOf('weather2') >= 0) frame = 'weather2';
	else if (linkElement.href.indexOf('weather3') >= 0) frame = 'weather3';
	else if (linkElement.href.indexOf('weather4') >= 0) frame = 'weather4';
	else if (linkElement.href.indexOf('weather5') >= 0) frame = 'weather5';
	else if (linkElement.href.indexOf('weather6') >= 0) frame = 'weather6';
	var elms = __ebi('weather1','weather2','weather3','weather4','weather5','weather6','weather1s','weather2s','weather3s','weather4s','weather5s','weather6s','weather1e','weather2e', 'weather3e','weather4e','weather5e','weather6e','weather1k','weather2k','weather3k','weather4k','weather5k');
	for (var p in elms) {
		if (!elms[p]) continue;
		if (elms[p].id.startsWith(frame)) addClass(elms[p],'active');
		else removeClass(elms[p],'active');
	}
	return false;
}

enhanceLinks = function(classNames) {
	var elms = __ebt("a");
	var elm;
	for (var i = 0; i < elms.length; i++) {
		elm = elms[i];
		for (var j = 0; j < classNames.length; j++) {
			if (hasClass(elm, classNames[j])) {
				fn = eval(classNames[j]);
				if (typeof(fn) == 'function') elm.onclick = function() {
					return fn(this);
				}
			}
		}
	}
}

function showInfo(url) {
	if (null !== url && url.length > 0) {
		var infoWin = window.open(url,'','scrollbars=yes,menubar=no,height=600,width=800,resizable=yes,toolbar=no,location=no,status=no');
	}
}

function showInfoWeather(url) {
	if (null !== url && url.length > 0) {
		var infoWin = window.open(url,'','scrollbars=yes,menubar=no,resizable=yes,height=600,width=800,toolbar=no,location=no,status=no');
	}
}

var Krka = {
    init: function () {
        $('.dotaznik').click(function(){
				pageTracker._trackEvent('Downloads', 'DOC-2011', '/sk/dotaznik.doc');
        });
        $('.data form select').change(Krka.filter);
		if($('.quiz-track').length > 0){
			$('.quiz-track').click(function(){
				pageTracker._trackEvent('Downloads', 'DOC', '/sk/dotaznik.doc');
			});
		}
    },

    filter: function () {
        $(this).parents('form').submit();
    }
};

$(document).ready(Krka.init);

window.oninit = function() {
	if (document.getElementById && document.getElementsByTagName) {
		enhanceLinks(['switchframes']);		
	}
}
launchInit = function() {
	if (document.getElementsByTagName && typeof(window.oninit) == 'function') {
		if (document.getElementsByTagName('body').length > 0) window.oninit();
		else setTimeout('launchInit()', 30);
	}
}
launchInit();
