﻿var blank = new Image();
blank.src = '/i/img/blank.gif';
var chat_loaded=false;
var chat_man=false;
function fixPng(png) {
	var src = png.src;
	if (!png.style.width) { png.style.width = $(png).width(); }
	if (!png.style.height) { png.style.height = $(png).height(); }
	png.src = blank.src;
	png.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "',sizingMethod='scale')";
}

function isValidEmail (email, strict) {
	if ( !strict ) email = email.replace(/^\s+|\s+$/g, '');
	return (/^([a-z0-9_\-]+\.)*[a-z0-9_\-]+@([a-z0-9][a-z0-9\-]*[a-z0-9]\.)+[a-z]{2,4}$/i).test(email);
}
function hide_message() {
	var element = 'div.message_board';
	if ( $(element).css('display') == 'none' ) {
		return false;
	}

	$(element)
		.css('opacity', '1')
		.animate({
			opacity:0
		}, 200, function() {
			$(this).css('display', 'none');
		});
}
function show_message( message ) {
	var screen_width = $(window).width();
	var screen_height = $(window).height();
	var screen_scroll = $(window).scrollTop();


	$('div.message_board')
		.html( message )
		.css('left', parseInt( screen_width / 2 ) - 100 )
		.css('top', parseInt( screen_height / 2 ) - 50 + screen_scroll )
		.css('opacity', '0')
		.css('display', 'block')
		.animate({
			opacity:1
		}, 200);
}
$(document).ready(function() {
	var temp, temp2;
	var badBrowser = (/MSIE ((5\.5)|6)/.test(navigator.userAgent) && navigator.platform == "Win32");
	if (badBrowser) {
		$('img[src$=.png]').each(function() {
			if (!this.complete) {
				this.onload = function() { fixPng(this) };
			}else {
				fixPng(this);
			}
		});
	}
	$('#comeina').click(function() {
		var toffset=$('#comeina').offset();
		$('#authorize_').css('left',toffset.left-98).css('top',toffset.top+22).show();
		$('#username').focus();
		return false;
	});
	var hover=false;
	$('#authorize_').hover(function() {
		hover=true;
	}, function() {
		hover=false;
	});
	$('html').click(function(){
		if (!hover) {
			$('#authorize_').hide();
		}
	});
	$('#close_but').click(function() {
			$('#authorize_').hide();
	});
	var $rub=$('ins.ru_show');
	$rub.html('&nbsp;').addClass('new_ru');
	var $rub=$('ins.ru');
	$rub.html('&nbsp;').addClass('new_ru_m');
	$('a.clear_list_a').click(function() {
		if (parseInt($('#cnt_compare_prod').html())==0) {
			return false;
		}
	});
	var $spec_rub=$('ins.ru_spec');
	$spec_rub.html('&nbsp;').addClass('new_ru_spec');
	var $prod_cart_price_ins=$('span.prod_cart_price ins');
	$prod_cart_price_ins.html('&nbsp;').addClass('new_ru_prod_cart');
	var $prod_cart_price_ins=$('td.prod_cart_item_cost ins');
	$prod_cart_price_ins.html('&nbsp;').addClass('new_ru_prod_cart2');

	var $prod_cart_price_ins=$('span.cart_head_noactive ins');
	$prod_cart_price_ins.html('&nbsp;').addClass('new_ru_prod_cart3');

	var $prod_cart_price_ins=$('span.prod_cart_itogo_price ins');
	$prod_cart_price_ins.html('&nbsp;').addClass('prod_cart_itogo_ru');
	var $prod_cart_price_ins=$('span.cart_head ins');
	$prod_cart_price_ins.html('&nbsp;').addClass('cart_ru_up');
	//find max height in products photo
	var $span_photo=$('span.photo_center');
	var max_height=0;
	$span_photo.each(function() {
		if ($(this).height()>max_height) {
			max_height=$(this).height();
		}
	});
	var dop_height=$('img.novelty_img').height();
	$span_photo.height(max_height);
	var is_ie=(/MSIE ((5\.5)|6|7)/.test(navigator.userAgent) && navigator.platform == "Win32");
	$span_photo.each(function() {
		if ($(this).parent().find('img.novelty_img').height() == null && !is_ie) {
			$(this).height(max_height+27);
		}
	});
	var now=new Date();
	var tomorrow=now;
	tomorrow.setDate(now.getDate()+1);
	tomorrow.setHours(0);
	tomorrow.setMinutes(0);
	tomorrow.setSeconds(0);
	tomorrow.setMilliseconds(0);
	var tnow,difference,dhours,dminutes,dseconds;
	var $timer=$('div#spec_clocks span');
	var thistimer=setInterval(function(i) {
		tnow=new Date();
		difference=new Date(parseInt(tomorrow.getTime()-tnow.getTime()));
		difference.setMilliseconds(0);
		dhours=difference.getUTCHours();
		dminutes=difference.getUTCMinutes();
		dseconds=difference.getUTCSeconds();
		if (parseInt(dminutes/10)==0) {
			dminutes='0'+dminutes;
		}
		if (parseInt(dseconds/10)==0) {
			dseconds='0'+dseconds;
		}
		var aaa=dhours+':'+dminutes+':'+dseconds;
		$timer.html(aaa);
	}, 1000);
	var $beznal =$('#beznal');
	var $organization_input=$('#organization_input');
	$beznal.click(function() {
		//показать графу для юрлиц
		$organization_input.show().find('input').focus();
	});
	$('input.input_radio').click(function() {
		$organization_input.hide();
	});
	$('#adres_dost_but').click(function() {
		if ($("input[name='username']").val().length<1) {
			alert('Необходимо ввести имя и фамилию');
			return false;
		}
		if ($("input[name='adress']").val().length<1) {
			alert('Необходимо ввести ваш почтовый адрес');
			return false;
		}
/*		var email=$("input[name='usermail']").val();
		if (!isValidEmail(email)) {
			alert('Введите правильный email');
			return false;
		}
*/
		if ( $(":radio[name=oplata]").filter(":checked").val() == 3 && $("input[name='organization']").val().length<1 ) {
			alert('Необходимо ввести название организации');
			return false;
		}
	});
	var $cart_bg_popup=$('#cart_bg_popup');
	var $cart_bg=$('#cart_bg');
	$('#show_pre_cart').click(function() {//открыть всплывающая корзина

		$("#full_bask").css('display', "block" );
		$("#dop_div").css('visibility','hidden');
	});
	$('#hide_pre_cart').click(function() {//скрыть всплывающая корзина
		$("#full_bask").css('display', "none" );
		$("#dop_div").css('visibility','visible');
	});
	$('input.cart_continue2').click(function() {//кнопка продолжить
		if ($beznal.attr('checked') && $('input.cart_input_text').val()=='') {
			alert('Введите название организации');
			return false;
		}
		return true;
	});
	var $text_search=$('#text_search');
	if ($text_search.attr('value')=='') {
		$text_search.addClass('text_search');
	}
	if ($text_search.attr('value')=='') {
		$text_search.attr('value', 'Поиск товара');
	}
	$text_search.focus(function() {
		if ($text_search.attr('value')=='Поиск товара') {
			$text_search.attr('value', '').removeClass('text_search');
		}
	}).blur(function() {
		if ($text_search.attr('value')=='') {
			$text_search.attr('value','Поиск товара').addClass('text_search');
		}
	});
	if ($('#cnt_compare_prod').html()==0) {
		$('div.compare_block').hide();
	}
	var ch=true;
	$('table.compare_table tr').each(function() {
		if (ch) {
			$(this).addClass('back_tr1');
		}else {
			$(this).addClass('back_tr2');
		}
		ch=!ch;
	});
	
	/**/
	$('#cheaper').click(function() {
		var t_offset=$('#cheaper').offset();
		$('#cheaper_form').css('left', t_offset.left-370).css('top', t_offset.top+20).show();
		$('#cheaper_form input.call_input[name="model"]').val($('h1.show_prod_title').text())
		return false;
	});
	
		$('#cheaper_form div.close_callback').click(function() {
		$('#cheaper_form #all_text').hide();
		$('#cheaper_form').hide();
		$('#cheaper_form *').show();
		$('#cheaper_form #loading').hide();
		
	});
	
	
	/**/
	$('#call_back').click(function() {
		var t_offset=$('#call_back').offset();
		$('#call_back_form').css('left', t_offset.left-100).css('top', t_offset.top).show();
		
		
		return false;
	});
	
	$('div.close_callback').click(function() {
		$('#all_text').hide();
		$('#call_back_form').hide();
		$('#call_back_form *').show();
		$('#loading').hide();
		
	});
	var this_location=window.location.toString();
	var t_pattern=/call_back/;
	if (t_pattern.test(this_location)) {
		$('#call_back').hide();
	}
	$('#online_con').click(function() {
		if (!chat_loaded) {
			$.getScript("/js/chat.js");
			chat_loaded=true;
		}
		return false;
	});
	$('#online_man').click(function() {
		if (!chat_man) {
			$.getScript("/js/chat_man.js");
			chat_man=true;
		}
		return false;
	});
	var wind_scroll=0;
	
	
	
	var $call_back_form = $('#call_back_form');
	var $all_text=$('#all_text');
	var $call_submit = $('input.call_submit');
	$call_submit.click(function() {
		$call_submit.hide();
		$('#loading').show();
		temp = $("input.call_input[name='phone']").val();
		temp = temp.length;
		temp2 = $("textarea.call_textarea[name='question']").val();
		temp2 = temp2.length;
		if ( temp > 5 && temp2 > 5 ) {
			$.post("/call_back", { phone: $("input.call_input[name='phone']").val(), name: $("input.call_input[name='name']").val(), question: $("textarea.call_textarea[name='question']").val() },
				function (data) {
					$('#loading').hide();
					//$('div.close_callback').click();
					$call_back_form.find('*').hide();
					temp = $call_back_form.position();
					$all_text.html('Спасибо, ваше сообщение отправлено').css('left', temp.left+5).css('top', temp.top+150);
					$all_text.show();
					setTimeout('$("div.close_callback").click()', 2000);
					$call_submit.show();
			});
		}else {
			temp = $call_back_form.position();
			$('#loading').hide();
			$all_text.html('Сообщение не может быть пустым').css('left', temp.left+5).css('top', temp.top+150);
			$all_text.show();
			setTimeout('$("#all_text").hide()', 2000);
			$call_submit.show();
		}
		return false;
	});
	
	
	/**/
	
	var $cheaper_form = $('#cheaper_form');
	//var $all_text=$('#all_text');
	var $cheaper_submit = $('#cheaper_form input.cheaper_submit');
	$cheaper_submit.click(function() {
		$cheaper_submit.hide();
		$('#cheaper_form #loading').show();
		temp54 = $("#cheaper_form input.call_input[name='model']").val();
		temp54 = temp54.length;
		temp25 = $("#cheaper_form input.call_input[name='email']").val();
		temp25 = temp25.length;
		if ( temp54 > 5 && temp25 > 5 ) {
			$.post("/cheaper", { model: $("#cheaper_form input.call_input[name='model']").val(), where:$("#cheaper_form input.call_input[name='where']").val(), price:$("#cheaper_form input.call_input[name='price']").val(), fio:$("#cheaper_form input.call_input[name='fio']").val(), email:$("#cheaper_form input.call_input[name='email']").val(), telephone:$("#cheaper_form input.call_input[name='telephone']").val() },
				function (data) {
					$('#cheaper_form #loading').hide();
					//$('div.close_callback').click();
					$cheaper_form.find('*').hide();
					temp33 = $cheaper_form.position();
					$all_text.html('Спасибо, ваше сообщение отправлено').css('left', temp33.left+5).css('top', temp33.top+150);
					$all_text.show();
					setTimeout('$("#cheaper_form div.close_callback").click()', 2000);
					$cheaper_submit.show();
			});
		}else {
			temp44 = $cheaper_form.position();
			$('#cheaper_form #loading').hide();
			$all_text.html('Сообщение не может быть пустым').css('left', temp44.left+5).css('top', temp44.top+150);
			$all_text.show();
			setTimeout('$("#all_text").hide()', 2000);
			$cheaper_submit.show();
		}
		return false;
	});
	
		var $overlay = $('div.background-overlay');
	$('span.block_video').click(function() {
		$overlay.show();
		var temp_html2='<object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/10jZEGfhWlE&amp;hl=ru_RU&amp;fs=1?rel=0"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/10jZEGfhWlE&amp;hl=ru_RU&amp;fs=1?rel=0" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object>', temp_html1='<object width="560" height="340"><param name="movie" value="http://www.youtube.com/v/D6OJEBdwAPA&amp;hl=ru_RU&amp;fs=1?rel=0"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/D6OJEBdwAPA&amp;hl=ru_RU&amp;fs=1?rel=0" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="560" height="340"></embed></object>';
		if ( $(this).attr('rel') == 'video1' ) {
			$overlay.html('<center class="center_video">'+temp_html1+'</center>');
		}else {
			$overlay.html('<center class="center_video">'+temp_html2+'</center>');
		}
	});
	$overlay.click(function() {
		$overlay.hide();
	});

	var old_images_in = 0, image_width = 100, delimiter_width = 10;
	var current_width = $('div.header_gallery').width() - 56, images_in = parseInt( current_width / ( image_width + delimiter_width ) ), images_count = $('div.header_gallery ul li').length;
	$('div.header_gallery ul').jcarousel({
		reloadCallback: function( carousel ){
			current_width = $('div.header_gallery').width() - 56, images_in = parseInt( current_width / ( image_width + delimiter_width ) ), images_count = $('div.header_gallery ul li').length;
			if ( images_count < images_in ) {
				images_in = images_count;
			}
			if ( old_images_in != images_in ) {
				old_images_in = images_in;
				var new_width = images_in * image_width + ( images_in - 1 ) * delimiter_width;
				$('div.jcarousel-skin-atletiko div.jcarousel-clip,div.jcarousel-skin-atletiko div.jcarousel-container').width( new_width );
			}
		}
	});
	$('div.header_gallery ul li a').fancybox({});
	if ( images_count < images_in ) {
		images_in = images_count;
	}
	if ( old_images_in != images_in ) {
		old_images_in = images_in;
		var new_width = images_in * image_width + ( images_in - 1 ) * delimiter_width;
		$('div.jcarousel-skin-atletiko div.jcarousel-clip,div.jcarousel-skin-atletiko div.jcarousel-container').width( new_width );
	}
	
	if ( !badBrowser ) {
		$('div.header_gallery ul li').append( '<div class="rounded_corners_left"></div><div class="rounded_corners_right"></div>' );
	}
	$('body').append('<div class="message_board"></div>');
	$('div.message_board').click( function() {
		hide_message();
	});
	if ( $('div.error_message').length ) {
		$('body').animate(
			{scrollTop:$('div.error_message').offset().top},
			500
		);
	}
	var default_input = {
			'name':'Ваше имя',
			'phone':'Телефон',
			'email':'E-mail',
			'square':'Площадь зала',
			'city':'Город',
			'tren':'Необходимые тренажеры',
			'dopinfo':'Дополнительная информация'
		};
	$('div.send_message form input[type=text], div.send_message form textarea').live( 'click',
		function( e ) {
			if ( $(this).val() == default_input[ $(this).attr('name') ] ) {
				$(this).val( '' );
			}
	});
	$('div.send_message form input[type=text], div.send_message form textarea').live( 'blur',
		function( e ) {
			if ( $(this).val() == '' ) {
				$(this).val( default_input[ $(this).attr('name') ] );
			}
	});
	$('div.send_message form').live( 'submit', function( e ) {
		var error_message = '', $self = $(this);
		var input = {
			'name':'имя',
			'phone':'телефон',
			'email':'e-mail',
			'square':'площадь зала',
			'city':'город',
			'tren':'необходимые тренажеры'
		};

		var post = {'javascript':true};
		var pr_name;
		for ( pr_name in input ) {
			if ( $self.find('input[name=' + pr_name + ']').length ) {
				var $input = $self.find('input[name=' + pr_name + ']');
				if ( $input.val().length < 2 || $input.val() == default_input[ $input.attr('name') ]) {
					error_message = 'Не заполнено поле ' + input[pr_name];
					break;
				}else {
					post[$input.attr('name')] = $input.val();
				}
			}
		}

		if ( error_message != '' ) {
			show_message( error_message );
			setTimeout('hide_message()', 2000);
			$self.find('input[name=' + pr_name + ']').focus();
		}else {
			if ( !$self.find('input[name=plan]').length ) {
				$.post(
					window.location.href,
					post,
					function(data) {
						$('div.error_message').remove();
						$('div.send_message').remove();
						$('#content').append( data );
					},
					'html'
				);
			}else {
				return true;
			}
		}
		e.preventDefault();
	});
	jQuery.each( jQuery.browser, function(i, val) {
		if ( i == "mozilla" && ( jQuery.browser.version.substr( 0, 3 ) == "1.9" || jQuery.browser.version.substr( 0, 3 ) == "2.0" ) )
			$('div.send_message input.button').css('padding-bottom', '4px');
	});
});
