$(function(){

   $('a[rel*=blank]').click(function() {
        window.open($(this).attr('href'));
        return false;
   });

   // index rent carousels
   function index_carousel($car_wrap, count)
   {
	   if (typeof count == 'undefined')
	   	count = 3;
	   
	   if ($car_wrap.length) {
	        var $lis = $('li', $car_wrap),
	            height = 0,
	            $car = $('ul', $car_wrap),
	            $over = $('.overflow', $car_wrap),
	            index = 0;
	            
	        for (var i = 0; i < count; i++)
	        	 height += $lis.eq(i).outerHeight(true);
		
		height -= 10;
	        
	        if ($over.height() < height)
	                $over.height(height);
	   };
	   $('.prev, .next', $car_wrap).click(function(){
	        if ($car.is(':animated'))
	                return false;
	        if ($(this).hasClass('next')) {
	                if (index + count >= $lis.length)
	                        return false;
	                index++;
	        } else {
	                if (index < 1)
	                        return false;
	                index--;
	        }
	        
	        $car.animate({
	                top: 0 - $lis.eq(index).position().top
	        }, 300);
	        
	        
	        $('.prev', $car_wrap).css({visibility: (index >= 1 ? 'visible' : 'hidden')});
	        $('.next', $car_wrap).css({visibility: (index + count < $lis.length ? 'visible' : 'hidden')});        
	           
	        return false;
	   });
   }
   
   index_carousel($('.index-rent'));
   index_carousel($('.index-aparts'));


   //index banner
   var B_WIDTH = 684,
       B_PERCENT = .85,
       B_TIME = 650,
       B_COUNT = 3,
       B_SLOGAN_TIME = 150,
       B_SHADOW_TIME = (($.browser.msie && $.browser.version < 8) ? 0 : 200),
       B_INTERVAL = 3000;
       
   var $banner = $('.index-header .banner');
   
   if ($banner.length) { 
	   var $b = $('.b', $banner),
	       banner_offset = $banner.offset(),
	       banner_width  = $banner.width(),
	       b_width = Math.ceil($b.eq(0).width()),
	       b_width_minified = Math.ceil((banner_width - b_width) / (B_COUNT - 1)),
	       b_interval;
	   
	   $b.each(function(){
	   	$(this).width(b_width);
		$(this).css({
	      		right: 'auto',
	      		left: $(this).offset().left - banner_offset.left,
	      		zIndex: $(this).css('zIndex')
	   	});
	   }).unwrap();
	   $b.not('.b1').find('.slogan').hide();
	   $banner.removeClass('nostate');
	   
	   function index_banner_resize()
	   {
	   	banner_width = $banner.width();
		b_width = Math.min(Math.ceil(banner_width * B_PERCENT), B_WIDTH); 
	   	b_width_minified = Math.ceil((banner_width - b_width) / (B_COUNT - 1));
	   
	   	$b.width(b_width);
		$b.find('.img').width(b_width);
	   	
		for (var i = 1; i < B_COUNT; i++) {
			$b.eq(i).css({
				right: 'auto',
				left: b_width_minified * i
			});
		}
	   }
	   index_banner_resize();
	   $(window).resize(index_banner_resize);
	   
	   function b_interval_click() {
	   	$b.each(function(){
			if ($(this).css('zIndex') == B_COUNT) {
			        if ($(this).find('.next').length) {
					$(this).find('.next').click();
				} else {
					$banner.find('.prev:eq(0)').click();			
				}
				return false;
			} else {
				return true;
			}		   
		});
	   }
	   b_interval = setInterval(b_interval_click, B_INTERVAL);
	   
	   $('.prev, .next', $banner).click(function(){
	   	if ($('.b:animated', $banner).length)
	   		return false;
	   	
	   	if (b_interval !== null)
	   		clearInterval(b_interval);
	   
	   	$('.slogan', $banner).animate({
			opacity: 0		   
		}, B_SLOGAN_TIME, function(){
			$(this).hide();
		});
	   	
	   	var $b_this = $(this).parent('.b'),
		    $b_next;
	   	
		if ($(this).hasClass('next')) {
			$b_this.prevAll('.b').add($b_this).each(function(i){
				$(this).find('.img').css({
					right: 'auto',
					left: 0
				}).end().css({
					right: 'auto',
					left: $(this).position().left
				}).animate({
					width: b_width_minified
				}, B_TIME, function(){
					$(this).css({
						zIndex: i + 1
					});
				}).css({
					overflow: 'visible'
				});
			});
			$b_this.nextAll('.b').animate({
				width: b_width
			}, B_TIME).css({
				overflow: 'visible'
			});
			$b_next = $b_this.next('.b');
		} else {
			$b_this.add($b_this.nextAll('.b')).each(function(i){
				$(this).find('.img').css({
					right: 0,
					left: 'auto'
				}).end().css({
					left: 'auto',
					right: banner_width - $(this).position().left - $(this).width()
				}).animate({
					width: b_width_minified
				}, B_TIME, function(){
					$(this).css({
						zIndex: B_COUNT - i - 1
					});
				}).css({
					overflow: 'visible'
				});
			});
			$b_this.prevAll('.b').animate({
				width: b_width
			}, B_TIME).css({
				overflow: 'visible'
			});
			$b_next = $b_this.prev('.b');
		}
		
		
		setTimeout(function(){
			$('.slogan', $b_next).css({
				display: 'block',
				opacity: 0
			}).animate({
				opacity: 1
			}, B_SLOGAN_TIME, function(){
				$(this).clearTypeFix();
			});
		}, B_SLOGAN_TIME * 2);
		
		setTimeout(function(){
			$b_next.css({
				zIndex: B_COUNT
			});
			setTimeout(index_banner_resize, 30);
			b_interval = setInterval(b_interval_click, B_INTERVAL);
		}, B_TIME);
	   	
	   	return false;
	   });
	   
	   $('img, .slogan', $banner).css({cursor: 'pointer'}).click(function(){
	   	if ($('.b:animated', $banner).length)
	   		return false;
	   		
		var $_b = $(this).parents('.b');
		
		setTimeout(function(){
			if ($_b.find('.slogan').is(':visible')) { // current
				if ($_b.find('.next').length) {
					$_b.find('.next').click();
				} else {
					$banner.find('.prev:eq(0)').click();			
				}
			} else { // not current
				var $next = $_b.next('.b'),
				    $prev = $_b.prev('.b');
				    
				    if ($next.length)
				    	next_index = parseInt($next.css('zIndex'));
				    else
				    	next_index = 0;
				    
				    if ($prev.length)	
				    	prev_index = parseInt($prev.css('zIndex'));
				    else
				    	prev_index = 0;
				    	
				if (next_index >= prev_index) {
					$next.find('.prev').click();
				} else {
				        $prev.find('.next').click();
				}
				
			}
		}, 40);
		return false;
	   });
    }
   
});


