/* -------------------------------------------------- *
 * Project scripts
 * -------------------------------------------------- *
 * Author: Morozov Igor
 * URL: http://www.morozoff.info/
 * Copyright: 2010 Morozov Igor
 ** -------------------------------------------------- */

$(document).ready(function () {
	if ($('#infopanel-fixed').length) {
		infopanelscroll();
	}

	if ($('.b-catitems').length) {
		catfluid();
	}

	$('input.i-p[title], input:text[title], textarea[title]').toggleVal({
		populateFrom:'title'
	});

	if ($('.open-listshops').length) {
		$('.open-listshops').click(function () {
			$('.pp-window').hide();
			$('#pp-shops').show().css('marginTop', $(window).scrollTop() + 'px');
			return false;
		});
	}
	;

	if ($('.constructor-allparam').length) {
		$('.constructor-allparam').click(function () {
			var $cont = $('.top-constructor');
			if ($(this).parents('.pp-constructor').length) {
				$cont.removeClass('active-constructor');
			}
			else {
				$cont.addClass('active-constructor');
			}
			return false;
		});
	}
	;

	/* login */
	$('.nv-r .nav-dealers:not(.logged) .enter').click(function () {
		$(".login-error").hide();
		$('#pp-loginform').show();
		return false;
	});

	$('.remind-block-popup, .remind-block-info').hide();

	$("#login-remind").click(function () {
		$(".login-error").hide();
		$('.login-block-popup').slideUp('slow');
		$('.remind-block-popup').slideDown('slow');

	});
	$('#returntologin').click(function () {
		$(".login-error").hide();
		$('#showaccept').parent().slideDown('fast');
		$('.remind-block-info').slideUp('fast');
		$('.remind-block-popup').slideUp('slow');
		$('.login-block-popup').slideDown('slow');
	});
	/* /login*/

	/* yellow submenu */
	$('.constructordrop-select').hide();
	$('.constructordrop__link').bind('click', function () {
		$(this).next().show();
		$(".constructordrop-select").bind("click",
				function () {
					$(this).hide();
				}).bind("mouseleave", function () {
			$('.constructordrop-select').hide();
		});
		return false;
	});
	/* /yellow submenu */
	/* vacancyinfo */
	$('.vacancyinfo').hide();
	$('.vacancyblock > h2 > a').toggle(function () {
		$(this).addClass('active');
		$(this).parent().parent().find('.vacancyinfo').slideDown('slow');
	}, function () {
		$(this).removeClass('active');
		$(this).parent().parent().find('.vacancyinfo').slideUp('slow');

	});

	/* bubble */
	$('.vacancyadd:not(.cart)').click(function () {
		var vac_id = $(this).find('a').attr('href').substring(1);
		var vac_name = $(this).parents('.vacancyblock').find('h2 span').html();
		$("#pp-sendvacancy h2").html("Анкета вакансии «" + vac_name + "»");
		$("#pp-sendvacancy input:hidden[name='A[vac_id]']").val(vac_id);
		$('#pp-sendvacancy').fadeIn('fast');
		return false;
	});

	$('#pp-sendvacancy .btn-submit').click(function () {
		$('#pp-sendvacancy').hide();
		return false;
	});
	/* /vacancyinfo */

	/* wishlist bubble */
	$('#pp-sendmail-link').click(function () {
		$('#pp-sendmail').show();
		return false;
	});
	$('#pp-sendmanager-link').click(function () {
		$('#pp-sendmanager').show();
		return false;
	});
	$('#pp-sendsizes-link').click(function () {
		$('#pp-sendsizes').show();
		return false;
	});
	$('#pp-senddesigner-link').click(function () {
		if ($('#pp-senddesigner').is(':empty')) {
			$('#pp-senddesigner').load("/_services/load-designer-win/", function () {
				$(this).find('input[name=from]').val(document.location.href);  // current url where return to
				$(this).css('top', $(window).scrollTop()+80);
				$(this).show();
			})
		} else {
			$('#pp-senddesigner').show();
		}
		return false;
	});

	if($("#pp-senddesigner-link").length) {
		if(document.location.href.search(/OK_des/)>0) {
			if($(".main-services-list-designer").length) {
				$(".main-services-list-designer").after("<div class='for-editor' style='margin-left:-40px;'><blockquote class='infotip' style='margin-right:10px'>Ваша заявка отправлена</blockquote></div>");
			} else {
				$("#pp-senddesigner-link").parents(".soon-items.m-soonitems-1:first").parent().before("<div class='for-editor' style='margin-top:20px;'><blockquote class='infotip'>Ваша заявка отправлена</blockquote></div>");
			}
		}
	}

	$('.basket-clear').click(function () {
		return confirm('Очистить?');
	})

	/* /wishlist bubble*/

	/* changeinfo */
	$('.changeinfo').click(function () {
		$('#pp-sendclientinfo').show();
		return false;
	});
	/* /changeinfo */

	/* history */
	$('.history-wrap .basket-grid').hide();

	$('.hi-link').toggle(function () {
		$(this).parent().next().slideDown();
		return false;
	}, function () {
		$(this).parent().next().slideUp();
		return false;
	});
	/* /history */
	$('.pp-window .close').live('click', function () {
		$('.pp-window').hide();
		return false;
	});

	/* infosizes-panel-viewer .hd .infosizes-sizes */
	$('.infosizes-panel-viewer .hd').bind('click', function (event) {
		$(this).parent().find('.infosizes-sizes').show().bind('mouseleave', function (event) {
			$('.infosizes-sizes').hide();
		});
		return false;
	});

	/*disquise link*/
	$('.b-photocard .big-ph a').click(function () {
		return false;
	});
});

