/* Fix the position of an element when it is about to be scrolled off-screen */
function smartPosition(obj) {
	if ( jQuery.browser.msie && parseInt(jQuery.browser.version, 10) < 7 ) {
		return;
	}
	
	jQuery(window).scroll(function() {
		// Detect if content is being scroll offscreen.
		if ( (document.documentElement.scrollTop || document.body.scrollTop) >= jQuery(obj).offset().top) {
			jQuery('#order-block').addClass('smartposition');
		} else {
			jQuery('#order-block').removeClass('smartposition');
		}
	});
};

$.updateDimensions = function() {
	
	bodyWidth = $(document).width();
	
	if (bodyWidth < 1005) {
		
		$('div.main').css('width', '990px');
		
	}
	else if (bodyWidth > 1005) {
		
		$('div.main').css('width', '100%');
		
	}
	
	if (bodyWidth > 1280) {
		
		$('div.main').css('width', '1280px');
		
	}

	
};

	function showShadow() { 
	$("#shadow").slideToggle("narmal");
	$("#order-block").slideToggle("narmal");
	return false;
	}	

	function closeShadow() { 
	$("#shadow").slideToggle("narmal");
	$("#order-block").slideToggle("narmal");
	return false;
	}	


$.validator.addMethod("phone", function(ph, element) {
	if (ph == null) {
	return false;
	}
	var stripped = ph.replace(/[\s()+-]|ext\.?/gi, "");
	return ((/\d{5,}/i).test(stripped));
}, "Введите правильный номер телефона!");