/******* pupup faders *******/
function faderResize(){
    var height = $('.all').height();
    if ($('#gal').length) {
        var galheight = $('#gal').offset().top + $('#gal').height() + 20;
        if (galheight > height)
            height = galheight;
    } 
    $('#fader').height(height).width($('.all').width());
};

function popup_show()
{
    $('body').append('<div id="fader"></div>');
    faderResize();
    $('#fader').css({
        opacity: '0'
    }).animate({
        opacity: '0.8'
    }, 100).attr('title', 'Закрыть').click(function(){
        popup_hide();
    });
    $(window).bind('resize', faderResize);
}

function popup_hide()
{
    $('#fader').animate({
        opacity: '0'
    }, 100, function(){
        $(this).remove();
        $('#gal').remove();
        $('#callback').hide().find('p').remove().end().find('form').show();
    });
    
    $(window).unbind('resize', faderResize);
    return false;
}
/******* / pupup faders *******/


function applyHandler(o, e, f) {
	e = (e instanceof Array) ? (arguments.callee(o, e[1], f), e[0]) : e;
	return (o.attachEvent) ? o.attachEvent(e, f) : (o.addEventListener) ? o.addEventListener(e.substr(2), f, false) : false;
} 




/*function getwidth() {
	var x = document.getElementById('srch');
	if(x) create_label(x,'Поиск по сайту');

	var w = document.documentElement.clientWidth;
	var p = '0.5%';
	if (w > 1024 && w <= 1200) p = '1%';
	else if (w > 1200 && w <= 1600) p = '1.5%';
	else if (w > 1600) p = '2%';
	var m = document.getElementById("menu");
	if (!m) return;
	else {
		var mnum = m.childNodes;
		for (var i = 0; i < mnum.length; i++) {
			if (mnum[i].tagName == "LI") {
				mnum[i].style.paddingRight = p;
			}
		}
	}

	var g = document.getElementById('gallery');
	var c = document.getElementById('control');
	var s = document.getElementById('slideshow');
	var sc = document.getElementById('scroller');
	if (!s) return;
	else {
		if (s.offsetWidth >= sc.offsetWidth) {
			g.style.padding = '0';
			g.style.marginLeft = '-5px';
			c.style.display = 'none';
			c.style.visibility = 'hidden';
			sc.style.left = '0';
		} else {
			g.style.padding = '0 20px';
			g.style.marginLeft = '0';
			c.style.display = 'block';
			c.style.visibility = 'visible';
			sc.style.left = '0';
		}
	}
}


applyHandler(window, ['onload', 'onresize'], getwidth);   */
window.onload = function(){
	var x = document.getElementById('srch');
	if(x) create_label(x,'Поиск по сайту');
};





