/**
 * @author <a href="http://media5.com">Media5</a> - <a href="mailto:banzalik@gmail.com">Бойченко Александр</a>
 * @description скрипты для m5 css framework
 */
 
 /**
 * @description полная загрузка страницы, картинки при этом уже загрузились
 */
 jQuery.noConflict();
 var addVideo=1;
 var addRVideo=1;
 
 
document.documentElement.id = "js" // добавляем тегу html id=js
 
window.onload=documentLoaded; // присваиваем функцию documentLoaded

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


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

	jQuery(window).resize(function(){
		  plakat();
	});

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

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


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

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

function plakat() {
	var getPageSizeArr=getPageSize();
	var pageWidth=getPageSizeArr[0];
	if ( pageWidth >1294) {
		jQuery(".plakat").show();		
	}
	if ( pageWidth <1030) {
		jQuery(".plakat").hide();		
	}
	if (pageWidth >1030 && pageWidth < 1294)
	{
		jQuery(".plakat").show();
		var wdh=(pageWidth-1020)/2;		
		jQuery(".plakat").width(wdh);
		
	}

	
	
}


function addVideoAnim(){	
			jQuery(".addVideo").hover(
				  function () {
					  if (addVideo){
						  addVideo=0;
						  jQuery(this).animate({backgroundPosition: '(0 35px)'}, function(){addVideo=1});
					}
				  }, 
				  function () {
					  if (addRVideo){
						  addRVideo=0;
							jQuery(this).animate({backgroundPosition: '(0 0)'}, function(){addRVideo=1});
					  }
				  }
				);
}

function menu(){
	var show=jQuery(".menu a.active").attr("rel");
	var submenu=jQuery(".submenu")
	submenu.hide();
	if(show) {jQuery("."+show).show();}	
	jQuery(".menu a").hover(function(){
		var ssow=jQuery(this).attr("rel");
		if (ssow){
			submenu.hide();
			jQuery("."+ssow).show();
			}	else {
			submenu.hide();		
			}	
	})
	
	jQuery(".menuFix").mouseout(function(){
			var show=jQuery(".menu a.active").attr("rel");
			var submenu=jQuery(".submenu")
			submenu.hide();	
			jQuery("."+show).show();		 
	}).hover(function(){
			var show=jQuery(".menu a.active").attr("rel");
			var submenu=jQuery(".submenu")
			submenu.hide();	
			jQuery("."+show).show();		 
	});
	
	
}


function comments(){
	jQuery("a.comments").click(function(){
			jQuery(".commentsPopup").toggle();
			return false;
	});
	jQuery(".commentsPopup a.close").click(function(){
			jQuery(".commentsPopup").toggle();
			return false;
	});
	jQuery("a.inblog").click(function(){
			jQuery(".inblogpopup").toggle();
			return false;
	});
	jQuery(".inblogpopup a.close").click(function(){
			jQuery(".inblogpopup").toggle();
			return false;
	});
	
}