// Intro page 
var stadAnimIntervalId=null;
var stadionX = -300;
var multiplier = 1;
var delayCount=100;
function stadionAnimation(elementId, imgWidth) 
{
    o=document.getElementById(elementId);
    if(o != null) {
        scrWidth = (document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body.clientWidth);
        if(scrWidth >= imgWidth) {
            o.style.left='0px';
            o.style.width='100%';
        }
        else if((delayCount++) > 60) {
            stadionX += multiplier * 2;
            if(stadionX <= scrWidth-imgWidth) {
                stadionX=scrWidth-imgWidth;
                multiplier*=(-1);
                delayCount=0;
            }
            if(stadionX >= 0) {
                stadionX=0;
                multiplier*=(-1);
                delayCount=0;
            }
            o.style.left=stadionX+'px';
            o.style.width=imgWidth+'px';
        }
    }
}

var strNews = ' --- Aktuality --- ';
var strNextMatch = ' nehrá sa ';
function scrollText(elementId) 
{
    var l = strNews.length;
    var a = strNews.substring(0, 1);
    strNews = strNews.substring(1, l)+a;
    document.getElementById(''+elementId).innerHTML = '&nbsp;&nbsp;Najbližšie: '+strNextMatch+'&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;'+strNews;
}

function GetStyle(el, styl) {
  if (el.currentStyle) return parseInt(el.currentStyle[styl.replace(/-/g,'')]);
  return parseInt(document.defaultView.getComputedStyle(el,null).getPropertyValue(styl.toLowerCase()));
}

function SetElementSource(elementId, picname)
{
  	o=document.getElementById(elementId);
  	if(o != null) o.src=""+picname;
}

var vwpcontent="";
function SetElementContent(elementId, content)
{
  	o=document.getElementById(elementId);
  	if(o != null) o.innerHTML=content;
}

function HideElementIf768(elementId)
{
    h = (document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight);
    if(h > 0 && h <= 770) {
      	o=document.getElementById(elementId);
      	if(o != null) o.style.display='none';
  	}
}

var onPlayback=false;
function playStopButton(bOver)
{
    if(onPlayback) {
        if(bOver) SetElementSource('playvideo', 'img/fk/stopon.png');
        else      SetElementSource('playvideo', 'img/fk/stop.png');
    }
    else {
        if(bOver) SetElementSource('playvideo', 'img/fk/playon.png');
        else      SetElementSource('playvideo', 'img/fk/play.png');
    }
}
function playVideo()
{
    o=document.getElementById('videowindow');
    shopbtn=document.getElementById('goshop');
    if(o != null) {
        w = (document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body.clientWidth);
        
        width = 754;
        height = 330;
        left = (w-width)/2+117;
        topp = 50;

        if(!onPlayback) {
            if(stadAnimIntervalId != null) {
                clearInterval(stadAnimIntervalId);
                stadAnimIntervalId=null;
            }
            
            o.style.display='block';
            o.style.left=left+'px';
            o.style.top=topp+'px';
            o.style.width=width+'px';
            o.style.height=height+'px';
            if(shopbtn != null) shopbtn.style.display='none';

            onPlayback=true;
        }
        else {
            stadAnimIntervalId=setInterval('stadionAnimation(\'stadionpic\', 1674)', 50);

            o.style.display='none';
            if(shopbtn != null) shopbtn.style.display='inline';

            onPlayback=false;
        }        
        playStopButton(true);
    }
}

// CMS
function adaptPageObjects() 
{
	o = document.getElementById('workspace');
	o2 = document.getElementById('workspaceinnerspace');
	if(o != null && o2 != null) {
        h=o2.clientHeight;
        h-=55;
        if(h < 500) h=500; 
        o.style.height = h+'px';
	}
                       
	o = document.getElementById('partners');
	if(o != null) {
      	scrWidth = (document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body.clientWidth);
      	left = 100+990+(scrWidth-990)/2;
      	o.style.left=left+'px';	
	}

}
function ConfirmNewLocation(alertText, httpLocation)
{
  if(confirm(alertText)) 
      document.location=httpLocation;
}

function ShowJsElement(elementId)
{
	o=document.getElementById(elementId);
	if(o != null) o.style.display="block";
	adaptPageObjects();
}

function HideJsElement(elementId)
{
	o=document.getElementById(elementId);
	if(o != null) o.style.display="none";
	adaptPageObjects();
}

function ShowHideJsElement(elementId)
{
    o=document.getElementById(elementId);
	if(o != null) {
      if(o.style.display!='block') o.style.display="block";
      else o.style.display="none";
    }
    adaptPageObjects();
}

function SetElementTextColor(elementId, color)
{
	o=document.getElementById(elementId);
	if(o != null) o.style.color=""+color;
}

function SetElementBorderColor(elementId, color)
{
	o=document.getElementById(elementId);
	if(o != null) o.style.borderColor=""+color;
}

