$(document).ready(function(){


   $('.hover').hover(
    function () {
        $(this).css('background-position','0 -'+parseInt($(this).css('width'))+'px');

    },
    function () {
        $(this).css('background-position','0px 0px');
    });

    $('.hover24').hover(

    function () {
        $(this).css('background-position','0 -24px');
        
    },
    function () {
        $(this).css('background-position','0px 0px');
    });

    $('.dfw-tool').each(function (){

        size=0;
        if($(this).is('.size-16')) size=16;
        else if ($(this).is('.size-24')) size=24;
        else if($(this).is('.size-32')) size=32;

       
        if($(this).attr('rel')) $(this).css('background', 'url('+$(this).attr('rel')+') no-repeat left top transparent');
        
        style={
            'margin-right':$(this).css('margin-right'), 
            'margin-left':$(this).css('margin-left'),
            'margin-top':$(this).css('margin-top'), 
            'margin-bottom':$(this).css('margin-bottom'),
            'position':$(this).css('position'),
            'top':$(this).css('top'),
            'bottom':$(this).css('bottom'),
            'left':$(this).css('left'),
            'right':$(this).css('right')
        };
        
        $(this).wrap('<span class="wrap size-'+size+'"></span>').css({'margin':'0', 'top':0, 'bottom':0, 'left':0, 'right':0, 'position':'static'}).parents('.wrap').css(style);
 

        $(this).hover(
        function () {
            if($(this).is('.flag')) $(this).css('background-position','0 -'+size+'px');
        //    if($(this).is('.blink')) $(this).toggleClass('blink blink-active');
        },
        function () {
            if($(this).is('.flag')) $(this).css('background-position','0px 0px');
         //   if($(this).is('.blink-active')) $(this).toggleClass('blink blink-active');
        });
      
    })

    $('.dfw-tool.blink').live('mousemove', function (){
        $(this).toggleClass('blink blink-active');
        $(this).parents('.wrap').addClass('wrap-acive');
    });
     $('.dfw-tool.blink-active').live('mouseout', function (){
        $(this).parents('.wrap').removeClass('wrap-acive'); 
        $(this).toggleClass('blink blink-active');
    });


    $('.numeric').live('keyup', function(){
        val=$(this).val();
        val=conv_numeric(val)
        $(this).val(val);
    })



});



$(document).ajaxComplete(function(){
    $('.hover24').hover(
    function () {
        $(this).css('background-position','0 -24px');

    },
    function () {
        $(this).css('background-position','0px 0px');
    });

});

window.isset = function (){
	 if (arguments.length===0) return false;
	 var buff=arguments[0];
	 for (var i=0; i<arguments.length; i++){
	  if (typeof(buff)==='undefined') return false;
	  buff = buff[arguments[i+1]];
	 }
	 return true;
	}


$.extend($.expr[":"], {
    "containsFind": function(elem, i, match, array) {
        return (elem.textContent || elem.innerText || "").toLowerCase
().indexOf((match[3] || "").toLowerCase()) >= 0;
    }
});

$(document).ajaxComplete(function(){

$('.selectfinder').each(function(){
    $(this).valDefault('← поиск');
})


$('.selectfinder').bind('keyup',function(){
    
    token=$(this).val();
    match=$(this).attr('name').match(/\[(.*?)\]/);
    if(!match) return;
    select_id=match[1];
    select=$('select#'+select_id);

    select.css('border','1px solid #555');
    select.css('height', ($("select#"+select_id).height()+2)+'px');
    


    if(token!='') {
    res= $("select#"+select_id+" > option:containsFind(" + token + ")");
    if( res.size() == 0 ) {
        $("select#"+select_id+" > option:first").attr("selected", "selected");
        $("select#"+select_id).css('background-color','#f99');
    } else {
        $("select#"+select_id).css('background-color','#fff');
    }

       $("select#"+select_id+" > option").css('background-color','#eee').css('color','#ccc');
       res.css('background-color','#fff').css('color','#000');
       $("select#"+select_id+" > option:containsFind(" + token + "):first").attr("selected", "selected");
        $(this).css({'border':'1px solid #498'});
    } else {
        $("select#"+select_id).css('background-color','#fff');
        $("select#"+select_id+" > option").css('background-color','#fff');
        $("select#"+select_id+" > option:first").attr("selected", "selected");
        $("select#"+select_id+" > option").attr('style','');
         $(this).css({'border':'1px solid #000'});
    }


});
});

var userAgent = navigator.userAgent.toLowerCase();
jQuery.browser = {
	version: (userAgent.match( /.+(?:rv|it|ra|ie|me)[\/: ]([\d.]+)/ ) || [])[1],
	chrome: /chrome/.test( userAgent ),
	safari: /webkit/.test( userAgent ) && !/chrome/.test( userAgent ),
	opera: /opera/.test( userAgent ),
	msie: /msie/.test( userAgent ) && !/opera/.test( userAgent ),
	mozilla: /mozilla/.test( userAgent ) && !/(compatible|webkit)/.test( userAgent )
};
//input[name='имя']




