// JavaScript Document
function getInfo(measure) {
	  var myWidth = 0, myHeight = 0, browserType = '';
	  if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myHeight = window.innerHeight;
		myWidth = window.innerWidth;
		browserType = 'non-ie';
	  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myHeight = document.documentElement.clientHeight;
		myWidth = document.documentElement.clientWidth;
		browserType = 'ie6+';
	  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myHeight = document.body.clientHeight;
		myWidth = document.body.clientWidth;
		browserType = 'ie4+';
	  }
	  if (measure == 'height') {return myHeight;}
	  if (measure == 'width') {return myWidth;}
	}

function backgroundPosition () {
	//////////////
	//////////////
	// BACKGROUND IMAGE
	//////////////
	var docHeight = getInfo('height');
	var docWidth = getInfo('width');
	var browserType = getInfo('browser');
	
	var areDimensionsSet;
		var imageHeightInitial = window.document.getElementById('bg').height;
		var imageWidthInitial = window.document.getElementById('bg').width;
	/*else {
		var imageHeightInitial = window.document.getElementById('bg').height;
		var imageWidthInitial = window.document.getElementById('bg').width;
	}*/
	
	var ratio = imageHeightInitial / imageWidthInitial;
	var screenRatio = docHeight / docWidth;
	
							if(screenRatio>ratio) {
								var imageHeightNew = window.document.getElementById('bg').height = docHeight;
								var imageWidthNew = window.document.getElementById('bg').width = imageHeightNew / ratio;
								window.document.getElementById('bg_holder').style.marginLeft = (docWidth - imageWidthNew)/2;
								areDimensionsSet =1;
								
								//window.document.getElementById('bg').style.display='block';
							}
							else {
								var imageWidthNew = window.document.getElementById('bg').width = window.document.getElementById('bg_holder').width = docWidth;
								var imageHeightNew = window.document.getElementById('bg').height = imageWidthNew * ratio;
								window.document.getElementById('bg').style.marginTop = (docHeight - imageHeightNew);
								areDimensionsSet =1;
									//window.alert('Высота:' + imageHeightInitial + '. Ширина:' + imageWidthInitial+ '. Соотношение:' + ratio + '. Соотношение экрана:' + screenRatio);	
								//window.document.getElementById('bg').style.display='block';
							}	
	
}
function positioning () {
	//REPOSITION THE BACKGROUND
	backgroundPosition ();
	
	
	var docHeight = getInfo('height');
	var docWidth = getInfo('width');
	var browserType = getInfo('browser');

	//DISPLAY EVERYTHING + hide preloader
	//window.document.getElementById('preloader').style.display = 'none';
	//window.document.getElementById('body_content').style.display = '';
	///////////////////
	
	
		
		//////////////
		// БОКОВОЙ ТЭГ
		//////////////
		/*if (docWidth<=1000) {
				window.document.getElementById('tag_right').style.display = 'none';
		}
		else {
				window.document.getElementById('tag_right').style.display = '';
		}*/
		
		//////////////
		// SCROLLBAR
		//////////////
		if (docHeight>802) {
				window.document.getElementById('body').style.overflowY = 'hidden';
		}
		else {
				window.document.getElementById('body').style.overflowY = '';
		}
		
		
		//////////////
		// CONTENTS POSITIONING
		//////////////
		if (docWidth<=990) {
				// window.document.getElementById('siteWidth').style.width = 800 + 20 + 'px';
				/*
				switch (browserType) {
					case (browserType=='non-ie'):window.innerWidth=1000+'px', alert('non-ie');
					case (browserType=='ie6+'):document.documentElement.clientWidth=1000+'px', alert('ie6+');
					case (browserType=='ie4+'):document.body.clientWidth=1000+'px', alert('ie4+');
				}
			alert(browserType.toString());
				*/
				window.document.getElementById('body_content').style.width = 990;
				//window.document.getElementById('main_content').style.width = 871 + 'px';
				window.document.getElementById('main_content').style.left = 40  + 'px';
				window.document.getElementById('top_menu').style.left = 40  + 'px';
		}
		else {
				window.document.getElementById('body').style.width = docWidth + 'px';
				window.document.getElementById('main_content').style.left = (docWidth - 900) / 2 + 'px';
					var leftOffset = window.document.getElementById('main_content').style.left;
					//var top_menu_holder = window.document.getElementById('top_menu_holder').style.width;
				// слева отступ бэкграунда -28
				window.document.getElementById('top_menu').style.left = leftOffset;
				window.document.getElementById('tag_right').style.display = '';
		}
	
	
		
		// CONTENT + SCROLLBARS HEIGHT
		if (docHeight <=773) {
			window.document.getElementById('left_content_bg').style.height = 623 + 'px';
			//SCROLLBAR
			window.document.getElementById('scrollbar_container').style.height = 623 - 30 + 'px';
			window.document.getElementById('scrollbar_content').style.height = 623 - 30 + 'px';
			//RIGHT CONTENT
			window.document.getElementById('right_content_bg').style.height = 623 + 129 + 'px';
		}
		else {
			window.document.getElementById('left_content_bg').style.height = docHeight - 118 + 'px';
			// SCROLLBAR
			window.document.getElementById('scrollbar_container').style.height = docHeight - 148 + 'px';
			window.document.getElementById('scrollbar_content').style.height = docHeight - 148 + 'px';
			//RIGHT CONTENT
			window.document.getElementById('right_content_bg').style.height = docHeight + 11 + 'px';
		}
	
	//window.document.getElementById('body').style.height = docHeight;
	//window.alert(docHeight.toString());
}

