$(function() {

	$("#Menu a").each(function(){
        var href = this.getAttribute('href', 2);

        var location = new String( document.location );
		var pos = location.indexOf('?');
		if (!(pos > 0)) {
			pos = location.length;
		}		
		location = location.substr(0, pos);
        location = location.substr(location.length - href.length, href.length );

        if ( location == href) {
            $(this).parent().addClass("ActiveMenu");
        }
    });
	
	$('#flt_submit').hide();
	$('#flt_sbt_div').show();
	$('#flt_sbt_lnk').click(function() {
		$('#ctlg_fltr').submit();
		return false;
	});
	/*
	$('#Content a.Zakaz').click(function() {
		var zid = $(this).attr('id').replace('zak', '');
		$('#uc-product-add-to-cart-form-' + zid).submit();
		return false;
	});
	
	/*$('#Content a.ToBoard').click(function() {
		var zid = $(this).attr('id').replace('zak', '');
		$('#uc-product-add-to-cart-form-' + zid).submit();
		return false;
	});*/
	
	$('a.InactivePhoto').mouseover(function() {
		var fid = $(this).attr('id').replace('lnksrc', '');
		var box_id = $('#MainPhotoLnk #MainPhotoBox'+fid).attr('id');
		
		if (!box_id) {
			$('#MainPhotoBox').hide();
			$('#MainPhotoLnk').html($('#bigsrc' + fid).html());
		} else {
			$('#MainPhotoBox').hide();
			$('#MainPhotoBox' + fid).show();
		}
		return false;
	});
	$('a.InactivePhoto').mouseout(function() {
		$('#MainPhotoLnk img').hide();
		$('#MainPhotoLnk').html($('#MainPhotoSave').html());
		return false;
	});
	
	if ($('#ActMSpan').length > 0) {
			var control_str = $('#ActMSpan').html();
			var about_str = $('#AboutBtm').html().substring(0, control_str.length);
			var help_str = $('#HelpBtm').html().substring(0, control_str.length);
			
			if (about_str == control_str) {
				$('#AboutBtm').html('<span class="ActiveBottomMenu"><span>' + about_str + ':</span></span>');
			}
			if (help_str == control_str) {
				$('#HelpBtm').html('<span class="ActiveBottomMenu"><span>' + help_str + ':</span></span>');
			}
	}
	
	
	
	//$("#sec1 ul:not(#sec1)").fadeOut();
	/*$('#sec1 li a').each(function ()
    {
        $(this).click(function (event)
        {
			$('#sec1 li').removeClass();
			$(this).addClass('active');
			$(this).parent().addClass('ActiveLi');
			$("#sec1 ul:not(#sec1)").slideUp();
            if ($(this).parent().parent().attr('id') == 'sec1') {
				//event.preventDefault(); // Prevents the browser to navigate to the specified url (href)
			} else {
				$(this).next().slideToggle(); // Toggle the targeted ul
			}
        });
    });
	*/
	
	$("#sec1 a").each(function(){
        var href = this.getAttribute('href', 2);

        var location = new String( document.location );
		var pos = location.indexOf('?');
		if (!(pos > 0)) {
			pos = location.length;
		}		
		location = location.substr(0, pos);
		
        location = location.substr(location.length - href.length, href.length );
	//alert(location + " --- " + href);
	
        if ( location == href) {
            $(this).parent().addClass("ActiveLi");
			$(this).parent().parent().slideToggle();
			$(this).parent().parent().parent().addClass("ActiveLi");
        }
    });
	$('#sec1').slideDown();
	$('#sec1 .ActiveLi a.active').next().slideDown();
	
	$("#sec2 a").each(function(){
        var href = this.getAttribute('href', 2);

        var location = new String( document.location );
		var pos = location.indexOf('?');
		if (!(pos > 0)) {
			pos = location.length;
		}		
		location = location.substr(0, pos);
        location = location.substr(location.length - href.length, href.length );

        if ( location == href) {
            $(this).parent().addClass("ActiveLi");
			$(this).parent().parent().slideToggle();
			$(this).parent().parent().parent().addClass("ActiveLi");
        }
    });
	$('#sec2').slideDown();
	$('#sec2 .ActiveLi a.active').next().slideDown();
	
	$("#sec3 a").each(function(){
        var href = this.getAttribute('href', 2);

        var location = new String( document.location );
		var pos = location.indexOf('?');
		if (!(pos > 0)) {
			pos = location.length;
		}		
		location = location.substr(0, pos);
        location = location.substr(location.length - href.length, href.length );

        if ( location == href) {
            $(this).parent().addClass("ActiveLi");
			$(this).parent().parent().slideToggle();
			$(this).parent().parent().parent().addClass("ActiveLi");
        }
    });
	$('#sec3').slideDown();
	$('#sec3 .ActiveLi a.active').next().slideDown();
	
	$("#sec4 a").each(function(){
        var href = this.getAttribute('href', 2);

        var location = new String( document.location );
		var pos = location.indexOf('?');
		if (!(pos > 0)) {
			pos = location.length;
		}		
		location = location.substr(0, pos);
        location = location.substr(location.length - href.length, href.length );

        if ( location == href) {
            $(this).parent().addClass("ActiveLi");
			$(this).parent().parent().slideToggle();
			$(this).parent().parent().parent().addClass("ActiveLi");
        }
    });
	$('#sec4').slideDown();
	$('#sec4 .ActiveLi a.active').next().slideDown();
	
	$('#cart-form-pane a.crtd').click(function() {
		var nid = $(this).attr('id').replace("crtd_", "");
		$('#CartBox').fadeOut();
		
		var link = $(this).attr('href');
		
		$.getJSON(link, function(json){
			var state = json.state;
			if (state = 1) {
				$('#CartBox').html(json.cart);
				$('#cart_tr_' + nid).fadeOut();
				$('#CartBox').fadeIn();
				$('#total_qty').html(json.total_qty);
				$('#total_cost').html(json.total_cost);
			} else {
				alert('Невозможно убрать товар из корзины!');
			}
			return false;
		});
		
		return false;
	});
	
	// Adding item to cart
	$('#Content a.ToBoard').click(function() {
		var nid = $(this).attr('id').replace("zak", "");
		var link = $(this).attr('href');
		$.getJSON(link, function(json){
			var state = json.state;
			if (state > 0) {
				$('#CartBox').html(json.cart);
				$('#CartBox').fadeIn();
			} else {
				alert('Невозможно добавить товар в корзину!');
			}
			return false;
		});
		$('#crt_msg_' + nid).fadeIn('slow');
		$('#crt_msg_' + nid).fadeOut('slow');
		return false;
	});
	$('#Content a.ToBoardEn').click(function() {
		var nid = $(this).attr('id').replace("zak", "");
		var link = $(this).attr('href');
		$.getJSON(link, function(json){
			var state = json.state;
			if (state > 0) {
				$('#CartBox').html(json.cart);
				$('#CartBox').fadeIn();
			} else {
				alert('Невозможно добавить товар в корзину!');
			}
			return false;
		});
		$('#crt_msg_' + nid).fadeIn('slow');
		$('#crt_msg_' + nid).fadeOut('slow');
		return false;
	});
	$('#Content a.ZakazEn').click(function() {
		var nid = $(this).attr('id').replace("zak", "");
		var link = $(this).attr('href') + '/' + $('#qnty').val();
		$.getJSON(link, function(json){
			var state = json.state;
			if (state > 0) {
				$('#CartBox').html(json.cart);
				$('#CartBox').fadeIn();
			} else {
				alert('Невозможно добавить товар в корзину!');
			}
			return false;
		});
		$('#crt_msg_' + nid).fadeIn('slow');
		$('#crt_msg_' + nid).fadeOut('slow');
		return false;
	});
	$('#Content a.Zakaz').click(function() {
		var nid = $(this).attr('id').replace("zak", "");
		var link = $(this).attr('href') + '/' + $('#qnty').val();
		$.getJSON(link, function(json){
			var state = json.state;
			if (state > 0) {
				$('#CartBox').html(json.cart);
				$('#CartBox').fadeIn();
			} else {
				alert('Невозможно добавить товар в корзину!');
			}
			return false;
		});
		$('#crt_msg_' + nid).fadeIn('slow');
		$('#crt_msg_' + nid).fadeOut('slow');
		return false;
	});
	$('#Content a.crt_clr').click(function() {
		var nid = $('#Content a.Zakaz').attr('id').replace("zak", "");
		var link = $(this).attr('href') + '/' + $('#qnty').val();

		$.getJSON(link, function(json){
			var state = json.state;
			if (state > 0) {
				$('#CartBox').html(json.cart);
				$('#CartBox').fadeIn();
			} else {
				alert('Невозможно добавить товар в корзину!');
			}
			return false;
		});
		$('#crt_msg_' + nid).fadeIn('slow');
		$('#crt_msg_' + nid).fadeOut('slow');
		return false;
	});
	
	
	// Color link animation
	$('#Content a.crt_clr').mouseover(function() {
		var nid = $(this).attr('id').replace("crt_clr_", "");
		$('#crt_img_' + nid).fadeIn('fast');
		return false;
	});
	$('#Content a.crt_clr').mouseout(function() {
		var nid = $(this).attr('id').replace("crt_clr_", "");
		$('#crt_img_' + nid).fadeOut('fast');
		return false;
	});
	
	$('#user-register legend').each(function() {
		if ($(this).html() == 'ubercart') {
			$(this).hide();
		}
	});
	
	$('#search-block-form #edit-submit-1').hide();
	$('#block-search-0 h3').hide();
	$('#play_lnk').mouseover(function() {
		$('#play_img').animate({'left': '+=250px', 'top': '+=250px'}, 'slow');
		return false;
	});
	$('#play_lnk').mouseout(function() {
		$('#play_img').animate({'left': '-=250px', 'top': '-=250px'}, 'slow');
		return false;
	});
	
	$('#cart-form-pane a.edit_lnk').click(function() {
		$('#cart-form-pane .EditZakaz a').show();
		$('#cart-form-pane div.ed_div').hide();
		
		var nid = $(this).attr('href').replace('ed_lnk_', '');
		$('#ed_qnty_frm_' + nid).show();
		
		$(this).hide();
		return false;
	});
	
	$('#cart-form-pane a.edt_qty_btn').click(function() {
		var nid = $(this).attr('id').replace('sbm_qty_e_', '');
		var cntr = $(this).attr('cntr');
		var qnty = $('#inp_qnty_' + nid + '_' + cntr).val();
		var link = $(this).attr('href') + '/' + qnty;
		$(this).attr('href', link);
	});

	$('#edit-pass').keypress(function(e){
		if(e.which == 13){
		$('#user-login-form').submit();
		}
	});
	
});

