var path = document.location.pathname;
if ( path.charAt(path.length-1) != '/' ) path = path+'/';

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() {		
	$("#hits-news .news a").hover(
		function () {
			var id = $(this).attr("rel");
			$(this).after('<div id="box-'+id+'" class="picture-box"><div class="img"><b class="tlc"></b><b class="trc"></b><b class="blc"></b><b class="brc"></b></div></div>');
			
			$("#box-"+id+" .img").append("<img src='images/tmp/cp1.jpg'/>")		
			
			return false;
		},
		function () {
			var id = $(this).attr("rel");
			$("#box-"+id).remove();
		}
	);
	
	if ( section )
		$("#items a[href^=/"+section+"]").parent().parent().parent().addClass("selected");
	
	$("#images .img a").click(
		function () {
			var src = $(this).attr("href");
			$("#big-image").attr('src', src);
			return false;
		}
	);
	
	if(typeof sIFR == "function") {
		sIFR.replaceElement("#items span", named({
			sFlashSrc: "/fonts/MyriadProItalic.swf", 			
			sColor: "#61625f",			
			sWmode: "transparent", 
			sFlashVars:"textalign=center"
		}));
		
		/*sIFR.replaceElement("#left-menu h1.selected span", named({
			sFlashSrc: "/fonts/MyriadProItalic.swf", 			
			sColor: "#000000",			
			sWmode: "transparent", 
			sFlashVars:"textalign=left"
		}));*/
		
		/*sIFR.replaceElement("#left-menu span", named({
			sFlashSrc: "/fonts/MyriadProItalic.swf", 			
			sColor: "#6c6c6c",			
			sWmode: "transparent", 
			sFlashVars:"textalign=left"
		}));*/
		
		
		sIFR.replaceElement("#data h1", named({
			sFlashSrc: "/fonts/MyriadProLightCondBold.swf", 			
			sColor: "#000000",			
			sWmode: "transparent", 
			sFlashVars:"textalign=left"
		}));
		
		sIFR.replaceElement(".ft", named({
			sFlashSrc: "/fonts/MyriadProItalic.swf", 			
			sColor: "#d60b10",			
			sWmode: "transparent", 
			sFlashVars:"textalign=left"
		}));
				
	};
    
	$(".gallery .data a, .services a").each(
		function() {
			var href = $(this).attr("href").replace('http://'+document.domain,'');							
			if ( href.charAt(href.length-1) != '/' ) href = href+'/';
			
			if ( path.indexOf(href) == 0 ) {                
				$(this).addClass("selected");
            }
				
			if ( $(this).attr("rel") > 0 ) {
				if ( document.location.pathname == '/' )					
					return;				
				
				if ( path.indexOf(href) == 0 ) {
					$(this).bind("click", onCategoryClick );
					$(this).click();					
				}
			}
		}
	);
	
	$("table tr:nth-child(even)").addClass("grey");
	
}

var ajaxRes = new Array();
var catStatus = new Array();

function onCategoryClick( e ) {
	var pid = $(this).attr("rel");
	
	if ( catStatus[pid] ) {
		$("#pid-"+pid).hide();
		$(this).parent().removeClass('selected');        
	}
	else {	
		if ( ajaxRes[pid] ) {	
			$("#pid-"+pid).show();
            $("#bottom-"+pid).show();
		}
		else {
			$(this).parent().after($('<div class="submenu" id="pid-'+pid+'">Загрузка...</div>'));
			$("#pid-"+pid).load("/ajax/catalogue-menu.php", { 'pid': pid }, onAjaxSuccess);
		}
		
        $(this).parent().addClass('selected');        
	}
	catStatus[pid] = !catStatus[pid];

	return false;
}

function onAjaxSuccess(data) {
	ajaxRes[$(this).attr("id").replace('pid-','') ] = data;		
	
	$("#"+$(this).attr("id")+" a").each (
		function () {
			var href = $(this).attr("href").replace('http://'+document.domain,'');			
			
			if ( $(this).attr("rel") > 0 ) {				
				
				if (path.indexOf(href) == 0) {
					$(this).bind("click", onCategoryClick );
					$(this).click();
				}
			}
			else if ( path.indexOf(href) == 0 )
				$(this).parent().addClass("selected");
		}
	);
}

function sendForm( name ) {
	try {
		var n = document.forms[name].length;		
		
		var i = 0;
		while ( i < n ) {		
			if ( document.forms[name].elements[i].value == '' && document.forms[name].elements[i].className != 'nrq' ) {
				alert('Не все поля заполнены');
				
				document.forms[name].elements[i].focus();
				return;
			}
		
			i++;
		}
		document.forms[name].submit();		
	}
	catch ( ex ) {
		alert(ex);
	}
}

function clearForm( name ) {
	try {
		var n = document.forms[name].length;		
		
		var i = 0;
		while ( i < n ) {		
			document.forms[name].elements[i].value = '';				
			i++;
		}		
	}
	catch ( ex ) {
		alert(ex);
	}
}

