pngFix_msie = false;

function gupl(href , name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( href );
  if( results == null )
    return "";
  else
    return results[1];
}

$(document).ready(function(){

	/* init */
	
    $(document).ready(function(){
          $('A[rel="open-in-new-window"]').each(function(){
               $(this).attr('target', '_blank');
          });
     });
    
	if(cmsBrowser.ie6){
		
		$(document).pngFix( { blankgif:'/images/free.gif' } );
		
	}
	
	if($('.contact_link').length > 0)
		$('.contact_link').nyroModal();


	$('#cat_menu .depth0 a, #cat_menu .depth1 a').each(function(){ /* disable links without children in cat_menu */
		if($('#cat_menu div table.'+$(this).attr('id')).length == 0)
			$(this).css('text-decoration','none');
			$(this).bind('click', function(){ return false; });
	});
	
	/* end of init */


	$('.descr_link').hover(function(){
		$(this).find('.prod_descript').show();
	},
	function(){
		$(this).find('.prod_descript').hide();
	});

	$('.photo_link').hover(function(event){
		
		if($(this).attr('title') != ''){
			$('#title').html('<img src="'+$(this).attr('title')+'" alt="">');
			
			$('#title').css({'top': event.clientY+$(window).scrollTop()+10,	'left': event.clientX+$(window).scrollLeft()+10}).show();
			$(this).attr('title', '');
		}
		
	},
	function(){

		if($(this).attr('title') == ''){
			
			$(this).attr('title', $('#title').children('img').attr('src'));
			$('#title').hide();
			
		}
		
	});


	$('.bn_link a').hover(
				function(){
					$(this).children('img').attr('src', '/images/banner_link_act.png');
					if(cmsBrowser.ie6){
						$(this).children('span').hide();
						$(this).children('img').show();
						$(this).pngFix( { blankgif:'/images/free.gif' } );
					}
		
				},
				function(){
					
					$(this).children('img').attr('src', '/images/banner_link.png');
					if(cmsBrowser.ie6){
 						$(this).children('span').hide();
 						$(this).children('img').show();
						$(this).pngFix( { blankgif:'/images/free.gif' } );

					}

				}
	);

	$('#param_search input:image').hover(
				function(){
					$(this).attr('src', '/images/banner_link_act.png');
					if (cmsBrowser.ie6) {
		  			$('#param_search').pngFix({blankgif: '/images/free.gif'});
		  		}
				},
				function(){
					$(this).attr('src', '/images/banner_link.png');
					if (cmsBrowser.ie6) {
		  			$('#param_search').pngFix({blankgif: '/images/free.gif'});
		  		}
				}
	);

	

	$('.login_form .image').hover(
				function(){
					$(this).attr('src', '/images/login_bt_act.gif');
				},
				function(){
					$(this).attr('src', '/images/login_bt.gif');
					
				}
	);

	$('.search_submit .image').hover(
				function(){
					$(this).attr('src', '/images/search_bt_act.gif');
				},
				function(){
					$(this).attr('src', '/images/search_bt.gif');
					
				}
	);



/* catalog */



	$('#cat_menu .brd table').hover(
    function(){

      $(this).addClass('brdg');
      if($(this).hasClass('depth0'))
        $('#cat_menu .brd thead tr').addClass('brdg');

      if($(this).parent('div').nextAll('div:visible').length == 0)
        $('#cat_menu .brd tfoot tr').addClass('brdg');




    },
    function(){

      $(this).removeClass('brdg');
      if($(this).hasClass('depth0'))
        $('#cat_menu .brd thead tr').removeClass('brdg');

      if($(this).parent('div').nextAll('div:visible').length == 0)
        $('#cat_menu .brd tfoot tr').removeClass('brdg');

    }
	);

function hidechildren(tis){
	
	if (tis.closest('table').hasClass('depth0')) {
		$('.children.depth1, .children.depth2, .children.depth3').parent('div').hide();
		$('#cat_menu a').removeClass('act');

		
	} else {
		if(tis.closest('table').hasClass('depth1')){
			$('.children.depth2, .children.depth3').parent('div').hide();
			$('#cat_menu .children.depth2 a, #cat_menu .children.depth3 a').removeClass('act');
		}
	}
	
	$('#cat_menu .brd table, #cat_menu .brd thead tr, #cat_menu .brd tfoot tr').removeClass('brdg');
	
}

	$('#cat_menu a.inner').click(function(){
		var tis = $(this); 
		
		if(!tis.hasClass('act')){
			
			$('.price_links, #index_news').hide();
			
			tis.closest('table').contents().find('a.inner').removeClass('act');
			
			hidechildren(tis);
         
			var div = $('.children.'+tis.attr('id')).parent('div');
            div.slideDown('normal');
            var offset = parseInt(div.offset().top, 10) / 2;
            $("html,body").animate({scrollTop: offset}, 900	);
            tis.addClass('act');
			
			if($('#datatable_wrapper').length > 0){ // скрываем список товаров, если кликнули по другому пункту меню
				$('#datatable_wrapper, a.buy_bt, #parametric').hide();
				if($('#cat_menu .brd tfoot').length == 0)
					$('#cat_menu .brd').append('<tfoot><tr class=""><td class="bl"><img width="10" height="10" alt="" src="/images/free.gif"/></td><td class="bb"/><td class="br"><img width="10" height="10" alt="" src="/images/free.gif"/></td></tr></tfoot>');
				
			}

		} else {
			
			tis.removeClass('act');
			
			hidechildren(tis);
			
		}
		
		if($('#cat_menu a.act').length == 0)
			$('.price_links, #index_news').show();
		
		return false;
	});

/* end of catalog */


	$('#login_lnk').click(function(){
		
		if($(this).hasClass('act')){
			
			$('.hidden_login').hide();
			
			$('.login_form form').css({'position':'static', 'height':'100%', 'width':'100%'});
			
			$(this).removeClass('act');
			
		} else {
		
			var width = $('.login_form form').width(); 

			$('.login_form form').css({'position':'absolute', 'height':'210px', 'width':width});
			
			$('.hidden_login').show();
			
			$(this).addClass('act');
			
		}
		
		return false;
		
	});

	/* удаление товаров из корзины */
	$('a.cart_del').live('click', function(){
	    $(this).closest('tr').find('input:text').val('0');
	    $('#form_recount_1').trigger('submit');
	    return false;
	});


	// сортировка в каталоге, при параметрическом поиске
	$('#datatable th a').click(function(){
	    
	    if($('#param_search').length>0){
		$('#param_search input[name=sort]').val(gupl($(this).attr('href'), 'sort'));
		$('#param_search input[name=sorttype]').val(gupl($(this).attr('href'), 'sorttype'));
		$('#param_search').submit();
	
		return false;
	    }
	    return true;
	});


	$('.datatable tbody tr').hover(function(){
			
			if($(this).next('tr').length == 0){
				$('.bottom-border td').addClass('act');
				$('.bottom-border td.bl').addClass('bl_act');
				$('.bottom-border td.br').addClass('br_act');
			}
			$(this).addClass('act');
	},
	function(){
				$('.bottom-border td').removeClass('act');
				$('.bottom-border td.bl').removeClass('bl_act');
				$('.bottom-border td.br').removeClass('br_act');
		$(this).removeClass('act');
	});
	

	$('.datatable input.image, .addtocart input.image').hover(function(event){
		$(this).css('background', 'url(/images/add_to_cart_hover.gif) top left no-repeat');


		$('#title').html('Добавить в заказ');

		$('#title').css({'top': event.clientY+$(window).scrollTop()+10,	'left': event.clientX+$(window).scrollLeft()-100, padding:'5px'}).show();

	},function(){
		$(this).css('background', 'url(/images/add_to_cart.gif) top left no-repeat');

		$('#title').hide();
	});
        
        if($('.dataTables_paginate').length){
            try{
                $('.dataTables_paginate').find('a').each(function(){
                    $(this).attr('href', $(this).attr('href') + '#datatable');
                });
            }catch(e){console.log(e)}
        }

});

