jQuery(document).ready(function()
{
 $('a[rel="lightbox"]').each(function() {$(this).lightBox();});
 try
 {
   $('#topimpnews-wrapper').scrollable({circular: true}).autoscroll({interval: 5000}).navigator({indexed:true});
   $('#anunturi_scroller').scrollable({circular: true}).autoscroll({interval: 7000});
   
   
   
   $("#mainscroller").scrollable({keyboard: 'static', circular: true, vertical: true}).navigator("#main_navi").autoscroll({interval: 7000});   
   
   $('#BottomNews').scrollable().navigator({indexed:true});
   
   
   setTimeout('$("img.Foto").vAlign()',10);
   
  var $allVideos = $("iframe[src^='http://www.youtube.com']");

  var newWidth = 660;
  
  $allVideos.each(function()
  {
    if ($(this).parent().hasClass('leftvideo')) {}
    else
    {
      $(this).data('aspectRatio', this.height / this.width).removeAttr('height').removeAttr('width');
      $(this).width(newWidth).height(newWidth * $(this).data('aspectRatio'));
    }
  });
   
 }
catch(ex) {}

 $("#MeniuLogat").bind("mouseleave",function(){
      $(this).hide();
    });

});

function switchGroupStiri(el, id)
{
  $('#top_nav_columns li').removeClass('active');
  $(el).parent().addClass('active');
  $('.latestnews').not("#" + id).hide();
  $('#' + id).show();
}

var ShowHideUltimeleArticoleStatus = 0;
function ShowHideUltimeleArticole(el)
{
  if (ShowHideUltimeleArticoleStatus == 0)
  {$('#UltimeleArticole .change').show();
    $(el).html('&laquo; Arata mai putine');
    ShowHideUltimeleArticoleStatus = 1;
  }
  else
  {$('#UltimeleArticole .change').hide();
    $(el).html('&raquo; Arata mai multe');
    ShowHideUltimeleArticoleStatus = 0;
  }
}

function SwitchBox(elementID)
{
  $('#PanouMesaje li').each(
  function()
   {if ($(this).hasClass(elementID))
      {$(this).addClass('active');}
     else
      {$(this).removeClass('active');}
   });

  $('#ColoanaDreapta .SwitchBox').each(
  function()
    { 
      if ($(this).attr('id') == elementID)
        {$(this).show();}
       else
        {$(this).hide();}
    });
}
 
 function ShowHide (id, prop)
 {var Div = document.getElementById(id);
   Div.style.display = prop;  
 }

function ChangeClass (id,value)
 {var Div = document.getElementById(id);
   Div.className = value;  
 }
 
function ChangeColor (id, color)
 {var Div = document.getElementById(id);
   Div.style.background = color;  
 }
 
function ChangePic (id , source)
 {var DIV = document.getElementById(id);
   DIV.src = source; 
 }
 
function ChangeText (id , Text)
 {document.getElementById(id).innerHTML = Text;  
 }
 
function ChangeLink (id, Link, Text)
 {var DIV = document.getElementById(id);
   DIV.href = Link;
   DIV.title = Text;
 }
 
 function Ascunde()
{var word = document.getElementById("searchword");
 if (word.value == 'Scrie si apasa enter ...') word.value ='';
}

function Arata()
{var word = document.getElementById("searchword");
 if (word.value == '') word.value ='Scrie si apasa enter ...';
}


function ShowHideComm(website,id)
{var Comentariu = document.getElementById('Comentariu-'+id);
  var Icon = document.getElementById('Icon-'+id);
  if (Icon.title == 'Ascunde Comentariu')
   {Icon.title = 'Arata Comentariu';
     Icon.alt = 'Arata Comentariu';
	 Icon.src = website+'images/max_box.jpg';
	 Comentariu.style.display = 'none';
   }
  else
   {Icon.title = 'Ascunde Comentariu';
     Icon.alt = 'Ascunde Comentariu';
	 Icon.src = website+'images/mini_box.jpg';
	 Comentariu.style.display = 'block';
   }
}

function ReplicaComm(id)
{$('.comment_innerform').each(function() {$(this).hide();});
  var com_form = $('#adauga_comentariu').detach();
  com_form.appendTo('#Form-'+id);
  $('#Form-'+id).each(function() {$(this).show();});
  $('#comment_form_ComentID').each(function() {$(this).val(id);});
}


function AscundeReplica(id)
{var com_form = $('#adauga_comentariu').detach();
  com_form.appendTo('#default_comment_form_location');
  $('#Form-'+id).each(function() {$(this).hide();});
  $('#comment_form_ComentID').each(function() {$(this).val(0);});
}

function ValidareDate()
{var LastID = document.getElementById('LastID').value;

     var Nick = document.getElementById('CommNume').value;
     var Email = document.getElementById('CommEmail').value;
	 var Text = document.getElementById('CommTextarea').value;

 if (Nick.length > 0 && Email.length>0 && Text.length>0)
  {if (Email.indexOf("@") >= 0 && Email.indexOf(".") >= 0 && Email.length > 5 )
     {return true;}
	else
	 {alert('Adresa de email nu este valida');}     
  }
 else
  alert ('Toate campurile trebuie sa fie completate!');
 return false;
}

