var playerVisible = false;
var videoVisible = false;

function alertSize(c_object) {
	  var myWidth = 0, myHeight = 0;
	  if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth - 355;
		myHeight = window.innerHeight;

		vid = document.getElementById("player3Div");
		if (vid) {
		  	vid.style.left = (window.innerWidth - 420) / 2;
			if (location.href.substr(0,4)!="http") {
			  	vid.style.top = (window.innerHeight - 350) / 2;
			} else {
			  	vid.style.top = getBodyScrollTop() + (window.innerHeight - 350) / 2;
			}
		}
	  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth - 355;
		myHeight = document.documentElement.clientHeight;
	  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth - 355;
		myHeight = document.body.clientHeight - 121;

		vid = document.getElementById("player3Div");
		if (vid) {
		  	vid.style.left = (document.body.clientWidth - 420) / 2;
			if (location.href.substr(0,4)!="http") {
			  	vid.style.top = (document.body.clientHeight - 350) / 2;
			} else {
			  	vid.style.top = getBodyScrollTop() + (document.body.clientHeight - 350) / 2;
			}
		}
	  }
	  window.status = document.body.clientWidth;
	  if (myHeight<580) myHeight = 580;
	  if (playerVisible) myHeight -= 32;
	  if (document.getElementById("top-menu")) myHeight -= 56;
	  //if (document.getElementById("order-menu")) myHeight -= 55;
	  //if (document.getElementById("index-menu")) myHeight -= 40;
	  if (document.getElementById("page-blank")) myHeight -= 30;

	  scr = document.getElementById("scrollable");

	  if (scr) {
		if (location.href.substr(0,4)!="http") {
			scr.style.height = myHeight;
	  	} else {
	  		//scr.style.width = myWidth;
	  		scr.style.overflow = "hidden";
	  	}
	  }
	  
	  if (myWidth < 600) myWidth = 600;
	  if (document.getElementById("playerDiv")) document.sonata2.setVariable('setWidth', myWidth);

	}

www=0;
function openImage(url, w, h){
	www++
	ls=window.open("", "Image"+www, "toolbar=0, menubar=0, status=0, width="+w+", height="+h)
	ls.document.write("<HTML><head><title>Соната. Не только классика etc.</title><style> body {margin:0}</style></head><BODY><a href='javascript:window.close();'><IMG SRC='" + url + "' alt='Закрыть' border='0'></a></body></html>");
	ls.document.close();
	ls.focus();
}

var btn_id = 0;
var btn_path = "";
var btn_play = "";
var btn_stop = "";

function stopSound() {
	document.Player.stop();
	if (btn_id !=0) {
		document.getElementById("btn"+btn_id).src = btn_path + btn_play;
		if (document.getElementById("btns"+btn_id))
			document.getElementById("btns"+btn_id).src = btn_path + btn_stop;
		btn_id = 0;
	}
}

function convertUrl (url) {
	if (location.href.substr(0,4)!="file") {
		return url;
	} else {
		drive = location.href.substr(8,1);
		elements = url.split("/");
		levels = 0;
		url2 = "";
		for (i=0; i<elements.length; i++) {
			if (elements[i]=="..") levels++;
			else url2 = url2 + "/" + elements[i];
		}

		url3 = location.href;
		for (i=0; i<=levels; i++) {
			url3 = url3.substr(0, url3.lastIndexOf("/"));
		}
		return (url3 + url2);
	}
}

function playSound(url, id, path, img_play, img_stop, img2_dis, img2_en) {
	if (id == btn_id) {
		stopSound();
		return;
	}
	if (location.href.substr(0,4)!="file") {
		url2 = url;
	} else {
		url2 = url;
		drive = location.href.substr(8,1);
		paths = path.split("/");
		levels = paths.length-1;
		url2 = url2.substr(3*levels, url2.length);
		url3 = location.href;
		for (i=0; i<=levels; i++) {
			url3 = url3.substr(0, url3.lastIndexOf("/"));
		}
		url2 = url3 + "/" + url2;
		url2 = unescape(url2);
	}
	//alert(url2);
	document.Player.Open(url2);
	if (btn_id !=0) {
		document.getElementById("btn"+btn_id).src = path + img_play;
	}
	document.getElementById("btn"+id).src = path + img_stop;
	
	if ((img2_en != "") && (img2_en != undefined)) {
		document.getElementById("btns"+id).src = path + img2_en;
	};
	btn_path = path;
	btn_id = id;
	btn_play = img_play;
	btn_stop = img2_dis;
}

