﻿// ===========================================================================
// subvoice.hu
// common JavaScript functions
// IE5.5, IE6SP2, IE7, FF2, Opera9, Safari3 on XPSP2 tested
// ===========================================================================


// toggleVisibility - Toggle the visibility of an item
// -> id - ID of element to toggle

function toggleVisibility(id)
{
  var obj=document.getElementById(id);
  obj.style.display=(obj.style.display!='none'?'none':'');
}


// mailTo - Compose mail address and trigger e-mail client
// -> id - ID of comment

function mailTo(id)
{
  var musr=document.getElementById('cmt_musr_'+id).innerHTML;
  var mdom=document.getElementById('cmt_mdom_'+id).innerHTML;
  window.location='mailto:'+musr+'@'+mdom;
}


// txtLength - Limit text length for TEXTAREA
// -> field - TEXTAREA object
//    countField - Field for displaying characters remaining
//    maxLimit - Maximum length of TEXTAREA field

function txtLength(field,countField,maxLimit)
{
  if (field.value.length>maxLimit) field.value=field.value.substring(0,maxLimit);
  else countField.value='(még '+(maxLimit-field.value.length)+' karakter írható be)';
}


// ===========================================================================
// Gallery popup div
// ===========================================================================


var objGal,objFreeze,objFrame;
var iDivHeight,iOpacity;
var bIE=/msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent) && (document.documentMode?document.documentMode<8:true);


// centerGallery - Center gallery DIV (vertically)
// -> iNewHeight - New height of gallery DIV to set, 0 means no change

function centerGallery(iNewHeight)
{
  // Centering is actually done by CSS, this only sets height and margin-top
  if (iNewHeight)
  {
    objGal.style.height=iNewHeight+'px';
    objGal.style.marginTop=-1*(iNewHeight/2)+'px';
  }
}


// initGallery - Initialize gallery DIV positioner
// Call from <body> onload event
// -> id - ID of DIV element

function initGallery(id)
{
  // Store element IDs for further reference
  objGal=document.getElementById(id);
  objFreeze=document.getElementById('freeze');
  objFrame=document.getElementById('popupcontent');
  
  // Save height and opacity specified in CSS
  iDivHeight=objGal.offsetHeight; iOpacity=.5;

  // Show gallery DIV
  if (!bIE) centerGallery(30); else centerGallery(iDivHeight);
  toggleVisibility(id);
  objGal.style.visibility='visible';
}


// showGallery - Display gallery window with selected image
// -> sImage - Image file name to display
//    sClass - Class to set for the image
//    sDescription - Description of image

function showGallery(sImage,sClass,sDescription)
{
  // Initialize freeze and gallery DIV
  objFreeze.style.display='block';
  if (!bIE)
  {
    objGal.style.height='30px';
    objFreeze.style.opacity=0;
  }
  toggleVisibility(objGal.id);

  // Show gallery DIV, fade if not IE
  if (!bIE) fade(30,iDivHeight,98,sImage,sClass,sDescription);
  else
  {
    showImage(sImage,sClass,sDescription);
    objFrame.style.display='block';
  }
}


// closeGallery - Hides the gallery popup

function closeGallery()
{
  // Hide gallery DIV, fade if not IE
  objFrame.style.display='none';
  if (!bIE) fade(iDivHeight,30,-98,'');
  else
  {
    toggleVisibility(objGal.id);
    objFreeze.style.display='none';
  }
}


// showImage - Shows image in gallery popup
// -> sImage - Image file name to show
//    sClass - Class to set for the image
//    sDescription - Description of image

function showImage(sImage,sClass,sDescription)
{
  objImage=document.getElementById('show');
  objImage.className=sClass;
  document.getElementById('description').innerHTML=sDescription;
  objImage.src=sImage;
}


// ===========================================================================
// Gallery fadein/fadeout
// Disabled in MSIE due to very poor performance
// ===========================================================================


var fadeFrom,fadeTo,fadeStep,fadeEnd,image,cls,desc;


// fade - Perform fading and height expanding
// -> iFrom - Start height of gallery DIV
//    iTo - End height of gallery DIV
//    iStep - Fade step for height
//    sImage - Image file name to show on timeout
//    sClass - Class to set for the image
//    sDescription - Description of image

function fade(iFrom,iTo,iStep,sImage,sClass,sDescription)
{
  fadeFrom=iFrom; fadeTo=iTo; fadeStep=iStep; image=sImage; cls=sClass; desc=sDescription;
  fadeEnd=false; fadeTimer();
}


// fadeTimer - Fade callback on timer

