var gal;

function set_page_size(val, name)
{
	$.cookie(name, val, {path: '/', expires: 30*24*60*60});
	document.location = document.location.href.replace(/p=\d+/,'');
}

$(function(){
	// partner links
	$('.partners-trans a').click(function(){
		window.open($(this).attr('href'));
		return false;
	});
	
	// phone switch
	
	$('.phone-switch span').click(function(){
	    $('.phone-switch span').removeClass('active');
	    num = $('.phone-switch span').index(this);
	    $('.switcher').removeClass('active');
	    $('.switcher:eq('+num+')').addClass('active');
		$(this).addClass('active');
	});
	
	// inputs
	$('input').attr('autocomplete', 'off');
	$('input.clearonfocus').each(function(){
		$(this).attr('autocomplete', 'off');
		$(this).data('label', $(this).attr('value'));
	}).focus(function(){
		if ($(this).val() == $(this).data('label'))
			$(this).val('');
	}).blur(function(){
		if ($(this).val() == '')
			$(this).val($(this).data('label'));
	});
	
	// item-order inputs
	$('.item-order .quantity, .cart-contents input.quantity').bind('change blur', function(){
		var $min = $(this).nextAll('small.min').eq(0);
		if ($min.length)
			var min  = parseInt($min.attr('class').replace(/min|-|\s/g, '')) || 0;
		else
			var min = 0;
		$(this).val(
			$(this).val().replace(/[^\d]/g, '') || 0
		);
		
		if (parseInt($(this).val()) < min)
			$(this).val(min);
	});
	$('.item-order .plus, .item-order .minus, .cart-contents .minus, .cart-contents .plus').click(function(){
		var $input = $(this).prevAll('.quantity');
		if ($(this).hasClass('plus'))
			$input.val(parseInt($input.val()) + 1).change();
		else
			$input.val(parseInt($input.val()) - 1).change();
			
		return false;
	});
	$('.item-order').parents('form').submit(function(){
		var $submit = $('input:submit', this);
		$.post('/cart/add/', $(this).serialize(), function(response) {
			if (response > 0) {
				popup_alert('Товар добавлен в корзину', $submit);
				$('.cart-count i').text(response);
				$('.cart-count-0').removeClass('cart-count-0');
			} else
				alert('Произошла ошибка. Проверьте введенные данные и попробуйте ещё раз');
		});
		return false;
	});
	
	$('.item-compact .item-order').click(function(e){
		stopEvent(e);
	});
	$('.item-compact .item-order .submit').click(function(e){
		stopEvent(e);
		$(this).parents('form').submit();
		$(this).parents('.item-order').find('.close').click();
		return false;
	});
	$('.item-compact .item-order .close').click(function(){
		$(this)
			.parents('.item-order')
			.siblings('.form-toggle').removeClass('form-toggle-minus').end()
			.animate({
				opacity: 0
			}, 1000, function(){
				$(this).hide();
			});
		return false;
	});
	$('.item-compact .form-toggle').click(function(){
		if (!$(this).hasClass('form-toggle-minus')) {
			$('.item-order-visible .close').click();
			$(this).addClass('form-toggle-minus').siblings('.item-order').addClass('item-order-visible').stop(true, true).css({
				display: 'block',
				opacity: 1
			});
		} else {
			$('.item-order-visible').stop(true, true).find('.close').click();
		}
		return false;
	});

	// question form
	gal = {
		height: {
			cur:  $('.gal').height(),
			prev: 0,
			form: 0
		},
		ask_top: 0,
		resize_time: 150
	};
	if ($('.ask-trigger').length) {
		gal.ask_top = Math.ceil($('.ask-trigger').position().top + $('.ask-trigger').height());
		if (!(gal.ask_top % 2))
			gal.ask_top++;
		/*var old_height = $('.gal').height(); 
		if (old_height < ask_top) {
			$('.gal, .gal .bg').height(ask_top);
		} */
		
		$('img', '.ask-form').hide();
		$('.ask-form').show();
		gal.height.form = $('table', '.ask-form').outerHeight(true) + 60;
		$('.ask-form').hide();
		$('img', '.ask-form').show();
		
		if (gal.ask_top > gal.height.form)
			gal.height.form = gal.ask_top;
		
		if (!(gal.height.form % 2))
			gal.height.form++;
	}
	
	$('.ask-trigger a').click(function(){
		if ($('.gal').is(':animated'))
			return false;
		
		var _do = function(){
			$('.ask-form').toggle().parents('.bg').toggleClass('bg-up');
			$('.ask-trigger').toggleClass('ask-trigger-active');
			if ($.browser.msie && $.browser.version < 7) {
				$('.ask-form').height($('.item-inside .gal').height());
			}
		} 
		
		if (!$('.ask-form').is(':visible')) {	
			if (gal.height.cur < gal.height.form) {
				gal.height.prev = gal.height.cur;
				$('.ask-form').height(gal.height.form);
				$('.gal').animate({
					height: gal.height.form
				}, gal.resize_time, function(){
					gal.height.cur = gal.height.form;
					_do();
				});
				$.scrollTo('#header', 500);
			} else {
				$('.ask-form').height('100%');
				_do();
			}
		} else {
			$('.ask-form .close').click();
		}
		
		return false;
	});
	$('.ask-form .close').click(function(){
		$('.ask-form').hide().parents('.bg').removeClass('bg-up');
		$('.ask-trigger').removeClass('ask-trigger-active');
		if (gal.height.prev) {
			$('.gal').animate({
				height: gal.height.prev
			}, gal.resize_time);
			gal.height.cur  = gal.height.prev;
			gal.height.prev = 0;
		}
		return false;
	});
	
	$('.ask-form').parents('form').submit(function(){ // валидатор
		var re = /\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;	
		var ok = true;
		
		$('#a-name, #a-email, #a-question, #a-cap').each(function(){
			if (!$(this).val().replace(/\s/g, '').length) {
				alert($('label[for=' + $(this).attr('id') + ']').attr('title'));
				$(this).focus();
				ok = false;
				return false;
			}	
		});
		
		if (ok && $('#a-email').val().match(re) == null) {
			alert('Некорректный адрес электронной почты');
			$('#a-email').focus();
			ok = false;		
		}
		
		if (!ok)
			return false;
	
		if ($(this).hasClass('ajax-ask')) {
			var $form = $(this);
			$.post($form .attr('action'), $form .serialize(), function(response){
				if (response == 1) {
					$('table', $form).hide();
					$('.ok', $form).show();
				} else {
					popup_alert(
						'Проверьте введенные данные и&nbsp;попробуйте повторить запрос',
						$form.find(':submit')
					);
					$('img.cap', $form).attr('src', '/cap/' + Math.random() + '.jpg');
					$('input#a-cap').val('').focus();
				}
			});
			return false; 
		}
	
		//$('a.close', this).click();
	});
	
	$('.login-form').parents('form').submit(function(){ // валидатор
				var re = /\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/; 
		var ok = true;
		
		$('#a-email, #pass').each(function(){
			if (!$(this).val().replace(/\s/g, '').length) {
				alert($('label[for=' + $(this).attr('id') + ']').attr('title'));
				$(this).focus();
				ok = false;
				return false;
			}	
		});
		
		if (ok && $('#a-email').val().match(re) == null) {
			alert('Некорректный адрес электронной почты');
			$('#a-email').focus();
			ok = false;		
		}
		
		if (!ok)
			return false;
	
		$('a.close', this).click();
	});
	
	$('.order-form').parents('form').submit(function(){ // валидатор
		var re = /\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
		var ok = true;

		$('#fio, #tel, #a-email').each(function(){
			if (!$(this).val().replace(/\s/g, '').length) {
				alert($('label[for=' + $(this).attr('id') + ']').attr('title'));
				$(this).focus();
				ok = false;
				return false;
			}
		});

		if (ok && $('#a-email').val().match(re) == null) {
			alert('Некорректный адрес электронной почты');
			$('#a-email').focus();
			ok = false;
		}

		if (!ok)
			return false;

		$('a.close', this).click();
	});
	
	$('.opinion-form').parents('form').submit(function(){ // валидатор
		var ok = true;

		$('#a-opinion').each(function(){
			if (!$(this).val().replace(/\s/g, '').length) {
				alert($('label[for=' + $(this).attr('id') + ']').attr('title'));
				$(this).focus();
				ok = false;
				return false;
			}
		});

		if (!ok)
			return false;

		$('a.close', this).click();
	});
	
	
	// history toggler
	$('h3.history-block .click').css({cursor: 'pointer'}).click(function(){
		$(this).parents('h3').toggleClass('history-expanded').toggleClass('history-collapsed');
		$(this).parents('h3').next('table').toggleClass('expanded').toggleClass('collapsed');
	});
	
	// index paginator
	var index_animated = false;
	var fade_time = 500;
	var scroll_time = 1000;
	var $index_pages = $('#index-pages');
	var $index_slides = $('#slides');
	var $index_submenu = $('#index-submenu');
	var $index_pages_as  = $('.paginator a:not(.prev):not(.next)', $index_pages);
	var index_paginator_last = false;
	$('#index-pages .paginator a').click(function(){
		if (index_animated)
			return false;
		
		var $slides = $('.slides.active .slide', $index_slides); 
		var $cur = $('.slides.active .slide-active', $index_slides);
		var time = scroll_time;
		
		if ($(this).hasClass('active')) {
			return false;
		} else if ($(this).hasClass('next')) {
			var $next = $cur.next('.slide');
			if (!$next.length) {
				//$next = $slides.eq(0);
				var $menu_li = $('#index-menu li.active').next('li');
				if (!$menu_li.length)
					$menu_li = $('#index-menu li:eq(0)');
				$menu_li.find('a').click();
			}
		} else if ($(this).hasClass('prev')) {
			var $next = $cur.prev('.slide');
			if (!$next.length) {
				//$next = $slides.eq($slides.length - 1);
				var $menu_li = $('#index-menu li.active').prev('li');
				if (!$menu_li.length)
					$menu_li = $('#index-menu li:last');
				index_paginator_last = true;
				$menu_li.find('a').click();
			}
		} else {
			var num = $index_pages_as.index(this);
			var $next = $slides.eq(num);
		}
		
		if (!$next.length)
			return false; 
	
		index_animated = true;
		var cur_index  = $slides.index($cur);
		var next_index = $slides.index($next);
		
		$index_pages_as.eq(cur_index).removeClass('active');
		$index_pages_as.eq(next_index).addClass('active');
		if (window.opera) {
			$index_pages.hide().show(1);
		}
		
		var time = Math.abs(next_index - cur_index) * scroll_time;
		if (time > scroll_time)
			time /= 2;
		
		var window_width = $(window).width();
		
		$('.slides.active').css({
			left: (0 - cur_index * 2) * window_width
		}).animate({
			left: (0 - next_index * 2) * window_width
		}, time, function(){
			$(this).css({
				left: (0 - next_index * 200) + '%'
			});
			$cur.removeClass('slide-active');
			$next.addClass('slide-active');
			
			index_animated = false;	
		});
		
	        return false;
	});
	
	// index switch
	$index_pages
		.add($index_slides)
		.add($index_submenu)
		.append('<div class="white-fader" />');
	$index_slides.find('.white-fader').height(700);
	$index_faders = $('.white-fader'); 
	$index_faders.hide();
		
	$('#index-menu a').click(function(){
		if (index_animated)
			return false;
			
		var $li = $(this).parents('li');
		if ($li.hasClass('active'))
			return false; 
	        var id = $li.attr('id').replace('menu-', '');
		
		$('.active', '#index-menu').removeClass('active');
		$li.addClass('active').find('.wrap').addClass('active');
		
		index_animated = true;
		$index_faders.css({
			display: 'block',
			opacity: 0
		}).animate({
			opacity: 1
		}, fade_time);
		
		setTimeout(function(){					
				
			// pages
			$('.cats .active', $index_pages).removeClass('active');
			$('#pages-' + id).addClass('active');
			$('.paginator .active', $index_pages).removeClass('active');
			if (index_paginator_last) {
			 	$('.paginator .last', $index_pages).addClass('active');
			} else {
				$('.paginator .first', $index_pages).addClass('active');
			}
			
			// slides
			$('.slide-active', $index_slides).removeClass('slide-active');
			if (index_paginator_last) {
				$('.slide:last', '#slides-' + id).addClass('slide-active');
			} else {
				$('.slide:eq(0)', '#slides-' + id).addClass('slide-active');
			}				
	
			$('.slides.active', $index_slides).removeClass('active');
			$('#slides-' + id).addClass('active').css({
				left: -200 * $('.slide-active').index('#slides-' + id + ' .slide') + '%'
			});
			
			// submenu
			$('.active', $index_submenu).removeClass('active');
			$('#submenu-' + id).addClass('active')
				
			$index_faders.animate({
				opacity: 0
			}, fade_time);
			
			index_paginator_last = false;
			
		}, fade_time);
		
		setTimeout(function(){
			$index_faders.hide();
			index_animated = false;
		}, fade_time * 2);
		
		return false;
	});
	
	// item-compact zoom
	$('.item-compact').mousemovedZoom({
		left: 80,
		top: 70,
		direction: -1
	});
	$('.item-compact:has(.name)').css({
		cursor: 'pointer'
	}).click(function(){
		document.location = $('.name', this).attr('href');
	});
	
	// item-inside save original picture
	$('.gal .download').click(function(){
		window.open($(this).attr('href'));
		return false;
	});
	
	// item-inside carousel
	var galtime = 500;
	var itemzoomed = false;
	var zoomOpts = {
		left: 274,
		top: 180,
		direction: -1,
		externalTrigger: true
	};
	if ($('.gal300').length) {
		zoomOpts.top = 250;
	} else if ($('.gal500').length) {
		zoomOpts.top = 300;
	}
	$('.item-inside .thumbs a').click(function(){
		if ($(this).parents('li').hasClass('active'))
			return false;
					
		var $li  = $(this).parent();
		var $lis = $('.item-inside .thumbs li');
		var next = $lis.index($li);
		var curr = $lis.index($('.item-inside .thumbs li.active'));
		
		$('.active', $(this).parents('ul')).removeClass('active');
		$li.addClass('active');
		
		itemzoomed = false;
		$('.item-inside .gal .slide-active').mousemovedZoom(zoomOpts, false);
		$('a.zoomout').removeClass('zoomout');
		
		$('.slide-active', $(this).parents('.gal')).removeClass('slide-active');
		var $next = $('.slide', $(this).parents('.gal')).eq(next);
		$next.addClass('slide-active');
		
		$('a.download', $(this).parents('.gal'))
			.attr(
				'href',
				$('.slide-active', $(this).parents('.gal'))
					.find('.small').attr('src')
					.replace('m.', '.')
					.replace('/ufiles/catalog/','/ufiles/catalog/download/')
			);
		
		var height = $next.find('.small').attr('height');
		var gal_height = 425;
		var top = 180;
		var clname = 'top180';
		if (height > 350) {
			gal_height = 647;
			top        = 300;
			clname     = 'top300';
		} else if (height > 250) {
			gal_height = 501;
			top        = 250;
			clname     = 'top250';
		} 
		
		zoomOpts.top = top;
		$next.mousemovedZoom(zoomOpts, false);
		
		if (gal_height != gal.height.cur) {
			$('.gal').animate({
				height: gal_height
			}, gal.resize_time);
			gal.height.cur = gal_height;
		}		
		
		$('.item-inside .gal .carousel').animate({
			left: 0 - 549 * next
		}, galtime * Math.abs(next - curr));
		
		return false;
	});
	$('.item-inside .gal .slide').mousemovedZoom(zoomOpts);
	$('.item-inside .gal .zoom').click(function(){
		var state = true;
		if (itemzoomed)
			state = false;
		$(this).toggleClass('zoomout');
		$('.item-inside .gal .slide-active').mousemovedZoom(zoomOpts, state);
		itemzoomed = state;
		return false;		
	});
  
});







