﻿function mineAddEvent(eventType, func, bool, elem){
	  if(window.addEventListener) elem.addEventListener(eventType, func, bool)
	  else if(window.attachEvent) {
		  var evType = 'on' + eventType;
		  elem.attachEvent(evType, func);
	  }
}




  function openWindowNow(event) {
    	event = event ? event : window.event;
    	event.stopPropagation ? event.stopPropagation() : event.cancelBubble = true;
    	var target = event.target ? event.target : event.srcElement;
		
		if(target.getAttribute('longdesc')){
    	    var info = target.getAttribute('longdesc').split(';');
		}
		else if(navigator.userAgent.indexOf('IE') !== -1 && navigator.userAgent.indexOf('Opera') == -1) var info = target.longdesc.split(';');

   	

		document.getElementById('textMessage').innerHTML = info[0];

    	document.getElementById('onNumber').innerHTML = info[1];

    	document.getElementById('onNumber2').innerHTML = info[1];

    	document.getElementById('cost').innerHTML = info[3];

		document.getElementById('download').setAttribute('href', info[4] );


    	oWindow.style.display = 'block';
		oWindow.style.zIndex  = '100';
    	//oWindow.style.display = 'table';



    	var x   = parseInt(document.getElementsByTagName('html')[0].offsetWidth/2);
    	var inX = parseInt(oWindow.offsetWidth/2);
    	var y   = parseInt(window.innerHeight/2);
    	var inY = parseInt(oWindow.offsetHeight/2);



      if(navigator.userAgent.indexOf('IE') !== -1 && navigator.userAgent.indexOf('Opera') == -1){
         y   = parseInt(document.body.clientHeight/2)+document.body.scrollTop;
    	 inY = parseInt(oWindow.clientHeight/2);
      }

    	oWindow.style.left = x-inX+'px';

    	oWindow.style.top  = y-inY+'px';

    	oWindow.style.opacity = '1';

    	oWindow.style.filter = 'alpha(opacity=100)';

    	return false;

  }







  if(navigator.userAgent.indexOf('IE') !== -1 && navigator.userAgent.indexOf('Opera') == -1){
      window.attachEvent('onscroll', function(){

         y   = parseInt(document.body.clientHeight/2)+document.body.scrollTop;

         inY = parseInt(oWindow.clientHeight/2);

         oWindow.style.top  = y-inY+'px';

      });
  }



  oWindow = document.getElementById('openWindow');




  mineAddEvent('mousedown', function(){oWindow.style.display='none'}, false, document.getElementById('closeWindow'));








  blocksWithInfo = document.getElementsByName('beacon');

  buySMSButtons = new Array();

  for(var i=0; i<blocksWithInfo.length; i++) {

     partsOfInfo = blocksWithInfo[i].title.split(';');

     partsOfInfo.push(blocksWithInfo[i].getAttribute('href'));



  	 imageForAttach = document.createElement('img');

  	 imageForAttach.setAttribute('longdesc', partsOfInfo[1]+';'+partsOfInfo[2]+';'+partsOfInfo[3]+';'+partsOfInfo[4]+';'+partsOfInfo[5]);
	 
  	 imageForAttach.setAttribute('src', 'images/smsBuy/buttons/button_sms_ru.gif');

  	 imageForAttach.setAttribute('name', 'buySMSButton');

  	 imageForAttachWrap = document.createElement('p');

  	 imageForAttachWrap.style.cssText = 'margin-bottom: 0; cursor: pointer;';

  	 imageForAttachWrap.setAttribute('align', 'center');

  	 imageForAttachWrap.appendChild(imageForAttach);

  	 sign = document.createElement('p');

  	 sign.style.cssText = 'margin: 0; text-align: center;';

  	 sign.innerHTML = partsOfInfo[3] + ' рублей';  

     document.getElementById(partsOfInfo[0]).appendChild(imageForAttachWrap);

     document.getElementById(partsOfInfo[0]).appendChild(sign);



  	 buySMSButtons.push(imageForAttach);

  }







  for(var i=0; i<buySMSButtons.length; i++) {
	  mineAddEvent('mousedown', openWindowNow, false, buySMSButtons[i]);
  }