function fadeTimer()
{
  // Step fading
  fadeFrom+=fadeStep;
  if (fadeStep<0) if (fadeFrom<=fadeTo) fadeFrom=fadeTo;
  if (fadeStep>0) if (fadeFrom>=fadeTo) fadeFrom=fadeTo;
  if (fadeFrom==fadeTo) fadeEnd=true;
  if (!fadeEnd) setTimeout("fadeTimer()",50);
  
  // Fade opacity and gradually expand window
  objGal.style.opacity=(fadeFrom/iDivHeight);
  objGal.style.filter='alpha(opacity='+fadeFrom/iDivHeight*100+')';
  centerGallery(fadeFrom);
  objFreeze.style.opacity=(fadeFrom/iDivHeight)*iOpacity;
  objFreeze.style.filter='alpha(opacity='+fadeFrom/iDivHeight*iOpacity*100+')';

  if (fadeEnd)
  {
    // Fade in ended, show image
    if (fadeStep>0)
    {
      showImage(image,cls,desc);
      objFrame.style.display='block';
    }
    
    // Fade out ended, hide gallery and freeze DIV
    else
    {
      toggleVisibility(objGal.id);
      objFreeze.style.display='none';
      showImage('','','');
    }
  }
}


// ===========================================================================
// Cookie handling
// ===========================================================================


function setCookie(sName,sValue,iDays)
{
	if (iDays)
	{
		var oDate=new Date();
		oDate.setTime(oDate.getTime()+(iDays*24*60*60*1000));
		var sExpires="; expires="+oDate.toGMTString();
	}
	else var sExpires="";
	document.cookie=sName+"="+encodeURIComponent(sValue)+sExpires+"; path=/";
}


function getCookie(sName)
{
	var aCookies=document.cookie.split(';');
	for (var i=0; i<aCookies.length; i++)
	{
		var sCookie=aCookies[i];
		while (sCookie.charAt(0)==' ') sCookie=sCookie.substring(1,sCookie.length);
		if (sCookie.indexOf(sName+"=")==0) return decodeURIComponent(sCookie.substring(sName.length+1,sCookie.length));
	}
	return '';
}

// ===========================================================================
// Szanomabics Pank
// ===========================================================================


var oSzan,oGo,oFakk,oOma,oTelju,oElse;
var bCanPank=true;


function telju()
{
  txtLength(oTelju,oElse,2000);
}


function szanomabics()
{
  var sSzan=getCookie('sv_cmt_nick');
  var sGo=getCookie('sv_cmt_mail');
  
  objFreeze=document.getElementById('freeze');
  oSzan=document.createElement('input');
  oSzan.className='szan';
  oSzan.value=sSzan;
  $('#szan').append(oSzan);
  
  oGo=document.createElement('input');
  oGo.className='go';
  oGo.value=sGo;
  $('#go').append(oGo);
  
  oFakk=document.createElement('input');
  oFakk.className='fakk';
  $('#fakk').append(oFakk);
  
  oOma=document.createElement('input');
  oOma.className='oma';
  oOma.type='checkbox';
  $('#oma').append(oOma);
  oOma.checked=(sSzan!='' || sGo!='');
  
  oElse=document.createElement('input');
  oElse.className='else';
  $(oElse).focus(function(oEvent) { this.blur(); });
  $('#else').append(oElse);
  
  oTelju=document.createElement('textarea');
  oTelju.className='telju';
  oTelju.rows=20;
  oTelju.cols=60;
  $(oTelju).focus(telju);
  $(oTelju).keyup(telju);
  $(oTelju).keydown(telju);
  $(oTelju).blur(function(oEvent) { oElse.value=''; });
  $('#telju').append(oTelju);
  
  $('#bics').click(pank);
  $(document).ajaxError(function()
  {
    alert("Hálózati hiba, kérlek próbáld újra.");
    bCanPank=true;
    $(document.body).css("cursor","");
    objFreeze.style.display='none';
    objFreeze.style.cursor='';
  });
  
  $('#pank').css('display','none');
  $('#szanomabics').css('display','block');
}


function oma()
{
  setCookie('sv_cmt_nick','',-1);
  setCookie('sv_cmt_mail','',-1);
  oOma.checked=false;
  oSzan.value=oGo.value='';
}


function pank()
{
  if ((oSzan.value=='' || oTelju.value=='') && bCanPank)
  {
    alert('A neved és a hozzászólásod megadása kötelező.');
    return;
  }
  var bOldPank; bCanPank=((bOldPank=bCanPank) && false);
  var sPank=window.location.href.split('#')[0];
  
  if (bOldPank)
  {
    objFreeze=document.getElementById('freeze');
    if (oOma.checked)
    {
      setCookie('sv_cmt_nick',oSzan.value,365);
      setCookie('sv_cmt_mail',oGo.value,365);
    }
    
    objFreeze.style.cursor='wait';
    objFreeze.style.opacity=iOpacity;
    objFreeze.style.filter='alpha(opacity='+(iOpacity*100)+')';
    objFreeze.style.display='block';
    
    $.post(
      '.'
    , {
        bics: sPank
      , szan: oSzan.value
      , go: oGo.value
      , fakk: oFakk.value
      , oma: oOma.checked
      , telju: oTelju.value
      }
    , function(bics)
      {
        window.location.href=sPank+'#komment'+bics.pank;
        window.location.reload();
      }
    , "json");
  }
}