/* stop event */
function stopEvent(e){if(e.preventDefault){e.preventDefault();e.stopPropagation();}else{e.returnValue=false;e.cancelBubble=true;}return false;}

// 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);

// mousemoved zoom
;(function($) {
$.fn.mousemovedZoom = function(o, act) {	
	var s = {
		left: 		 null,
		top:  		 null,
		bigSelector: 	 '.big',
		smallSelector:	 '.small',
		direction:	 -1, // 1 - в ту же сторону, -1 - в противоположную
		timeout:	 250,
		animatetime:	 100,
		noZoomIn:	 true,
		nozoomtime:	 75,
		externalTrigger: false
	};
	
	$.extend(s, o);
	
	if (typeof act == 'boolean') { // externalTrigger: .mousemovedZoom(true|false)
		return this.each(function(){
			
			if (act === true) {
				$(this).data('off', false);
				zoomIn($(this));
			} else if (act === false) {
				$(this).data('off', true); 
				zoomOut($(this));
			}	
		});	
				
	}
	
	return this.each(function(){
		var $big   = $(s.bigSelector, this);
		if (!$big.length) {
			$(this).data('$big', false);
			return;
		} else {
			$(this).data('$big', $big);
		} 
		
		var $small = $(s.smallSelector, this);
		if (!$small.length) {
			$(this).data('$small', false);
			return;
		} else {
			$(this).data('$small', $small);
			if ($small.attr('height') && $small.attr('width')) {
				$(this).data('small_width', $small.attr('width'));
				$(this).data('small_height', $small.attr('height'));
			} else {
				$(this).data('small_width', $small.width());
				$(this).data('small_height', $small.height());
			}
		}
		
		$(this).data('width', $(this).width());
		$(this).data('height', $(this).height());
		
		
		
		
		$(this).mouseenter(function(){
			
			if (s.externalTrigger)
				return;
			
			var $t = $(this);
			$t.data('timeout', true);
			$t.data('timeout_resource', setTimeout(function(){
				zoomIn($t);
			}, s.timeout));
				
		}).mouseleave(function(){
			
			if (s.externalTrigger)
				return;
			zoomOut($(this));
			
		}).mousemove(function(e){
		
			var d = $(this).data();
			
			if (d.off || d.timeout || !d.$big || !d.$small || !d.big_width || !d.big_height)
				return;
			
			
			var x = e.clientX - $(this).offset().left + (document.documentElement.scrollLeft || self.pageXOffset || 0);  
			var y = e.clientY - $(this).offset().top + (document.documentElement.scrollTop  || self.pageYOffset || 0);

			if (s.direction >= 0) {
				x = x * d.big_width / d.width - (d.big_width - d.width) / 2;
				y = y * d.big_height / d.height - (d.big_height - d.height) / 2;
			} else {
				x = (d.big_width + d.width) / 2 - x * d.big_width / d.width;
				y = (d.big_height + d.height) / 2 - y * d.big_height / d.height;			
			} 
			
			d.$big.add(d.$small).css({
				left: x,
				top:  y
			});
		
		});
		
	});
	
	function zoomIn($t)
	{
		var d = $t.data();
				
		if (!d.$big || !d.$small)
			return;
		
		if (!$t.data('big_width') || $t.data('big_height')) {
			if (d.$big.attr('height') && d.$big.attr('width')) {
				$t.data('big_width', d.$big.attr('width'));
				$t.data('big_height', d.$big.attr('height'));
			} else {
				var big_image  = new Image();
				big_image.src  = d.$big.attr('src');
				$t.data('big_width', big_image.width);
				$t.data('big_height', big_image.height);
			}
		}
		
		d.$big.css({
			opacity:    0,
			display:    'block'      
		});
		d.$big.add(d.$small).css({
			position:   'absolute',
			left:	    (s.left? s.left: d.width / 2),
			top:        (s.top?  s.top:  d.height / 2)
		});
		
		var zoomed = {
			marginLeft: 0 - Math.round(d.big_width / 2),
			marginTop:  0 - Math.round(d.big_height / 2),
			width:	    d.big_width,
			height:     d.big_height
		};
		
		if (s.noZoomIn) {
		 	d.$big.add(d.$small).css(zoomed);
		 	d.$big.animate({
			 	opacity: 1
			}, s.nozoomtime);
			d.timeout = false;
		} else {
			d.$big.add(d.$small).css({
				marginLeft: 0 - Math.round(d.small_width / 2),
				marginTop:  0 - Math.round(d.small_height / 2),
				width:	    d.small_width,
				height:     d.small_height
			}).animate(zoomed, s.animatetime, function(){
				d.timeout = false;
			});
		}
		
	};
	
	function zoomOut($t)
	{
		var d = $t.data();
			
		if (!d.$big || !d.$small)
			return;
			
		if (d.timeout_resource)
			clearTimeout($t.data('timeout_resource'));
		
		d.$big.stop(true, true).css({
			display: 'none'
		});
		d.$small.stop(true, true).animate({
			marginLeft: 0 - Math.round(d.small_width / 2),
			marginTop:  0 - Math.round(d.small_height / 2),
			width:	    d.small_width,
			height:	    d.small_height,
			left:	    s.left? s.left: d.width / 2,
			top:        s.top?  s.top:  d.height / 2	    
		}, s.animatetime);
	};
};
})(jQuery);