function preload(arrayOfImages) {
    $(arrayOfImages).each(function(){
        $('<img/>')[0].src = this;
        // Alternatively you could use:
        // (new Image()).src = this;
    });
} 


function strip_quote( str ){
    str=str.replace(/(\n|\r)/gi, '');
    return strip_tags(str.replace(/<div.*?\/div>/gi, ''));
}

function nl2br( str ) {
	return str.replace(/([^>])\n/g, '$1<br/>');
}

function br2nl( str ) {
	return str.replace(/<br\s*\/*>/g, "\n");
}
function strip_tags (string) { 
 return string.replace(/<\/?[^>]+>/gi, '');
}


function print_r(arr, level) {
    var print_red_text = "";
    if(!level) level = 0;
    var level_padding = "";
    for(var j=0; j<level+1; j++) level_padding += "    ";
    if(typeof(arr) == 'object') {
        for(var item in arr) {
            var value = arr[item];
            if(typeof(value) == 'object') {
                print_red_text += level_padding + "'" + item + "' :\n";
                print_red_text += print_r(value,level+1);
		}
            else
                print_red_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
        }
    }

    else  print_red_text = "===>"+arr+"<===("+typeof(arr)+")";
    return print_red_text;
}

 function in_array(what, where) {
    for(var i=0; i<where.length; i++)
        if(what == where[i])
            return true;
    return false;
}


$(document).ready(function(){


    $('.spoiler-title').hover(
    function () {
        $(this).addClass('spoiler-title-active');
    },
    function () {
        $(this).removeClass('spoiler-title-active');
    });

	$('.spoiler-title').bind('click', function(){
                p=$(this).parent();
                c=p.find('*').size();
   
                if(c==1) {
                    tag=p.get(0).nodeName.toLowerCase();
                    if(tag=='div' || tag=='p') body=p;
                }
                if(c>1) {
                    body=$(this);
                }  

                for(i=0;i<3;i++) {
                    body=body.next();
                    if(body.is('.spoiler-body')) break;
                }

		if(body.is(':hidden')) {
			body.fadeIn(300);
		} else {
			body.fadeOut(300);
		}
                return false;
	});
        
});

function spoiler(selector) {
    if($(selector).is(':hidden')) {
            body.fadeIn(300);
    } else {
            body.fadeOut(300);
    }
}

function conv_numeric(val) {
     val=parseInt(val.replace(/[^0-9]/g,''));
     if(!val) val=0;
     return val;
}



(function($) {
    $.fn.valDefault = function (text){
            $(this).attr('rel',text);

            if($(this).val()=='') {
                $(this).val(text)
                $(this).addClass('blur');
            }


            $(this).bind("focus", function(){
                    text=$(this).attr('rel');
                    if($(this).val()==text) {
                            $(this).val("");
                            $(this).removeClass('blur');
                    }
            });

            $(this).bind("keyup", function(){

                    text=$(this).attr('rel');
                    if($(this).val()==text) {
                            $(this).val("");
                            $(this).removeClass('blur');
                    }
            });

            $(this).bind("blur", function(){
                    text=$(this).attr('rel');
                    if($(this).val()=='' || $(this).val()==text) {
                            $(this).addClass('blur');
                            $(this).val(text);
                    }
            });
        }

    $.fn.blockEnter =   function (){
        $(this).find('input').keydown(function(event){
            if(event.keyCode == 13) {
                event.preventDefault();
                return false;
            }
        });
    }
    $.fn.runMenu =   function (){
        $(this).find('li').hover(
            function () {
                $(this).addClass('hover');
                $(this).find('> ul').show();
            },
            function () {
                $(this).removeClass('hover');
                $(this).find('> ul').hide();
            });
   }

   $.fn.setHover=function (){
              $(this).hover(
            function () {
                $(this).addClass('hover');
            },
            function () {
                $(this).removeClass('hover');
            });
   }

   $.fn.realVal=function () {
       val=$(this).val();
       if(val==$(this).attr('default')) val='';
       return val;
   }

    $.fn.setSearchForm=function () {
        $(this).submit(function () {
           if($(this).find('input[type=text]').is('.blur')) return false;
        });
    }

     $.fn.serializeJSON=function() {
          var json = {};
         jQuery.map($(this).serializeArray(), function(n, i){
            json[n['name']] = n['value'];
         });
         return json;
     };
 
     $.fn.fidsJSON=function() {
         var json = {};
         jQuery.map($(this).find('input, select, textarea'), function(n, i){
            json[n['id']] = n['value'].substr(0,64);
         });
         return json;
     };
     
     $.fn.fieldsJSON=function() {
          var json = {};
         jQuery.map($(this).find('input, select, textarea'), function(n, i){
            json[n['name']] = n['value'];
         });
         return json;
     };
     
 })(jQuery);
