$(document).ready(function(){




        $("img#showmenu").click(function(){
            $(this).hide();
            $("ul.first_level > li").removeClass("hidden");
        });
        
        /****************************/


        var steck = [];
        steck[0] = $("ul.first_level").get(0);

        function createSteck(obj, callback) {
            var inside = false;
            var position = 0;
         //   var change = false;
            var length = steck.length;
            var last_el = steck[steck.length-1];
            steck[steck.length] = obj.get(0);

            for (var i = 0; i < steck.length; i++) {
                if (steck[i].parentNode == obj.get(0).parentNode) {
                    position = i;
                    break;
                }
            }
            if (position > 0) {
                steck.length = position + 1;
                steck[position] = obj.get(0);
            }
            if (callback != undefined) {
                callback(obj);
            }

            if ((length >= steck.length)&&(last_el != steck[steck.length-1])) {
        //        console.log(last_el);
                $(last_el).children("div").children("ul").slideUp('fast');
                var last = $(last_el).children("div").children("ul").get(0);
             //   console.log($(last).children("li").children("div").children("ul"));
                $(last).children("li").children("div").children("ul").slideUp('fast');
             //   change = true;
            }
        //    console.log("end --->",steck);
           // return change;
        }

//        function hideEl(obj) {
//            console.log(obj);
//            console.log($(obj).children("div").children("ul"));
//            $(obj).children("div").children("ul").slideUp("normal");
//        }

        function cutSteck(obj){
            if (obj == null) {
                obj = $(steck[steck.length-1]);
            }
         //       console.log("bjoa",$(obj).get(0),steck[steck.length-1],"--",steck[steck.length-1] == $(obj).get(0));
            if (steck[steck.length-1] == $(obj).get(0)) {
                $(obj).children("div").children("ul.levelmenu").stop(true, true).slideUp("normal", function(){
                    steck.length--;
                })
            }
        //    console.log(steck);
        }


         $("ul.first_level > li").hover(function(){
             createSteck($(this), function(obj){
                 $(obj).children("div").children("ul.secondmenu").stop(true, true).slideDown("normal", function(){
                 });
             });
         },function(){});
         
         $("ul.secondmenu").hover(function(){},function(){
            if (steck[steck.length-1].parentNode == $(this).get(0)) {
                $(this).slideUp('fast');
                $("ul.levelmenu").slideUp('fast');
                steck.length--;
            }  
         });

         $("ul.secondmenu > li").hover(function(){
             createSteck($(this));
             $(this).children("div").children("ul.levelmenu").stop(true, true).slideDown("normal", function(){});
         },function(){
//             console.log($(this).parent("li.child"));
             //createSteck($(this).parent("li.child"));
            
 //            cutSteck($(this));
         });

         $("ul.first_level").hover(function(){},function(){
             $("ul.levelmenu").slideUp('fast');
             $("ul.secondmenu").slideUp('fast');
         });

         $("ul.levelmenu").hover(function(){},function(){
             $(this).slideUp('fast');
                    steck.length--;
         });
         $("ul.levelmenu > li").hover(function(){
              createSteck($(this));
             $(this).children("div").children("ul.levelmenu").slideDown("normal", function(){});
         },function(){
//             console.log($(this));
//             $(this).children("div").children("ul.levelmenu").slideUp("normal", function(){});
            // var childm = $(this).children("div").children("ul.levelmenu").get(0);
         });
        /****************************/

	//header send resume
//	$(".requirements > a.button").click(function(){
//		$('div.item').removeClass('send');
//		var div = $(this).parents('div.item').addClass('send');
//		txt = $('a.title', div).html();
//		$('form#feedbackForm input[name=vacancy]').val(txt);
//		$('form#feedbackForm, div.message_sent').appendTo(div);
//		return false;
//	});
	
	var fldvls = {};
	var frms = document.forms;
	for (i = 0; i < frms.length; i++) {
		frm = frms[i];
		var name =  frm['name'];
		fldvls[name] = {};
		var flds = $('input[type!=checkbox]', frm).get();
		for (var j in flds) {
			fld = flds[j]['name'];
			fldvls[name][fld] = flds[j].value;
			var val = flds[j]['value'];
			$(flds[j]).focus(function(){
				var frm = $(this).parents('form');
				if ($(this).attr('value') == fldvls[$(frm).attr('name')][$(this).attr('name')]) {
					$(this).attr('value', '');
				}
			});
			$(flds[j]).blur(function(){
				if ($(this).attr('value') == '') {
					var frm = $(this).parents('form');
					$(this).attr('value',fldvls[$(frm).attr('name')][$(this).attr('name')]);
				}
			});
		}
	}
	
	var forms = $('form').get();
	for (var i in forms) {
		var form = forms[i];
		$("a.order, a.send, a.fdbbutton, img.send", form).bind('click', function(e){
			submitForm(this);
		});
	}
});

