/**
 * @author <a href="http://meia5.com">Media5</a> - <a href="mailto:banzalik@media5.com">Бойченко Александр</a>
 * @description скрипты для m5 css framework
 */

 /**
 * @description полная загрузка страницы, картинки при этом уже загрузились
 */
window.onload=documentLoaded; // присваиваем функцию documentLoaded

/**
 * @description вызов функций при полной загрузке страницы, картинки при этом уже загрузились
 */
function documentLoaded(){

}


/**
 * @description вызов функций при полной загрузке DOM дерева, картинки при этом еще не загрузились
 */
$(document).ready(function(){
			autoclearInput();
			m5formsWidthNormalize();
			popups();
			faq();
			li_last();
})


function li_last() {$('#topmenu li:last').css({'paddingRight':0})}


/**
 * @description очистка инпутов, при клике на них
 */
function autoclearInput(){
	$(".autoclear").each(function(){
		$(this).attr("defaultvalue",$(this).attr("value")); // записываем в defaultvalue значение по умолчанию, необходимо для проверки нового стекста со старым
	});

	$(".autoclear").click(function(){
		if ($(this).attr("value")==$(this).attr("defaultvalue")) { // проверяем совпадают ли value и defaultvalue
			$(this).attr("value", ""); // сбрасываем значение value
		}
	})
}


/**
 * @description выравнивание input[type=text], input[type=password], textarea
 */
function m5formsWidthNormalize(){
	if ($.browser.msie && $.browser.version<7) {
		$("select.m5formsNormalize").each(function(i){
			var m5formnormalizepadding=Math.ceil(Number(String($(this).css("padding-left")).slice(0,-2)))+Math.ceil(Number(String($(this).css("padding-right")).slice(0,-2)));
			var m5formnormalizeborder= Math.ceil(Number(String($(this).css("border-left-width")).slice(0,-2)))+Math.ceil(Number(String($(this).css("border-right-width")).slice(0,-2)));
			var m5formnormalizewidth=Math.ceil(Number($(this).width()))+m5formnormalizepadding+m5formnormalizeborder*2;
			$(this).width(m5formnormalizewidth);
		})
	} else {
		$("input[type=text].m5formsNormalize, input[type=password].m5formsNormalize, textarea.m5formsNormalize").each(function(i){
			var m5formnormalizepadding=Math.ceil(Number(String($(this).css("padding-left")).slice(0,-2)))+Math.ceil(Number(String($(this).css("padding-right")).slice(0,-2)));
			var m5formnormalizewidth=Math.ceil(Number($(this).width()))-m5formnormalizepadding;
			$(this).width(m5formnormalizewidth);
			})
		$("select.m5formsNormalize").each(function(i){
			var m5formnormalizepadding=Math.ceil(Number(String($(this).css("padding-left")).slice(0,-2)))+Math.ceil(Number(String($(this).css("padding-right")).slice(0,-2)));
			var m5formnormalizeborder= Math.ceil(Number(String($(this).css("border-left-width")).slice(0,-2)))+Math.ceil(Number(String($(this).css("border-right-width")).slice(0,-2)));
			if ($.browser.msie && $.browser.version<8){
				var m5formnormalizewidth=Math.ceil(Number($(this).width()))+m5formnormalizepadding+m5formnormalizeborder*2;
			} else {
				var m5formnormalizewidth=Math.ceil(Number($(this).width()))+m5formnormalizepadding+m5formnormalizeborder;
			}
			$(this).width(m5formnormalizewidth);
		})
	}
}

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;
	}
	var totalWidth=0;
	if (pageWidth>windowWidth) {totalWidth=pageWidth} else {totalWidth=windowWidth}
	var totalHeight=0;
	if (pageHeight>windowHeight) {totalHeight=pageHeight} else {totalHeight=windowHeight}

	return [totalWidth,totalHeight];
}

function close_popups () {
	$("#shadow").hide();
	$(".popup").hide();
	$("#rightBanners").show();
	$("#diamant_banner").show();
}

function popups (){
	var shadow=getPageSize();
	$("#shadow").height(shadow[1]);
	$("#shadow").click(function(){
		close_popups();
	$("#rightBanners").show();
	})
	$(".close").click(function(){
		close_popups();
	})
}

function showShadow(){
	$("#diamant_banner").hide();
	$("#shadow").show();
	$("#rightBanners").hide();
}

function showPopup (id) {
	showShadow();
	$("#"+id).show();
}
function faq(){
	$(".faq>.faqRow").hover(
      function () {
		  $(this).addClass("faqRowHover");
      },
      function () {
		  $(this).removeClass("faqRowHover");
      }
    );

	$(".faq>.faqRow h2").click(
      function () {
		  $(this).parent().parent().toggleClass("faqRowActive");
      });


}


