(function($){
	$.fn.orderbox = function(options){
		var $mainelement = $(this);
		// настройки по умолчанию
		var options = $.extend({
			handler: '', // дирректория ajax-обработчика
			action: 'order', // действие, передаваемое обработчику
			loader: '/template/ico/loader.gif' //изображение загрузчика
		},options);

		$('div.edit',this).click(function(){
				var element = $(this).parent('div');
				if($('>ul',element).height() > 150) {$('>ul',element).height(150); $('>ul',element).width($('>ul',element).width()+10);}
				$('>ul li',element).hover(function(){
					if($(this).attr('rec_id')!='search') $(this).css('background-color', '#749a2c').attr('selected', 'selected');
				}, function(){
					if($(this).attr('rec_id')!='search') $(this).css('background-color', 'transparent').removeAttr('selected');
				});
				$(element).hover(function(){
				}, function(){
					/*$('ul li input',this).val('');
					$('ul li',this).show();
					$('ul',element).hide(); 
					$('div.jScrollPaneContainer',element).hide(); 
					$('ul',element).jScrollPaneRemove();*/
				});
				$('>ul',element).show();
				$('>div.jScrollPaneContainer',element).show();
				$('>ul',element).jScrollPane({scrollbarMargin: 0, showArrows: true});
				$('ul input',element).trigger("focus");
		});

		/*$('ul input',this).keypress(function (e){
			$ul = $(this).parent('li').parent('ul');
			switch(e.keyCode){
				case 38://UP
					if($("li[selected = 'selected']",$ul).length){
						$thisUL = $("li[selected = 'selected']",$ul);
						$prevUL = $thisUL.prev();
						if($prevUL.attr('rec_id')!='search'){
							$thisUL.css('background-color', 'transparent').removeAttr('selected');
							$prevUL.css('background-color', '#749a2c').attr('selected', 'selected');
						}
					}else{
						$thisUL = $('li:first',$ul);
						if($thisUL.attr('rec_id')=='search') $thisUL = $thisUL.next();
						$thisUL.css('background-color', '#749a2c').attr('selected', 'selected');
					}
				break;
				case 40://DOWN
					if($("li[selected = 'selected']",$ul).length){
						$thisUL = $("li[selected = 'selected']",$ul);
						$nextUL = $thisUL.next();
						if($nextUL.attr('rec_id')!='search'){
							$thisUL.css('background-color', 'transparent').removeAttr('selected');
							$nextUL.css('background-color', '#749a2c').attr('selected', 'selected');
						}
					}else{
						$thisUL = $('li:first',$ul);
						if($thisUL.attr('rec_id')=='search') $thisUL = $thisUL.next();
						$thisUL.css('background-color', '#749a2c').attr('selected', 'selected');
					}
				break;
			}
		});*/

		$('>ul li',this).click(function(){
				var element = $(this).parent('ul').parent('div').parent('div');
				if($(this).attr('rec_id')!='search'){
					var modal = $('<div></div>').appendTo(document.body);
					modal.css({height: $(document).height(),width: '100%',background: 'url('+options.loader+') center center no-repeat #000',position: 'absolute',top: 0,left: 0,opacity: 0.5,filter: 'Alpha(Opacity=50)',zIndex: 2000});
					$.post( options.handler, { action: options.action, id1: $('>div.edit',element).attr('rec_id'), id2: $(this).attr('rec_id'), parent_id: $('>div.edit',element).attr('parent_id'), page: $('>div.edit',element).attr('page') }, function(data){
                        //alert(data);
                        $('#content_admin').html(data.content);
                        modal.remove(); init(); 
                    }, "json");
				}
		});

		$('>ul li input',this).keyup(function(){
				var element = $(this).parent('li').parent('ul').parent('div').parent('div');
				var search = $(this).val();
				$.each($('ul li',element), function(){
					var str = $(this).text();
					var reg = new RegExp(search.toLowerCase(), "i");
					var result = reg.test(str.toLowerCase());
					if(!result && $(this).attr('rec_id')!='search' && $(this).attr('rec_id')!=0) $(this).hide();
					else $(this).show();
				});
				$('ul',element).jScrollPane({scrollbarMargin: 0, showArrows: true});
		});
	};
	
	
	
	
	
})(jQuery);

