var KROK_SCROLLU = 10;
var TIMER_SCROLLU = 10;
var scroll_timer=null;
var active_page=1;
//var left_akt_page = 1;
var scroll_y = 0;
var new_page = 0;
var previous_page = 0;
var browser = navigator.appName;
var left_akt_y = 0;
var left_height = 0;
var left_pocet_stran = null;
var va=1;
var krok;
var do_poloha = 0;


	
function load() {

	design_repair();
	
	/* zistuje mouse position pre scroller */	
	if(document.getElementById('scr_main') == null) return; 
	document.onmousemove = getMousePosition;
	document.getElementById('scr_main').onclick = function(e){ getMouseXY(e); return false;};
}


/* uprawy elementy aby sedeli pre IE7 & Netscape */
function design_repair() {
	
	var browserName=navigator.appName; 
	var browserVer=parseInt(navigator.appVersion);
	
	/* zistuje ci sa jedna o IE7 a viac */
	var IE7 = ((document.all)&&(navigator.appVersion.indexOf("MSIE 7.")!=-1)) ? true : false;
	
	/* ak je IE7 */
	if(IE7 == true)
	{
		/*zarovname sekcie v lavo*/
		document.getElementById('sect').style.top = '-9px';
		document.getElementById('content2').style.height =(document.getElementById('content2').offsetHeight - 46) + ( 14 - ( (document.getElementById('content2').offsetHeight) % 14 ) )+'px';
		
		/*zarovname tabulku kontaktu*/
		
		if(document.getElementById('t_cont') == null) return;
		else {
			document.getElementById('t_cont').style.marginLeft = '-17px';
		}
	}
	else
		document.getElementById('sect').style.top = '0px';

	//if(document.getElementById('content2') == null) return;
	
	if(IE7 == true)
	{
		return;
	} else {
	
		var x = document.getElementById('content2').offsetHeight;
		if(x == 403 || x == 402) return;
		var zvysok = ( (x) % 14 );

			var aaa = 14 - zvysok;
			/* 43 su marginy top & bottom, 3 je velkost patternu [toho obdlznicka] */
				var aa = document.getElementById('content2').style.height = ( (x-43) + (aaa-3)  )+'px';  
	}
}

function getMousePosition(e) {
	
  if (IE) { // grab the x-y pos.s if browser is IE
    tempX = event.clientX + document.body.scrollLeft;
    tempY = event.clientY + document.body.scrollTop;
  } else {  // grab the x-y pos.s if browser is NS
    tempX = e.pageX;
    tempY = e.pageY;
  }
  // catch possible negative values in NS4
  if (tempX < 0){tempX = 0;}
  if (tempY < 0){tempY = 0;}  
  
  return true
}
function getMouseXY(e) {
			
  if (IE) { // grab the x-y pos.s if browser is IE
    x = event.clientX + document.body.scrollLeft;
    y = event.clientY + document.body.scrollTop;
  } else {  // grab the x-y pos.s if browser is NS
    x = e.pageX;
    y = e.pageY;
  }  

  // catch possible negative values in NS4
  if (x < 0){x = 0;}
  if (y < 0){y = 0;}  
  
  parent_y = y - getPosition(document.getElementById('scr_main')).y; 
  
  pager(va,parent_y) 
  
  return true
}
/*

vola sa pri strankowani [samotne posuvanie obsahu], z neho potom scroller

*/
function pager(va,parent_y) {
	if(va == null && parent_y == null) return;
	
	if(browser == 'Microsoft Internet Explorer') {
		posun = 24;
	}
	else posun = 15;
	
	var left_velkost = Math.floor(left_vyska/left_pocet_stran);
	
	if(parent_y){
		left_akt_page = Math.floor( parent_y/left_velkost );	
		va = left_akt_page + 1;
	}
	else {
		left_akt_page = (va-1) * left_velkost;
	}
	
	if(va==null && parent_y != null) {
		left_akt_page = Math.floor( parent_y/left_velkost );	
		va = left_akt_page + 1;
	}

	if(va==1){
  	do_poloha = 0;
  	left_do_poloha = 0;
  }else{
  	do_poloha = -(document.getElementById( 'id_'+(va-1) ).offsetTop + posun);
  	left_do_poloha = (va-1) * left_velkost;
	}
	
	// klikne na stranku, co uz chcel raz animovat (animuje sa)
	if(scroll_timer!=null){
		if( va==new_page || left_akt_page == new_page){
			// zrus timer, skoc
    	clearInterval(scroll_timer);
  	  scroll_timer = null;
		  getElement("articles_special_text").top = do_poloha + 'px';
		  document.getElementById("scr_scrol").style.top = Math.floor(left_do_poloha) + 'px';
		  scroll_y = do_poloha;
		  left_akt_y = left_do_poloha;
	    active_page = new_page;
	    return;
		}else{
		  // uz sa animuje, naaaach si pockaaa
			return;
		}
	}

	var posun_textu = Math.abs(scroll_y-do_poloha);
	var posun_scrollu = Math.abs( left_akt_y-left_do_poloha);

  if(scroll_y < do_poloha){
  	krok = KROK_SCROLLU;
  	left_krok = -(posun_scrollu/posun_textu)*KROK_SCROLLU;
		if( isNaN(left_krok) ) {
			left_krok = null;
		}
  }else{
  	krok = -KROK_SCROLLU;
  	left_krok = (posun_scrollu/posun_textu)*KROK_SCROLLU;
  	if( isNaN(left_krok) ) {
			left_krok = null;
		}
  }

	if(va != null && parent_y == null) {
  	new_page = va;
  }
  else if(parent_y != null) {
  	new_page = left_akt_page+1;
	}
	else if(va == parent_y) {
		new_page = va+1;
	}
	else return false;

	scroll_timer=setInterval('my_scrool('+do_poloha+','+left_do_poloha+','+krok+','+left_krok+')', TIMER_SCROLLU);
	
	act_page();
}

	