function infopanelscroll() {
	var $window = $(window);
	var $panel = $('#infopanel-fixed');
	var $panell__pos = $panel.offset().top;

	$window.bind('scroll', function () {
		var post = $(window).scrollTop()
		if (post > $panell__pos) {
			$panel[0].style.top = post - $panell__pos + 'px'
		}
		else {
			$panel[0].style.top = 0
		}
	});

	$('.pp-link', $panel).click(function () {
		var $t = $(this);
		var $t__href = $t.attr('href');
		$('.pp-window').hide();

		$($t__href).show();
		if ($t__href !== '#pp-sendmail') {
			$($t__href).css('marginTop', $panel.offset().top + 'px')
		}
		;

		return false;
	});
}

function catfluid() {
	var $cat = $('.b-catitems');

	var prodInRow, blockWidth, catmax;
	$(window).bind('resize', function () {
		blockWidth = $cat.parent().width();
		if ($cat.parent().hasClass('catitems-fld')) blockWidth -= 15;
		prodInRow = Math.floor(blockWidth / 170);
		diff = prodInRow * 170;
		//$cat_sps = Math.floor((blockWidth - diff) / (prodInRow-1));
		$cat_sps = Math.floor((blockWidth - diff) / (prodInRow - 1));

		if ($cat.length > 1) {
			catmax = 0;
			$cat.each(function () {
				if ($('li', this).length > catmax) {
					catmax = $('li', this).length;
				}
			});
			if (catmax <= prodInRow) {
				$cat_sps = Math.floor((blockWidth - (catmax * 170)) / (catmax - 1));
			}
		}
		else if ($('div.i', $cat).length <= prodInRow) {
			$cat_sps = Math.floor((blockWidth - diff) / ($('li', $cat).length - 1));
		}
		//$('li',$cat).css('marginLeft',$cat_sps+'px');
		//$cat.css({marginLeft: '-'+$cat_sps+'px', marginRight: '-1px'});
		//$('li',$cat).css({marginRight: $cat_sps+'px'});
		//$cat.css({marginLeft: '0px', marginRight: '-'+($cat_sps+1)+'px'});

		$cat.each(function () {
			$('li', this).css({marginLeft:$cat_sps + 'px', marginRight:'0'});
			for (i = 0; i < $('li', this).length; i += prodInRow) {
				$($('li', this).get(i)).css({marginLeft:'0'});
			}
			$(this).css({marginLeft:'0', marginRight:'0'});
		});
	});
	$(window).resize();

}
