$(document).ready(function(){

	// Makeup adding
	if ($("#header .mainmenu li a").length!=0) {
		$("#header .mainmenu li").each(function(){
			$(this).html("<div class='st'><div class='sr'><div class='sb'><div class='sl'>"+$(this).html()+'</div></div></div></div>');
		});
	}
	$("<div id='stripes'><div class='relative'><table cellpadding='0' cellspacing='0'><tr><td class='color_1'>&nbsp;</td><td class='color_2'>&nbsp;</td><td class='color_3'>&nbsp;</td><td class='color_4'>&nbsp;</td></tr></table><div class='fade'></div></div></div>").appendTo("#header");
	$("<div class='corners'><div class='cont'><div class='left'><div class='png'></div></div><div class='right'><div class='png'></div></div></div></div>").prependTo("#illustration");
	$("<div class='background'><table cellpadding='0' cellspacing='0'><tr><td class='left'>&nbsp;</td><td class='center'>&nbsp;</td><td class='right'>&nbsp;</td></tr></table><p>"+$("#logo").html()+"</p></div>").prependTo("#illustration");
	if ($("#header .lists").length!=0) {
		$($("#header .lists").html()).appendTo("#header .corners .cont"); $("#header .lists").remove();
	}
	$("<div class='blends div_p'>"+$("#header p.blends").html()+'</div>').appendTo("#header .corners .cont"); 				$("#header p.blends").remove();
	$("<div class='linoleum div_p'>"+$("#header p.linoleum").html()+'</div>').appendTo("#header .corners .cont"); 			$("#header p.linoleum").remove();
	$("<div class='technical div_p'>"+$("#header p.technical").html()+'</div>').appendTo("#header .corners .cont"); 		$("#header p.technical").remove();
	$("<div class='plastic div_p'>"+$("#header p.plastic").html()+'</div>').appendTo("#header .corners .cont");				$("#header p.plastic").remove();
	$("<div class='floors div_p'>"+$("#header p.floors").html()+'</div>').appendTo("#header .corners .cont");				$("#header p.floors").remove();
	$("<div class='brick div_p'>"+$("#header p.brick").html()+'</div>').appendTo("#header .corners .cont");				$("#header p.brick").remove();
	/*$("<div class='keramogranit div_p'>"+$("#header p.keramogranit").html()+'</div>').appendTo("#header .corners .cont"); 	$("#header p.keramogranit").remove();
	$("<div class='banners div_p'>"+$("#header p.banners").html()+'</div>').appendTo("#header .corners .cont"); 			$("#header p.banners").remove();*/
	
	/////////////////////////////////////////////////////////////////////////////////////////////////////////
	// Illustration Lists
	
	// Submitting coordinats of list in Array
	var coords = {
		start: {
			blends: {
				sideXName: 'right',
				x: new Array(),
				y: new Array()
			},
			linoleum: {
				sideXName: 'left',
				x: new Array(),
				y: new Array()
			},
			technical: {
				sideXName: 'right',
				x: new Array(),
				y: new Array()
			},
			plastic: {
				sideXName: 'left',
				x: new Array(),
				y: new Array()
			}
		},
		end: {
			blends: {
				x: new Array()
			},
			linoleum: {
				x: new Array()
			},
			technical: {
				x: new Array()
			},
			plastic: {
				x: new Array()
			}
		}
	};
	var ulDuration = 1000;
	var iDuration = 1000;
	// Function setting start list position coordinates
	var setStartListPosition = function(className,sideY,startY,stepY,directionY) {
		var counter = 0;
		var sumY = startY;
		$('.'+className+' li').each(function(){
			coords['start'][className]['x'][counter] = (-1)*$(this).get(0).clientWidth;
			$(this).css(coords['start'][className]['sideXName'],coords['start'][className]['x'][counter]+'px');
			$(this).css(sideY,sumY+'px');
			coords['start'][className]['y'][counter] = sumY;
			sumY = sumY+(directionY*stepY);
			counter++;
		});
	}
	// Function setting end list position coordinates
	var setEndListPosition = function(className,startX,stepX,directionX) {
		var counter = 0;
		var sumX = startX;
		$('.'+className+' li').each(function(){
			coords['end'][className]['x'][counter] = sumX;
			sumX = sumX+(directionX*stepX);
			counter++;
		});
	}
	// Calback function of animation
	var callBack = function(A,B){return function(){
			if (B=='end') $('ul.'+A).css('z-index','60'); else $('ul.'+A).css('visibility','hidden');
		}
	}
	// Function of animation show effect of list-object
	var showAnimation = function(className){
		var method = 'easeInOutBack';		// Method of moving
		var duration = ulDuration;
		var obj = $('.'+className+' li');
		
		var typeEvent = 'end';
			
		if (coords['start'][className]['sideXName']=='left') {
			var count = 0;
			obj.each(function(){
				$(this).stop()
					   .animate({left: coords[typeEvent][className]['x'][count]},duration,method,callBack(className,typeEvent));
	     		count++;
			});
		} else {
			var count = 0;
			obj.each(function(){
				$(this).stop()
					   .animate({right: coords[typeEvent][className]['x'][count]},duration,method,callBack(className,typeEvent));
	     		count++;
			});
		}
		$('ul.'+className).css('visibility','visible');
	}
	// Function of animation hide effect of list-object
	var hideAnimation = function(className){
		var method = 'easeInOutBack';		// Method of moving
		var duration = ulDuration;
		var obj = $('.'+className+' li');
		
		typeEvent = 'start';
			
		if (coords['start'][className]['sideXName']=='left') {
			var count = 0;
			obj.each(function(){
				$(this).stop()
					   .animate({left: coords[typeEvent][className]['x'][count]},duration,method,callBack(className,typeEvent));
	     		count++;
			});
		} else {
			var count = 0;
			obj.each(function(){
				$(this).stop()
					   .animate({right: coords[typeEvent][className]['x'][count]},duration,method,callBack(className,typeEvent));
	     		count++;
			});
		}
		$('ul.'+className).css('z-index','1');
	}
	// Function div-tags fading
	var divFadeIn = function(){
		var obj = $('#illustration .div_p');
		if ($('#ie6').length==0) obj.fadeIn(); else obj.css('display','block');
	}
	// Function div-tags fading
	var divFadeOut = function(){
		var obj = $('#illustration .div_p');
		if ($('#ie6').length==0) obj.fadeOut(); else obj.css('display','none');
	}
	// Function for starting show ul-animation
	var startShowAnimation = function(){
		divFadeIn();
		showAnimation('blends');
		showAnimation('linoleum');
		showAnimation('technical');
		showAnimation('plastic');
	}
	// Function for starting hide ul-animation
	var startHideAnimation = function(){
		divFadeOut();
		hideAnimation('blends');
		hideAnimation('linoleum');
		hideAnimation('technical');
		hideAnimation('plastic');
	}
	// Function illustration show/hide animation
	var iShowAnimation = function(){
		var method = 'easeOutCubic';		// Method of moving
		var duration = iDuration;
		var iObj = $('#illustration');
		var cObj = $('#illustration .corners');
		var bObj = $('#illustration .background');
		
		iObj.stop().animate({height: 898},duration,method);
		cObj.stop().animate({top: 187},duration,method);
		bObj.stop().animate({top: 0},duration,method);
		$('#stripes .fade').fadeIn(1000);
	}
	// Function illustration show/hide animation
	var iHideAnimation = function(){
		var method = 'easeOutCubic';		// Method of moving
		var duration = iDuration;
		var iObj = $('#illustration');
		var cObj = $('#illustration .corners');
		var bObj = $('#illustration .background');
		
		iObj.stop().animate({height: 25},duration,method);
		cObj.stop().animate({top: -235},duration,method);
		bObj.stop().animate({top: -424},duration,method);
		$('#stripes .fade').fadeOut(1000);
	}
	// Function of milk/water-replace link text
	var milkWater = function(obj1,obj2){
		var buf = obj1.text();
		obj1.text(obj2.text());
		obj2.text(buf);
	}
	// Set and submit coordinates to array
	setStartListPosition('blends','top',21,30,1);			setEndListPosition('blends',40,18,1);
	setStartListPosition('linoleum','top',31,30,1);			setEndListPosition('linoleum',50,18,1);
	setStartListPosition('technical','bottom',21,30,1);		setEndListPosition('technical',40,18,1);
	setStartListPosition('plastic','bottom',51,30,1);		setEndListPosition('plastic',60,18,1);
	//if ($('#header').hasClass('noShowing')) $('#stripes .fade').hide();
	// Start
	if ($("#header").hasClass('noShowing')) $("#header p.show a").removeClass('pastMoved');
	if ($("#header p.show a").hasClass('pastMoved')) startShowAnimation();
	else {$('#illustration .background .center').addClass('delta'); milkWater($('#header p.show a'),$('#header p.show a').next());}
	$('#header p.show a').click(function(){
		if ($(this).hasClass('pastMoved')) {
			startHideAnimation();
			milkWater($(this),$(this).next());
			$(this).removeClass('pastMoved');
			setTimeout(function(){iHideAnimation();$('#illustration .background .center').addClass('delta');},ulDuration-300);
		} else {
			iShowAnimation();
			milkWater($(this),$(this).next());
			$(this).addClass('pastMoved');
			$('#illustration .background .center').removeClass('delta');
			setTimeout(function(){startShowAnimation();},iDuration-200);
		}
		return false;
	});
	if ($('#position').length!=0) $('#position').parent().css('padding-right','0');
	/////////////////////////////////////////////////////////////////////////////////////////////////////////
	
	// ONLOAD //////////////////////////////////////////
	var isChrome = function() {
		if (navigator.userAgent.toLowerCase().indexOf('chrome') > -1) {
			return true;
			chrome_version = navigator.userAgent.replace(/^.*Chrome\/([\d\.]+).*$/i, '$1')
		} else return false
	}
	var listIn = function() {
		var duration = 100;
		$('#position-list dl').each(function(){
			function getfunc(A){return function(){$(A).animate({opacity:1},400);}}
			setTimeout(getfunc(this),duration);
			duration = duration + 100;
		});
	}
	var loadFuncs = function() {
		if ($('#position-list dl').length!=0) listIn();
	}
	if (isChrome()) $(window).load(loadFuncs); else $(window).load(loadFuncs());
	
	// Hover events
	$("#navigation .mainmenu li a")
		.mouseover(function(){
			$(this).parent().parent().parent().parent().parent().addClass("hv");
		})
		.mouseout(function(){
			$(this).parent().parent().parent().parent().parent().removeClass("hv");
		});
	
	// Click events
	var price_plash_duration = 300;
	var price_plash_method = 'easeOutCubic';
	$('#price .types .opt a').click(function(){
		if (!$(this).parent().hasClass('cur')) {
			$('#price .types p').removeClass('cur');
			$('#price .types .plash').animate({top:1},price_plash_duration,price_plash_method,function(){ $('#price .types .opt').addClass('cur'); });
		}
		return false;
	});
	$('#price .types .ret a').click(function(){
		if (!$(this).parent().hasClass('cur')) {
			$('#price .types p').removeClass('cur');
			$('#price .types .plash').animate({top:25},price_plash_duration,price_plash_method,function(){ $('#price .types .ret').addClass('cur'); });
		}
		return false;
	});
	
	if ($(".virtual_pos").length!=0) {
		$(".virtual_pos").each(function(){
			if ($(this).find(".cur").length==0) $(this).remove();
		});
		$(".virtual_pos").show();
	}
	
	if ($('#catalog_all').length!=0) {
		$('#catalog_all tr td:nth-child(2),#catalog_all tr td:nth-child(3)').addClass('fix');
		var color_counter = 0;
		$("#catalog_all td").each(function(){
			color_counter++;
			$(this).addClass("color_"+color_counter);
		});
	}
	if ($('#catalog_part').length!=0) {
		var color_counter = 0;
		$("#catalog_part td").each(function(){
			color_counter++;
			$(this).addClass("color_"+color_counter);
		});
	}
	
	// Switcher
	$("#switcher a").click(function(){
		var dd = $("#position dl");
		var li = $(this).parent();
		var cur = $("#switcher .cur");
		if (!li.hasClass("cur")) {
			if (li.hasClass("min")) {
				dd.addClass("min").removeClass("max");
				cur.removeClass("cur");
				li.addClass("cur");
			}
			if (li.hasClass("max")) {
				dd.addClass("max").removeClass("min");
				cur.removeClass("cur");
				li.addClass("cur");
			}
		}
		return false;
	});
	
	// Click events
	var price_plash_duration = 300;
	var price_plash_method = 'easeOutCubic';
	var test_price_flag = 0;
	var difference_prices = function(old_price,new_price){
		if (old_price!=new_price) {
			if (old_price>new_price) var coeff = -1; else coeff = 1;
			var time = 200;
			var resol = 5;
			var delta = resol*Math.abs(old_price-new_price)/time;
			var now_price = old_price;
			var price_text = $("#price .text span");
			var counter = 0;
			var interval = setInterval(function(){ now_price = now_price + coeff*delta; price_text.text(now_price.toFixed()); counter += resol; if (counter==time) clearInterval(interval); },resol);
		}
	}
	$("#price *").disableTextSelect();
	$("#price .types p").each(function(){ $(this).find("small").text(test_price_flag); test_price_flag++; });
	$('#price .types p a').click(function(){
		var p = $(this).parent();
		if (!p.hasClass('cur')) {
			$('#price .types p.cur').removeClass('cur');
			var numb = parseInt($(this).parent().find("small").text());
			$('#price .types .plash').stop().animate({top:(numb*25)+1},price_plash_duration,price_plash_method,function(){ p.addClass('cur'); });
			difference_prices(parseInt($("#price .text span").text()),parseInt(p.find("span").text()));
		}
		return false;
	});
	$("#price .text").css("padding-top",($("#price").height()-$("#price .text").height())/2+"px");
	
});

formField = function(block, objid, formid, default_text) {
		
	this.obj = document.getElementById(objid);
	this.form = (formid != null) ? document.getElementById(formid) : null;
	this.default_text = default_text;

	this.state = { value: false };
	
	this.obj.value = this.default_text;
	this.obj.onfocus = function(state, obj) { return function() { if (!state.value) {obj.value = ''; $(obj).addClass('focus');} } } (this.state, this.obj);
	this.obj.onblur  = function(state, obj, default_text) { return function() { if (!state.value) {obj.value = default_text; $(obj).removeClass('focus');}} } (this.state, this.obj, this.default_text);
	this.obj.onkeyup = function(state, obj) { return function() { state.value = obj.value; } } (this.state, this.obj);
	if (this.form != null) this.form.onsubmit = function(state) { return function() { if (!state.value) return false; } } (this.state);
}