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() {		
	
	/*if(typeof sIFR == "function") {
		sIFR.replaceElement("h1.title", named({
			sFlashSrc: "/fonts/HeliosCond.swf", 			
			sColor: "#0d63b0",
			sWmode: "transparent"			
		}));
                
        sIFR.replaceElement("h1.subtitle", named({
			sFlashSrc: "/fonts/HeliosCond.swf", 			
			sColor: "#333333",
			sWmode: "transparent",
            sCase: "upper"
		}));
	};*/
    
    $("#catalogue-menu a[class!='banner']").each(
		function() {
			var href = $(this).attr("href").replace('http://'+document.domain,'');							
			if ( href.charAt(href.length-1) != '/' ) href = href+'/';
            
			if ( path.indexOf(href) == 0/* || href == "/products/s1/" */) {
				$(this).parent().addClass("current");
                
                if ( !$(this).attr("rel") )
                    $(this).parent().after($('<div class="item-bottom current1"></div>'));
            }
				
			if ( $(this).attr("rel") > 0 ) {
				
				if ( document.location.pathname == '/'/* && href != "/products/s1/" */)					
					return;				
				                
				if ( path.indexOf(href) == 0/* || href == "/products/s1/" */) {
                    $(this).bind("click", onCategoryClick );
					$(this).click();
                }
			}
		}
	);

    try {		
        $("table.tech tr:nth-child(odd)").addClass("grey");
	} catch (e) {}
}

var ajaxRes = new Array();
var catStatus = new Array();

function onCategoryClick( e ) {
	var pid = $(this).attr("rel");
    var href = $(this).attr("href").replace('http://'+document.domain,'');							
    if ( href.charAt(href.length-1) != '/' ) href = href+'/';
	
	if ( catStatus[pid] ) {
		$("#pid-"+pid).css("display","none");
		$(this).parent().removeClass('current');
        $("#bottom-"+pid).hide();
	}
	else {	
		if ( ajaxRes[pid] ) {	
			$("#pid-"+pid).css("display","block");
            $("#bottom-"+pid).show();
		}
		else {
			if ( href == "/products/s1/" ) {
                $(this).parent().after($('<div id="bottom-'+pid+'" class="item-bottom current1"></div><div class="submenu" id="pid-'+pid+'">Загрузка...</div>'));
                $("#pid-"+pid).load("/ajax/catalogue-menu.php", { 'pid': pid }, onAjaxSuccess);
            }
            else
                $(this).parent().after($('<div id="bottom-'+pid+'" class="item-bottom current1"></div>'));
			
		}
		
        $(this).parent().addClass('current');
        //alert("#"+$(this).parent().attr("id"));
        //$("#"+$(this).parent().attr("id")+" + .item-bottom").addClass("");
	}
	catStatus[pid] = !catStatus[pid];

	return false;
}

function onAjaxSuccess(data) {	    
	ajaxRes[$(this).attr("id").replace('pid-','') ] = data;
	//$("#"+$(this).attr("id")+" a[@rel]").bind("click", onCategoryClick );
	
	$("#"+$(this).attr("id")+" a").each(
		function () {
			var href = $(this).attr("href").replace('http://'+document.domain,'');
			
			//alert(path);
			
			if ( $(this).attr("rel") > 0 ) {				
				$(this).bind("click", onCategoryClick );
				if ( path.indexOf(href) == 0 )
					$(this).click();
			}
			else if ( path.indexOf(href) == 0 )
				$(this).parent().addClass("current");
		}
	);
}

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('Ia ana iiey caiieiaiu');
				
				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);
	}
}