function DisplayOptions (nume, prop)
 {var DIV = document.getElementById('TopOverContent');
   var DivOver = document.getElementById(nume);
   var DivContent = document.getElementById(nume+'-content');
   DIV.innerHTML = '<div id="CloseBut"><a href="javascript:ShowHide('+"'TopOverContent','none'"+');">close x</a></div>' + DivContent.innerHTML;
   DIV.style.display = prop;
   var LeftDist = DivOver.offsetLeft + 20;
   if (LeftDist + 272 > 930 ) LeftDist = 930-272;
   LeftDist = LeftDist + 'px';
   DIV.style.left = LeftDist;
 }

function SwitchOptions(nume, vmod)
{var divName = nume+'-content';
  $('.top_popup').each(function()
    {if ($(this).attr('id') == divName && vmod == 'show')
       {$(this).show();}
      else
       {$(this).hide();}
    });
}
function Font(increment)
{
  $('#Articol-Main-Body, #Articol-Main-Body *').each(function()
  {
    var originalFontSize = parseFloat($(this).css('font-size'));
    if ((originalFontSize < 25 && increment > 0) || (originalFontSize > 13 && increment < 0))
    {var originalLineHeightSize = parseFloat($(this).css('line-height'));
      $(this).css('font-size', parseFloat(originalFontSize + increment) + 'px');
      $(this).css('line-height', parseFloat(originalLineHeightSize + increment) + 'px');
    }
  });
/*
  var Size = ArticolBody.style.fontSize;
  var FontArray = Size.split("px");
  Size = parseInt(FontArray[0]);
  Size += increment;
  if (Size >30) Size = 30;
  if (Size <8) Size = 8;
  ArticolBody.style.fontSize = Size+"px";
  
  var elements = ArticolBody.getElementsByTagName('strong');
  for (i = 0; i<elements.length; i++)
   {elements[i].style.fontSize = Size+"px";
   }
  
  var elements = ArticolBody.getElementsByTagName('b');
  for (i = 0; i<elements.length; i++)
   {elements[i].style.fontSize = Size+"px";
   }
  
  var elements = ArticolBody.getElementsByTagName('i');
  for (i = 0; i<elements.length; i++)
   {elements[i].style.fontSize = Size+"px";
   }
  
  var elements = ArticolBody.getElementsByTagName('a');
  for (i = 0; i<elements.length; i++)
   {elements[i].style.fontSize = Size+"px";
   }*/
}


function UltimeleArticole (Class, Num)
 { 
   for (i=4; i<=Num; i++)
    document.getElementById('Articol-'+i).className = Class;
	
	var DIV = document.getElementById('Ultimele-Articole-But');
	if (Class == 'Vizibil')
	 {DIV.innerHTML = '<a href="javascript:UltimeleArticole('+"'Ascuns','"+ Num +"'"+');">Arata mai putine</a>'; 
	 }
	else
	 {DIV.innerHTML = '<a href="javascript:UltimeleArticole('+"'Vizibil','"+ Num +"'"+');">Arata mai multe</a>';}
 }

function popup(message,URL) {
	
	var return_value = confirm(message);

	// TEST TO SEE IF TRUE|FALSE RETURNED
	if (return_value == true) {
		window.location = URL;
	} 
}

 function StergeAnunt(ID)
  {var params = 'ID='+ID;
    var myAjax = new Ajax.Request( 'http://www.antidotul.ro/anunturi/sterge.php', 
		{method: 'get', 
			parameters: params, 
      onCreate: function() 
       {$('StergeLink'+ID).update('wait...');},
			onComplete: function(originalRequest) 
        {var Resp = originalRequest.responseText;

          if (Resp == '1')
           {$('Anunt'+ID).remove();}
          else
           {$('StergeLink'+ID).update(Resp);}
        }
    });
  }
 
 function StergeComentariu(ID)
  {var params = 'ID='+ID;
  
    var myAjax = new Ajax.Request( 'http://www.antidotul.ro/sterge-comentariu.php', 
		{method: 'get', 
			parameters: params, 
      onCreate: function() 
       {$('StergeLink-'+ID).update('wait...'); 
         
       },
			onComplete: function(originalRequest) 
        {var Resp = originalRequest.responseText;
          if (Resp == '1')
           {$('Comentariu-'+ID).parentNode.remove(); 
           }
          else
           {$('StergeLink-'+ID).update(Resp);}
        }
    });
  }


function getFormFields(FormID)
{var FormValues = $('#' + FormID + ' *').serializeArray();
  /*for(var i=0; i<FormValues.length; i++)
  { FormValues[i]['value'].replace('"','\"');
  }*/

  var params = 'RequestType=form&RequestFileName='+FormID+'&FormFields='+encodeURIComponent($.toJSON(FormValues));
  return params;
}

