$(document).ready(function() {
	
	var myAnimation = false;
	
	$('#phloginbox input').keyup(function(event) {
		if (event.keyCode == '13') {
			if ($('#phloginbox input[name=auth_name]').val() != '' && $('#phloginbox input[name=auth_pass]').val() != '') {
				$('#top_login_from').get(0).submit();
			}
		}		
	});

    $('#regionSelect').click(function() {
        $('.popregion').toggle('fast');
        return false;
    });

    $('.region .darr').click(function() {
        $('.popregion').toggle('fast');
        return false;
    });


    $('#regionOptions').change(function(e){
    	window.location.href = window.location.href = '/riznica_requests/?setRegion=' + this.value;

    });
	
	$('.phsearch input[name=q]').focus(function() {
		if ($(this).val() == 'Введите название товара') {
			$(this).val('');
		}
	});
	$('.phsearch input[name=q]').blur(function() {
		if (jQuery.trim($(this).val()) == '') {
			$(this).val('Введите название товара');
		}
	});

	$('.clmenu ul li a').hover(function() {
		$(this).parent().parent().addClass('hover');
	}, function() {
		$(this).parent().parent().removeClass('hover');
	});
	
	$('.footer-consult a, .fcreduce a').click(function() {
		if ($('.fconsult').hasClass('hidden-total')) {
			$('.fconsult').removeClass('hidden-total').animate( { height: 216, top: -216} );
		} else {
			$('.fconsult').animate( { height: 0, top: 0}, function() {
				$(this).addClass('hidden-total');
			});
		}
		return false;
	});
	
	$('.pmbutton a').click(function() {
		$('.pmbutton').fadeOut(function() {
			$('.pagemenu ul').slideToggle(function() {
				$('.pmbutton2').slideToggle();
			});
		});
		return false;
	});
	
	$('.pmbutton2 a').click(function() {
		$('.pmbutton2').slideToggle(function() {
			$('.pagemenu ul').slideToggle(function() {
				$('.pmbutton').fadeIn();
			});
		});
		return false;
	});

	$('a#phusermenu').click(function() {
		$('.phsubmenu').slideToggle();
		return false;
	});	

	$('a#phuserlogin').click(function() {
		$('#phloginbox').slideToggle();
		return false;
	});	

	$('.catalogblock a').hover(function() {
		$(this).parent().children('span').addClass('active');
	}, function() {
		$(this).parent().children('span').removeClass('active');
	});
	
	$('.lastproducts ul li').hover(function() {
		$(this).addClass('active');
	}, function() {
		$(this).removeClass('active');
	});
	
	$('.deals table tr').hover(function() {
		$(this).addClass('active');
		$(this).prev().addClass('prevactive');
	}, function() {
		$(this).removeClass('active');
		$(this).prev().removeClass('prevactive');
	});
	
	$('#sdc-open-close, #sdc-close').click(function() {
		$('.sendmessagebox').slideToggle();
		return false;
	});
	
	$('.chooseparamslink').click(function() {
		$('#chooseparamsbox').slideToggle();
		return false;
	});
	
	if ($('#product-ic-thumbs').length) {
		if ($('#product-ic-thumbs ul li:first').position().left >= 60) {
			$('#product-ic-left a').addClass('disabled');
		}
		if ($('#product-ic-thumbs ul li:last').position().left <= 60) {
			$('#product-ic-right a').addClass('disabled');
		}
	}
	
	$('#product-ic-right a').click(function() {
		if ($('#product-ic-thumbs ul li:last').position().left > 60 && !myAnimation) {
			myAnimation = true;
			$('#product-ic-thumbs ul li').animate( { left: '-=60'}, 'fast', function() {
				myAnimation = false;
				$('#product-ic-thumbs ul li').each(function() {
					if ($(this).position().left == 60) {
						$('#productpreviewpic').attr('src', $(this).find('div.image_sm img').attr('src'));
						$('#productpreviewpic').parent().children('img.image_full').attr('src', $(this).find('div.image_big img').attr('src'));
					}
				});
				if ($('#product-ic-thumbs ul li:last').position().left <= 60) {
					$('#product-ic-right a').addClass('disabled');
				}
				if ($('#product-ic-thumbs ul li:first').position().left < 60) {
					$('#product-ic-left a').removeClass('disabled');
				}
			} );
		}
		return false;
	});
	
	$('#product-ic-left a').click(function() {
		if ($('#product-ic-thumbs ul li:first').position().left < 60 && !myAnimation) {
			myAnimation = true;
			$('#product-ic-thumbs ul li').animate( { left: '+=60'}, 'fast', function() {
				myAnimation = false;
				$('#product-ic-thumbs ul li').each(function() {
					if ($(this).position().left == 60) {
						$('#productpreviewpic').attr('src', $(this).find('div.image_sm img').attr('src'));
						$('#productpreviewpic').parent().children('img.image_full').attr('src', $(this).find('div.image_big img').attr('src'));
					}
				});
				if ($('#product-ic-thumbs ul li:last').position().left > 60) {
					$('#product-ic-right a').removeClass('disabled');
				}
				if ($('#product-ic-thumbs ul li:first').position().left >= 60) {
					$('#product-ic-left a').addClass('disabled');
				}
			} );
		}
		return false;
	});
	
	$('#product-shops ul li, #contactslist ul li').hover(function() {
		$(this).addClass('active');
	}, function() {
		$(this).removeClass('active');
	});
	
	$('.cllist ul li').hover(function() {
		$(this).addClass('active');
	}, function() {
		$(this).removeClass('active');
	});
	
	/*
		product page icons
	*/
	$('#productpreviewpic-block img').hover(function() {
		$(this).parent().children('.popupicons').show();
	});
	
	$('#productpreviewpic-block').mouseleave(function() {
		$('.popupicons').hide();
	});

	
	/*
		last product icons
	*/

	$('.lastproducts .productlink').hover(function() {
		$('.popupicons').hide();
		$(this).parent().children('.popupicons').show();
	});
	
	$('.lastproducts .popupicons').mouseleave(function() {
		$('.popupicons').hide();
	});
	
	$('.popupicons .iconview').click(function() {

		imagefull = $(this).parent().parent().children('img.image_full');
	
		body_height = $(document).height() || document.documentElement.scrollHeight;
		$('.popupdescr-bg').css("height", body_height);
		
		inner_height = window.innerHeight || $(window).height();
		$('.popupdescr').css('top', ($(document).scrollTop() + inner_height/2 - 240));
		$('.popupdescr').css('width', imagefull.width());
		$('.popupdescr').css('margin-left', -1*imagefull.width()/2);
			
		$('.popupdescr').prepend('<div style="margin: 0 auto;"><img src="' + imagefull.attr('src') + '" alt="" /></div>');
		
		$('.popupdescr-bg').show();
		$('.popupdescr').show();
	});
	
    $('.popupdescr .popupclose').click(function() {
		$('.popupdescr').hide();
		$('.popupdescr-bg').hide();
		
		$('div.popupdescr img').remove();

	});	
	
	$('#popup-cart a.linkopenclose').click(function() {
		popupcart = $('#popup-cart');
		if (popupcart.hasClass('opened')) {
			$('#popup-cart-block').slideToggle('fast', function() {
				popupcart.animate( {right: '-=300'}, 'fast', function() {
					popupcart.removeClass('opened');
				});
			});
		} else {
			popupcart.animate( {right: '+=300'}, 'fast', function() {
				$('#popup-cart-block').slideToggle('fast');
				popupcart.addClass('opened');
			});
		}
		return false;
	});
		
});

	//filter functions
	function cmsApplyUrl(name, value){
	        reg_name = name.replace(/(\[|\])/g, '\\\$1');

		var re = new RegExp(reg_name + "=[^&]*&?", 'g');
		var link = window.location.href.replace(re, '');
		var separator = link[link.length-1] == '?' || link[link.length-1] == '&' ? '' : '&';
		
		if ( link.indexOf('?') == -1 ){
			if (link[link.length-1] != '/') {
				separator = '/?';
			}
			else {
				separator = '?';
			}
			
		}

		//empty filter
		if ( value == '' ){ 
			value = 'null';
		}
		
		window.location.href = link + separator + name + '=' + value;

	}	

