var mySound = {};
var positionStep = 0; // шаг для прокрутки 2сек
var g_bPlayMode   = false;
var g_iPlayVolume = 5;

var g_bSMLoaded = false;
var g_bSoundCreated = false;

function SavePlayState()
{

  SetCookie ('PlayMode',   getStrFromBool(g_bPlayMode)) ;
  SetCookie ('PlayVolume', g_iPlayVolume) ;

}

function LoadPlayState()
{
  var sPlayMode;

  sPlayMode = GetCookie ('PlayMode',getStrFromBool(g_bPlayMode));

  g_bPlayMode   =  getBoolFromStr(sPlayMode); 
  g_iPlayVolume = GetCookie ('PlayVolume' ,g_iPlayVolume) ;
}

soundManager.url = g_URLPrefix+'/libraries/soundmanager/soundmanager2.swf'; // url swf файла

// инициализируем soundManager
soundManager.onload = function() {
        g_bSMLoaded = true;

        LoadPlayState();

        createSoundControl(document.getElementById('soundControl'));

        UpdateSoundState(document.getElementById('modeControl'),document.getElementById('volumeControl'));

        if (g_bPlayMode)
        {
	  createSound();  	  
        } 

}

// Промотка вперед на 2сек
function next() {
	mySound.position += positionStep;
	if (mySound.position > mySound.duration) mySound.position = mySound.duration;
	mySound.setPosition(mySound.position);
}
// Промотка назад на 2сек
function prev() {
	mySound.position -= positionStep;
	if (mySound.position < 0) mySound.position = 0;
	mySound.setPosition(mySound.position);
}
// Громкость
function setVolume(_vol,el)
{
  if (g_bSoundCreated ) mySound.setVolume(_vol*10);

  g_iPlayVolume=_vol;

  SavePlayState();
  UpdateSoundVolumeState(el.parentNode.parentNode);

}


function createSound()
{
  // создаем объект аудио файла!
  var sURL;
  positionStep = 0; // шаг для прокрутки 1.39сек

  if( (self.location.href.lastIndexOf("rastn/index") == -1) &&
      (self.location.href.lastIndexOf("low/index") == -1) &&
      (self.location.href.lastIndexOf("organisation/index") == -1) &&
      (self.location.href.lastIndexOf("family/index") == -1) &&
      (self.location.href.lastIndexOf("book/index") == -1) &&
      (self.location.href.lastIndexOf("info/index") == -1)
     )
  { 
     sURL= g_URLPrefix+'/libraries/soundmanager/media/glavnew.mp3'; // url mp3 файла
     positionStep = 0;//1*60*1000+39*1000; // шаг для прокрутки 1.39сек
  }
  else 
  {
      sURL= g_URLPrefix+'/libraries/soundmanager/media/rass.mp3'; // url mp3 файла
    positionStep = 0; // шаг для прокрутки 1.39сек
  }

  mySound = soundManager.createSound({
  	id: 'aSound',
  	url: sURL,// url mp3 файла
  	volume: g_iPlayVolume*10, // громкость воспроихводимого файла
  	autoLoad: true
  });

  g_bSoundCreated=true;

  setTimeout(StartSound,750);


}

function StartSound()
{
  if (g_bSoundCreated)
  {
    if (positionStep>0) 
    {
           mySound.position = positionStep;
  	if (mySound.position > mySound.duration) mySound.position = mySound.duration;
  	mySound.setPosition(mySound.position);
    }

    mySound.play();

  } else
  {
     if (g_bSMLoaded)
     {
	  createSound();
     } else
     {
          //waiting while Sound manager loaded
	  setTimeout(StartSound,750);
     }

  }

}



function ToggleStartSound(el)
{


  g_bPlayMode = !g_bPlayMode;
  SavePlayState();
  UpdateSoundModeState(el);


  if (g_bSoundCreated)
  {
    mySound.togglePause();
  } 
  else
  {
    StartSound();
  }


}

function UpdateSoundState(elMode,elVolume)
{
  UpdateSoundModeState(elMode);
  UpdateSoundVolumeState(elVolume);
}

function UpdateSoundModeState(el)
{
  if (g_bPlayMode)
  {
    el.innerHTML="Отключить звук";
  } else
  {
    el.innerHTML="Включить звук";
  }

}

function UpdateSoundVolumeState(el)
{

	var _ol = el;//document.getElementsByID('ol')[0];
	var _a = _ol.getElementsByTagName('a');
	for (var i=0; i < _a.length; i++) {
		if (i < g_iPlayVolume) _a[i].className = 'active';
		else _a[i].className = '';
	}

}


function createSoundControl(el)
{
  el.style.visibility='visible';

}


function getStrFromBool(value)
{
  if (value)  return '1' ;
  else        return '0' ;

}

function getBoolFromStr(value)
{
  if (value=='1')  return true ;
  else        return false ;

}


function SetCookie (name, value) 
{var largeExpDate = new Date ();
 largeExpDate.setTime(largeExpDate.getTime() + (365 * 24 * 3600 * 1000));    
 document.cookie = name + "=" + escape(value) +
        "; expires=" + largeExpDate.toGMTString()+
        "; path=/";        
}

function GetCookie(name,defValue) {
var cookie = " " + document.cookie;
var search = " " + name + "=";
var setStr = defValue;
var offset = 0;
var end = 0;
	if (cookie.length > 0) {
		offset = cookie.indexOf(search);
		if (offset != -1) {
			offset += search.length;
			end = cookie.indexOf(";", offset)
			if (end == -1) {
				end = cookie.length;
			}
			setStr = unescape(cookie.substring(offset, end));
		}
	} else
        setStr=defValue;

	return(setStr);
}

