function fixEvent( e ) {
	// получить объект событие для IE
	e = e || window.event;

	// добавить pageX/pageY для IE
	if ( e.pageX == null && e.clientX != null ) {
		var html = document.documentElement;
		var body = document.body;
		e.pageX = e.clientX + (html && html.scrollLeft || body && body.scrollLeft || 0) - (html.clientLeft || 0);
		e.pageY = e.clientY + (html && html.scrollTop || body && body.scrollTop || 0) - (html.clientTop || 0);
	}

	// добавить which для IE
	if (!e.which && e.button) {
		e.which = e.button & 1 ? 1 : ( e.button & 2 ? 3 : ( e.button & 4 ? 2 : 0 ) );
	}

	return e;
}

jQuery( document ).ready(function(){

	jQuery("div.question_ico_tips").each(function(){
		jQuery( this ).show().scRedraw().css("opacity", 0).hide();
	});

	jQuery(".question_ico_label").mouseover(function(){
		jQuery( this ).prev("div.question_ico_tips").show().stop().animate({opacity: 1},{duration: 200});
	}).mouseout(function(){
		jQuery( this ).prev("div.question_ico_tips").stop().animate({opacity: 0},{duration: 200, complete: function(){jQuery(this).hide()}});
	});

	jQuery(".toggle_reason").click(function(){

		var self = jQuery(".more_reasons_main");

		if ( self.css("margin-top") == "-5000px" ) {
			self.css({ "margin-top": ( "-" + ( self.height() / 2 ) + "px" ) }).hide();
		}
		if ( self.css('display') == 'none' ) {
			self.fadeIn( 200 );
		} else {
			self.fadeOut( 200 );
		}

		return false;
	});

	jQuery("a.external").click(function(){
		window.open( this.href );
		return false;
	});

	jQuery("a.facebook, a.twitter").click(function(){
		window.open( this.href );
		return false;
	}).mouseover(function(){
		jQuery( this ).stop().animate({
			"top": "20px"
		}, 100);
	}).mouseout(function(){
		jQuery( this ).stop().animate({
			"top": "35px"
		}, 100);
	});

	jQuery(".rfcaptcha").click(function(){
		var captcha = jQuery("#captcha_id");
		if ( !captcha[0].org_src ) {
			captcha[0].org_src = captcha[0].src;
		}
		captcha[0].src = captcha[0].org_src + "&" + Math.random() + '=' + Math.random();
		return false;
	});

	jQuery(".button_direction a").click(function(){
		var el = jQuery(".contact_form");
		var d = jQuery(".directions");
		if ( el.css('display') !== 'none' ) {
			el.slideUp(200);
			d.slideDown(200, function(){
				if ( !this.initialized_direction ) {
					d.find(".input").scRedraw();
					initDirection( contacts_settings );
					this.initialized_direction = true;
				}
			});
			jQuery( this ).find(".mad").hide();
			jQuery( this ).find(".sam").show();
			jQuery( ".button_direction" ).removeClass("kapelka").addClass("konvert");
		} else {
			el.slideDown(200);
			d.slideUp(200);
			jQuery( this ).find(".sam").hide();
			jQuery( this ).find(".mad").show();
			jQuery( ".button_direction" ).removeClass("konvert").addClass("kapelka");
		}
		return false;
	});

	// -------------------------------------------------------------------------
	// Простые эффекты для Portfolio
	// -------------------------------------------------------------------------
	var direction = [
		[	// Эффект появления слева направо
			{"duration": 300},
			{"left": "-261px", "top": 0},
			{"left": "0px"},
			{"left": "-261px"}
		],
		[	// Эффект появления сверху вниз
			{"duration": 300},
			{"left": 0, "top": "-200px"},
			{"top": "0px"},
			{"top": "-200px"}
		],
		[	// Эффект появления снизу вверх
			{"duration": 300},
			{"left": 0, "top": "200px"},
			{"top": "0px"},
			{"top": "200px"}
		],
		[	// Эффект появления справа налево
			{"duration": 300},
			{"left": "261px", "top": 0},
			{"left": "0px"},
			{"left": "261px"}
		],
		[	// Эффект появления с центра блока разварачиваеться с плавным изменением прозрачности
			{"duration": 300, step: function( now, data ){
				if ( data.prop == "left" ) {
					jQuery( data.elem ).find(".slide_fon").css("left", "-" + now + "px" );
					jQuery( data.elem ).find(".slide_content").css("margin-left", "-" + now + "px" );
					jQuery( data.elem ).find(".controls").css("left", "-" + now + "px" );
				}
				if ( data.prop == "top" ) {
					jQuery( data.elem ).find(".slide_fon").css("top", "-" + now + "px" );
					jQuery( data.elem ).find(".slide_content").css("margin-top", "-" + now + "px" );
				}
			}},
			{"opacity": 0, "left": "130px", "top": "100px", "width": "0px", "height": "0px"},
			{"opacity": 1, "left": 0, "top": 0, "width": "261px", "height": "200px"},
			{"opacity": 0, "left": "130px", "top": "100px", "width": "0px", "height": "0px"}
		],
		[	// Эффект появления с zoom'ом
			{"duration": 300, step: function( now, data ){
				if ( data.prop == "manualParam" ) {
					jQuery( this ).css({
						"transform": "scale(" + ( now ) + ")",
						"-o-transform": "scale(" + ( now ) + ")",
						"-moz-transform": "scale(" + ( now ) + ")",
						"-webkit-transform": "scale(" + ( now ) + ")",
						"-ms-transform": "scale(" + ( now ) + ")"
					});
					if ( jQuery.browser.msie ) {
						if ( jQuery.browser.version == 7 || jQuery.browser.version == 8 ) {
							now = now < 0.001 ? 0.001 : now;
							this.style.zoom = now;

							if ( jQuery.browser.version == 7 ) {
								this.style.left = ( 130 - ( 130 * now ) ) + "px";
								this.style.top = ( 100 - ( 100 * now ) ) + "px";
							} else {
								this.style.left = ( ( 130 - ( 130 * now ) ) / now ) + "px";
								this.style.top = ( ( 100 - ( 100 * now ) ) / now ) + "px";
							}
						}
					}
				}
			}},
			{"left": 0, "top": 0, "width": "261px", "height": "200px", "transform": "scale(0)", "-o-transform": "scale(0)", "-moz-transform": "scale(0)", "-webkit-transform": "scale(0)", "-ms-transform": "scale(0)"},
			{"manualParam": 1},
			{"manualParam": 0}
		],
		[	// Эффект появления с zoom'ом и поворотом / не работает в IE < 9
			{"duration": 300, step: function( now, data ){
				if ( data.prop == "manualParam" ) {
					jQuery( this ).css({
						"transform": "scale(" + ( now ) + ") rotate(" + ( 360 - ( 360 * now ) ) + "deg)",
						"-o-transform": "scale(" + ( now ) + ") rotate(" + ( 360 - ( 360 * now ) ) + "deg)",
						"-moz-transform": "scale(" + ( now ) + ") rotate(" + ( 360 - ( 360 * now ) ) + "deg)",
						"-webkit-transform": "scale(" + ( now ) + ") rotate(" + ( 360 - ( 360 * now ) ) + "deg)",
						"-ms-transform": "scale(" + ( now ) + ") rotate(" + ( 360 - ( 360 * now ) ) + "deg)"
					});
				}
			}},
			{"left": 0, "top": 0, "width": "261px", "height": "200px", "transform": "scale(0)", "-o-transform": "scale(0)", "-moz-transform": "scale(0)", "-webkit-transform": "scale(0)", "-ms-transform": "scale(0)"},
			{"manualParam": 1},
			{"manualParam": 0}
		],
		[	// Эффект появления с плавным изменением прозрачности / не работает в IE < 9
			{"duration": 300},
			{"opacity": 0, "left": 0, "top": 0},
			{"opacity": 1},
			{"opacity": 0}
		]
	];

	jQuery(".portfolio_project .prj").each(function(){

		if ( jQuery.browser.msie ) {
			if ( jQuery.browser.version == 7 || jQuery.browser.version == 8 ) {
				direction[5][1]["zoom"] = 0.001;
				direction.length = 6;
			}
		}

		var self = jQuery( this ),
			dirN = Math.floor( Math.random() * direction.length ),
			dir = direction[ dirN ];

		for( var i = 0; i < direction.length; i++ ) {
			if ( self.hasClass( "effect_" + i ) ) {
				dirN = i;
				dir = direction[ i ];
				break;
			}
		}

		self.find(".descr").css( dir[ 1 ] );

		if ( jQuery(".effects_infos").length > 0 ) {
			self.find(".info div").mouseover(function(){
				jQuery( this ).stop().animate({opacity: 0.5}, {duration: 300});
			}).mouseout(function(){
				jQuery( this ).stop().animate({opacity: 0}, {duration: 300});
				jQuery(".effects_infos").fadeOut( 300 );
			}).click(function(){
				showInfos( dirN );
			});
		}

		self.mouseover(function(e){
			if ( self[0]._timerHandler ) {
				clearTimeout( self[0]._timerHandler );
				self[0]._timerHandler = null;
			} else {
				self.find(".descr").stop( true, false ).animate( dir[ 2 ], dir[ 0 ] );
			}
		}).mouseout(function(e){
			self[0]._timerHandler = setTimeout(function(){
				self.find(".descr").stop( true, false ).animate( dir[ 3 ], dir[ 0 ] );
				self[0]._timerHandler = null;
			}, 1);
		});
	});

	function showInfos( dirN ) {
		var e = fixEvent(),
			self = jQuery(".effects_infos");

		if ( self.length > 0 ) {
			self.find( "div.i" ).hide();
			self.find( "div.info_" + dirN ).show();

			var h = self.show().height(),
				l = ( e.pageX + self.width() + 10 ) > document.body.scrollWidth ? ( e.pageX + self.width() + 20 ) - document.body.scrollWidth : 0;

			self.find( "div.info_" + dirN ).scRedraw();

			self.hide();

			self.css( { "left": ( e.pageX - l ) + "px", "top": ( e.pageY - h - 10 ) + "px" } );

			self.fadeIn( 300 );
		}
	}
	// -------------------------------------------------------------------------
	// end code for effects
	// -------------------------------------------------------------------------


	jQuery(".lang_select_box .lang_select").each(function(){

		var hd = jQuery( this ).next().show();

		jQuery( this ).css( { "width": jQuery( this ).width() + "px" } ).click(function( e ){
			if ( hd.css('display') != 'none' ) {
				hd.slideUp(100);
			} else {
				jQuery( this ).css( 'background-position', 'right -140px' );
				hd.slideDown(100);
			}
			e.stopPropagation();
		});
		hd.hide();

		jQuery(".lang_box").css({"width": jQuery(".lang_box").width() + "px", "height": jQuery(".lang_box").height() + "px" });
		jQuery(".lang_select_box").css({ "position": "absolute" }).scRedraw();
	});

	jQuery( this ).mouseup(function(){
		jQuery( ".lang_select_box .lang_select" ).css( 'background-position', 'right -110px' ).next().slideUp(100);
	});

	(function(){

		var slideshow = jQuery(".slideshow_box"),
			slides = slideshow.children(),
			slides_len = slides.length,
			cur_index = 0,
			handler = null,
			busy = false,
			stop = false;

		slides.each(function( index, elem ){
			if ( index != cur_index ) {
				jQuery( elem ).css( { "opacity": 0, "margin-left": "0px" } );
			}
		});

		function goto_slide( t, index ) {

			var next_index = 0;
			if ( typeof index !== "number" ) {
				if ( ( cur_index + 1 ) == slides_len ) {
					next_index = 0;
				} else {
					next_index = ( cur_index + 1 );
				}
			} else {

				next_index = index;

				if ( !busy ) {
					if ( handler != null ) {
						clearTimeout( handler );
						handler = null;
					}
					stop = true;
				}
			}

			if ( next_index != cur_index && !busy ) {

				busy = true;

				jQuery( slides[ next_index ] ).css( "z-index", 2 ).animate({
					opacity: 1
				}, {
					duration: 1500,
					complete: function(){
						jQuery( slides[ cur_index ] ).animate({
							opacity: 0
						}, {
							duration: 500,
							complete: function() {
								jQuery( this ).css( "z-index", 1 );
								jQuery( slides[ next_index ] ).css( "z-index", 1 );

								cur_index = next_index;

								if ( !stop ) {
									handler = setTimeout( goto_slide, 3000 );
								}

								busy = false;
							}
						})
					}
				});
			}
		}

		setTimeout( goto_slide, 3000 );
	})();

	jQuery(".cost_calculator_main").each(function(){

		var main = jQuery( this ),
			self = main.find(".button_box"),
			pos = self.current_step || 0,
			tds = main.find(".steps_line td"),
			blocks = main.find(".forms_block .questions");

		self.find(".next button").click(function(){
			tds.eq( pos ).removeClass("active");
			blocks.eq( pos ).hide();

			pos++;
			if ( pos >= tds.length ) {
				pos = tds.length - 1;
			}

			tds.eq( pos ).addClass("active");
			blocks.eq( pos ).show();

			if ( pos > 0 ) {
				self.find(".prev").show();
			}
			if ( pos == tds.length - 1 ) {
				self.find(".next").hide();
				self.find(".send").show();
			}
		});

		self.find(".prev button").click(function(){
			tds.eq( pos ).removeClass("active");
			blocks.eq( pos ).hide();

			pos--;
			if ( pos < 0 ) {
				pos = 0;
			}

			tds.eq( pos ).addClass("active");
			blocks.eq( pos ).show();

			if ( pos == 0 ) {
				self.find(".prev").hide();
			}
			if ( pos < tds.length - 1 ) {
				self.find(".send").hide();
				self.find(".next").show();
			}
		});

		self.find(".send button").click(function(){

			jQuery.ajax({
				async: false,
				type: "post",
				url: "/ajax/calc.php",
				data: jQuery(".cost_calculator_main form").serialize(),
				dataType: "json",
				success: function( data ) {

					var found = false;

					if ( data.error == 0 ) {
						jQuery.each( data.postFields, function( key, error ) {
							jQuery(".cost_calculator_main form").find("[name*=" + key + "]").parent().next().html( '&nbsp;' );
						});

						jQuery.each( data.postErrors, function( key, error ) {
							jQuery(".cost_calculator_main form").find("[name*=" + key + "]").parent().next().html( error );
							found = true;
						});

						if ( !found ) {
							jQuery(".cost_calculator_main form").submit();
						}
					}
				}
			});

			return false;
		});
	});

	function questionnaire_calculate() {
		var cost = 0;

		jQuery(".switch_l1:checked").each(function() {

			var price = 0,
				cls = this.className.split(" ");

			for( var i = 0; i < cls.length; i++ ) {
				if ( cls[ i ].indexOf('price_') >= 0 ) {
					price = parseInt( cls[ i ].split('_').pop(), 10 );
				}
			}

			cost += price;

			jQuery( this ).parent().parent().next().find(".switch:checked").each(function() {

				var price = 0,
					cls = this.className.split(" ");

				for( var i = 0; i < cls.length; i++ ) {
					if ( cls[ i ].indexOf('price_') >= 0 ) {
						price = parseInt( cls[ i ].split('_').pop(), 10 );
					}
				}

				cost += price;
			});
		});

		if ( cost < parseInt( jQuery(".cost_calculator_main .callus_limit").html(), 10 ) ) {
			jQuery("#questionnaire_cost_hidden").val( "$" + cost );
			Cufon.replace( jQuery(".cost_calculator_main .cost").html( "$&nbsp;" + cost ), { fontFamily: 'Harabara' });
		} else {
			jQuery("#questionnaire_cost_hidden").val( jQuery(".cost_calculator_main .callus_label").html() );
			Cufon.replace( jQuery(".cost_calculator_main .cost").html( jQuery(".cost_calculator_main .callus_label").html() ), { fontFamily: 'Harabara', fontSize: "16px" });
		}
	}

	jQuery(".cost_calculator_main .switch").click(function() {
		questionnaire_calculate();
	});

	jQuery(".cost_calculator_main .switch_l1").click(function() {
		if ( jQuery( this ).is(":radio") ) {
			jQuery( this ).parent().parent().parent().find(".q_specifications").hide();
		}
		if ( jQuery( this ).attr("checked") ) {
			jQuery( this ).parent().parent().next().show();
		} else {
			jQuery( this ).parent().parent().next().hide();
		}
		questionnaire_calculate();
	});


});