(function($){
	$.fn.multibox = function(options){
		var element = $(this);
		var selected = '';
		// настройки по умолчанию
		var options = $.extend({
			action: 'order', // действие, передаваемое обработчику
			loader: '/template/ico/loader.gif' //изображение загрузчика
		},options);

		$('>ul li',element).hover(
			function(){
				if($(this).attr('type')!=1) $(this).css('backgroundColor', '#749a2c');
			},
			function(){
				if($(this).attr('type')!=1) $(this).css('backgroundColor', 'transparent');
			}
		);

		$('>ul li',element).click(
			function (){
				if($(this).attr('type')==0){ $(this).css('backgroundColor', '#749a2c'); $(this).attr('type',1); }
				else{ $(this).css('backgroundColor', 'transparent'); $(this).attr('type',0); }
				selected = '';
				$.each($("li[type=1]",element), function(){ selected += $(this).attr('rec_id')+'|'; });
				$('>input',element).val(selected);
			}
		);

		element.jScrollPane({scrollbarMargin: 0, showArrows: true});
		element.parent('div').css({position: 'relative', marginBottom: '14px'});

	};
})(jQuery);

(function($){
	$.fn.checkbox = function(options){
		var element = $(this);
		var selected = '';
		// настройки по умолчанию
		var options = $.extend({
			check: '/template/ico/on.gif', // стоит галочка
			uncheck: '/template/ico/off.gif' // не стоит галочка
		},options);

		$('>ul li img',element).click(
			function (){
				if($(this).parent('li').attr('rel')==0){ $(this).parent('li').attr('rel',1); $(this).attr('src',options.check); }
				else{ $(this).parent('li').attr('rel',0); $(this).attr('src',options.uncheck); }
				selected = '';
				$.each($("li[rel=1]",element), function(){ selected += $(this).attr('val')+'|'; });
				$('>input',element).val(selected);
                if($('>input',element).valid() == 1) 1;
			}
		);
	};
})(jQuery);


function listbox(obj){
	jQuery(function($){
		$('ul', obj).css('margin-bottom','-'+$(this).height()+'px');
		$('ul li',obj).click(function(){$(this).parent('ul').siblings('div').text($(this).text()); $('input',$(this).parent('ul').parent('div')).val($(this).attr('rel')); $(this).parent('ul').siblings('div').css('background','url(/template/ico/select.gif) top right no-repeat #749a2c'); $(this).parent('ul').hide(); if($('input',$(this).parent('ul').parent('div')).valid() == 1) 1;});
		$('ul li',obj).hover(function(){$(this).css('background-color', '#749a2c');}, function(){$(this).css('background-color', 'transparent');});
		obj.hover(function(){}, function(){$('div',this).css('background','url(/template/ico/select.gif) top right no-repeat #749a2c'); $('ul',this).hide();});
		$('div',obj).click(function(){
            var $div = $(this).parent('div');
			if($('ul',$div).width() < $(this).width()) $('ul',$div).width($(this).width()-2);
			if($('ul',$div).height() > 200) $('ul',$div).height(200);
			$(this).css('background','url(/template/ico/select_s.gif) top right no-repeat #749a2c');
			$('ul',$div).show();
		});
	});
}

function info(obj, hideTarget){
    hideTarget = hideTarget || false;
    obj.hover(
        function(){$('div',this).show();},
        function(){$('div',this).hide();}
    );
    if (hideTarget) {
        $('div',obj).mouseover(function(){
            $(this).hide();
        });
    }
}