function SetElementBgColor(elementId, color)
{
	o=document.getElementById(elementId);
	if(o != null) o.style.backgroundColor=""+color;
}
function SetElementValue(elementId, value)
{
	o=document.getElementById(elementId);
	if(o != null) o.value=value;
}
function ChangeLocationByValue(httpLink, elementId)
{
    document.location=''+httpLink+document.getElementById(elementId).value;
}

function doIframe()
{
	o = document.getElementById('shopframe');
	if(o != null) {
  	if(o.contentDocument) if(o.contentDocument.body) if(o.contentDocument.body.offsetHeight) o.style.height = o.contentDocument.body.offsetHeight+35+'px';
    else if(o.contentWindow) if(o.contentWindow.document) if(o.contentWindow.document.body) if(o.contentWindow.document.body.scrollHeight) o.style.height = o.contentWindow.document.body.scrollHeight+'px';
	}
}
function adaptIframe()
{
  if(document.getElementById) setInterval('doIframe()', 300);
}

function CreateLeagueYearChangeHttpLink(pageId, elementId)
{
  var link = 'index.php?id='+pageId;
  o=document.getElementById(elementId);
  if(o != null) link = 'index.php?id='+pageId+'&year='+o.value;
  return link;
}

function CreateMatchTipHttpLink(pageId, elementId)
{
  var link = 'index.php?id='+pageId;
  o=document.getElementById(elementId);
  if(o != null) link = 'index.php?id='+pageId+'&match='+o.value;
  return link;
}

function ActivateUpcomingEventsTables(elementId)
{
  o0=document.getElementById('upcomingeventstable0');
  o1=document.getElementById('upcomingeventstable1');
  o0b=document.getElementById('upcomingeventstable0btn');
  o1b=document.getElementById('upcomingeventstable1btn');

  if(o0 != null && o1 != null) {  
    if(elementId == 0) {
        o1.style.display='none';
        o0.style.display='block';
    }
    else {
        o0.style.display='none';
        o1.style.display='block';
    }
  }
  if(o0b != null && o1b != null) {  
    if(elementId == 0) {
        o0b.style.borderBottom='#5074A1 2px solid';
        o1b.style.borderBottom='0px';
    }
    else {
        o1b.style.borderBottom='#5074A1 2px solid';
        o0b.style.borderBottom='0px';
    }
  }
}

function SwitchElementGroup(elementBaseId, idFrom, idTo, idShow)
{
    lastgroup=idShow;
    for(i=idFrom;i<idTo;i++) {
      	o=document.getElementById(elementBaseId+i);
      	if(o != null) o.style.display="none";
    }
	o=document.getElementById(elementBaseId+idShow);
	if(o != null) o.style.display="block";
}
    
var slideshowimages;
var slideshowstarted=false;
var swapcontent=null;
function SlideshowKeyChecker(e)
{
   var KeyID = (window.event) ? event.keyCode : e.keyCode;
   switch(KeyID) {
        case 37: // left
            SlideshowShowScroll(-1);
            break;
        case 39: // right
            SlideshowShowScroll(1);
            break;
        case 27: // esc
            SlideshowClose();
            break;
   }
}
function SlideshowAdaptSize()
{
   img=document.getElementById('slideshowwindowimgage');
   if(img != null) {
       img.style.height='auto';
       
       h = (document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight);
       height=img.clientHeight;
       if(height > h-100) { 
            height=h-100;
            img.style.height=height+'px'; 
       }
    }
}
function SlideshowShow(bitmapname)
{                   
    document.onkeyup = SlideshowKeyChecker;
    slideshowstarted=false;
	o = document.getElementById('slideshowwindow');
	if(o != null) {
	   h = (document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight);
	   o.style.height=h+'px';
	   o.style.display='block';
	   SetElementSource('slideshowwindowimgage', bitmapname);
	   SlideshowAdaptSize();
	}	
}
function SlideshowClose()
{
    document.onkeyup = null; 
    slideshowstarted=false;
	o = document.getElementById('slideshowwindow');
	if(o != null) {
	   o.style.display='none';
	}

	o = document.getElementById('mainitem2');
	if(o != null && swapcontent != null) {
        o.innerHTML=swapcontent;
	    swapcontent=null;
    }
}
function SlideshowShowScroll(left_right)
{
   l = slideshowimages.length;
   if(l < 1) return;
   
   o = document.getElementById('slideshowwindowimgage');
   if(o == null) return;
   
   for(j=o.src.length-1;j>=0;j--) if(o.src.charAt(j) == '/') break;
   subsrc = o.src.substring(j+1, o.src.length);
   
   test=false;
   
   if(left_right == -1) {
      for(i=0;i<l-1;i++) {
          for(j=slideshowimages[i+1].length-1;j>=0;j--) if(slideshowimages[i+1].charAt(j) == '/') break;
          subim = slideshowimages[i+1].substring(j+1, slideshowimages[i+1].length);
          subim = escape(subim);
          if(subim == subsrc) {
              SetElementSource('slideshowwindowimgage', slideshowimages[i]);
              SlideshowAdaptSize();
              test=true;
              break;
          }
      }
      if(!test) SetElementSource('slideshowwindowimgage', slideshowimages[l-1]);
   }
   if(left_right == 1) { 
      for(i=1;i<l;i++) {
          for(j=slideshowimages[i-1].length-1;j>=0;j--) if(slideshowimages[i-1].charAt(j) == '/') break;
          subim = slideshowimages[i-1].substring(j+1, slideshowimages[i-1].length);
          subim = escape(subim);
          if(subim == subsrc) {
              SetElementSource('slideshowwindowimgage', slideshowimages[i]);
              SlideshowAdaptSize();
              test=true;
              break;
          }
      }
      if(!test) SetElementSource('slideshowwindowimgage', slideshowimages[0]);
   }
   
   if(slideshowstarted) setTimeout("SlideshowShowScroll("+left_right+")", 3700);
}
function SlideshowStartStop()
{
    if(!slideshowstarted) {
        slideshowstarted=true;
        setTimeout("SlideshowShowScroll(1)", 1000);
    }
    else {
        slideshowstarted=false;
    }
}

