var loginTimeout;

    $(document).ready(function() {
    	checkMainMenu();
    					   
    	$(".selectList select#pro").selectbox().bind('change', function(){
    		window.location.href = $(this).val();												
    	});
    	
    	$(".selectList select#curs").selectbox().bind('change', function(){
    		window.location.href = $(this).val();												
    	});
    			
    	$(".selectList select#currency").selectbox().bind('change', function(){
    		window.location.href = $(this).val();												
    	}); 
        
    	$(".selectList select#country").selectbox().bind('change', function(){
            var obj = $('#'+$(this).val());

            if(obj[0] != undefined){
                $('div.selectList2').addClass('readonly').css('display', 'none');
                obj.parents('div.selectList2').removeClass('readonly').css('display', 'block');
                obj.selectbox().bind('change', function(){
                    setAtt('region_hide', $(this).val());
            	});        
                
                setAtt('country_hide', $(this).val());
            }else{
                $('div.selectList2').addClass('readonly').css('display', 'none');
                $('#default_region').css('display', 'block');
                $('#default_region select').selectbox();
            }						
    	});
             		 
    	/*   ------------- begin LEFT SELECT BOXES -----------------   */ 
    	
    	$(".searchFormBlock select").selectbox().bind('change', function(){
    		window.location.href = $(this).val();												
    	});     
    	/*   ------------- end LEFT SELECT BOXES -----------------   */
    	
    	$('#loginLink').click(function(){
    		var act = $('#loginLink').attr('class');
    		
    		if(act == 'active'){
    			$(this).removeClass('active');
    			$('#loginPopupBlock').css('display', 'none');
                $('#podlojka').css('display', 'none');                
    		}else{
    			$(this).addClass('active');
    			$('#loginPopupBlock').css('display', 'block');
                $('#podlojka').css('display', 'block');
    		}
    	});
    			  
    	$("#loginPopupBlock .close").click(function(){
    		clearTimeout(loginTimeout);
    		$("#loginLink").removeClass("active");
    		$("#loginPopupBlock").css("display", "none");										
    	});        
    	
    	if($(".radioBox input").size() > 0){
    		$(".radioBox input").ezMark();
    	}
    	if($(".radioboxesRow label input").size() > 0){
    		$(".radioboxesRow label input").ezMark();	
    	}  
    	
    	$(".sortBlock .jquery-selectbox").mouseenter(function(){
    		$(this).addClass("selecthover");
    	});
    	$(".sortBlock .jquery-selectbox").mouseleave(function(){
    		if($(this).find(".jquery-selectbox-list").css("display") != "block"){
    			$(this).removeClass("selecthover");
    		}
    	});
    	$(".graphicBlock .jquery-selectbox").mouseenter(function(){
    		$(this).addClass("selecthover");
    	});
    	$(".graphicBlock .jquery-selectbox").mouseleave(function(){
    		if($(this).find(".jquery-selectbox-list").css("display") != "block"){
    			$(this).removeClass("selecthover");
    		}
    	});
    });

    function showLoginPopup(){
    	clearTimeout(loginTimeout);
    	$("#loginLink").addClass("active");
    	$("#loginPopupBlock").css("display", "block");	
    }
    
    function hideLoginPopup(){
    	clearTimeout(loginTimeout);
    	loginTimeout = setTimeout(function(){
    		$("#loginLink").removeClass("active");
    		$("#loginPopupBlock").css("display", "none");	
    									   }, 500);
    }

    function hidePopup(id){
    	$("#podlojka").css("display", "none");
    	$("#" + id).css("display", "none");
    }
    
    function showPopup(id){
    	$("#podlojka").css("display", "block");
        $("#podlojka").css("background-color", "#333");
    	$("#" + id).css("display", "block");	
    }
    
    function hideParentDiv(lnk){
    	$(lnk).parents("div").eq(0).hide("slow");
    }
    
    function hide_block(obj) {
        $(obj).hide("slow");
    }
    
    var idTimeout;
    function showSubmenu(li, idName){
    	clearTimeout(idTimeout);
    	
    	$(".subProposeListBlock").attr("class", "subProposeListBlock");
    	
    	$("#" + idName).addClass("active" + ($(li).index() + 1));
    	$("#" + idName).unbind("mouseover");
    	$("#" + idName).unbind("mouseout");
    	
    	$("#" + idName).mouseover(function(){
    		showSubmenu(li, idName);
    	});
    	$("#" + idName).mouseout(function(){
    		hideSubmenu(li, idName);
    	});
    }
    
    function hideSubmenu(li, idName){
    	idTimeout = setTimeout(function(){
    		$("#" + idName).removeClass("active" + ($(li).index() + 1));
    	}, 500);
    }
    
    function getData(obj){
        var idName = obj.id; 
        var level = obj.title;
        
        $(obj).parent("ul").eq(0).find("li").removeClass("active");
        $(obj).addClass("active");    
    
    	$("div.buttonsRightText").attr("style", "display:block");
    
    	$.ajax({
    	  url: '/ajax_form.php?post='+idName+'&level='+level ,
    	  success: function( data ) {
    		  $("#ul" + level).html($(data));
    		  $("#ul" + level).show("slow");
    
    		  if(level == 1){
    			  $("#ul2").html("");
    			  $("#ul3").html("");
    		  }
    		  
    		  if(level == 2){
    			  $("#ul3").html("");
    		  }
    		  
    		  $("#ul" + level).find("li").click(function(){
                 $("#selectid").val(this.id);
    			 setActive($(this), $("#ul" + level)); 
    		  });
    	  }
    	});
    } 
    
    function setActive(li, ul){
    	$("a#submit").unbind("click");

    	if(li.attr("class") == "empty active"){
    		$("div.buttonsRightText").attr("style", "display:none");
            $("a#submit").removeClass("disabled");
            $("a#submit").addClass("active");
            $("a#submit").attr("href", "");
    		$("a#submit").click(function(){
                if($(this).attr('rel') !== ""){
                    var link = $(this).attr('rel') +'/edit/?want=' + $('input#want').attr('value') + '&product=' + $('input#product').attr('value') + '&item_id=' + $('input#selectid').attr('value') + '&create=ok';
                    window.location.href = link;
                    return false;	
                }else{	  
    	            document.forms['form1'].submit(); return false;
                }
    		});            
    	}
        else{
    		$("div.buttonsRightText").attr("style", "display:block");
            $("a#submit").removeAttr("href");
            $("a#submit").removeClass("active");
            $("a#submit").addClass("disabled");
    		$("a#submit").click(function(){
                 return false;
    		});   
    	}   
    }

    function initFieldAddedRows(id, num_rows){
    	var tab = $("#" + id);
    	var numbe = tab.find("input[type='text']").size();
        
    	tab.find("input[type='text']").unbind("blur");
    
        if(numbe%4 == 0 && numbe < 200)
        {
        	tab.find("input[type='text']").eq(numbe - 8).blur(function(){
        		addFieldsRow();
        	});
        }
    
        if(numbe%3 == 0 && numbe < 150)
        {
            
        	tab.find("input[type='text']").eq(numbe - 6).blur(function(){
        	   alert(num_rows);
        		addFieldsRow();
        	});
        }
        
    	function addFieldsRow(){
    		tab.find("tr").last().clone().appendTo(tab);
            
            var num = tab.find("td.numColumn").size();
    
            tab.find("tr").last().find
    		tab.find("tr").last().find(".numColumn").text((num+i) + ".");
    		tab.find("tr").last().find(".nameColumn3 input").attr("id", "in_" + num+i + "_1");
            tab.find("tr").last().find(".descriptionColumn input").attr("id", "in_" + num+i + "_2");
            tab.find("tr").last().find(".priceColumn input").attr("id", "in_" + num+i + "_3");
            tab.find("tr").last().find(".countColumn input").attr("id", "in_" + num+i + "_4");
            
    		tab.find("tr").last().find(".nameColumn3 input").attr("name", "in_" + num+i + "_1");
            tab.find("tr").last().find(".descriptionColumn input").attr("name", "in_" + num+i + "_2");
            tab.find("tr").last().find(".priceColumn input").attr("name", "in_" + num+i + "_3");
            tab.find("tr").last().find(".countColumn input").attr("name", "in_" + num+i + "_4");
            
            tab.find("tr").last().find("input").val("");
    		initFieldAddedRows(id);	
    	}
    }
    
    function addFieldsRow2(id, num_rows){
        var tab = $("#" + id);
        
        for(i=0; i<=(num_rows-1); i++){
            var num = tab.find("td.numColumn").size()+1;
    
            if(num > 150){
                break;
            }
                    
            tab.find("tr").last().clone().appendTo(tab);
    
            //tab.find("tr").last().find
        	tab.find("tr").last().find(".numColumn").text(num + ".");
        	tab.find("tr").last().find(".nameColumn3 input").attr("id", "in_" + num + "_1");
            tab.find("tr").last().find(".descriptionColumn input").attr("id", "in_" + num + "_2");
            tab.find("tr").last().find(".priceColumn input").attr("id", "in_" + num + "_3");
            tab.find("tr").last().find(".countColumn input").attr("id", "in_" + num + "_4");
            
        	tab.find("tr").last().find(".nameColumn3 input").attr("name", "in_" + num + "_1");
            tab.find("tr").last().find(".descriptionColumn input").attr("name", "in_" + num + "_2");
            tab.find("tr").last().find(".priceColumn input").attr("name", "in_" + num + "_3");
            tab.find("tr").last().find(".countColumn input").attr("name", "in_" + num + "_4");
            
            tab.find("tr").last().find("input").val("");
        }
    }
    
    function checkMainMenu(){
    	var td = $("#menu td.active");
    	
    	if(td.index() > 0){
    		$("#menu td").eq(td.index() - 1).addClass("prevTd");	
    	}
    }


