(function($) {
  var userAgent = navigator.userAgent.toLowerCase();
  var ie_version = -1;
  if (navigator.appName == 'Microsoft Internet Explorer') {
   var re = new RegExp("msie ([0-9]{1,}[\.0-9]{0,})");
   if (re.exec(userAgent) != null)
   ie_version = parseFloat(RegExp.$1);
  }
  $.browser = {
    version: (ie_version>-1)?ie_version:(userAgent.match( /.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/ ) || [0,'0'])[1],
    safari: /webkit/.test( userAgent ),
    opera: /opera/.test( userAgent ),
    msie: /msie/.test( userAgent ) && !/opera/.test( userAgent ),
    mozilla: /mozilla/.test( userAgent ) && !/(compatible|webkit)/.test( userAgent )
  };
})(jQuery);


jQuery.preloadImages = function(){
	for(var i = 0; i<arguments.length; i++){
		jQuery("<img>").attr("src", arguments[i]);
	}
};
$.preloadImages("/bitrix/templates/fomedia_index/images/pop_up_top_bg.gif", "/bitrix/templates/fomedia_index/images/pop_up_bottom_bg.gif", "/bitrix/templates/fomedia_index/images/button_close.gif", "/bitrix/templates/fomedia_index/images/button.gif");


function  getPageSize(){
	var xScroll, yScroll;

	if (window.innerHeight && window.scrollMaxY) {
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else if (document.documentElement && document.documentElement.scrollHeight > document.documentElement.offsetHeight){ // Explorer 6 strict mode
		xScroll = document.documentElement.scrollWidth;
		yScroll = document.documentElement.scrollHeight;
	} else { // Explorer Mac...would also work in Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}

	var windowWidth, windowHeight;
	if (self.innerHeight) { // all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}

	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else {
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}

	return [pageWidth,pageHeight,windowWidth,windowHeight];
}

function getBodyScrollTop(){
	return self.pageYOffset || (document.documentElement && document.documentElement.scrollTop) || (document.body && document.body.scrollTop);
}


function show_feedback(){
var tmp = Array();
tmp = getPageSize();

	if(tmp['3'] > 612){
		if($.browser.version == 6){
			$('#feedback_pop_up').addClass('ie6_fixed');
		}else{
		document.getElementById('feedback_pop_up').style.top = ((tmp['3']-612)/2)+'px';		
		}
	}else{
		if($.browser.version == 6){
			document.getElementById('feedback_pop_up').style.top = (getBodyScrollTop())+'px';
		}else{
			document.getElementById('feedback_pop_up').style.top = (getBodyScrollTop())+'px';
		}	
	}
	document.getElementById('feedback_pop_up').style.display = 'block';
	show_shadow();
}

function show_add_question(){
var tmp = Array();
tmp = getPageSize();

	if(tmp['3'] > 612){
		if($.browser.version == 6){
			$('#add_question_pop_up').addClass('ie6_fixed');
		}else{
		document.getElementById('add_question_pop_up').style.top = ((tmp['3']-612)/2)+'px';		
		}
	}else{
		if($.browser.version == 6){
			document.getElementById('add_question_pop_up').style.top = (getBodyScrollTop())+'px';
		}else{
			document.getElementById('add_question_pop_up').style.top = (getBodyScrollTop())+'px';
		}	
	}
	document.getElementById('add_question_pop_up').style.display = 'block';
	show_shadow();
}


function show_order_call(){
var tmp = Array();
tmp = getPageSize();
//alert($('#widget_phone').val());
if ($('#widget_phone').val() != '+7 ....') $('#call_phone').val($('#widget_phone').val());

	if(tmp['3'] > 396){
		if($.browser.version == 6){
			$('#order_call_pop_up').addClass('ie6_fixed');
		}else{
		document.getElementById('order_call_pop_up').style.top = ((tmp['3']-396)/2)+'px';		
		}
	}else{
		if($.browser.version == 6){
			document.getElementById('order_call_pop_up').style.top = (getBodyScrollTop())+'px';
		}else{
			document.getElementById('order_call_pop_up').style.top = (getBodyScrollTop())+'px';
		}	
	}
	document.getElementById('order_call_pop_up').style.display = 'block';
	show_shadow();
}

function hide_feedback(){
	document.getElementById('feedback_pop_up').style.display = 'none';
}
function hide_add_question(){
	document.getElementById('add_question_pop_up').style.display = 'none';
}
function hide_order_call(){
	document.getElementById('order_call_pop_up').style.display = 'none';
}


function show_shadow(){
    document.getElementById('overlay').style.display = 'block';
}
function hide_shadow(){
    document.getElementById('overlay').style.display = 'none';
}


function show_faq_cur(obj){
	$(obj).parent().parent().parent().css('display', 'none');
	$(obj).parent().parent().parent().parent().find('.faq_cur').css('display', 'block');
}
function show_faq(obj){
	$(obj).parent().parent().css('display', 'none');
	$(obj).parent().parent().parent().find('.faq_first').addClass('faq_visited');
	$(obj).parent().parent().parent().find('.faq_first').css('display', 'block');
}
