// $Id: changes.js 7863 2009-08-19 12:33:25Z alexions $

$(document).ready(function() {
    $('.cm-product-block-link').click(function () {
        $('.cm-product-blocks').hide();
        $('#product_block_' + $(this).attr('rel')).fadeIn();
        
        if ($('#product_block_' + $(this).attr('rel')).length) {
			if ($('#top-tools-container').hasClass('cm-buttons-floating')) {
				jQuery.scrollToElm($('#product_block_' + $(this).attr('rel')), 50);	
			} else {
				jQuery.scrollToElm($('#product_block_' + $(this).attr('rel')), 95);								
			}
		}
		
		$('.cm-product-block-link').removeClass('active');
	    $(this).addClass('active');

        if ($('#product_block_' + $(this).attr('rel')).length) {
			document.title = $('#product_block_' + $(this).attr('rel')).attr('rel');
		}
		
		window.scrollBy(0, 1);
		window.scrollBy(0, -1);
		
	    return false;
    });    
});

function fn_change_filter_features(s, subcategory, filter, name){

	var rel = $(s).attr('rel');
	var old_features_hash = $('#features_hash_'+subcategory).attr('value');

	if (rel == 'delete_features_hash') {
		var new_features_hash = old_features_hash.replace($(s).attr('id'), "");

		$(s).attr('href', $(s).attr('href')+'.'+new_features_hash);
		document.getElementById('a_'+$(s).attr('id')).style.display = "block";

		$('#a_'+$(s).attr('id')).attr('href', index_script+'?dispatch=categories.view&category_id='+subcategory+'&features_c=Y&features_hash='+$(s).attr('id'));
		$(s).parent().remove();
		
	} else {
		if (old_features_hash) {
			$(s).attr('href', $(s).attr('href')+'.'+old_features_hash);
		}
		s.style.display = "none";
		$('<span>'+name+'\n\
			<a href="'+index_script+'?dispatch=categories.view&category_id='+subcategory+'&show_product=Y&features_c=Y&features_hash="\n\
			class="cm-ajax" onclick="fn_change_filter_features(this, \''+subcategory+'\', \''+filter+'\', \''+name+'\')" id="'+rel+'"\n\
			 rel="delete_features_hash" rev="pagination_contents_'+subcategory+'">&nbsp;&nbsp;&nbsp;X</a></span>').appendTo($(s).parent());
	}
	
	document.getElementById('select_wrap_'+subcategory+'_'+filter).style.display = "none";
	
//	jQuery.ajaxRequest($(s).attr('href'), {
//		result_ids: 'pagination_contents_'+subcategory
//	}); 
};

function fn_toggle_mainbox_body(category_id)
{
	$('#mainbox_body_' + category_id).toggle();
	window.scrollBy(0, 1);
	window.scrollBy(0, -1);
};

function fn_change_filter_features_js(s, subcategory, filter, name){
	alert('a');

	var feature_product = $('.product_feat', $('#pagination_contents_' + subcategory));
	feature_product.parent().hide();
	var j = 0;
	var arraylength = feature_product.length;
	fn_print_r(arraylength);
	for(var key in feature_product) {
/*		if ($(feature_product[key ]).attr('value')) {
			var pos = $(feature_product[key ]).attr('value').indexOf('.' + $(s).attr('id') + '.');
//		};
		if (-1 < $(feature_product[key ]).attr('value').indexOf('.' + $(s).attr('id') + '.')) {
			$(feature_product[key ]).parent().show();
			alert('da');
		} else {
			alert('net');
		};
		};*/
//		if ($(feature_product[key ]).attr('value')) {
//			fn_print_r($(feature_product[key ]).attr('value'));
//		}
		j = j+1;
	};
	alert(j);
	
	for (var i = 0; i <= arraylength; i++) {
		if ($(feature_product[i]).attr('value') && -1 < $(feature_product[i]).attr('value').indexOf('.' + $(s).attr('id') + '.')) {
//			$(feature_product[key ]).parent().show();
fn_print_r($(feature_product[i]).attr('id'));
		} 
	}
	
	
	
//	fn_print_r($(s).attr('id'));
	
//	for(var key in data) alert(data[key ]); 
	
	alert('b');

	
};

var loaded_blocks = {};

function fn_load_category_products_block(category_id)
{
	if( !$('#product_block_' + category_id).is(':visible') ) {
		return;
	}
	var oElm = document.getElementById ('mainbox_title_' + category_id);
	var a = findXY(oElm);
	var b = findScroll();
	var c = findSize();
	if ((a.y + a.h) < b.top || (a.x + a.w) < b.left || a.y > (b.top + c.height) || a.x > (b.left + c.width)) {
		// Passed
	} else {
		// Visible
		if (loaded_blocks[category_id] != 'Y') {
			loaded_blocks[category_id] = 'Y';
			jQuery.ajaxRequest(index_script + '?dispatch=categories.view&category_id=' + category_id + '&show_products=Y',
				{result_ids:'mainbox_body_' + category_id});
		}
	}
}

function fn_load_product_category_products_block(product_id, category_id)
{
	if( !$('#product_block_' + category_id).is(':visible') ) {
		return;
	}
	var oElm = document.getElementById('mainbox_title_' + category_id);
	var a = findXY(oElm);
	var b = findScroll();
	var c = findSize();
	if ((a.y + a.h) < b.top || (a.x + a.w) < b.left || a.y > (b.top + c.height) || a.x > (b.left + c.width)) {
		// Passed
	} else {
		// Visible
		if (loaded_blocks[category_id] != 'Y') {
			loaded_blocks[category_id] = 'Y';
			jQuery.ajaxRequest(index_script + '?dispatch=products.view&product_id=' + product_id + '&category_id=' + category_id + '&show_products=Y',
				{result_ids:'mainbox_body_' + category_id});
		}
	}
}

function findScroll() 
{
	return { top: self.pageYOffset || (document.documentElement && document.documentElement.scrollTop) || (document.body && document.body.scrollTop),
			 left: self.pageXOffset || (document.documentElement && document.documentElement.scrollLeft) || (document.body && document.body.scrollLeft) };
}

function findSize()
{
	return {width: window.innerWidth?window.innerWidth:((document.all)?document.body.offsetWidth:null),
			height: window.innerHeight?window.innerHeight:((document.all)?document.body.offsetHeight:null)};
}

function findXY(oElm)
{
	var x = 0; var y = 0; var w = oElm.offsetWidth; var h = oElm.offsetHeight;
	while (oElm) {
			x += oElm.offsetLeft;
			y += oElm.offsetTop;
			oElm = oElm.offsetParent;
	}
	return {x:x, y:y, w:w, h:h };
}

