(function($) {

	$(document).ready(function() {

		// -- определяем высоту #shadow
		$('#shadow').css('height', $('body').height()+'px');
		
		// -- устанавливаем ширину ul в #ajaxGalleryBlock
		$('#preview ul').css('width', ($('#scroll li').length*150)+'px');		
		
		$('#shadow').live('click', function() {
			$('#ajaxGalleryBlock').fadeOut();
			$('#pic_editor').fadeOut();
			$('#ajaxAuthPanel').remove();
			$('#subscribeForm').remove();
			
			$(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-870)/2)+'px';
			var elTop  = Math.round(doc.scrollTop+(doc.clientHeight-765)/2)+'px';
			var id = $(this).attr('href').substring(1);
			$('#shadow').css('height', $('body').height()+'px').show();
			$('#ajaxGalleryBlock').css({'top':elTop,'left':elLeft}).fadeIn();
			$('#ajaxGalleryBlock #mainImg img')
				.attr("src","/application/includes/img/b/"+id+".jpg")
				.attr("onClick","nextImg("+id+");");
			$('#mainNote').text($(this).parent().find('span').text());
			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');			
		// });
		$('#closeAjaxAuthPanel').live('click',function(){
		  $('#ajaxAuthPanel').remove();
		  $('#shadow').hide();
		  $('#subscribeForm').remove();
		  return false;
		})
		
		// -- 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
		
	});
	
	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();
	}

})(jQuery);
function in_array(needle, haystack, strict) {   // Checks if a value exists in an array
	var found = false, key, strict = !!strict;
	for (key in haystack) {
	  if ((strict && haystack[key] === needle) || (!strict && haystack[key] == needle)) {
		found = true;
		break;
	  }
	}
	return found;
}
function is_array( mixed_var ) {
	return ( mixed_var instanceof Array );
}
function empty( mixed_var ) {
	return ( mixed_var === "" || mixed_var === undefined || mixed_var === "undefined" || mixed_var === 0   || mixed_var === "0" || mixed_var === null  || mixed_var === false  ||  ( is_array(mixed_var) && mixed_var.length === 0 ) );
}
