function lightboxActivate(){
	$('a.lightbox').lightBox({
		displayRel: true,
		fixedNavigation: true,
		minImageWidth: 300,
		overlayBgColor: '#444',		
		overlayOpacity:	0.8,
		txtImage: "Фото",
		txtOf: "из"
	});
}
function toCartSubmit(toggle){
	if(toggle == 'bind'){
		$('#toCartSubmit').unbind().bind('click', function(){
			$('#cartNotification').slideDown('medium').animate({'opacity':'1.0'}, 4000, function(){
				$(this).slideUp('medium');
			});
			return false;
		});
	}
	else if(toggle == 'unbind'){
		$('#toCartSubmit').unbind();
	}
}
//check if only one unchecked section left
function selectionCheck(){
	var sectionTables = $('.sectionT');
	var unselectedSections = new Array();
	for( var i=0; i < sectionTables.length; i++){
		if($('#'+$(sectionTables[i]).attr('id')+' input:checkbox:checked').length == 0)
			unselectedSections.push($(sectionTables[i]).attr('data-section'));
	}
	return unselectedSections;
}
function applyEvents(){
	$("#tableCombinations").mousemove(function(e){
		$('#loaderImg').css({'position':'absolute', 'top':(e.pageY-2),'left':(e.pageX+15)});
	});
	$('#tableCombinations input[type=checkbox]').unbind().bind('click', function(){
		var clickedID = $(this).attr('id');
		var clickedSection = $(this).attr('data-section');
		
		//ucheck all position for a section except clicked
		$('input[data-section="'+$(this).attr('data-section')+'"]:not(input[data-position="'+$(this).attr('data-position')+'"])').attr('checked', false);
		
		var unselectedSections = selectionCheck();
		options.data = jQuery.extend(options.data, {
			clicked: clickedID, 
			uselectedSec: unselectedSections.length
		});
		
		if(unselectedSections.length == 1){
			options.data = jQuery.extend(options.data, {
				lastSection: unselectedSections[0],
				operation: 'getCombData'
			});
		}
		else if(unselectedSections.length == 0){
			options.data = jQuery.extend(options.data, {
				lastSection: clickedSection,
				operation: 'completeSelection'
			});
		}
		$('#purchase').ajaxSubmit(options);
	});
}
// pre-submit callback 
function preCallback(formData, jqForm, options) {
	$('#loaderImg').show();
} 
// post-submit callback 
function postCallback(responseText, statusText)  {
	var jsonObj = jQuery.parseJSON(responseText);
	if(jsonObj!='' && jsonObj!='undefined'){
		$('#tcHOLDER').html(jsonObj.contentHTML);
		$('#product_price').html(jsonObj.finalPrice);
		$('#product_presence').html(jsonObj.finalPresence);
		if(jsonObj.last == 'true')
			toCartSubmit('unbind');
		else
			toCartSubmit('bind');
	}
	applyEvents();	
	$('#loaderImg').hide();
	lightboxActivate();
}

if($('#tcHOLDER').length != 0){
	var preloadImg = new Image();
	preloadImg.src = '../images/classic/lightbox/loadingCirc.gif';
	var options = { 
		data:		   {a_action: 'ajaxFetchWares', includeFile : "resource"},
		//target:        '#tcHOLDER',   // target element(s) to be updated with server response 
		url:		   window.location+'/s/',
		beforeSubmit:  preCallback,  // pre-submit callback 
		success:       postCallback  // post-submit callback 
	}; 
}


