// JavaScript Document

$(document).ready(function(){
	$('a.collapse').wrap('<div/>').parent().next('p').hide();
	//slide up/down
	$('a.collapse').toggle(
		function(){
			$(this).addClass('opened').parent().next().slideDown('fast');
		},
		function(){
			$(this).removeClass('opened').parent().next().slideUp('fast');
		}
	);
	//up
	$('#up,.up').click(function(){
		$('body').scrollTo(0,500);
		return false
	});

	$('#subscribe>a,.subscr').click(function(){
		if(!$('#overlay2').html()){
			$('<div id="overlay"></div><div id="overlay2"></div>').appendTo('body');
			$('.subscribe').clone().css('display','inline-block').appendTo('#overlay2');
			$('#overlay2').css('top',getscrolltop()+112 + 'px');
			inp('.subscribe :text');
			clz();subm();
		}
		return false
	});
// zzzzzzzzzzzzzzzzzz



	$('a.submit').click(function(){
	  var el = $(this).parents('form:first');
	  if(el.hasClass('validate')){
		validate(el);
	  }
	  else{
		el.submit();
	  }
	  return false
	});

	$('input[type=submit]').click(function(){

	  var el = $(this).parents('form:first');
	  if(el.hasClass('validate')){
		validate(el);
	  }
	  else{
		  return true;
	  }
	  return false
	});

	$('#topMenu a').each(function(){
		$('<span>' + $(this).text() + '</span>').insertBefore($(this));
	});

	inp(':text:not(".quant"),:password');
	inp('input:hidden', true);

	faqbar();

	if ($('#country_field').size() && !$('#country_field').hasClass('noupdateplz')) {
		updateCityList($('#country_field input').val(), $('#city_field input').val());
	}
//slide up down
	$('#faq>li>.dotted').click(function(){
		if($(this).hasClass('open')){
			$(this).next().slideUp('fast',function(){
				$(this).prev().removeClass('open').parent().removeClass('active');
			});
		}
		else{
			$(this).addClass('open').parent().addClass('active');
			$(this).next().slideDown('fast');
		}
		return false
	});
//slide up down lessons for <a></a>
	$('#lessons>li>.dotted').click(function(){
		if($(this).hasClass('opened'))
			$(this).removeClass('opened').next().find('div.anons:first').slideDown(200).next().slideUp(200).parents('li:first').next().find('a:first').removeClass('opened').children().text('Развернуть');
		else
			$(this).addClass('opened').next().find('div.anons:first').slideUp(200).next().slideDown(200).parents('li:first').next().find('a:first').addClass('opened').children().text('Свернуть');
		return false
	});
//slide up/down
	$('#faq ul div a').click(function(){
		$(this).parents('ul:first').slideUp('fast',function(){
			$(this).prev().removeClass('open').parent().removeClass('active');
		});
	});
//slide up/down lessons for button
	$('#lessons ul .navigator a').click(function(){
		if($(this).hasClass('opened'))
			$(this).removeClass('opened').children('span').text('Развернуть').parents('ul:first').children('li.data').children('div.anons').slideDown(200).next().slideUp(200).parents('ul:first').prev().removeClass('opened');
		else
			$(this).addClass('opened').children('span').text('Свернуть').parents('ul:first').children('li.data').children('div.anons').slideUp(200).next().slideDown(200).parents('ul:first').prev().addClass('opened');
		return false;
	});
	$('#faqExpand a').click(function(){
		if ($(this).hasClass('opened')) {
			$('div#faqExpand a').html('<span>Свернуть всё</span>');
			$('#faq>li').addClass('active').find('ul').slideDown('5000');
		} else {
			$('div#faqExpand a').html('<span>Развернуть всё</span>');
			$('#faq>li').removeClass('active').find('ul').slideUp('5000');
		}
 		return false
	});

	$('div.faqQuestion>a').click(function(){
		$('<div id="overlay"></div><div id="overlay2">').appendTo('body');
		$('#faqQuestion').clone().css('display','inline-block').appendTo('#overlay2');
		$('#overlay2').css('top',getscrolltop()+112 + 'px');
		inp('#faqQuestion :text');
        $.each($('.faqCategory>em'), function () {
            $(this).html($('#cur_cc_name').val());
        });
        $.each($('.selectvalue'), function() {
            $(this).attr('value', $('#cur_cc_id').val());
        });
		clz();subm();faqbar();
		return false
	});

	$('a.faqq').click(function(){
		$('<div id="overlay"></div><div id="overlay2">').appendTo('body');
		$('#faqQuestion').clone().css('display','inline-block').appendTo('#overlay2');
		$('#overlay2').css('top',getscrolltop()+112 + 'px');
		inp('#faqQuestion :text');
        $.each($('.faqCategory>em'), function () {
            $(this).html($('#cur_cc_name').val());
        });
        $.each($('.selectvalue'), function() {
            $(this).attr('value', $('#cur_cc_id').val());
        });
		clz();subm();faqbar();
		return false
	});


	$('a#tellFriendContent').click(function(){
		$('<div id="overlay"></div><div id="overlay2">').appendTo('body');
		$('#tellFriend').clone().css('display','inline-block').appendTo('#overlay2');
		$('#overlay2').css('top',getscrolltop()+112 + 'px');
		inp('#tellFriend :text');
		clz();subm();faqbar();
		return false
	});

	$('a#orderConsultLink').click(function(){
		if($('#orderConsult').size()) {
			$('<div id="overlay"></div><div id="overlay2"></div>').appendTo('body');
			$('#orderConsult').clone().css('display','inline-block').appendTo('#overlay2');
			$('#overlay2').css('top',getscrolltop()+112 + 'px');
			//inp('#orderConsult :text');
			clz();subm();
		}
		return false
	});

	$('a.orderVyezdLink').click(function(){
		if($('.orderVyezd').size()){
			$('<div id="overlay"></div><div id="overlay2"></div>').appendTo('body');
			$('.orderVyezd').clone().css('display','inline-block').appendTo('#overlay2');
			$('#overlay2').css('top',getscrolltop()+112 + 'px');
			//inp('.orderVyezd :text');
			clz(); subm();
		}
		return false
	});

	$('#center p img[align=right]').addClass('imgRight');

	$('.subMenu a[rel]').click(function(){
		$('.subMenu a[rel]').removeClass('active');
		$(this).addClass('active');
		var col = $(this).attr('rel').split(',');
		$('#'+col[0]+'>div').hide();
		$('#'+col[1]).show();
		return false
	});
//calendar go (seminar)
	if($('#calendar').size()>0){
		$('#calendar td.month').each(function(){
			var i = $(this).attr('id').replace('month', '') - 1;
			$(this).calendarLite({currentMonth: i});
		});

		var calendarCol,calendarWi,maxscroll;
		calendarWidth();
		$(window).resize(function(){
			calendarWidth();
		});
// <-- prev		next -->
		$('#calendar .month:last').addClass('lst');
		$('#prev').click(function(){
			/*if($('#calendar').scrollLeft()>0){*/
			$('#calendar').scrollTo( {top:'0', left:'-=262px'}, 400 );
			return false
		});
		$('#next').click(function(){
			/*if($('#calendar').scrollLeft()<maxscroll){*/
			$('#calendar').scrollTo( {top:'0', left:'+=262px'}, 400 );
			return false
		});




// qqqqqqqqqqqq





// ------------------------------------------------------------------------------------------------------------------------------------------------------------

//vudelenie dat na calendare
		$('#lessons span.date span').each(function(){
			$(this).attr('title',$(this).text());
			var dates = $(this).text();
			$('#calendar td[title=' + dates + ']').addClass('active');
		});
//day work
		$('#calendar .active span').click(function(){
		//delete class='show'
			$('#lessons > li').each(function(){
				$(this).removeClass('show');
			});
		//delete class='span.select' - day
			$('#calendar .active span.select').each(function(){
				$(this).removeClass('select');
			});
		//delete class='a.select' - month
			$('#calendar .monthName>a').each(function(){
				$(this).removeClass('select');
			});
		//add class='show'
			var date = $(this).parent().attr('title');
			$('#lessons span[title=' + date + ']').each(function(){
				$(this).parents('ul:first').parent('li:first').addClass('show');
			});
		//show li or not
			$('#lessons > li').each(function(){
				if($(this).hasClass('show')) $(this).fadeIn(100);
				else $(this).fadeOut(100);
			});
		//add dateTitle
			$('#dateTitle').text(date+':');
		//add class = 'select' to span
			$(this).addClass('select');
			return false;
		});
//month tooltip
		$('.monthName>a').hover(
			function(){
				$('#monthtooltip').show();
				document.onmousemove = function(e){
					var cursor = mousePageXY(e);
					$('#monthtooltip').css('left',cursor.x-21+'px').css('top',cursor.y+23+'px');
				};
			},
			function(){
				$('#monthtooltip').hide();
			}
		);
//month work
		$('.monthName>a').click(function(){
		//delete class = 'show'
			$('#lessons > li').each(function(){
				$(this).removeClass('show');
			});
		//delete class='span.select' - day
			$('#calendar .active span.select').each(function(){
				$(this).removeClass('select');
			});
		//delete class='a.select' - month
		$('#calendar .monthName>a').each(function(){
			$(this).removeClass('select');
		});
		//add class='show'
			var el = $(this).attr('href').split('.');var elm = el[1]+'.'+el[2];
			$('#lessons span[title$=' + elm + ']').each(function(){
				$(this).parents('ul:first').parent('li:first').addClass('show');
			});
		//show li or not?
			$('#lessons > li').each(function(){
				if($(this).hasClass('show')) $(this).fadeIn(100);
				else $(this).fadeOut(100);
			});
		//add dateTitle
			$('#dateTitle').text($(this).text()+':');
		//add class='a.select'
			$(this).addClass('select');

			return false;
		});
	//init current month
		if(!location.href.match(/\?/)) {
			var month1 = $('#month1 .monthName>a');
			var el = month1.attr('href').split('.');var elm = el[1]+'.'+el[2];
			$('#lessons span[title$=' + elm + ']').each(function(){
				$(this).parents('ul:first').parent('li:first').addClass('show');
			});
		//show li or not?
			$('#lessons > li').each(function(){
				if($(this).hasClass('show')) $(this).fadeIn(100);
				else $(this).fadeOut(100);
			});
		//add class='a.select'
			month1.addClass('select');
		//add dateTitle
			$('#dateTitle').text(month1.text()+':');
		}
	}//calendar end

	$('.feedback a').click(function(){
		$('<div id="overlay"></div><div id="overlay2"></div>').appendTo('body');
		$('.feedbackform').clone().css('display','inline-block').appendTo('#overlay2');
		$('#overlay2').css('top',getscrolltop()+112 + 'px');
		inp('#feedForm :text');
		clz();subm();inp(':text,:password,.txarr');
		return false
	});

	$('#login .password').click(function(){
		$(this).next().focus().end().remove();
	});
	$('#login :password').blur(function(){
		if($(this).val() == ''){
			$('<span class="password">Пароль</span>').insertBefore($(this));
			$('#login .password').click(function(){
				$(this).next().focus().end().remove();
			});
		}
	});


	$('a[rel=tooltip]').hover(
		function(){
			$('<div id="monthtooltip">' + $(this).attr('title') + '</div>').appendTo('body');
				document.onmousemove = function(e){
					var cursor = mousePageXY(e);
					$('#monthtooltip').show().css('left',cursor.x-21+'px').css('top',cursor.y+23+'px');
				};
		},
		function(){
			$('#monthtooltip').remove();
		}
	);

	$('ul.shopmenu > li').each(function(){if($(this).find('ul').size()>0){$(this).find('a:first').toggle(function(){$(this).next().slideDown();},function(){$(this).next().slideUp();});}});
    $('ul#leftMenu li a.mm_item_0').bind('click', function(e){
        if (!$(this).hasClass('mm_item_0')) {
            return true;
        }

        var _ul = $(this).next('ul');
        var _d = $(_ul).css('display');
        if (_d == 'none') {
            $(_ul).slideDown('fast');
            $(this).css('color', '#F58320');
        } else {
            $(_ul).slideUp('fast');
            $(this).css('color', '#666666');
        }

        return false;
    });

	$('a.submit_ajax').live('click', function(){
	    var vals = $(this).parents('form').serialize();
		//$('div#putcart').css('display', 'block');
		$.post('/netcat/modules/netshop/post.php', vals, function(data){
            var page = data.split('::::::::');
			$('div#cart').html(page[0]);
			$('div#tovar_area').remove();

			if ($("#center2 .content").length > 0) {
				$('#center2 .content').after(page[1]);
			} else {
			    $('div#way').after(page[1]);
			}

			//$('div#putcart').css('display', 'none');
		});
		return false;
    });

	$('a.tovar_feedback').click(function(){
		$('<div id="overlay"></div><div id="overlay2"></div>').appendTo('body');
		$('#tovarFeedback').clone().css('display','inline-block').appendTo('#overlay2');
		$('#tovarFeedback').css('top',getscrolltop()+112 + 'px');
		inp('#tovarFeedback :text');
		clz();subm();inp(':text,:password,.txarr');
		return false
	});

	$('a.spec_feedback').click(function(){
		$('<div id="overlay"></div><div id="overlay2"></div>').appendTo('body');
		$('#specFeedback').clone().css('display','inline-block').appendTo('#overlay2');
		$('#specFeedback').css('top',getscrolltop()+112 + 'px');
		inp('#specFeedback :text');
		clz();subm();inp(':text,:password,.txarr');
		return false
	});

	$('#tcr tr').each(function(){
		$(this).find('td:last').addClass('lst');
	});

	total();

	$('#tcr input.quant').live('keyup', function(e){
		var cc= e.keyCode;
		if (cc >= 97 && cc <= 105) cc = cc - (97-49);
		if(cc > 31 && (cc < 48 || cc > 57)) {
			return false
		}
		//total();
		cart_recalc();
    });

	$('td.del a').live('click', function(){
		var _tr = $(this).parents('tr');
		var _input = $('td input.quant', _tr);
		//.val('0');
		$(_input).val('0');
		cart_recalc();
	});

	$('input#rbDelivery12').bind('click', function(){
		delivery_mode(1);
	});

	$('input#rbDelivery2').bind('click', function(){
		delivery_mode(2);
	});

	$('input#rbDelivery1').bind('click', function(){
		delivery_mode(3);
	});

	$('#netshop_cart_contents input.quant').live('keydown', function(e){
		if (e.keyCode == 13) {
		    return false;
		}
	});

	$('td.country2 .faqBar a').click(function(){
		if ($(this).hasClass('noupdateplz')) { alert('boo'); return false };
		changeCountry(this);
	});

	$('#city_field2 ul li a').click(function(){
		if ($(this).hasClass('noupdateplz')) { alert('boo'); return false };
		changeTown(this);
	});
});
