$(document).ready(function() {

	var myAnimation = false;

/*
    $('.searchinput input:text').focus(function() {
        $('.searchform').removeClass('transparency');
    });
	
    $('.searchinput input:text').blur(function() {
        $('.searchform').addClass('transparency');
    });	
*/	
	$('.popupauth input').keyup(function(event) {
		if (event.keyCode == '13') {
			$('#top_login_from').get(0).submit();
		}		
	});

    $('.auth #enterdown').click(function() {
        $('.popupauth').fadeIn('fast');
    });
	
    $('.auth #enterup').click(function() {
        $('.popupauth').fadeOut('fast');
    });
	
    $('.auth #enterdown2').click(function() {
        $('.popupauth').fadeIn('fast');
    });
	
    $('.auth #enterup2').click(function() {
        $('.popupauth').fadeOut('fast');
    });
	
    $('#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;

    });
	
	
    $('.searchfilter').click(function() {
		if ($('.popupsearchfilter').hasClass('hidden')) {
			$('.popupsearchfilter').removeClass('hidden');
		} else {
			$('.popupsearchfilter').addClass('hidden');
		}
    });
	
    $('.popupsearchfilter a').click(function() {
		$('.searchform #searchfilter').html($(this).html());
		var curval = $(this).html();
		var tval='';
		if (curval == 'Искать везде') tval = 'web';
		if (curval == 'Библиотека') tval = 'lib';
		if (curval == 'Ризница') tval = 'riznica';
		
		$('input[name=searchfilter]').attr('value', tval);
	});

    $('a.iconview').click(function() {
		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));
		
		if ($.browser.msie) {
			$('select').hide();
		}

		$this = $(this);
		var container = $this.parents('div.padding:first');
		var name = container.find('div.header').html();
		var author = container.find('div.izdat').html();
		var image = container.find('img.image_full').attr('src');

		$('div.popupdescr').find('div.header').html(name);
		$('div.popupdescr').find('div.producer').html(author);
		$('div.popupdescr').find('div.cover img').attr('src', image);

		//alert($(this).attr('id').substr(8));
		
		$('.popupdescr-bg').show();
		$('.popupdescr').show();
	});
	
    $('.popupdescr .popupclose').click(function() {
		$('.popupdescr').hide();
		$('.popupdescr-bg').hide();
		
		$('div.popupdescr').find('div.cover').html('<img src="img/tmp/big_cover.jpg" alt="" />');

		if ($.browser.msie) {
			$('select').show();
		}
	});

    $('.product-coverthumbs ul li a, .product-coverpic a').click(function() {
		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));
		
		if ($.browser.msie) {
			$('select').hide();
		}

		$this = $(this);
		/*var container = $this.children('img').attr('src');*/
		var container = $this.parent().children('div.hidden').html();

		$('div.popupdescr').find('div.cover img').attr('src', container);

		//alert($(this).attr('id').substr(8));
		
		$('.popupdescr-bg').show();
		$('.popupdescr').show();
		
		return false;
	});

    $('.icondeliveryblock a').click(function() {
		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));
		
		if ($.browser.msie) {
			$('select').hide();
		}

		$this = $(this);
		/*var container = $this.children('img').attr('src');*/
		var container = $this.parent().children('div.hidden').html();

		$('div.popupdescr').find('div.cover').html(container);

		//alert($(this).attr('id').substr(8));
		
		$('.popupdescr-bg').show();
		$('.popupdescr').show();
		
		return false;
	});
	
	$('#product-ic-left 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 img').attr('src'));
					}
				});
			} );
		}
		return false;
	});
	
	$('#product-ic-right 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 img').attr('src'));
					}
				});
			} );
		}
		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;

	}