/*---- NEW !!! AUTOCOMPLETTE ---*/
    function closeAutocomplette () {
        $('div.finds').hide();
        $('div.substrates').remove();
    }

    function findReg(obj, event, regs_arr){

        if(obj.id == 'country'){
           var search_arr = regs_arr[obj.id]; 
        }else if(obj.id == 'region'){
            var search_arr = regs_arr[obj.id][$('#country_hide').val()];
        }else if(obj.id == 'city'){
            var search_arr = regs_arr[obj.id][$('#region_hide').val()];
        }else{
            return false;
        } 
                 
        var val = obj.value;
        var finds = $(obj).parent().children('div.finds');    
        var expr = new RegExp(val, 'i');
        var show_block = false;
        var add_html = '';
        var str = '';
        
        i = 0;
        for (var key in search_arr) {
            if(search_arr[key].search(expr) != -1){
                show_block = true;
                i += 1;
                add_html += '<span id="' + key + '" reg="' + obj.id + '" onclick="addReg(this)">' + search_arr[key] + '</span>';
                str = search_arr[key];
            }
        }

        if(show_block == true){
            finds.html(add_html);
            finds.show();
            $('body').prepend('<div class="substrates" onclick="closeAutocomplette();"></div>');
        }else{
            setAtt(obj.id + '_hide', 0);
            finds.hide();
        }
        
        if(val.length >= 4 && i == 1) {
            addReg(finds.find('span'));
        }
        
        if(finds.find('span').length == 1 && val.toLowerCase() == str.toLowerCase()){
            addReg(finds.find('span'));
        }
        
        if(event.keyCode == 13 && show_block == true) {
            addReg(finds.find('span:first'));
        }
    }

    function addReg(obj2){
        setAtt($(obj2).attr('reg'), $(obj2).html());
        setAtt($(obj2).attr('reg') + '_hide', $(obj2).attr('id'));
        $(obj2).parent().hide();
    }  

    function setAtt(in_id, val){
        document.getElementById(in_id).setAttribute('value', val);
        document.getElementById(in_id).value = val;
        document.getElementById(in_id).setAttribute('readonly', 'readonly');
    }
    
    function delAtt(in_id, obj){
    	document.getElementById(in_id).setAttribute('value', '');
        document.getElementById(in_id).value = '';
        document.getElementById(in_id).removeAttribute('readonly');
        
        document.getElementById(in_id + '_hide').setAttribute('value', 0);
        document.getElementById(in_id + '_hide').value = 0;

        $('#' + in_id).parent('div').children('img.fieldIcon').attr('src', '/_i/regionIcon.png');

        $('#' + in_id).parent('div').removeClass('act');
        $('#' + in_id).removeClass('act');
                
        obj.style.display = 'none';
    }        

