window._public = $("script[src$='jquery-1.3.2.min.js']").attr("src").replace(/\/js\/.*/, "/");

(function(){
	$.fn.toggleValue = function(attr){
		return this.each(function(){
			var obj = $(this),
				v = obj.attr(attr);
			obj.focus(function(){
				$(this).val(($(this).val() == v) ? '' : $(this).val());
			});
			obj.blur(function(){
				$(this).val(($(this).val() == '') ? v : $(this).val());
			});
		});
	}

	$.fn.trueHeight = function(){
		var height = 0;
		this.each(function(){
			var obj = $(this),
				display = obj.css('display');

			obj.css({'display': 'block','visability':'hidden'});

			height = obj[0].offsetHeight;

			obj.css({'display': display,'visability':'none'});


		});
		return height;
	}

	$.fn.boxShow = function(o){
		function scrollTop() {
			return window.pageYOffset || (document.documentElement && document.documentElement.scrollTop) || (document.body && document.body.scrollTop) || 0;
		}

		function clientHeight() {
			return window.innerHeight || (document.documentElement && document.documentElement.clientHeight) || (document.body && document.body.clientHeight) || 0;
		}

		return this.each(function(){
			var obj = $(this),
				show = o.show ? o.show : false,
				filter = $(o.filter),
				box = $(o.box),
				close = $(o.close),
				is_video = o.is_video || false;

			if(show){
				box.css('top',(clientHeight()-box.trueHeight())/2 + scrollTop()+'px');
				filter.show();
				box.show();
			}

			obj.click(function(){
				box.css({
					'top':(clientHeight()-box.trueHeight())/2 + scrollTop()+'px',
					'display': 'block',
					'visability': 'hidden'
				});
				if(is_video) {
					box.append($("<div />").attr("id", "popup_video_container"));
				}

				filter.show();
				box.show();

				if(is_video) {
					var
						flashvars = o.flashvars,
						params = {
							allowfullscreen: "true",
							allowscriptaccess: "always"
						},
						attributes = {};
						swfobject.embedSWF(_public + "flash/player.swf", "popup_video_container", "630", "430", "10.0.0", _public + "flash/expressInstall.swf", flashvars, params, attributes);
				}

				return false;
			});

			close.click(function(){
				if(is_video) {
					$("#popup_video_container").remove();
				}
				filter.hide();
				box.hide();
				return false;
			});
		});
	}

	$.fn.textZoom = function(pr){
		return this.each(function(){
			var obj = $(this)
				step = pr.step ? pr.step : 1,
				cpos = 0,
				inc = pr.increase,
				dec = pr.decrease,
				from = pr.from ? pr.from : -3,
				to = pr.to ? pr.to : 3,
				fsize = [];

			obj.find('*').each(function(i,o){
				fsize.push(parseInt($(o).css('font-size')));
			});

			inc.bind('click', function(){
				if (fsize[0] + (step * to) > parseInt($(obj.find('*')[0]).css('font-size'))) {
					cpos++;
					obj.find('*').each(function(i, o){
						$(o).css('font-size', fsize[i] + (cpos * step) + 'px');
					});
				}
				return false;
			});

			dec.bind('click', function(){
				if (fsize[0] + (step * from) < parseInt($(obj.find('*')[0]).css('font-size'))) {
					cpos--;
					obj.find('*').each(function(i, o){
						$(o).css('font-size', fsize[i] + (cpos * step) + 'px');
					});
				}
				return false;
			});
		});
	}

	$.fn.toolTip = function(o){
		$('body').append('<div class="tooltip">\
								<p></p>\
							</div>');
		return this.each(function(){
			var obj = $(this);

			obj.mouseenter(function(){
				$('.tooltip').show().find('p').text(obj.attr('title'));
				obj.attr('title','');
			});

			obj.mousemove(function(e){
				$('.tooltip').css({'top':e.pageY-70+'px','left':e.pageX-330+'px'});

			});

			obj.mouseleave(function(){
				var p = $('.tooltip').find('p');
				obj.attr('title', p.text());
				$('.tooltip').hide();
				p.text("");
			});
		});
	}

	$.fn.galleryPreview = function(pr){
		var obj = $(this),
			preview = pr.preview ? $(pr.preview) : null,
			info = pr.info ? $(pr.info) : null,
			next = pr.next ? $(pr.next) : null,
			prev = pr.prev ? $(pr.prev) : null,
			index = 0;

		function preload(item){
			var image = new Image();
			preview.find('img').hide();

			$(image).load(function(){
				preview.find('img').attr('src', $(this).attr('src'));
				preview.removeClass('loading');
				preview.find('img').fadeIn("slow");
			}).attr('src',item.attr('href'));

			info.html($('.tooltip').find('p').text() || item.attr("title"));
		}

		next.click(function(){
			if (index < obj.length-1){
				index++;
				preload($(obj[index]));
			}
			return false;
		});

		prev.click(function(){
			if (index > 0) {
				index--;
				preload($(obj[index]));
			}
			return false;
		});

		return obj.each(function(i,el){
			$(el).click(function(e){
				preload($(this));
				index = i;
				return false;
			});
		});
	}

})(jQuery);

$(function(){
	$('.top form a').toggle(function show() {
		$(this).parent('form').find('input').stop(true,true).slideDown();
	},

	function hide(){
		$(this).parent('form').find('input').stop(true,true).slideUp();
	});

	$('.top form input:nth-child(2)').toggleValue('title');
	$('#header').bind("contextmenu", function(){
        return false;
    });
	
	function check_width(width){
			if ($('body').width() < width){
				$('.shadow_l,.shadow_r').hide();
			}else {
				$('.shadow_l,.shadow_r').show();
			}
		}
		
	check_width(1100);
	
	window.onresize = function(){
		check_width(1100);
	}
	
	try {
		$(function(){
			$('.carousel > div > ul').simpleCarousel({
				btnNext: '.carousel a.next',
				btnPrev: '.carousel a.prev',
				navigation: '.carousel > ul',
				visibles: 5,
				speed: 800,
				itemWidth: 110
			});

			$('.columns').textZoom({
				increase: $('.zoom a:nth-child(2)'),
				decrease: $('.zoom a:nth-child(1)'),
				step: 4,
				from: -1,
				to: 1
			});

			$('.carousel > div ul.no_lightbox li a').galleryPreview({
				preview: '.preview',
				info: '.info',
				next: '.preview a:nth-child(2)',
				prev: '.preview a:nth-child(1)'
			});

			if($('.carousel > div > ul li a').lightBox !== undefined) {
				$('.carousel > div > ul li a').lightBox({
					imageLoading: _public + 'images/site_images/lightbox/lightbox-ico-loading.gif',
					imageBtnClose: _public + 'images/site_images/lightbox/lightbox-btn-close.gif',
					imageBtnPrev: _public + 'images/site_images/lightbox/lightbox-btn-prev.gif',
					imageBtnNext: _public + 'images/site_images/lightbox/lightbox-btn-next.gif',
					txtImage: 'Снимка',
					txtOf: 'от'
				});
			}

			$('div.gallery + .carousel > div ul li a').toolTip();
		});
	} catch(e) {
		//alert(e.message);
	}
});