$(document).ready(function() {
	
	$('.phone').cycle({
	    timeout: 10000, 
	      delay: -2000 
	});

	$('.testimonials').cycle({
	      timeout: 10000,
		   prev: '#prev',
		   next: '#next'
	});

	$(".testimonials a").click(function(event){
		return false;
	});

	
	getTwitters('tweet', {
	  id: 'adviko', 
	  count: 1, 
	  enableLinks: true, 
	  ignoreReplies: true, 
	  clearContents: true,
	  template: '<p>%text%</p>'
	});
/*	$(".links-info-container span").tipTip({
		maxWidth: "200px",
		   delay: "0",
	  edgeOffset: 3
		});*/
						   
	$.updateDimensions();	
	$(".shadow").height ($(".main").height());
	$(".links-info-middle-left").height ($(".links-info-middle-center").height());
	$(".links-info-middle-right").height ($(".links-info-middle-center").height());
	$(".links-info-top-center").width ($(".links-info-middle-center").width());
	
	$("#close").click(closeShadow);
	$("#order").click(showShadow);
	$("ul.question-answer-list li blockquote").hide();
	$("ul.question-answer-list li a").bind('click', function() {
		$(this).next('blockquote').slideToggle();
		return false;
	});
	$("ul.open-close-list li blockquote").hide();
	$("ul.open-close-list li h2").bind('click', function() {
		$(this).next('blockquote').slideToggle();
		return false;
	});
	
	$(window).resize(function() {
							  
		$.updateDimensions();

	});
	
	$('img.guy').hide();
	$('#active-guy').hover(function() {
		$('img.guy').show()
					.animate({opacity : '0.0'}, 0)
					.animate({top: '130px'}, 100)
					.animate({
						top: '-130px',
						opacity : '1.0'
					 }, 150)
					.animate({top: '-100px'}, 150)
					.animate({top: '-120px'}, 150)
	}, function() {
		$('img.guy').animate({top: '-135px'}, 150)
					.animate({
						top: '110px',
						opacity : '0.0'
					}, 100)
	});
	
	$('#orderForm').validate({
		success: function(label) {
			label.parent("div").hide();
			label.parent("div").prev("div").removeClass('error-input');
		},
		errorElement: "i",
		errorPlacement: function(error, element) {
			error.appendTo( element.parent("div").next("div") );
			element.parent("div").addClass('error-input');
			element.parent("div").next("div").show();
		},
		highlight: function( element, errorClass, validClass ) {
			$(element).parent("div").addClass('error-input');
			$(element).parent("div").next("div").show();
		},
		unhighlight: function( element, errorClass, validClass ) {
			$(element).parent("div").removeClass('error-input');
			$(element).parent("div").next("div").hide();
		},
		rules: {
			name: "required",
			phone: {
				phone: true,
				required: true
			},
			email: {
				email: true,
				required: true
			},
			message: "required"
		},
		messages: {
			name: "Введите свое имя",
			phone: "Введите правильный номер телефона",
			email: "Введите корректный адрес электропочты",
			message: "Введите ваше сообщение"
		},
		submitHandler: function(form) {
			var name = $(form).contents().find('input[name="name"]').val();
			var phone = $(form).contents().find('input[name="phone"]').val();
			var email = $(form).contents().find('input[name="email"]').val();
			var message = $(form).contents().find('textarea[name="message"]').val();
			//disable form elements
			$(form).contents().find('input').attr('disabled', 'disabled');
			$(form).contents().find('textarea').attr('disabled', 'disabled');
			$.post(
				'send.php',
				{
					name: name,
					phone: phone,
					email: email,
					message: message,
					ajax: 'true'
				},
				function(data) {
					if (data=='true') {
						$(form).after('<p>Ваше сообщение успешно отправлено. В ближайшее время с Вами свяжется наш менеджер.</p>');
						$(form).hide();
					} else {
						$(form).prepend('<p>Во время отправки сообщения возникла ошибка, попробуйте еще раз или <a href="about.html">позвоните по телефону</a></p>');
						$(form).contents().filter('input').attr('disabled', '');
						$(form).contents().filter('textarea').attr('disabled', '');
					}
				}
			);
			return false;
		}
	});
	
	$('#orderForm2').validate({
		success: function(label) {
			label.parent("div").hide();
			label.parent("div").prev("div").removeClass('error-input');
		},
		errorElement: "i",
		errorPlacement: function(error, element) {
			error.appendTo( element.parent("div").next("div") );
			element.parent("div").addClass('error-input');
			element.parent("div").next("div").show();
		},
		highlight: function( element, errorClass, validClass ) {
			$(element).parent("div").addClass('error-input');
			$(element).parent("div").next("div").show();
		},
		unhighlight: function( element, errorClass, validClass ) {
			$(element).parent("div").removeClass('error-input');
			$(element).parent("div").next("div").hide();
		},
		rules: {
			name2: "required",
			phone2: {
				phone: true,
				required: true
			},
			email2: {
				email: true,
				required: true
			},
			message2: "required"
		},
		messages: {
			name2: "Введите свое имя",
			phone2: "Введите правильный номер телефона",
			email2: "Введите корректный адрес электропочты",
			message2: "Введите ваше сообщение"
		},
		submitHandler: function(form) {
			var name = $(form).contents().find('input[name="name2"]').val();
			var phone = $(form).contents().find('input[name="phone2"]').val();
			var email = $(form).contents().find('input[name="email2"]').val();
			var message = $(form).contents().find('textarea[name="message2"]').val();
			//disable form elements
			$(form).contents().find('input').attr('disabled', 'disabled');
			$(form).contents().find('textarea').attr('disabled', 'disabled');
			$.post(
				'send.php',
				{
					name: name,
					phone: phone,
					email: email,
					message: message,
					ajax: 'true'
				},
				function(data) {
					if (data=='true') {
						$(form).after('<p>Ваше сообщение успешно отправлено. В ближайшее время с Вами свяжется наш менеджер.</p>');
						$(form).hide();
					} else {
						$(form).prepend('<p>Во время отправки сообщения возникла ошибка, попробуйте еще раз или <a href="about.html">позвоните по телефону</a></p>');
						$(form).contents().filter('input').attr('disabled', '');
						$(form).contents().filter('textarea').attr('disabled', '');
					}
				}
			);
			return false;
		}
	});
	
	smartPosition('#order-position');

	$('div.links-info-container span').each(function() {
		var thisText = $(this).text();
		var thisTitle = $(this).attr('title');
		$(this).after('<div class="links-info" style="display: none;"><table cellpadding="0" cellspacing="0"><tr><td class="info-bg1"><div>&nbsp;</div></td><td class="info-bg2">&nbsp;</td><td class="info-bg3"><div>&nbsp;</div></td></tr><tr class="indent3"><td class="info-bg4">&nbsp;</td><td class="info-bg5"><p><strong>'+thisText+'</strong></p><p>'+thisTitle+'</p></td><td class="info-bg6">&nbsp;</td></tr><tr class="indent3"><td class="info-bg7">&nbsp;</td><td class="info-bg8"><div class="info-bg8-bg1"><div class="info-bg8-bg2"></div><div class="info-bg8-bg3"></div></div></td><td class="info-bg10">&nbsp;</td></tr></table></div>')
		$(this).attr('title', '');
	});
	
	$('div.links-info-container span').hover(function() {
		$(this).next('div.links-info').show();
	}, function() {
		$(this).next('div.links-info').hide();
	});


	$('#offer').cycle({
  		     fx: 'scrollHorz',
        timeout: 6000,
		   prev: 'div.offer-nav-prev',
		   next: 'div.offer-nav-next'
	});
	
	
});