/*

zabezpecuje nam animovany pohyb scrollu

*/
function my_scrool(do_poloha, left_do_poloha, krok, left_krok){
	scroll_y = scroll_y + krok;
	left_akt_y = left_akt_y + left_krok;

  if((krok > 0)&&(scroll_y >= do_poloha)) {
		scroll_y=do_poloha;
	}
  if((krok < 0)&&(scroll_y <= do_poloha)) {
		scroll_y=do_poloha;
	}
	

  if((left_krok > 0)&&(left_akt_y >= left_do_poloha)) {
		left_akt_y=left_do_poloha;
	}
  if((left_krok < 0)&&(left_akt_y <= left_do_poloha)) {
		left_akt_y=left_do_poloha;
	}

  if((scroll_y==do_poloha)&&(left_akt_y==left_do_poloha)){
    clearInterval(scroll_timer);
    scroll_timer=null;
    active_page=new_page;
  }
  
  if(  (new_page == left_pocet_stran) && ( (left_do_poloha + left_velkost != left_vyska) )  )
	{ 
		var rozdiel = left_vyska - (left_do_poloha + left_velkost);
			
		document.getElementById( 'scr_scrol' ).style.height = (left_velkost+rozdiel)+'px';
	}
	else {
		document.getElementById( 'scr_scrol' ).style.height = left_velkost+'px';
	}
  getElement("articles_special_text").top = scroll_y + 'px';

  document.getElementById("scr_scrol").style.top = Math.floor(left_akt_y) + 'px';
}


/* 

meni triedu stylu pre aktualnu stranku

*/
function act_page() {
	if( new_page == null || document.getElementById('pager_'+active_page+'') == null ) return;
	
	//ak niesme na prvej strane..
	if( previous_page != 0 ) {
		document.getElementById('pager_'+previous_page+'').className = 'pager_li';
		document.getElementById('pager_'+new_page+'').className = 'pager_li_sel';
		
		previous_page = new_page;
	}
	else {
		document.getElementById('pager_'+active_page+'').className = 'pager_li_sel';
		previous_page = active_page;
	}
}

/*
 * meni hover styl
 *	
 *	pride obj [momentalne cez 'this', element kt funkciu zawolal], podla neho zmeni class
 */  
function hover_page(obj) {
	
	if(obj.className == 'pager_li') {
		obj.className = 'pager_li_sel';
	}
	else if(obj.className == 'pager_li_sel') {

		//ak sa hoveruje aktivna stranka...
		if( obj.id == document.getElementById('pager_'+active_page+'').id ) {
			return;
		}
		else {
			obj.className = 'pager_li';
		}
	}
	else return;
	
	//ak sme na prvej stranke...
	if(new_page == 0) return;
	document.getElementById('pager_'+new_page+'').className = 'pager_li_sel';
}