jQuery(function($){

	if ($(window).width()<=1024) $("a#btn-write").css("width","64px");
	

	$('#callDialog').dialog({
		title: 'Мы вам перезвоним!',
		autoOpen: false,
		closeText: '',
		bgiframe: true,
		modal: true,
		width: 384,
		//height: 400,
		resizable: false,
		draggable: true,
		position: 'center'
		
	});
	$('#writeDialog').dialog({
		title: 'Напишите нам!',
		autoOpen: false,
		closeText: '',
		bgiframe: true,
		modal: true,
		width: 384,
		//height: 400,
		resizable: false,
		draggable: true,
		position: 'center'
		
	});

$('a#btn-call').click(function(){$('#callDialog').dialog('open');});
$('a#btn-write').click(function(){$('#writeDialog').dialog('open');});


$("form#form input[type='text']").click(function(){
		if ($(this).attr("change")=="false" || $(this).val()==$(this).attr("lab")) {
			$(this).val("");
		}
		$(this).attr("change",true);
		
		
	});
	
	$("form#form input[type='text']").blur(function(){
		
			if ($(this).attr("change")=="true" && $(this).val()=="")
			{
				$(this).val($(this).attr("lab"));
			}
		
		
	});
	$("form#form textarea").blur(function(){
		
			if ($(this).attr("change")=="true" && $(this).val()=="")
			{
				$(this).val($(this).attr("lab"));
				
			}
		
		
	});
	
	
	$("form#form textarea").click(function(){
		if ($(this).attr("change")=="false" || $(this).val()==$(this).attr("lab")) {
			$(this).val("");
		}
		$(this).attr("change",true);
	});
	
	$("form#form input[type='text']").live("keypress",function(){
		if ($(this).attr("change")=="false" || $(this).val()==$(this).attr("lab")) {
			$(this).val("");
		}
		$(this).attr("change",true);
		
		
		
	});
	$("form#form textarea").live("keypress",function(){
		if ($(this).attr("change")=="false" || $(this).val()==$(this).attr("lab")) {
			$(this).val("");
		}
		$(this).attr("change",true);
		
		
	});
	
	$("form#form input[type='submit']").click(function() {
		$("form#form input[type='text']").each(function() {
			if ($(this).attr("change")=="false" || $(this).val()==$(this).attr("lab"))
			{$(this).attr("change",true);
				$(this).val("");
			}
		
		});
		$("form#form textarea").each(function() {
			if ($(this).attr("change")=="false" || $(this).val()==$(this).attr("lab"))
			{$(this).attr("change",true);
				$(this).val("");
			}
		
		});
		
	
	});
	
	
						
					$("#form").validate({
						
						submitHandler: function(form) {
									$(form).ajaxSubmit({
									
										success: function(data){
											$(form).resetForm();
											alert("Ваша сообщение успешно отправлено!");
											$("form#form input[type='text']").attr("change","false");
											$("form#form textarea").attr("change","false");
											$("div#callDialog").dialog("close");
											
										}
										
									});
								},
								rules:{
									"form[name]": "required",
									"form[text]": {required: true, spamtags: true},
									"form[phone]": {required: true}
									
									
									
								},
								messages : {
									"form[name]": "Введите ваше имя",
									"form[text]": {required: "Введите вопрос", spamtags: "Вопрос не должен содержать спец. символы, html теги!"},
									"form[phone]": {required: "Введите телефон"}
									
									
								}
							});
						
	$("form#form2 input[type='text']").click(function(){
		if ($(this).attr("change")=="false" || $(this).val()==$(this).attr("lab")) {
			$(this).val("");
		}
		$(this).attr("change",true);
		
		
	});
	
	$("form#form2 input[type='text']").blur(function(){
		
			if ($(this).attr("change")=="true" && $(this).val()=="")
			{
				$(this).val($(this).attr("lab"));
			}
		
		
	});
	$("form#form2 textarea").blur(function(){
		
			if ($(this).attr("change")=="true" && $(this).val()=="")
			{
				$(this).val($(this).attr("lab"));
				
			}
		
		
	});
	
	
	$("form#form2 textarea").click(function(){
		if ($(this).attr("change")=="false" || $(this).val()==$(this).attr("lab")) {
			$(this).val("");
		}
		$(this).attr("change",true);
	});
	
	$("form#form2 input[type='text']").live("keypress",function(){
		if ($(this).attr("change")=="false" || $(this).val()==$(this).attr("lab")) {
			$(this).val("");
		}
		$(this).attr("change",true);
		
		
		
	});
	$("form#form2 textarea").live("keypress",function(){
		if ($(this).attr("change")=="false" || $(this).val()==$(this).attr("lab")) {
			$(this).val("");
		}
		$(this).attr("change",true);
		
		
	});
	
	$("form#form2 input[type='submit']").click(function() {
		$("form#form2 input[type='text']").each(function() {
			if ($(this).attr("change")=="false" || $(this).val()==$(this).attr("lab"))
			{$(this).attr("change",true);
				$(this).val("");
			}
		
		});
		$("form#form2 textarea").each(function() {
			if ($(this).attr("change")=="false" || $(this).val()==$(this).attr("lab"))
			{$(this).attr("change",true);
				$(this).val("");
			}
		
		});
		
	
	});
	
	
						
					$("#form2").validate({
						
						submitHandler: function(form) {
									$(form).ajaxSubmit({
									
										success: function(data){
											$(form).resetForm();
											alert("Ваша сообщение успешно отправлено!");
											$("form#form2 input[type='text']").attr("change","false");
											$("form#form2 textarea").attr("change","false");
											$("div#writeDialog").dialog("close");
											
										}
										
									});
								},
								rules:{
									"form[name]": "required",
									"form[text]": {required: true, spamtags: true},
									"form[email]": {required: true, email: true},
									"form[phone]": {required: true}
									
									
									
								},
								messages : {
									"form[name]": "Введите ваше имя",
									"form[text]": {required: "Введите вопрос", spamtags: "Вопрос не должен содержать спец. символы, html теги!"},
									"form[email]": {required: "Введите email", email: "Введите корректный email(например: ivan@mail.ru)"},
									"form[phone]": {required: "Введите телефон"}
									
									
								}
							});					

});
