$(function() {

	//video in overlay
	$('.video').live('click', function() {
		lightbox('video');
	});
	
	//changeParameter
	function changeParameter(param, value) {
		var url = (window.location).toString();
		var str = '';
		var pIndex = url.indexOf(param);
		if(pIndex == -1){
			window.location = url + "&" + param+'='+value;
			return true;
		}
		for (i = pIndex; i <= url.length - 1; i++) {
			if (url.charAt(i) == '&') {
				break;
			} else {
				str = str + url.charAt(i);
			}
		}
		url = url.replace(str, param+'='+value);
		window.location = url;
	}

	//fucntion to add parameter to url
	document.addParameter = function(param, value) {
		var url = (window.location).toString();
		if (url.indexOf("?") != -1) {
			changeParameter(param, value);
		} else {
			url = url + '?' + param + '=' + value;
			window.location = url;
		}
	}
});

var time;

function slider() {
	if ($('.slides_width')) {
	var el = $('.slides_width');
	if (parseInt(el.css('right')) < 6790) {
		el.animate({
			'right':'+=970px'}, 'slow'
				);
	} else {
		el.animate({
			'right':'0'}, 'slow'
				);
	}
	time = setTimeout("slider()", 6000);
	}
};

slider();

$(function() {
	//open text on homePage
	$('#open').click(function() {
		if ($('.open_close').height() == 335) {
			$('.open_close').animate({
				height: '3120px'
			});
			$(this).text('Коротко');
		} else {
			$('.open_close').animate({
				height: '335px'
			});
			$(this).text('Подробно');
		}
		return false;
	});
	
	$('.product_detial img').click(function() {
		lightbox($(this));
	});
	
	if ($('.slides_width')) {
	$('.slides_width').hover(function() {
		clearTimeout(time);
	}, function() {
		slider();
	});
	}
	
	if ($('.product_detial h3 a, .product_detial h3 span')) {
	$('.product_detial h3 a, .product_detial h3 span').click(function() {
		if ($(this).parent().next('p').is(':visible')) {
			$(this).parent().next('p').hide();
			$(this).parent().removeClass('up');
			$(this).parent().addClass('down');
		} else
		{
			$(this).parent().next('p').show();
			$(this).parent().removeClass('down');
			$(this).parent().addClass('up');
		}
		return false;
	});}
	
	//link for images from info
	function urlToImage() {
		var links = $('.info li a');
		var image;
		$('.info li img').each(function() {
			image = $(this).attr('src');
			$(this).parents().filter('li').find('.text').before("<img src='" + image + "' alt='' width='100'/>");
			$(this).remove();
		});
		links.each(function() {
			image = $(this).parents().filter('li').find('img').attr('src');
			$(this).parents().filter('li').find('img').remove();
			if ($(this).attr('class') == 'video') {
				$(this).parents().filter('li').find('.text').before("<a href='" + $(this).attr('href') + "' class='video no_icon'><img src='" + image + "' alt='' width='100'/></a>");
			} else {
				$(this).parents().filter('li').find('.text').before("<a href='" + $(this).attr('href') + "'><img src='" + image + "' alt='' width='100'/></a>");
			}
		});

		$('.special img').each(function() {
			if ($(this).parents('.in').find('a').length > 0) {
				if ($(this).parents('.in').find('a').attr('class') == 'video') {
					$(this).before("<a href='" + $(this).parents('.in').find('a').attr('href') + "' class='video no_icon'><img src='" + $(this).attr('src') + "' alt='' width='100'/></a>");
				} else {
					$(this).before("<a href='" + $(this).parents('.in').find('a').attr('href') + "'><img src='" + $(this).attr('src') + "' alt='' width='100'/></a>");
					}
				$(this).remove();
			} else {
				$(this).attr('width', '100');
				$(this).removeAttr('height');
			}
		});
	};

	urlToImage();
});

//open lightbox
function lightbox(link) {
	var text = '';
	$('.lightbox').show();
	if (link == 'video') {
		text = '<embed height="230" width="368" flashvars="file=http://ikoblenz.ru/file/flv_about_video.flv&image=file/flv_about_video_pic.jpg" allowscriptaccess="always" allowfullscreen="true" quality="high" name="player" id="player" style="" src="file/player.swf" type="application/x-shockwave-flash">';
	} else {

	if (link.attr('src') == null) {
		text = link.find('img').attr('src');
	} else {
		text = link.attr('src');
	}
	text = "<img src=" + text + " />";
	}
	var el = $('.lightbox');
	el.find('.box_in .text_here').html(text);
	$('.lightbox .box').css('width', 'auto');
	$('.lightbox .box').width($('.lightbox .box_in').width());
	var wid = el.find('.box').width();
	var box = el.find('.box');
	var hig = el.find('.box').height();
	box.css('left', '50%');
	box.css('margin-left', -wid / 2);
	//box.css('top', link.offset().top);
	box.css('top', '50%');
	box.css('margin-top', -hig / 2);
	box.css('position', 'fixed');
	return false;
};



//close lightbox
$('.black_bg, .close_box').live('click', function() {
	$('.lightbox').hide();
	return false;
});