function ajaxFileUpload(field, id, callback)
{
	var form = $(field).parents('form');
	var furl = $(form).get(0).action.replace('submit', 'submitFile');
//	console.log(furl);
	var file = field.id;

	$(".waiting")
	.ajaxComplete(function(){
		$(this).prev('a.order, a.send', form).show();
		$(this).remove();
		callback();
	})
	/*.ajaxStart(function(){
		$(this).show();
	})*/;

	$.ajaxFileUpload({
		url: furl,
		secureuri: false,
		fileElementId: file,
		data: {'id':id},
		dataType: 'json',
		complete: function() {
		},
		success: function (data, status) {
//			if(typeof(data.error) != 'undefined') {
//				if(data.error != '') {
//					alert(data.error);
//				} else {
//					alert(data.msg);
//				}
//			}
		},
		error: function (data, status, e) {
//			alert(e);
		}
	})
	return false;
}

function submitForm(a) {
	var form = $(a).parents('form');
		$('input, textarea, select', form).parents('td.two').removeClass('error');
		$('img.code', form).parents('td.two').removeClass('error');
	if (form) {
		var files = $("input[type=file]", form).get();
		for (var i in files) {
			var uploaded = $('input[name=uploaded]',form).get(0);
			var fld = files[i];
			uploaded.value += ((uploaded.value != '') ? ', ' : '') + fld.value;
		}
       
		$(a).hide().after('<img class="waiting" src="images/loading.gif" alt="uploading..." />');
       // console.log("there");
		$.postJSON($(form).attr('action'), $(form).serialize(), function(data){
              if (data.result != true) {
				$(a).show().next().remove();
				if (data.inc_fields){
					for (var i in data.inc_fields) {
						$('input[name=' + data.inc_fields[i] + '], select[name=' + data.inc_fields[i] + '], textarea[name=' + data.inc_fields[i] + ']', form).parents('td.two').addClass('error');
					}
				}
				if (data.captcha && data.captcha == true) {
					$('input.code', form).parents('td.two').addClass("error");
				}
			} else {
				if (files.length > 0 && data.id) {
					for (var i in files) {
						ajaxFileUpload(files[i], data.id, function(){
							$(a).show().next().remove();
							$(form).children().hide();
							$("select[name=formtype]", $('form', $(form).parent())).hide();
							$('div.message_sent', $(form).parent()).show();
						});
					}					
				} else {
					$(a).show().next().remove();
					$(form).children().hide();
					$("select[name=formtype]", $('form', $(form).parent())).hide();
					$('div.message_sent', $(form).parent()).show();
				}
			}
		})
	}
	return false;
}

$.postJSON = function(url, data, callback) {
	$.post(url, data, callback, "json");

};
/************************/

$(document).ready(function(){
     //ссылки новостей
    $("a.news_link").hover(function(){
            $(this).prev("p").addClass("item_date_current");
        }, function(){
            $(this).prev().removeClass("item_date_current");
    });
    $("div.news_item > a").hover(function(){
            $(this).prev("p").addClass("choose");
        }, function(){
            $(this).prev().removeClass("choose");
    });
    //ссылки футера
    $(" div.artARTERY > p > a").hover(function(){
            $(this).parent().next("a").addClass("hovering");
        }, function(){
            $(this).parent().next().removeClass("hovering");
    });
    //поиск
    $("a.search").click(function () {
            $("div.search").slideDown("slow");
        });
        $("img.s_close").click(function(){
            $("div.search, div.ask_question").slideUp("slow");
    });
    //задай вопрос
    $("a.head").click(function () {
        $("div.ask_question").slideDown("slow");
    });
});
           
/***********************/
$(document).ready(function(){
	// AUTOLOAD CODE BLOCK (MAY BE CHANGED OR REMOVED)
	jQuery(function($) {
		$("a[rel^='lightbox']").slimbox({counterText:img_full_txt}, null, function(el) {
			return (this == el) || ((this.rel.length > 8) && (this.rel == el.rel));
		});
	});
});
