/* AJAX functions */

function GetXmlHttpObject() {
  var xmlHttp;
  try {
    // Firefox, Opera 8.0+, Safari
    xmlHttp = new XMLHttpRequest();
  } catch(e) {
    // Internet Explorer
    try {
      xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch(e) {      
      xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");      
    }    
  } 
  return xmlHttp;
}

function getMedia(content, id, autoplay) {
  if(typeof(id)=="undefined") {
    id = 0;
  }
  if(typeof(autoplay)=="undefined") {
    autoplay = 0;
  }  
  xmlHttp = GetXmlHttpObject();
  if(xmlHttp == null) {
    alert ("Your browser does not support AJAX!");
    return false;
  } 
  xmlHttp.onreadystatechange = function() {
    if(xmlHttp.readyState == 4) {
      document.getElementById('media-content').innerHTML = xmlHttp.responseText;
    }
  }
  var host = location.hostname;
  xmlHttp.open("GET", "http://"+host+"/media?content="+content+"&id="+id+"&autoplay="+autoplay+"", true);
  xmlHttp.send(null);
  if(content=="video") {
    if($('link-image').hasClass('active')) {
      $('link-image').removeClass('active');
    }  
    $('link-video').addClass('active');
  }
  if(content=="image") {
    if($('link-video').hasClass('active')) {
      $('link-video').removeClass('active');
    }  
    $('link-image').addClass('active');
  }
  if(content=="videos-list") {
    if($('link-video').hasClass('active')) {
      $('link-video').removeClass('active');
    }
    if($('link-image').hasClass('active')) {
      $('link-image').removeClass('active');
    }
  }
  return true;  
}

function rate(category, id, value) {
  xmlHttp = GetXmlHttpObject();
  if(xmlHttp == null) {
    alert ("Your browser does not support AJAX!");
    return false;
  } 
  xmlHttp.onreadystatechange = function() {
    if(xmlHttp.readyState == 4) {
      document.getElementById('rating').innerHTML = xmlHttp.responseText;
    }
  }
  var host = location.hostname;  
  xmlHttp.open("GET", "http://"+host+"/rating?category="+category+"&id="+id+"&value="+value+"", true);
  xmlHttp.send(null);  
  return true; 
}

/* MooTools functions */

window.addEvent('domready', function() {
  if($('latest-articles')) {
    var images = $$('#la-image img');
    var news = $$('#la-text div');
    var id = 0;   
    $A(images).erase(images[0]).each(function(image) {
      $(image).setStyle('opacity', '0');                  
    });
    news[0].addClass('active');           
    for(var i = 0; i < news.length; i++) { 
      news[i].setProperty('id', i);                       
      news[i].addEvent('mouseover', function(e) {         
        timer = $clear(timer);          
        if($('la-image').getElementById('active')) {
          $('la-image').getElementById('active').fade(0);
          $('la-image').getElementById('active').removeProperty('id');               
        }
        $A(news).each(function(news) {
          if(news.hasClass('active')) {
            news.removeClass('active');
          }
        }); 
        $A(images)[this.getProperty('id')].fade(1);
        $A(images)[this.getProperty('id')].setProperty('id', 'active');
        news[this.getProperty('id')].addClass('active');      
        id = this.getProperty('id');
        timer = slideShow.periodical(7000);          
      });                                          
    }
    var slideShow = function() {                                
      $A(images)[id].fade(0);
      $A(images)[id].removeProperty('id');
      $A(news)[id].removeClass('active');
      id++;
      if(id==4) {
        id = 0;
      }
      $A(images)[id].fade(1); 
      $A(images)[id].setProperty('id', 'active');
      $A(news)[id].addClass('active');               
    }
    var timer = slideShow.periodical(7000); 
  } 
   
  if($('gallery')) {     
    var maxX = ($$('#gallery-images img').length - 2) * 116;    
    var posX = 0;
    $('left').addEvent('click', function(e) {      
      if(posX > 0) {    
        posX = posX - 116;
        $('gallery-scroller').scrollTo(posX, 0);                                         
      }           
      return false; 
    });
    $('right').addEvent('click', function(e) {      
      if(posX != maxX) {    
        posX = posX + 116;
        $('gallery-scroller').scrollTo(posX, 0);       
      }                
      return false; 
    });
  }    
      
  if(document.getElementById('rating')) {
    var stars = document.getElementById('rating').getElementsByTagName('a');
    for(var i = 0; i < stars.length; i++) {                        
      stars[i].onmouseover = function() {       
        for(var i = 1; i <= 5; i++) {              
          var id = "s" + i;               
          document.getElementById(id).style.display = "none";                             
        }              
        var id = this.getAttribute('id');
        var num = id.slice(4);                  
        for(var i = 1; i <= num; i++) {
          var id = "rate" + i;                         
          document.getElementById(id).style.background = "url(/images/star03.jpg) no-repeat left top";   
        }   
      };
      stars[i].onmouseout = function() {    
        for(var i = 1; i <= 5; i++) {
          var id = "s" + i;               
          document.getElementById(id).style.display = "block";
        }
        var id = this.getAttribute('id');
        var num = id.slice(4);            
        for(var i = 1; i <= num; i++) {
          var id = "rate" + i;               
          document.getElementById(id).style.background = "url(/images/star04.jpg) no-repeat left top";
        }      
      };  
    }
  }
  
  if($('email-friend')) {    
    $('email-friend').setStyle('display', 'none');    
    $('send-article').addEvent('click', function(e) {
      $('email-friend').setStyle('display', 'block');           
      return false;
    });
    $('submit-friend').addEvent('click', function(e) {
      fields = $$('#email-friend input');            
      for(var i = 0; i < 3; i++) {
        if(fields[i].value == "") {
          alert("You have to enter your name, your email and your friend's email!");        
          return false;
        }      
      }
      var exp = new RegExp('^[a-zA-Z0-9]+[.a-zA-Z0-9_-]*@[a-zA-Z0-9_-]+[.a-zA-Z0-9_-]*\\.[a-zA-Z0-9_-]{2,4}$');    
      var mail = document.getElementById('email-sender').value;    
      if(!mail.match(exp)) {
        alert("Your email is incorrect!");
        return false;   
      }
      mail = document.getElementById('email-recipient').value; 
      if(!mail.match(exp)) {
        alert("Your friend's email is incorrect!");
        return false;   
      }      
    });      
  }
  
  if($('send-comment')) {    
    $('send-comment').setStyle('display', 'none');    
    $('add-comment').addEvent('click', function(e) {
      $('send-comment').setStyle('display', 'block');           
      return false;
    });      
    $('submit-comment').addEvent('click', function(e) {
      if($('send-comment')['author'].value == "" || $('send-comment')['comment'].value == "") {
        alert("You have to enter your name and comment!");        
        return false;
      }           
    });      
  } 
  
  if($('link-video')) {
    $('link-video').addClass('active'); 
  }
  
  $('newsletter-form').addEvent('submit', function(e) {    
    var exp = new RegExp('^[a-zA-Z0-9]+[.a-zA-Z0-9_-]*@[a-zA-Z0-9_-]+[.a-zA-Z0-9_-]*\\.[a-zA-Z0-9_-]{2,4}$');    
    var mail = document.getElementById('email-subscriber').value;    
    if(!mail.match(exp)) {
      alert("Your email is incorrect!");
      return false;   
    }
  });
  
  if($('membership-form')) {        
    $('membership-form').addEvent('submit', function(e) {      
      fields = $$('#membership-data input');            
      for(var i = 0; i < 8; i++) {
        if(fields[i].value == '') {
          alert("You have to enter all necessary data!");        
          return false;
        }      
      }
      fields = $$('#membership-login input');            
      for(var i = 0; i < 3; i++) {
        if(fields[i].value == '') {
          alert("You have to enter all necessary data!");        
          return false;
        }      
      }      
      var exp = new RegExp('^[a-zA-Z0-9]+[.a-zA-Z0-9_-]*@[a-zA-Z0-9_-]+[.a-zA-Z0-9_-]*\\.[a-zA-Z0-9_-]{2,4}$');    
      var mail = document.getElementById('email').value;    
      if(!mail.match(exp)) {
        alert('Your email is incorrect!');
        return false;   
      }
      var password1 = document.getElementById('password').value;
      var password2 = document.getElementById('confirm-password').value; 
      if(password1!=password2) {
        alert('Passwords don\'t match!');
        return false;   
      }            
    });      
  }
  
  if($('adverts3')) {
    var divs = $$('#adverts3 div');
    var i = 1;
    $A(divs).each(function(argument) {
      argument.setStyles({
        display: 'none',
        position: 'absolute',
        top: '20px'
      });
      argument.setProperty('id', 'sponsor' + i);              
      i++;
    });
    $('sponsor1').setStyle('display', 'block');
    var count = $A(divs).length;
    var showSponsor = function() {      
      $('sponsor' + i).setStyle('display', 'none');
      if(i==count) {
        i = 1;
      } else {      
        i++;
      }               
      $('sponsor' + i).setStyle('display', 'block'); 
    };
    showSponsor.periodical(10000, 0, i = 1);
  }            
});

/* submenu drop-down function */

submenu = function() {  
  if(document.all && document.getElementById) {
    navRoot = document.getElementById("nav");
    for(i = 0; i < navRoot.childNodes.length; i++) {
      node = navRoot.childNodes[i];
      if(node.nodeName == "LI") {
        node.onmouseover = function() {
          this.className += "over";
          if(this.childNodes.length > 2) {
            this.style.margin = "0 0 -2px 0";
          }          
        };
        node.onmouseout = function() {
          this.className = this.className.replace("over", "");
          if(this.childNodes.length > 2) {
            this.style.margin = "0";
          }          
        };
      }
    }
  }
}
window.onload = submenu;
