var timeout = null;
var preloadedimages = new Array();

if (-1 == location.href.indexOf('localhost')) {
   var gCesta = 'http://www.sexystream.cz/';
} else {
   var gCesta = 'http://localhost/sexystream/';   
}
var gCesta1 = 'http://www.funnz.biz/';
var gCesta2 = 'http://www.zvireciweb.cz/';

function preload(cesta, index) { 
  if (typeof(preloadedimages[cesta]) != 'undefined')
    return;
  preloadedimages[cesta] = new Image();
  if (index == 1) {
    preloadedimages[cesta].src = (gCesta1 + 'gf/stream/' + cesta).replace(/\.jpg/, 'r.jpg');
  } else if (index == 2) {
    preloadedimages[cesta].src = (gCesta2 + 'gf/stream/' + cesta).replace(/\.jpg/, 'r.jpg');
  } else {
    preloadedimages[cesta].src = (gCesta + 'gf/stream/' + cesta).replace(/\.jpg/, 'r.jpg');
  }  
}  

function startPreview(id, pocObr, adr, pocet, index){
  preload(adr + pocObr, index);
  document.getElementById('id-' + id).src = preloadedimages[adr + pocObr].src;
  
  var num = (pocObr.replace(/\.jpg/, '') * 1) + 1;
  if (num > pocet) {
     num = 1;
  }
  timeout = setTimeout("startPreview('" + id + "', '" + num + ".jpg', '" + adr + "', " + pocet + "," +  index + ")", 800);
}

function stopPreview(id, pocObr, adr, index){
  clearTimeout(timeout);
   if (index == 1) {
    preloadedimages[cesta].src = (gCesta1 + 'gf/stream/' + cesta).replace(/\.jpg/, 'r.jpg');
  } else if (index == 2) {
    preloadedimages[cesta].src = (gCesta2 + 'gf/stream/' + cesta).replace(/\.jpg/, 'r.jpg');
  } else {
    preloadedimages[cesta].src = (gCesta + 'gf/stream/' + cesta).replace(/\.jpg/, 'r.jpg');
  }    
}

var Search = {
   
   checkSearchForm: function() {
      var fraze = document.getElementById('fraze').value;
      fraze = fraze.replace(/^\s+/, '');
      if (fraze == '') {
         return false;
      }   
   }
}

var Cookies = {

   get: function (name) {
      var start = document.cookie.indexOf( name + "=" );
      var len = start + name.length + 1;
      if ((!start) && (name != document.cookie.substring(0, name.length))) {
         return null;
      }
      if (start == -1) return null;
      var end = document.cookie.indexOf(";", len);
      if (end == -1) end = document.cookie.length;
      return unescape(document.cookie.substring(len, end));
   }
}

var Rating = {

   rate_show: function(mark) {
      var slovniHodnoceni = new Array('hrozné', 'jakž takž', 'dobré', 'ujde', 'paráda');      
      
      for (i = 1; i < 6; i++) {
         if (i <= mark) {
            document.getElementById('rating-' + i).src = document.getElementById('rating-' + i).src.replace(/star[01]b[g]?/i, 'star1bg');
            document.getElementById('rating-info').innerHTML = slovniHodnoceni[i-1];
         } else {
            document.getElementById('rating-' + i).src = document.getElementById('rating-' + i).src.replace(/star[01]b[g]?/i, 'star0b');
         }
      }
   },
   
   disable_element: function(id){
      document.getElementById(id).onclick = null;
      document.getElementById(id).onmouseover = null;
   },
   
   
   rate_statechange: function(xmlhttp){
      document.getElementById("rating-info").innerHTML = xmlhttp.responseText;
      Rating.disable_element('rating-1');
      Rating.disable_element('rating-2');
      Rating.disable_element('rating-3');
      Rating.disable_element('rating-4');
      Rating.disable_element('rating-5');
   },
   
   rate: function (streamId, mark, typ) {
       var cookie = Cookies.get('ssRate');
       var nalezeno = false;
       if ((cookie != null) && (cookie != '')) { 
          var tmp = cookie.split('-');
          
          for (i = 0; i < tmp.lenght; i++) {
             if (tmp[i] == streamId) {
                nalezeno = true;
                break;
             }
          }
       }  
        
       if (false == nalezeno) {  
          var xmlhttp = (XMLHttpRequest ? new XMLHttpRequest : (ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : false));
          if (!xmlhttp) {
              return false;
          }
          xmlhttp.onreadystatechange= function () {
            Rating.rate_statechange(xmlhttp); 
          }
          xmlhttp.open("GET", gCesta + "direct/ajax/index.php?func=rate&streamId=" + streamId + "&mark=" + mark + '&typ=' + typ, true);
          xmlhttp.send(null);
          return true;
       }   
   }
}

var Page = {
   addToFav: function() {
      var title = window.document.title;
      var url = window.location;
      
      if (window.sidebar) {
         window.sidebar.addPanel(title, 'http://www.sexystream.cz/?fav', '');
      } else if( window.external ) {
         window.external.AddFavorite('http://www.sexystream.cz/?fav', title);
      } else if(window.opera && window.print) {
         alert('Pro uložení sexystream.cz do oblíbených stiskněte CTRL + D!');
      } 
   }
}