/* 

posuva o stranku spat

*/
function pager_up(max_pages) {
	if(active_page == 1) return;
	pager(active_page - 1);
}
/* 

posuva o stranku dalej

*/
function pager_down(max_pages) {
	if( active_page == max_pages ) return;
	pager(active_page + 1);
}


function size_pager_left() {
	if(left_pocet_stran == null) return;

	max_height = 241;
	// max_height je vyska stredu, kde to pobehuje
	
	left_velkost = Math.floor( max_height / left_pocet_stran );

	document.getElementById( 'scr_scrol' ).style.height = left_velkost+'px';
}







// setStyleByClass: given an element type and a class selector,
// style property and value, apply the style.
// args:
//  t - type of tag to check for (e.g., SPAN)
//  c - class name
//  p - CSS property
//  v - value
var ie = (document.all) ? true : false;

function setStyleByClass(t,c,p,v){
	var elements;
	if(t == '*') {
		// '*' not supported by IE/Win 5.5 and below
		elements = (ie) ? document.all : document.getElementsByTagName('*');
	} else {
		elements = document.getElementsByTagName(t);
	}
	for(var i = 0; i < elements.length; i++){
		var node = elements.item(i);
		for(var j = 0; j < node.attributes.length; j++) {
			
			if(node.attributes.item(j).nodeName == 'class') {
				if(node.attributes.item(j).nodeValue == c) {
					eval('node.style.' + p + " = '" + v + "'");
				}
			
			}
		}
	}
}
// getStyleByClass: given an element type, a class selector and a property,
// return the value of the property for that element type.
// args:
//  t - element type
//  c - class identifier
//  p - CSS property
function getStyleByClass(t, c, p) {
	// first loop over elements, because if they've been modified they
	// will contain style data more recent than that in the stylesheet
	var elements;
	if(t == '*') {
		// '*' not supported by IE/Win 5.5 and below
		elements = (ie) ? document.all : document.getElementsByTagName('*');
	} else {
		elements = document.getElementsByTagName(t);
	}
	for(var i = 0; i < elements.length; i++){
		var node = elements.item(i);
		for(var j = 0; j < node.attributes.length; j++) {
			if(node.attributes.item(j).nodeName == 'class') {
				if(node.attributes.item(j).nodeValue == c) {
					var theStyle = eval('node.style.' + p);
					if((theStyle != "") && (theStyle != null)) {
						return theStyle;
					}
				}
			}
		}		
	}
	// if we got here it's because we didn't find anything
	// try styleSheets
	var sheets = document.styleSheets;
	if(sheets.length > 0) {
		// loop over each sheet
		for(var x = 0; x < sheets.length; x++) {
			// grab stylesheet rules
			var rules = sheets[x].cssRules;
			if(rules.length > 0) {
				// check each rule
				for(var y = 0; y < rules.length; y++) {
					var z = rules[y].style;
					// selectorText broken in NS 6/Mozilla: see
					// http://bugzilla.mozilla.org/show_bug.cgi?id=51944
					ugly_selectorText_workaround();
					if(allStyleRules) {
						if((allStyleRules[y] == c) ||
						   (allStyleRules[y] == (t + "." + c))) {
							return z[p];
						}			
					} else {
						// use the native selectorText and style stuff
						if(((z[p] != "") && (z[p] != null)) &&
						   ((rules[y].selectorText == c) ||
						    (rules[y].selectorText == (t + "." + c)))) {
							return z[p];
						}
					}
				}
			}
		}
	}

	return null;
}


function getElement(id) {

	if (document.getElementById)
      return document.getElementById(id).style;
      
  else if (document.layers)
      return document.layers[id];
      
  else if (document.all)
      return document.all[id].style;
      
  else {
      alert("DHTML nepodporovana");
      return false;
  }
}


function getPosition(e){ 
     var left = 0; 
     var top  = 0; 
 
     while (e.offsetParent){ 
          left += e.offsetLeft; 
          top  += e.offsetTop; 
          e     = e.offsetParent; 
     } 
 
     left += e.offsetLeft; 
     top  += e.offsetTop; 
 			
     return {x:left, y:top}; 
} 
	
