(function($) {

	$(document).ready(function() {
		
		$('#deal_state_form').submit(function() {
			var number = $('#dsf_number').val();
			var pin    = $('#dsf_pin').val();
			$.ajax({
				url: '/main/det_deal_state/'+number+'/'+pin,
				dataType: 'json',
				success: function(response) {
					if(response.state == 'ok') {
						alert(response.text);
					} else {
						alert('Номер счёта или пин-код указаны не правильно');
					}
				}
			});
			return false;
		});
		
		$('.val_label').focus(function() {
			if($(this).val() == $(this).attr('rel')) {
				$(this).val('');
			}
		});
		$('.val_label').blur(function() {
			if($(this).val() == '') {
				$(this).val($(this).attr('rel'));
			}
		});
		
		/* menu */
			$('.main_menu td').hover(
				function(){
					if($(this).find('.href').hasClass('childs')){
						$(this).addClass('hover');
					}
				},
				function(){
					$(this).removeClass('hover');
				}
			)
			.each(function() {  
				$(this).find('.sub_item:last a').css('background-image', 'none');  
				//alert($(this).find('.sub_item').length);
				//alert('!');  
			}); ;
		/* menu - END */
		
		$('.news_rubriks-page .rubrik:last .line').hide();
		
		

		// -- определяем высоту #shadow
		//$('#shadow').css('height', $(document).height()+'px');
		
		// -- устанавливаем ширину ul в #ajaxGalleryBlock
		$('#preview ul').css('width', ($('#scroll li').length*150)+'px');		
		
		$('#shadow').live('click', function() {
			$('#ajaxGalleryBlock').fadeOut();
			$('#pic_editor').fadeOut();
			$(this).hide();
		});
		
		// -- скроллер в фотогаллерее
		var sliderCounter = 0;
		$('#rightArrow').live('click',function() {
			var widthPreview = $('#scroll li img').width();
			if((Math.ceil(($('#scroll ul a').length/2)-1)) == sliderCounter) {
				sliderCounter = 0;
			} else {
				sliderCounter++;
			}
			$('#scroll ul').animate({
				marginLeft :'-'+2*(sliderCounter*widthPreview)+'px'
			}, 400);
		});
		
		$('#leftArrow').live('click',function() {
			var widthPreview = $('#scroll li img').width();
			if(sliderCounter !== 0) {
				sliderCounter--;
			} else {
				sliderCounter = Math.ceil(($('#scroll ul a').length/2)-1);
			}
			$('#scroll ul').animate({
				marginLeft:'-'+2*(sliderCounter*widthPreview)+'px'
			}, 400);
		});
		
		// -- клик на фото превью в основном документе
		$('.ajaxGallery a').removeAttr('onClick').live('click', function() {
			var doc    = document.documentElement || document.body;
			var elLeft = Math.round(doc.scrollLeft+(doc.clientWidth-840)/2)+'px';
			//var elLeft = Math.round(doc.scrollLeft+(doc.clientHeight-791)/2)+'px';
			var elTop  = Math.round(doc.scrollTop)+'px';				
			var id = $(this).attr('href').substring(1);
			$('#shadow').fadeIn();
			$('#shadow').css({ 'height' : $(document).height() +'px'});
			$('#ajaxGalleryBlock').css({'top':elTop,'left':elLeft}).fadeIn();
			$('#ajaxGalleryBlock #mainImg img')
				.attr("src","/application/includes/img/b/"+id+".jpg")
				.attr("onClick","nextImg("+id+");");
			var note = $('#photo_note_'+id).text();
			if(note != '') {
				$('#mainNote').text(note).show();
			} else {
				$('#mainNote').text(note).hide();
			}
			return false;
		});
		
		// -- основное меню
		$('#menu_block li').mouseover(function() {
			this.flag = true;
			setTimeout('showMenu("'+$(this).attr('id')+'");', 5);
		});
		$('#menu_block li').mouseout(function() {
			this.flag = false;
			setTimeout('hideMenu("'+$(this).attr('id')+'");', 5);
		});
		
		if($.browser.msie && $.browser.version == '6.0') {
			$('#menu_block li.preactive').next().addClass('next');
		}
		
		// $('#menu_block li.deep_2').mouseover(function(event) {
			// $(this).addClass('hover');
			// $(this).parent().addClass('mouseover');				
		// });
		// $('#menu_block li.deep_2').mouseout(function(event) {
			// $(this).removeClass('hover');
			// $(this).parent().removeClass('mouseover');			
		// });
		
		
		// && новости, переход в рубрику
		$('.rubrik_title').css('cursor','pointer').click(function() {
			location.href = $(this).attr('rel');
		})
		
		// -- select begin
		var flag  = false;
		var index = 8;
		$('div.select').each(function() {
			var select = $('div.select[id='+$(this).attr('id')+']');
			select.css('z-index', index--);
			select.find('div.input').live('click', function() {
				$('div.select ul').hide();
				var ul = select.find('ul');
				ul.css('display', ((ul.css('display')=='block')?'none':'block'));
				$(this).blur();
				flag = true;
			});
			select.find('li').live('click', function() {
				select.find('div.input').text($(this).text());
				var input = select.find('input');
				var input_id = input.attr('id');
				if(input.attr('id') == 'change_floor_1') {
					$('#change_floor_1_insert').load('/main/load_room_list/', {'floor_str': $(this).text()});
				}
				if(input.attr('id') == 'change_floor_2') {
					$('#change_floor_2_insert').load('/main/load_room_list_free/', {'floor_str': $(this).text()});
				}
				input.attr('value', $(this).text());
				select.find('ul').toggle();
				select.find('li').removeClass('active');
				$(this).addClass('active').blur();
				flag = true;
			});
		});
		
		$('body').live('click', function() {
			if(!flag) {
				$('div.select ul').hide();
			}
			flag = false;
		});
		
		$('div.select ul li').live('mouseover', function() {
			$(this).addClass('hover');
		});
		
		$('div.select ul li').live('mouseout', function() {
			$(this).removeClass('hover');
		});
		// -- select end
		      
		$('.imgLeft').wrap('<div class="wrapImg"><div></div></div>').css({			
			'float': 'none',
			'margin': '0',	
			'border': '1px solid #ccc'
		});
		$('.wrapImg').append('<span style="color: #a09d9d; position: absolute; left: 0; top: ' + ($(this).find('.imgLeft').attr('height') + 30)+ 'px;">' + $(this).find('.imgLeft').attr('title') + '</span>');  
		if ($.browser.msie) {
			$('.wrapImg span').css({
				'position': 'relative',
				'left': '-' + ($(this).find('.imgLeft').attr('width') + 40)+ 'px'
			});
		}

		
	});  
	 
	showImg = function(id) {
		var doc    = document.documentElement || document.body;
		var elLeft = Math.round(doc.scrollLeft+(doc.clientWidth-870)/2)+'px';
		var elTop  = Math.round(doc.scrollTop+(doc.clientHeight-765)/2)+'px';
		$('#shadow').show();
		$('#ajaxGalleryBlock').css({'top':elTop,'left':elLeft}).fadeIn();
		$('#mainImg').html('<a href="#" onClick="return nextImg('+id+');"><img src="/application/includes/img/b/'+id+'.jpg" height="600" /></a>');
	}
	
	replaceImg = function(id, o) {
		$('#mainImg').html('<a href="#" onClick="return nextImg('+id+');"><img src="/application/includes/img/b/'+id+'.jpg" height="600" /></a>');
		$('#mainNote').text($(o).parent().find('span').text());
		return false;
	}
	
	nextImg = function(id) {
		for(var i in photo_ids) {
			if(photo_ids[i] == id) {
				next = parseInt(i)+1;
				if(photo_ids[next] == undefined) {
					next = 0;
				}
				$('#mainImg').html('<a href="#" onClick="return nextImg('+photo_ids[next]+');"><img src="/application/includes/img/b/'+photo_ids[next]+'.jpg" height="600" /></a>');
				$('#mainNote').text($('#scroll a[href=#'+photo_ids[next]+']').parent().find('span').text());
				break;
			}
		}
		return false;
	}

	hideMenu = function(nameid) {
		var sm = document.getElementById(nameid);
		if(!sm.flag) {
			var o  = $('#'+nameid);		
			o.removeClass('hover');
			o.parent().removeClass('mouseover');
			if($.browser.msie && $.browser.version == '6.0') {
				o.find('ul').parent().next().removeClass('next');
				if(!o.hasClass('preactive')) {
					$('#menu_block li.preactive').next().addClass('next');
				}
			}
		}
	}
	
	showMenu = function(nameid) {
		var sm = document.getElementById(nameid);
		if(sm.flag) {
			var o  = $('#'+nameid);		
			o.addClass('hover');
			o.parent().addClass('mouseover')
			if($.browser.msie && $.browser.version == '6.0') {
				o.find('ul').parent().next().addClass('next');
				if(!o.hasClass('preactive')) {
					$('#menu_block li.preactive').next().removeClass('next');
				}
			}
		}
	}

	closeGallery = function() {
		$('#ajaxGalleryBlock').fadeOut();
		$('#shadow').hide();
	}
	   
	// && отправка формы
	$('.f_submit').live('click', function() {
		$(this).parents().filter('form').submit();		
	});	 
	$(':image').css('display', 'none'); 	
	
	// && бордер на фото по макету
 
})(jQuery);
