var topImageMaxHeight=400;

function ReplaceImageBackground(options)
{
	var backgroundImgElement = jQuery(options.source);
	var destinationElement = jQuery(options.destination);
	var backgroundImg = document.location.protocol+"//"+document.location.host+"/"+backgroundImgElement.attr("src");
	
	var size = getImgSize(backgroundImg);

	var backgroundWidth = size.width
	var backgroundHeight = size.height
	var backgroundCss = "url('"+backgroundImg+"')";
	var minWidth = 900;
	var minHeight = 700;
	if (backgroundWidth > minWidth || backgroundHeight > minHeight) {
		backgroundCss += " no-repeat fixed center";
	}else if (backgroundWidth < minWidth && backgroundHeight < minHeight){
		backgroundCss += " repeat";
	}else if (backgroundWidth < minWidth) {
		backgroundCss += " repeat-x";
	}else if (backgroundHeight < minHeight) {
		backgroundCss += " repeat-y";
	}

	destinationElement.css("background", backgroundCss);
	
	return {
		sourceElement: backgroundImgElement,
		destinationElement: destinationElement,
		width: size.width,
		height: size.height
	}
}

function getImgSize(imgSrc)
{
	var newImg = new Image();
	newImg.src = imgSrc;
	return {
		width: newImg.width,
		height: newImg.height
	}
}

jQuery(document).ready(function() {
	ReplaceImageBackground({source: "#img-place img", destination: "body"});
	
	//Make fancy dropdowns.
	jQuery('.fancyCombo').each(function() { jQuery(this).dropdownReplacement({selectCssWidth: 130, ellipsisSelectText: true, ellipsisText: '...', charWidth:7}) });
});

function initAll(){
	jQuery(document).ready(function() {
		//align girl in header
		placeHeaderOperator();
	});
		
	$(window).load(function () {
		//align girl in header		
		placeHeaderOperator();	
		$('.top .indexOperator img').css({'opacity':0, 'visibility':'visible'}).fadeTo('slow',1);	
	});
}

function initIndex()
{
	initAll();
	
	jQuery(document).ready(function() {
		/*var data = ReplaceImageBackground({source: "img.caption-image", destination: "#center div.caption"});
		data.destinationElement.css("height", Math.min(data.height, topImageMaxHeight) + "px");
		jQuery("#panel-right").css("top", (Math.min(data.height, topImageMaxHeight)-70)*(-1) + "px")
				.css("margin-bottom", (Math.min(data.height, topImageMaxHeight)-70)*(-1) + "px");*/

		//Make bottom widgets behave
		jQuery('#bottom .widget').each(function(index) {
			jQuery(this).after(jQuery(document.createElement("div")).addClass('vseparator').addClass('after-'+(index%3)));
		});
	});
}

function initIndexInner()
{
	initAll();
	var menuRefreshTimeout;
	jQuery(document).ready(function() {
		//align sidemenu
		menuRefreshTimeout = setInterval('placeSideMenu()',10);
	});
	
	jQuery(window).resize(function() {
		placeSideMenu();
	})
	
	$(window).load(function () {
		// place sidemenu
		placeSideMenu();
		clearInterval(menuRefreshTimeout);
		

		
	});

}

function placeSideMenu(){

	var menuHeight = $('#panel-right').outerHeight(false);
	var contentHeight = $('#panel-left').outerHeight(false);

	if (contentHeight < menuHeight  ){
	  $('#panel-left').css('height',menuHeight  );
	}

	var contentTopEdge = $('#panel-left').offset().top;
	var contentLeftEdge = $('#panel-left').offset().left;
	var menuLeftEdge = contentLeftEdge  + $('#panel-left').outerWidth();
	$('#panel-right').css({'top': contentTopEdge+'px','left':menuLeftEdge , 'position':'absolute'});
}

function placeHeaderOperator(){
	var headerHeight = $('.top').height();
	var imageHeight = $('.top .indexOperator img').height();
	$('.top .indexOperator').css('top',headerHeight-imageHeight);
}




function initIndexProfile()
{
	
}