function getHightWidthBrowser(measure) {
	  var myWidth = 0, myHeight = 0, browserType = '';
	  if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myHeight = window.innerHeight;
		myWidth = window.innerWidth;
		browserType = 'non-ie';
	  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myHeight = document.documentElement.clientHeight;
		myWidth = document.documentElement.clientWidth;
		browserType = 'ie6+';
	  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myHeight = document.body.clientHeight;
		myWidth = document.body.clientWidth;
		browserType = 'ie4+';
	  }
	  if (measure == 'height') {return myHeight;}
	  if (measure == 'width') {return myWidth;}
	  if (measure == 'browser') {return browserType;}
	}

function popUpPositioning () {
	var docHeight = getHightWidthBrowser('height');
	var docWidth = getHightWidthBrowser('width');
	var browserType = getHightWidthBrowser('browser');
	
	window.document.getElementById('grey_screen').style.height = docHeight + 'px';
	window.document.getElementById('grey_screen').style.width = docWidth + 'px';
	
	var imageHeightInitial = window.document.getElementById('image_url').height;
	var imageWidthInitial = window.document.getElementById('image_url').width;
	var ratio = imageHeightInitial / imageWidthInitial;
	if (imageHeightInitial + 52 > docHeight) {
		 var imageHeight = docHeight - 60; // i intentionally decreased height to leave a small threshold between brouser and image
		 var imageWidth = imageHeight / ratio;
		 window.document.getElementById('image_url').height = imageHeight;
		 window.document.getElementById('image_url').width = imageWidth;
		 if (( navigator.userAgent.indexOf("Opera") !=-1  && navigator.appName == "Opera" ) || ( navigator.userAgent.indexOf("Opera") !=-1  &&  navigator.userAgent.indexOf("MSIE") !=-1)) {window.document.getElementById('image_container').style.width = imageWidth;}
		 //window.document.getElementById('image_url').className = 'pop_up_squeeze';
		 //imageHeightInitial = "0";
		 //window.document.getElementById('image_url').style.height = imageHeight + 'px';
		 //window.document.getElementById('image_url').style.width = imageWidth + 'px';
		 //window.document.getElementById('image_container').style.width = imageWidth + 'px';
	}
	else { // else if (imageHeightInitial + 52 <= docHeight)
		//window.document.getElementById('image_url').height = imageHeightInitial;
		var imageHeight = window.document.getElementById('image_url').height;
		var imageWidth = window.document.getElementById('image_url').width;
		if (( navigator.userAgent.indexOf("Opera") !=-1  && navigator.appName == "Opera" ) || ( navigator.userAgent.indexOf("Opera") !=-1  &&  navigator.userAgent.indexOf("MSIE") !=-1)) {window.document.getElementById('image_container').style.width = imageWidth;}
		
		//
		//var imageStyleHeight = window.document.getElementById('image_url').height;
		//var contHeight = window.document.getElementById('image_container').height;
		//window.alert('Height is ok. \n Image recent height '+imageHeight);
	}
	
	var imageContainerHeight = imageHeight + 52;
	var imageContainerWidth = imageWidth + 20;
	//var imageContainerHeight = getElementHeight('image_container');
	//var imageContainerWidth = getElementWidth('image_container');
	window.document.getElementById('image_container').style.top = (docHeight - imageContainerHeight)/2 + 'px';
	window.document.getElementById('image_container').style.left = (docWidth - imageContainerWidth)/2 + 'px';
	
	window.document.getElementById('spamash_pic_loader_container').style.top = (docHeight - 19)/2 + 'px';
	window.document.getElementById('spamash_pic_loader_container').style.left = (docWidth - 220)/2 + 'px';
}