function PrintPagePopupWindow(pageLink)
{
    ppwnd=window.open(pageLink, "", "location=0,status=0,scrollbars=1,width=800,height=500");
}

var lastOverlayItemId=null;
function OverlayFormKeyChecker(e)
{
   var KeyID = (window.event) ? event.keyCode : e.keyCode;
   if(KeyID == 27) { // esc
       o = document.getElementById(lastOverlayItemId);
       if(o != null) o.style.display='none';
   }
}
function ShowOverlayForm(elementId)
{   
    lastOverlayItemId=elementId;
                
    document.onkeyup = OverlayFormKeyChecker;
	o = document.getElementById(elementId);
	if(o != null) {
	   h = (document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight);
	   o.style.height=h+'px';
	   o.style.display='block';
	}	
}
function CloseOverlayForm()
{
   o = document.getElementById(lastOverlayItemId);
   if(o != null) o.style.display='none';
}
var aiprandomizer=0;
function animateIntroPics(images,height)
{
  imax=false;
  
  for(i=2;i<5;i++) {
    o = document.getElementById('slshdyn'+i);
    if(o == null) break;

    w = GetStyle(o, 'width');

    if(w == 0) {
        if(i == 2) if((aiprandomizer++) >= images) aiprandomizer=2; 
            
        imgid = parseInt(aiprandomizer)+i;
        if(imgid > images) imgid-=images;
        
        o.src="img/temp/slide"+imgid+".png";
        o.style.display='inline';
    }    
    
    w+=15;
    h=w/(160/height);
    if(w > 160) w=160;
    if(h > height) h=height;
    l=(160-w)/2;
    t=-(height+h)/2;
    
    o.style.width=w+'px';
    o.style.height=h+'px';
    o.style.left=l+'px';
    o.style.top=t+'px';
    
    if(w < 40) break;
    if(w >= 160) {
        o.style.display='none';
        if(i==4) imax=true;
        if((oo = document.getElementById('slshstat'+i)) != null) oo.src=o.src; 
    }
  }
  
  if(imax) {
    for(i=2;i<5;i++) if((o = document.getElementById('slshdyn'+i)) != null) o.style.width='0px';
    setTimeout('animateIntroPics('+images+','+height+')', 3000);
  } 
  else setTimeout('animateIntroPics('+images+','+height+')', 50);
}

function limitText(elementId) 
{
    o = document.getElementById(elementId);
	if(o != null) if(o.value.length >= 400) o.value = o.value.substring(0, 400);
}

function SizeAreaByLines(elementId)
{
  o = document.getElementById(elementId);
  if(o!=null) {
      if(navigator.appName.indexOf("Microsoft Internet Explorer") == 0) {
          lines=o.value.split(/\r?\n|\r/).length;
          lines=lines*16;
          o.style.height=lines+'px';
          h=o.scrollHeight;
          o.style.height=h+'px';
      }
      else {
          o = document.getElementById(elementId);
          o.style.height='16px';
          h=o.scrollHeight;
          o.style.height=h+'px';
      }
      adaptPageObjects();
  }
  limitText(elementId);
}

function ajaxUpdateElement(elementId, src)
{
  if(window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
  }
  else {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }

  xmlhttp.onreadystatechange=function() 
  {
    if(xmlhttp.readyState==4 && xmlhttp.status==200) {
        document.getElementById(''+elementId).innerHTML=xmlhttp.responseText;
    }
  }
  
  xmlhttp.open("GET",src,true);
  xmlhttp.send();
}

function chatAutoUpdate(id)
{
  ajaxUpdateElement('chatcontent','chatrefresh.php?id='+id);
  adaptPageObjects();
  setTimeout("chatAutoUpdate("+id+")", 5000);
}

function searchGoogle()
{
   o = document.getElementById('searchval');
   if(o!=null) document.location='http://www.google.com/search?q='+o.value+'&sitesearch=http%3A%2F%2Fwww.fkmoldava.sk';
}
