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 span", named({
			sFlashSrc: "/fonts/YanusC.swf", 			
			sColor: "#939598",			
			sWmode: "transparent",
			sCase: "upper"
		}));
	};
    
	$("#menu a").hover(
		function() {			
			$(this).parent().addClass("hover");
			$(this).parent().prev().addClass("mleft");
			$(this).parent().next().addClass("mright");
		},
		function() {
			$(this).parent().removeClass("hover");
			$(this).parent().prev().removeClass("mleft");
			$(this).parent().next().removeClass("mright");
		}
	);
	
	$("#submenu 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).parent().addClass("selected");
			
			if ( $(this).attr("rel") > 0 ) {
				$(this).bind("click", onCategoryClick );														
				
				if ( document.location.pathname == '/' )					
					return;				
				
				if ( path.indexOf(href) == 0 )
					$(this).click();
				
			}
		}
	);

	
    try {
        $("table.params tr:nth-child(even)").addClass("grey");
        
		if ( path != '/' ) {			            
            $("#menu a[href^='/"+section+"']").parent().addClass("selected");
			$("#menu a[href^='/"+section+"']").parent().prev().addClass("mleft");
			$("#menu a[href^='/"+section+"']").parent().next().addClass("mright");
            
            /*$("#catalogue a").each (
                function () {
                    
                    var href = $(this).attr("href");                    
                    if ( path.indexOf(href) == 0 ) {
                        
                        var src = $("#catalogue a[href='"+href+"'] img").attr('src').replace('default','selected');                        
                        $("#catalogue a[href='"+href+"'] img").attr('src', src);                        
                        $('<div id="arrow"></div>').appendTo("#catalogue a[href='"+href+"']");                        
                        return;
                    }
                }
            );*/            
		}        
	} catch (e) {}	
}

var ajaxRes = new Array();
var catStatus = new Array();

function onCategoryClick( e ) {
	var pid = $(this).attr("rel");
	
	if ( catStatus[pid] ) {
		$("#pid-"+pid).css("display","none");
		$(this).parent().parent().next().removeClass('prev-selected');
		$(this).parent().removeClass('selected');
		$(this).parent().parent().removeClass('selected');
	}
	else {		
		$(this).parent().addClass('selected');
		$(this).parent().parent().addClass('selected');
		$(this).parent().parent().next().addClass('prev-selected');
		if ( ajaxRes[pid] ) {	
			$("#pid-"+pid).css("display","block");
		}
		else {			
			
			$(this).parent().after($('<div id="pid-'+pid+'" class="submenu">Загрузка...</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[@rel]").bind("click", onCategoryClick );
	
	$("#"+$(this).attr("id")+" a").each(
		function () {
			var href = $(this).attr("href").replace('http://'+document.domain,'');
			
			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("selected");
		}
	);
}


function checkForm ( name ) {
	var res = true;
	$("#"+name+" input, #"+name+" textarea").each(
		function () {
			if ( $(this).hasClass("required") && $(this).val() == '' ) {
				res = false;
				$(".message.required", $(this).parent()).show();
			}
			else if ( $(this).hasClass("email") && $(this).val() != '' ) {
				var emailTest = '^[_\\.0-9a-zA-Z-]+@([0-9a-zA-Z][0-9a-zA-Z_-]+\\.)+[a-z]{2,4}$';
				var regex = new RegExp(emailTest);
				if ( !regex.test( $(this).val() ) ) {
					res = false;
					$(".message.email", $(this).parent()).show();
				}
			}
		}
	);
	setTimeout("$('#"+name+" .message').fadeOut();",5000);
	return res;
}
