var PAGE_MIN_WIDTH = 760;
var PAGE_MAX_WIDTH = 1100;

function fitWidth() {
	if(document.body.clientWidth > PAGE_MAX_WIDTH) return PAGE_MAX_WIDTH + "px";
	if(document.body.clientWidth < PAGE_MIN_WIDTH) return PAGE_MIN_WIDTH + "px";
	return "100%";
}


function fitHScrollBlock() {
	if(document.body.clientWidth > PAGE_MAX_WIDTH) return 510 + "px";
	if(document.body.clientWidth < 900) return 310 + "px";
	return (document.body.clientWidth - 590 ) + "px";
}


var scrollablePosition = -1;
var scrollStep = 0;
var scrollStepWidth = 100;
var minMargin = 100;
var scrollbarMargin = 40;
			
function scrollBlockLeft(){
	
	var container_width = document.getElementById('scrollcontainer_1').clientWidth;
	var content_width = document.getElementById('scrollable').clientWidth;
	var content_position = scrollablePosition - (scrollStep * scrollStepWidth);
	
	var scrollbar_width = document.getElementById('scrollbar').clientWidth;
	
	var totalSteps = Math.ceil((minMargin - container_width + content_width) / scrollStepWidth);
	
	var margin = container_width - (content_width + content_position);

	if(margin < minMargin){
		
		scrollStep++;
		$(".scrolltable").animate({left: scrollablePosition - (scrollStep * scrollStepWidth) + "px"}, 1000 );
		
		var scrollMarkerPosition = scrollbarMargin + Math.floor((scrollbar_width - 2 *  scrollbarMargin - 15) * scrollStep / totalSteps); 
		$(".scroll_marker").animate({left: scrollMarkerPosition + "px"}, 1000 );
	}
}

function scrollBlockRight(){
	var container_width = document.getElementById('scrollcontainer_1').clientWidth;
	var content_width = document.getElementById('scrollable').clientWidth;
	var content_position = scrollablePosition - (scrollStep * scrollStepWidth);
	
	
	var scrollbar_width = document.getElementById('scrollbar').clientWidth;
	
	var totalSteps = Math.ceil((minMargin - container_width + content_width) / scrollStepWidth);
	
	
	if(content_position < -1) {
		scrollStep--;

		$(".scrolltable").animate({left: scrollablePosition - (scrollStep * scrollStepWidth) + "px"}, 1000 );
		
		var scrollMarkerPosition = scrollbarMargin + Math.floor((scrollbar_width - 2 *  scrollbarMargin - 15) * scrollStep / totalSteps); 
		$(".scroll_marker").animate({left: scrollMarkerPosition + "px"}, 1000 );
	}
}


function setScrollMarker(){
	
	var container_width = document.getElementById('scrollcontainer_1').clientWidth;
	var content_width = document.getElementById('scrollable').clientWidth;
	var content_position = scrollablePosition - (scrollStep * scrollStepWidth);
	
	var scrollbar_width = document.getElementById('scrollbar').clientWidth;
	var totalSteps = Math.ceil((minMargin - container_width + content_width) / scrollStepWidth);
	if(scrollStep > totalSteps) scrollStep = totalSteps;
	var scrollMarkerPosition = scrollbarMargin + Math.floor((scrollbar_width - 2 *  scrollbarMargin - 15) * scrollStep / totalSteps);
	
	document.getElementById('scroll_marker_1').style.left = scrollMarkerPosition + "px";
	document.getElementById('scroll_marker_2').style.left = scrollMarkerPosition + "px";
	document.getElementById('scroll_marker_3').style.left = scrollMarkerPosition + "px";
}



var selected_menu_item = null;
var expanderOverFlag = false;

function expandAction(section){
	
	var item_id = "contextmenu_item_" + section;
	var block_id = "contextmenu_submenu_" + section;
	
	if(selected_menu_item != null){
		if(section == selected_menu_item){
			selected_menu_item = null;
			document.getElementById("contextmenu_submenu_" + section).style.display = "none";
			document.getElementById("contextmenu_item_" + section).style.display = "block";
		}
		else{
			document.getElementById("contextmenu_submenu_" + selected_menu_item).style.display = "none";
			document.getElementById("contextmenu_item_" + selected_menu_item).style.display = "block";
			
			document.getElementById("contextmenu_submenu_" + section).style.display = "block";
			document.getElementById("contextmenu_item_" + section).style.display = "none";
			
			selected_menu_item = section;
		}
	}
	else{
		document.getElementById("contextmenu_submenu_" + section).style.display = "block";
		document.getElementById("contextmenu_item_" + section).style.display = "none";
		
		selected_menu_item = section;
	}
}

function contextMenuAction(arg, actType){
	if(expanderOverFlag && actType == 1) expandAction(arg);
	if((!expanderOverFlag) && actType == 2) window.location = arg;
}


function findElementPos(elemFind)
{
	var elemX = 0;
	var elemY = 0;
	do {
		elemX += elemFind.offsetLeft;
		elemY += elemFind.offsetTop;
	} while ( elemFind = elemFind.offsetParent )


	return Array(elemX, elemY);
}

function showModelInfo(name, cost){
	document.getElementById("popup_img").src = "/resources/images/common/modelinfo/" + name + ".gif"
	document.getElementById("popup_cost").innerHTML = cost;
	
	var blockWidth = document.getElementById(name).offsetWidth;
	showPopup(name, -Math.round((140 - blockWidth) / 2), 36);
}

function hideModelInfo(){
	hidePopup();
}


function openImage(title, img_url, width, height){
	var str = "scrollbars=yes, height=" + height + ",innerHeight=" + height;
	 str += ",width=" + width + ",innerWidth=" + width;
	 
	 if (window.screen) {
	   var ah = screen.availHeight - 20;
	   var aw = screen.availWidth - 10;
	
	   var xc = (aw - width) / 2;
	   var yc = (ah - height) / 2;
	
	   str += ",left=" + xc + ",screenX=" + xc;
	   str += ",top=" + yc + ",screenY=" + yc;
	 }
	 
	wnd = window.open("", "photo", str);
	wnd.document.open();

	wnd.document.write("<html><head><title>" + title + "</title></head><body style='background-color: RGB(220, 220, 209);'>");
	
	wnd.document.write("<table cellspacing='0' cellpaddin='0' border='0' width='100%' height='100%'>");
	wnd.document.write("<tr><td align='center' valign='middle'>");
	wnd.document.write("<img src='" + img_url + "'>");
	wnd.document.write("</tr></td></table>");
	
	wnd.document.write("</body></html>");

	wnd.document.close(); 
	wnd.focus();
}


function checkField(field, type){
	if(field.value){
		if(type == "float"){
			field.value = field.value.replace(/,/, ".");
			field.value = field.value.replace(/[^\d\.]/, "");
			
			if(matches = field.value.match(/^\d+(\.\d*)?/)) field.value = matches[0];
		}
		if(type == "int"){
			field.value = field.value.replace(/[^\d]/, "");
		}
	}
}
