var myWind = $(window);


function centerBgStretcherImages(){
	var myWindWidth = myWind.width();
	var myWindHeight = myWind.height();

	$('#bgstretcher LI').each(function(){
		var myImg = $('IMG',this);
		var top = Math.round((myWindHeight - myImg.height())/2);
		var left = Math.round((myWindWidth - myImg.width())/2);
		$(this).css({
			'top':top+'px',
			'left':left+'px'
		});
	});
}

var myInterval = setInterval(centerBgStretcherImages,10);




var snapWidthToColumn = {

selector_inner:'.AtomText, .listing-description, .item-closeup',
columnWidth:56,
gutterWidth:33,
execute:function(){
	var me = this;
	$(this.selector_inner).each(function(){
		$($(this).parent().get(0)).css('overflow-x','hidden');
		//document.title = $($(this).parent().get(0)).width();
		var realWidth = $($(this).parent().get(0)).width()-parseInt($(this).css('margin-left'));
		var resultWidth = Math.floor((realWidth+me.gutterWidth)/(me.columnWidth+me.gutterWidth))*(me.columnWidth+me.gutterWidth)-me.gutterWidth;
		//document.title = $($(this).parent().get(0)).width()+' '+realWidth+' '+parseInt($(this).css('margin-left'))+' '+parseInt($(this).css('margin-right'));
		//$($(this).parent().get(0)).width()-parseInt($(this).css('margin-left'))-parseInt($(this).css('margin-right'));
		$(this).width(resultWidth);
	});
}
};


function fancyHeaderVerticalAlign(){

	$('.fancy-header').css('margin-top',($('.fancy-header').height()>36)?(36):(54));

}

function normalizeContentBlockHeight(){
	$('.default-container-inner').height('auto');
	var cHeight = $('.default-container-inner').height();
	if(cHeight>210&&cHeight<392)$('.default-container-inner').height(392);
}

function normalizeMenuItems(){
	var maxHeight=0;
	$('#default-container .top-menu a').each(function(){
		var height = $(this).height();
		if (maxHeight<height)maxHeight=height;
	});

	$('#default-container .top-menu a').each(function(){
		var height = $(this).height();
		if (maxHeight>height){
			$(this).css('margin-top',(maxHeight-height)+'px');
		}
	});

}

function ieCorrection(){
	if(!$.browser.msie) return;
	if($.browser.version>7) return;

	$('#footer').css({
		'position':'absolute',
		'top':(document.body.scrollTop+ document.body.clientHeight -45)+'px'
	});

}


function positionSsButtons(){
	var winHeight = $(window).height()-150;

	$('.ssButton').each(function(){
		$(this).css('top',winHeight);
	});
}

$(function(){
	snapWidthToColumn.execute();
	//ieCorrection();
	fancyHeaderVerticalAlign();
	normalizeContentBlockHeight();
	normalizeMenuItems();
	positionSsButtons();
	$(window).resize(function(){
		snapWidthToColumn.execute();
		fancyHeaderVerticalAlign();
		//ieCorrection();
		positionSsButtons();
	});

	$(window).scroll(function(){

		ieCorrection();
	});





});