function popup_alert(text, $button)
{
	$('body').append('<div class="popup"><a href="#" class="close"></a>' + text + '</div>');
	var $popup = $('body > .popup:last');
	$popup.css({
		position: 'absolute',
		top: $button.offset().top + Math.ceil($button.height() / 2),
		left: $button.offset().left + Math.ceil($button.width() / 2) 
	});
	var popupleft = $popup.offset().left;
	var overright = popupleft + $popup.outerWidth() - $(window).width();
	if (overright > 0) {
		$popup.css({
			left: $button.offset().left + $button.width() - $popup.outerWidth(),
			marginLeft: 0
		});					
	}	
	$('.close', $popup).click(function(){
		$popup.remove();
		return false;
	});
	$popup.animate({
		opacity: 0
	}, 2500, function(){
		$popup.remove();
	});
} 

function showpopup(popup){
	getscroll();
	wheight = $(window).height();
	popupheight = popup.height();
	popupheight2 = popup.height()/2;
	docheight = $(document).height();
	if(popupheight>wheight) {
        popup.css('top',yScroll + 10).show();
	}
	else {
        popup.css('top',yScroll + wheight/2 - popupheight2).show();
	}
	$('.overlay').css('height',docheight).css('opacity','0.1').show();
}

function getscroll(){
	if (self.pageYOffset){
		yScroll = self.pageYOffset;
		xScroll = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){
		yScroll = document.documentElement.scrollTop;
		xScroll = document.documentElement.scrollLeft;
	} else if (document.body){
		yScroll = document.body.scrollTop;
		xScroll = document.body.scrollLeft;
	}
}
 
