function change(n) {
	if (document.getElementById(n).value == '') {
		document.getElementById(n).value = 'Поиск по сайту'
	}
	document.getElementById(n).style.color = '';
}

jQuery.fn.center = function () {
	this.css("position","fixed");
	this.css("top", (($(window).height() - this.outerHeight()) / 2) + "px");
	this.css("left", (($(window).width() - this.outerWidth()) / 2)  + "px");
	return this;
}


$(document).ready(function(){
	$.get('/blocks/tech-menu/', function(data) {
		$('#topblock').prepend(data);
	});
	$.get('/blocks/slider/', function(data) {
		$('#header').html(data);
		
		var container = $('div.sliderGallery');
		var ul = $('ul', container);
		var container2 = $('div.slider');
		var hl = $('span', container2);
		var array_crd1 = [55, 120, 170, 190, 240, 279, 320, 390, 420, 560, 700];
		var array_crd2 = [88, 158, 222, 311, 399, 497, 588, 669, 766, 840];
		var array_crd3 = [0, 55, 120, 170, 190, 240, 279, 320, 390, 420, 560];
		var array_crd4 = [0, 88, 158, 222, 311, 399, 497, 588, 669, 766];

		var temp_crd = [0, 55, 120, 170, 190, 240, 279, 320, 390, 420, 560, 700];
		var tmp_x = 0;
		var tmp_y = 0;
		var tmp_i = 0;
		var itemsWidth = ul.innerWidth() - container.outerWidth();

		$('.slider', container).slider({
			min: 0,
			max: itemsWidth,
			handle: '.handle',
			stop: function (event, ui) {
				ul.animate({'left' : ui.value * -1}, 500);
				tmp_y = ui.value;
				tmp_x = 1;
			},
			slide: function (event, ui) {
				ul.css('left', ui.value * -1);
				hl.css('left', ui.value * 1);
			}
		});
		
		$('#rigth_arrow').click(function() { 
			if(tmp_x != 0){
				for(i=0;i<9;i++){
					if((temp_crd[i]<=tmp_y)&&(temp_crd[i+1]>=tmp_y)){
						tmp_i = i;
					} 
				}
			}
			ul.css('left', array_crd1[tmp_i]*-1);
			hl.css('left', array_crd2[tmp_i]);
			if(tmp_i < 9){
				tmp_i+=1;
			}
			tmp_x = 0;
		});

		$('#left_arrow').click(function() {        
			if(tmp_x != 0){
				for(i=0;i<9;i++){
					if((temp_crd[i]<=tmp_y)&&(temp_crd[i+1]>=tmp_y)){
						tmp_i = i+1;
					} 
				}
			}
			if(tmp_i > 0){
				tmp_i-=1;
			}
			ul.css('left', array_crd3[tmp_i]*-1);
			hl.css('left', array_crd4[tmp_i]);
			tmp_x = 0;
		});
	});
	$.get('/blocks/search/', function(data) {
		$('#search').html(data);
	});
	$.get('/blocks/glavnoemenu/', function(data) {
		$('#glavnoemenu').html(data);
	});
	$.get('/blocks/nijneemenu/', function(data) {
		$('#nijneemenu').html(data);
	});
	$.get('/blocks/special/', function(data) {
		$('#special').prepend(data);
		$('#innerspecial').html(data);
	});
	$.get('/blocks/mainnews/', function(data) {
		$('#news').html(data);
	});
	$.get('/blocks/leftmenu/', function(data) {
		$('#leftmenu').html(data);
	});

	$(document).click(function(e) { 
		// Check for left button
		if (e.button == 0) {
			$('.fixedpopup').fadeOut(100); 
		}
	});

	$('.fixedpopup').click(function(e) {
		e.stopPropagation();
	});

	$('a[href="/inquire/"]').click (function () { 
		$('.fixedpopup').hide();
		if (!($('#popup_callback').html()!='')) {
			$('#popup_callback').html('<div id = "closebutton" style="position:absolute; top:5px; right:5px; width:20px; height:20px; background:url(/images/close.png);"></div><iframe src="/inquire/" style="border:none; height:100%; width:100%; overflow:hidden;" scrolling=no></iframe>');
		}
		$('#popup_callback').center().show(); 
		$('#closebutton').click (function(e) { 
		// Check for left button
			if (e.button == 0) {
				$('.fixedpopup').fadeOut(100);
			}
		});
		return false; 
	});

	/*var leftcol_height = $('#leftcol').height();
	var rightcol_height = $('#rightcol').height();
	if (rightcol_height < leftcol_height) {
		$('#rightcol_in_in').animate({height: leftcol_height - 25}, 1000);
	}*/
	$('#showaddorder').click(function() {
		$('#addorder').slideToggle('slow');
	});
});