function pmenu (id) {
	var h = document.getElementById("popupmenu" + id);
	if (!h) return;
	else {
		if (h.style.display == "block") h.style.display = "none";	
		else h.style.display = "block";
	}
}





function popup (id, w, h, p) {
	var a = p + id + '.jpg';
	var x = (document.body.clientWidth - w) / 2;
	var y = (document.body.clientHeight - h) / 2;
	picWin = open('','','width=' + w + ',height=' + h + ',left=' + x + ',top=' + y + ',screenX=' + x + ',screenY=' + y +  ',toolbar=no,location=no,directories=no,copyhistory=no,scrollbars=no,menubar=no,resizable=no,status=no');
	picWin.document.write('<html><head><title>KR Properties</title></head><body style="margin:0;padding:0;"><img src="' + a + '" width="' + w + '" height="' + h + '" alt="" onclick="window.close();" title="Закрыть" style="cursor:pointer;" /></body></html>');
}






function movie (n, w, h) {
	document.write('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="' + w + '" height="' + h + '"><param name="allowFullScreen" value="false" /><param name="movie" value="' + n + '" /><param name="quality" value="high" /><param name="wmode" value="transparent" /><param name="autoStart" value="-1" /><embed src="' + n + '" quality="high" wmode="transparent" width="' + w + '" height="' + h + '" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></object>');
}






function input_over (obj, text) {
	if (obj.value == text) {
		obj.style.color = '#000';
		obj.value = '';
	}
}

function input_out (obj, text) {
	if (obj.value == '' || obj.value == text) {
		obj.style.color = '#b3b3b3';
		obj.value = text;
	}
}

function create_label (obj, text) {
	obj.onfocus = function () { input_over (this, text); };
	obj.onblur = function () { input_out (this, text); };
	input_out (obj, text);
}






// cleartype fix for IE
;(function($) {
	$.fn.clearTypeFix = function() {
		if ($.browser.msie)
			return this.each(function() {
				if (this.style.filter != 'undefined' &&
					this.style.removeAttribute)
					this.style.removeAttribute('filter');
			});
	};
})(jQuery);