/*---- NEW !!! AUTOCOMPLETTE ---*/

    /*---- AUTOCOMPLETTE ---*/

/*
    function auto(i_id){
        cou_id = document.getElementById('country_hide').getAttribute("value");
        reg_id = document.getElementById('region_hide').getAttribute("value");

        if(i_id == "country"){
        	$("input#country").autocomplete("/request.php?do=country", {
        		width: 280,
        		minChars: 2,
        		selectFirst: true,
        		max:30,
        	}).result(function(click, item) {
                setAtt('country', item[0]);
                setAtt('country_hide', item[1]);
                cou_id = item[1];
        	});   
        }
        
        if(i_id == "region" && ( cou_id != '0' || cou_id != 'undefined' || cou_id != null)){
        	$("input#region").autocomplete("/request.php?do=region&id=" + cou_id, {
        		width: 290,
        		minChars: 2,
        		selectFirst: true,
        		max:30,
        	}).result(function(click, item) {
                setAtt('region', item[0]);
                setAtt('region_hide', item[1]);
                reg_id = item[1];
        	});                
        }

        if(i_id == "city" && (reg_id != '0' || reg_id != 'undefined' || reg_id != null)){
        	$("input#city").autocomplete("/request.php?do=city&id=" + reg_id, {
        		width: 460,
        		minChars: 2,
        		selectFirst: true,
        		max:30,
        	}).result(function(click, item) {
                setAtt('city', item[0]);
                setAtt('city_hide', item[1]);
        	});                
        }
    }
    
    */
    /*---- END autocomplette ---*/
    

    /*function doResize(){
    	if($(".rightColumn .infoBlock2").size() == 1){
    		$(".rightColumn .infoBlock2").eq(0).height($(".wrapper").height() - $("#header").height() - $("#menu").height() - $("#footer").height() - $("#topBanner").height() - $("#menuBot").height() - 150 + "px");
    	}
    }*/      