function submitForm(FormID)
{ 
  globalRequest($('#'+FormID+' .response'),getFormFields(FormID));
}

function globalRequest(el,Params)
{$.ajax({
        url : '/ajax-request.php',
        dataType : 'json',
        type: 'POST',
        data: Params,
        beforeSend:function()
        {try
           {el.html('Va rugam asteptati...');
             el.show();
           }
          catch(e){}
        },
        success : function(responseData, textStatus, XMLHttpRequest)
        {
          //alert(XMLHttpRequest.responseText);
          try
          {
            if (responseData.error == 1)
              {
                //alert(responseData.error_message);
                return;
              }
          } catch(e) {}

          try
          {
            if (responseData.ResponseType == 'form')
              {
                if (responseData.bad_fields_count > 0)
                  {
                    //$('.tooltip').each(function() { $(this).remove(); });
                    for (var i=0; i<responseData.bad_fields_count; i++)
                      {
                        var input_el = $('#'+responseData.SourceFile+' .field_'+responseData.bad_fields[i].key);
                        input_el.attr('title',responseData.bad_fields[i].message);
                        input_el.tooltip({position: "center right",offset: [-2, 10],effect: "fade",opacity: 0.9});
                        try
                        {
                          var toolTip = input_el.data("tooltip").getTip();
                          toolTip.html(responseData.bad_fields[i].message);
                        }
                        catch(e){}
                        
                        input_el.data("tooltip").show();
                        input_el.parent().addClass('wrong');
                      }

                    $('#'+responseData.SourceFile+' .wrong').each(function() {
                      var this_name = $(this).children(".field").attr('name');
                      var is_bad = false;
                      for (var i=0; i<responseData.bad_fields_count; i++)
                        {
                          if (responseData.bad_fields[i].key == this_name)
                            is_bad = true;
                        }
                      if (!is_bad)
                      {var input_el = $(this);
                        input_el.removeClass('wrong');
                        input_el.attr('title','');
                        $(this).children(".field").data("tooltip").getTip().remove();

                      }
                    });
                  }
                 else
                  {
                     $('#'+responseData.SourceFile+' .wrong').removeClass('wrong');

                  }
              }

            if (el)
              {
                if (responseData.message)
                  {el.text(responseData.message);
                    if (responseData.bad_message == 1)
                      {el.addClass('red');}
                    else
                      {el.removeClass('red');}
                  }
                else
                  {
                    el.hide();
                  }
              }
            if (responseData.js) {eval(responseData.js);}
            if (responseData.url) 
             {window.location = responseData.url;
               return;
             }
          }
          catch(e) 
          {alert(e.message);
          }
        },
        error : function(XMLHttpRequest, textStatus, errorThrown) {
          alert(errorThrown)
        }
      });
}



function requestPermisionBeforeRequest(message, el, Params) {

	var return_value = confirm(message);

	// TEST TO SEE IF TRUE|FALSE RETURNED
	if (return_value == true) {
		globalRequest(el,Params);
	}
}


/* Poll */

// Global variable definitions
// DB column numbers
var OPT_ID = 0;
var OPT_TITLE = 1;
var OPT_VOTES = 2;

var votedID;

$(document).ready(function()
{
  try
  {
    $("#poll").submit(formProcess); // setup the submit handler

    if ($("#poll-results").length > 0 ) {
      animateResults();
    }

    if ($.cookie('vote_id')) {
      $("#poll-container").empty();
      votedID = $.cookie('vote_id');
      $.getJSON("poll.php?vote=none",loadResults);
    }
    
  }
  catch(ex) {}
});

function formProcess(event){
  event.preventDefault();

  var id = $("input[@name='poll']:checked").attr("value");
  id = id.replace("opt",'');

  $("#poll-container").fadeOut("slow",function(){
    $(this).empty();

    votedID = id;
    $.getJSON("poll.php?vote="+id,loadResults);

    $.cookie('vote_id', id, {expires: 365});
    });
}

function switchYoutubeVideo(url)
{
  $(".youtube .leftvideo iframe").attr('src', url);
}


(function ($) {
    $.fn.vAlign = function() 
    {
        return this.each(function(i)
        {
	   
	   var elh = $(this).height(); //new element height
	   var ph = $(this).parent().height(); //parent height
           //alert($(this).attr('src') + " -> " + $(this).width() + " x " + elh + " - " + ph);
           //alert(el)
	   if(elh > ph) 
           { 
               devider = 2;
               
               if (elh > ph * 3) devider = 8;
               else if (elh > ph * 2.5) devider = 6;
               else if (elh > ph * 1.5) devider = 4;
               
               var nh = (ph - elh) / devider; //new margin to apply
               
               var diff = ph - nh - elh ;
               
               //alert($(this).attr("title") + diff);
               
               if (diff > 0) nh += diff;
               
               //alert($(this)+ nh);
	       $(this).css('margin-top', nh + "px");
	   }
	   
	   
        });
     };
})(jQuery);
