var path = document.location.pathname;
if ( path.charAt(path.length-1) != '/' ) path = path+'/';

$(document).ready(init);

var epos = document.location.pathname.indexOf('/',1) == -1 ? document.location.pathname.length : document.location.pathname.indexOf('/',1)-1;
var section = document.location.pathname.substr(1, epos );

$(document).ready(init);

function init() {
	$("#menu a").hover(
		function () {		
			var img_src = $("img", $(this)).attr("src");
			//var img_name = img_src.substr(1+img_src.lastIndexOf('/')).replace('.gif','');			
			$("img", $(this)).attr({src: img_src.replace("default", "hover") });
			
			/*$("#"+img_name).addClass('left');			
			$("#"+img_name+" + a + div").addClass('right');
			unhlCurrentItem()*/
		},
		function () {			
			var img_src = $("img", $(this)).attr("src");
			//var img_name = img_src.substr(1+img_src.lastIndexOf('/')).replace('.gif','');
			$("img", $(this)).attr({src: img_src.replace("hover", "default") });	

			/*$("#"+img_name).removeClass('left');			
			$("#"+img_name+" + a + div").removeClass('right');
			hlCurrentItem()*/
		}
	);	
	
	$("#section-title").attr({src:"/images/titles/"+section+".gif"});
	correctCatalogueMenuItems();
	hlCurrentItem();
}

function correctCatalogueMenuItems() {
	$("#catalogue-menu h2 img").each(
		function () {
			if ( $(this).get(0).height > 12 ) {				
				$(this).parent().parent().parent().addClass("long");
			}
			
		}
	);
}

function hlCurrentItem() {
	$("#menu a").each (
		function () {
			var href = $(this).attr("href").replace('http://'+document.domain,'');
			
			if ( path.indexOf(href) == 0 ) {				
				var img_src = $("img", $(this)).attr("src");
				var img_name = img_src.substr(1+img_src.lastIndexOf('/')).replace('.gif','');			
				$("img", $(this)).attr({src: img_src.replace("default", "selected") });
				
				$("#"+img_name).addClass('left');			
				$("#"+img_name+" + a + div").addClass('right');
			}
		}
	);
}

function unhlCurrentItem() {
	$("#menu a").each (
		function () {
			var href = $(this).attr("href").replace('http://'+document.domain,'');
			
			if ( path.indexOf(href) == 0 ) {
				var img_src = $("img", $(this)).attr("src");
				var img_name = img_src.substr(1+img_src.lastIndexOf('/')).replace('.gif','');
				$("img", $(this)).attr({src: img_src.replace("selected", "default") });	

				$("#"+img_name).removeClass('left');			
				$("#"+img_name+" + a + div").removeClass('right');
			}
		}
	);
}

/*function init() {
	$("#menu a").hover(
		function () {
			unhlCurrentItem();
		
			$(this).addClass("selected");
		
			var img_src = $("img", $(this)).attr("src");
			var img_name = img_src.substr(1+img_src.lastIndexOf('/')).replace('.gif','');
			
			$("img", $(this)).attr({src: img_src.replace("default", "selected") });
			
			$("#menu a.selected + div").addClass('right');
			$("#menu #m_"+img_name).addClass('left');
			
			if ( img_name == 'company' ) $("#content img.left").hide();
		},
		function () {			
			var img_src = $("img", $(this)).attr("src");
			var img_name = img_src.substr(1+img_src.lastIndexOf('/')).replace('.gif','');
			$("img", $(this)).attr({src: img_src.replace("selected", "default") });
			
			$("#menu a.selected + div").removeClass('right');
			$("#menu #m_"+img_name).removeClass('left');
			
			$(this).removeClass("selected");
			
			if ( img_name == 'company' ) $("#content img.left").show();
			
			hlCurrentItem();
		}
	);	
	
	$("#section-title").attr({src:"/images/titles/"+section+".gif"});
	
	hlCurrentItem();
	setImagesBorder();
	hlCatalogueItem();
}

function hlCurrentItem() {
	$("#menu a").each (
		function () {
			var href = $(this).attr("href").replace('http://'+document.domain,'');
			
			if ( path.indexOf(href) == 0 ) {
				$(this).addClass("selected");
		
				var img_src = $("img", $(this)).attr("src");
				var img_name = img_src.substr(1+img_src.lastIndexOf('/')).replace('.gif','');
				
				$("img", $(this)).attr({src: img_src.replace("default", "selected") });
				
				$("#menu a.selected + div").addClass('right');
				$("#menu #m_"+img_name).addClass('left');
				
				if ( img_name == 'company' ) $("#content img.left").hide();
			}
		}
	);
}

function unhlCurrentItem() {
	$("#menu a").each (
		function () {
			var href = $(this).attr("href").replace('http://'+document.domain,'');
			
			if ( path.indexOf(href) == 0 ) {
				var img_src = $("img", $(this)).attr("src");
				var img_name = img_src.substr(1+img_src.lastIndexOf('/')).replace('.gif','');
				$("img", $(this)).attr({src: img_src.replace("selected", "default") });
				
				$("#menu a.selected + div").removeClass('right');
				$("#menu #m_"+img_name).removeClass('left');
				
				$(this).removeClass("selected");
				
				if ( img_name == 'company' ) $("#content img.left").show();
			}
		}
	);
}

function setImagesBorder() {	
	$("img.withborder").each (
		function () {
			$(this).replaceWith('<div class="img"><img src="/images/content/img-tlc.png" width="15" height="15" class="tlc png"><img src="/images/content/img-trc.png" width="15" height="15" class="trc png"><img src="'+$(this).attr('src')+'"/><img src="/images/content/img-blc.png" width="15" height="15" class="blc png"><img src="/images/content/img-brc.png" width="15" height="15" class="brc png"></div>');						
		}
	);	
}

function hlCatalogueItem() {
	$("#catalogue-menu div.data a").each (
		function () {
			var href = $(this).attr("href").replace('http://'+document.domain,'');
			
			if ( path.indexOf(href) == 0 ) {
				$(this).addClass("selected");
			}
		}
	);
}*/

function submitForm( name ) {
	try {
		var n = document.forms[name].length;		
		
		var i = 0;
		while ( i < n ) {		
			if ( document.forms[name].elements[i].value == '' ) {
				alert('Не все поля заполнены');
				document.forms[name].elements[i].focus();
				return;
			}
		
			i++;
		}
		document.forms[name].submit();		
	}
	catch ( ex ) {
		alert(ex);
	}
}

function clearForm( name ) {
	for ( var i = 0; i < document.forms[name].length; i++ )
		document.forms[name].elements[i].value = '';
	
}