/*
 * Cookie plugin
 * Copyright (c) 2006 Klaus Hartl (stilbuero.de)
 * Dual licensed under the MIT and GPL licenses.
 */
jQuery.cookie=function(name,value,options){if(typeof value!='undefined'){options=options||{};var expires='';if(options.expires&&(typeof options.expires=='number'||options.expires.toGMTString)){var date;if(typeof options.expires=='number'){date=new Date();date.setTime(date.getTime()+(options.expires*24*60*60*1000));}else{date=options.expires;}expires='; expires='+date.toGMTString();}var path=options.path?'; path='+options.path:'';var domain=options.domain?'; domain='+options.domain:'';var secure=options.secure?'; secure':'';document.cookie=[name,'=',encodeURIComponent(value),expires,path,domain,secure].join('');}else{var cookieValue=null;if(document.cookie&&document.cookie!=''){var cookies=document.cookie.split(';');for(var i=0;i<cookies.length;i++){var cookie=jQuery.trim(cookies[i]);if(cookie.substring(0,name.length+1)==(name+'=')){cookieValue=decodeURIComponent(cookie.substring(name.length+1));break;}}}return cookieValue;}}; 


/**
 * jQuery.ScrollTo - Easy element scrolling using jQuery.
 * Copyright (c) 2007-2009 Ariel Flesler - aflesler(at)gmail(dot)com | http://flesler.blogspot.com
 * Dual licensed under MIT and GPL.
 * Date: 5/25/2009
 * @author Ariel Flesler
 * @version 1.4.2
 *
 * http://flesler.blogspot.com/2007/10/jqueryscrollto.html
 */