function soundStopped() {
	if (btn_id !=0) {
		document.getElementById("btn"+btn_id).src = btn_path + btn_play;
		if ((btn_stop != "") && (btn_stop != undefined)) {
			document.getElementById("btns"+btn_id).src = btn_path + btn_stop;
		}
		btn_id = 0;
	}
}

function playMusic(url, duration, desc){
	if (videoVisible) stopVideo();
	playerVisible = true;
	alertSize();
	document.getElementById("playerDiv").style.display = "block";
	document.sonata2.setVariable('outMessage', "play|"+url+"|"+duration+"|"+desc);
};

function playVideo(url, desc, rootPath){
	alertSize();
	if (playerVisible) stopMusic();
	url = convertUrl(url);
	//alert(url);
	document.getElementById("player3Div").style.display = "block";
	document.sonata3.setVariable('skinPath', rootPath + "img/");
	document.sonata3.setVariable('outMessage', "play|"+url+"|0|"+desc);
	videoVisible = true;
};

function stopMusic(){
	document.sonata2.setVariable('outMessage', "stop");
};

function stopVideo(){
	document.sonata3.setVariable('outMessage', "stop");
	document.getElementById("player3Div").style.display = "none";
	videoVisible = false;
};

var isInternetExplorer = navigator.appName.indexOf("Microsoft") != -1;
// Handle all the FSCommand messages in a Flash movie.
function sonata2_DoFSCommand(command, args) {
	var sonata2Obj = isInternetExplorer ? document.all.sonata2 : document.sonata2;
	if (command == "closePlayer") {
		document.getElementById("playerDiv").style.display = "none";
		playerVisible = false;
		alertSize();
	}
}
function sonata3_DoFSCommand(command, args) {
	var sonata3Obj = isInternetExplorer ? document.all.sonata3 : document.sonata3;
	if (command == "closePlayer") {
		document.getElementById("player3Div").style.display = "none";
		videoVisible = false;
	}
}
// Hook for Internet Explorer.
if (navigator.appName && navigator.appName.indexOf("Microsoft") != -1 && navigator.userAgent.indexOf("Windows") != -1 && navigator.userAgent.indexOf("Windows 3.1") == -1) {
	document.write('<script language=\"VBScript\"\>\n');
	document.write('On Error Resume Next\n');
	document.write('Sub sonata2_FSCommand(ByVal command, ByVal args)\n');
	document.write('	Call sonata2_DoFSCommand(command, args)\n');
	document.write('End Sub\n');
	document.write('Sub sonata3_FSCommand(ByVal command, ByVal args)\n');
	document.write('	Call sonata3_DoFSCommand(command, args)\n');
	document.write('End Sub\n');
	document.write('</script\>\n');
}

function sonataExit() {
	if (confirm("Вы действительльно хотите завершить работу с ресурсом «Соната»?")) {
		prot = location.href.substr(0,4);
		if (prot == "http") 
			window.close();
		else 
			location.href = "app:exit";
	}
};

document.onmousedown=mouseDownEvent;
document.onmousemove=mouseMoveEvent;
document.onmouseup=mouseUpEvent;
var dx;
var dy;
var imgDragging = null;

function mouseDownEvent() {
  if (event.srcElement.getAttribute("id") == "playerHead") {
  	//imgDragging = event.srcElement;
  	imgDragging = document.getElementById("player3Div");
	//alert(imgDragging.tagName);
	dx = event.offsetX;
    dy = event.offsetY;
	//alert(dy);
    //imgDragging.style.zIndex = 1;
  } else imgDragging = null;
  event.cancelBubble = true;
  event.returnValue = false;
}

function mouseMoveEvent() {
  if (imgDragging != null) {
  	imgDragging.style.pixelLeft = event.clientX - dx + document.body.scrollLeft;
    imgDragging.style.pixelTop  = event.clientY - dy + document.body.scrollTop;
    event.cancelBubble = true;
    event.returnValue = false;
  }
}

function mouseUpEvent() {
  if (imgDragging != null) {
	//imgDragging.style.zIndex = 0;
    imgDragging = null;
  }
}

function getBodyScrollTop() {
	return self.pageYOffset || (document.documentElement && document.documentElement.scrollTop) || (document.body && document.body.scrollTop);
}