function radioButtonId(btn) {
    var cnt = -1;
    for (var i=btn.length-1; i > -1; i--) {
        if (btn[i].checked) {cnt = i; i = -1;}
    }
    if (cnt > -1) return btn[cnt].id; // returns radio buttons id (thoough it can return its value etc
    else return null;
}
                  
///// ОБРАЩЕНИЕ К PHP ПРИ ПОМОЩИ AJAX
function phpAjaxAction (phpFileName, currentLocation, pollsDate) {	

var theForm = window.document.getElementById('front_page_poll').poll;
    var selectedRadioId =  radioButtonId(theForm);
                  
			var xmlHttpReq = false;
			var self = this;

			// Mozilla/Safari
			if (window.XMLHttpRequest) {
				self.xmlHttpReq = new XMLHttpRequest();
			}

			// IE
			else if (window.ActiveXObject) {
				self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
			}

			self.xmlHttpReq.open('POST', phpFileName, true);
			self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			self.xmlHttpReq.onreadystatechange = function() {

				if (self.xmlHttpReq.readyState == 4) {
					updatepage(self.xmlHttpReq.responseText, currentLocation);
				}
			}

			//var sentDataArray = new Array;
			//var sendData = 'entryDate=' + currentTimeFunc ();

			var sendData=new Array();
			sendData[0]=selectedRadioId; 
			sendData[1]=pollsDate;

			var sendDataArray = 'sendDataArray='+sendData;
			self.xmlHttpReq.send(sendDataArray);
}

function updatepage(phpResponse, currentLocation){ // This function pastes php responce code to the last but one row in the table
  //if (typeOfActionCompleted == 'uploadPhoto') window.alert(phpResponse);
  //window.location.href = currentLocation;
  window.alert(phpResponse);
}
///// КОНЕЦ ОБРАЩЕНИЯ К PHP ПРИ ПОМОЩИ AJAX

// VIX CUSTOM SCROLLER FUNC (GITHUB LIVEPIPE)
function addingGitHubScrollBar () {
	var scrollbar = new Control.ScrollBar('scrollbar_content','scrollbar_track');  
  
/*	$('scroll_down_50').observe('click',function(event){  
		scrollbar.scrollBy(-50);  
		event.stop();  
	});  
	  
	$('scroll_up_50').observe('click',function(event){  
		scrollbar.scrollBy(50);  
		event.stop();  
	});  
	  
	$('scroll_top').observe('click',function(event){  
		scrollbar.scrollTo('top');  
		event.stop();  
	});  
	  
	$('scroll_bottom').observe('click',function(event){  
		//to animate a scroll operation you can pass true  
		//or a callback that will be called when scrolling is complete  
		scrollbar.scrollTo('bottom',function(){  
			if(typeof(console) != "undefined")  
				console.log('Finished scrolling to bottom.');  
		});  
		event.stop();  
	});  
	  
	$('scroll_second').observe('click',function(event){  
		//you can pass a number or element to scroll to  
		//if you pass an element, it will be centered, unless it is  
		//near the bottom of the container  
		scrollbar.scrollTo($('second_subhead'));  
		event.stop();  
	});  
	  
	$('scroll_third').observe('click',function(event){  
		//passing true will animate the scroll  
		scrollbar.scrollTo($('third_subhead'),true);  
		event.stop();  
	});  
	  
	$('scroll_insert').observe('click',function(event){  
		$('scrollbar_content').insert('<p><B>Inserted: ' + $('repeat').innerHTML + '</B></p>');  
		//you only need to call this if ajax or dom operations modify the layout  
		//this is automatically called when the window resizes  
		scrollbar.recalculateLayout();  
		event.stop();  
	});  */
}