;(function(d){var k=d.scrollTo=function(a,i,e){d(window).scrollTo(a,i,e)};k.defaults={axis:'xy',duration:parseFloat(d.fn.jquery)>=1.3?0:1};k.window=function(a){return d(window)._scrollable()};d.fn._scrollable=function(){return this.map(function(){var a=this,i=!a.nodeName||d.inArray(a.nodeName.toLowerCase(),['iframe','#document','html','body'])!=-1;if(!i)return a;var e=(a.contentWindow||a).document||a.ownerDocument||a;return d.browser.safari||e.compatMode=='BackCompat'?e.body:e.documentElement})};d.fn.scrollTo=function(n,j,b){if(typeof j=='object'){b=j;j=0}if(typeof b=='function')b={onAfter:b};if(n=='max')n=9e9;b=d.extend({},k.defaults,b);j=j||b.speed||b.duration;b.queue=b.queue&&b.axis.length>1;if(b.queue)j/=2;b.offset=p(b.offset);b.over=p(b.over);return this._scrollable().each(function(){var q=this,r=d(q),f=n,s,g={},u=r.is('html,body');switch(typeof f){case'number':case'string':if(/^([+-]=)?\d+(\.\d+)?(px|%)?$/.test(f)){f=p(f);break}f=d(f,this);case'object':if(f.is||f.style)s=(f=d(f)).offset()}d.each(b.axis.split(''),function(a,i){var e=i=='x'?'Left':'Top',h=e.toLowerCase(),c='scroll'+e,l=q[c],m=k.max(q,i);if(s){g[c]=s[h]+(u?0:l-r.offset()[h]);if(b.margin){g[c]-=parseInt(f.css('margin'+e))||0;g[c]-=parseInt(f.css('border'+e+'Width'))||0}g[c]+=b.offset[h]||0;if(b.over[h])g[c]+=f[i=='x'?'width':'height']()*b.over[h]}else{var o=f[h];g[c]=o.slice&&o.slice(-1)=='%'?parseFloat(o)/100*m:o}if(/^\d+$/.test(g[c]))g[c]=g[c]<=0?0:Math.min(g[c],m);if(!a&&b.queue){if(l!=g[c])t(b.onAfterFirst);delete g[c]}});t(b.onAfter);function t(a){r.animate(g,j,b.easing,a&&function(){a.call(this,n,b)})}}).end()};k.max=function(a,i){var e=i=='x'?'Width':'Height',h='scroll'+e;if(!d(a).is('html,body'))return a[h]-d(a)[e.toLowerCase()]();var c='client'+e,l=a.ownerDocument.documentElement,m=a.ownerDocument.body;return Math.max(l[h],m[h])-Math.min(l[c],m[c])};function p(a){return typeof a=='object'?a:{top:a,left:a}}})(jQuery);
