 $(document).ready(function(){

$('.lowBar').hide();

	//On Click Event ARROW
	$('.arrow').click(function() {
		
		var activeBar = $(this).parents(".bararrow");
		
		if($(this).hasClass('active') === false){
			$(this).addClass('active');
			$(activeBar).children(".lowBar").fadeIn();
			}else{
				$(this).removeClass('active');
				$(activeBar).children(".lowBar").hide();
				};
		});		

		
function parseW(aB) {
    var wnum = $(aB).text();
	var num = parseInt(wnum);
	return num;
	};
	
function parseWW(aB) {
    var wnum = $(aB).val();
	var num = parseInt(wnum);
	return num;
	};
	
function WW(aB) {
	var xx = $(aB).parents('.bararrow').find('.confirmation').attr('name');
	if(xx!='conf_off'){
		$(aB).parents('.bararrow').find('.barUpdate').addClass('barUpdate_on');
		$(aB).parents('.bararrow').find('.barUpdate').attr('name', 'update_on');
		}
	};
	
function WWstartweek(aB) {
	var startweekday = $(aB).parents('.bararrow').find('.startweek').val();
	var olSWD = $(aB).parents('.bararrow').find('.bullets');
	var arr1 = [ 'on', 'on', 'on', 'on', 'on', 'on'];
	var arr2 = [ 'on', 'off', 'on', 'off', 'on', 'off'];
	var arr3 = [ 'on', 'off', 'off', 'on', 'off', 'off' ];
	var arr4 = [ 'on', 'off', 'off', 'off', 'on', 'off'];
	var arr5 = [ 'on', 'off', 'off', 'off', 'off', 'on'];
	var arr6 = [ 'on', 'off', 'off', 'off', 'off', 'off'];
	var pDATA = $(aB).parents('.bararrow').find('.window').text();
	var arr = eval('arr'+pDATA);
	var lichildSWD = startweekday-30;
	var lichildSWDoff = 2;
	var indexON =0;
	var indexBOL =0;
	
	jQuery.each( [1,2,3,4,5,6], function(i, v){
	lichildSWDoff++;
	olSWD.find('li:nth-child('+lichildSWDoff+')').attr('name','off');
	olSWD.find('li:nth-child('+lichildSWDoff+')').removeClass('on');
	olSWD.find('li:nth-child('+lichildSWDoff+')').addClass('off');
	olSWD.find('li:nth-child('+lichildSWDoff+')').attr('index','');
 	});
	jQuery.each( [1,2,3,4,5,6], function(i, v){
	lichildSWD++;
	indexON++;
    olSWD.find('li:nth-child('+lichildSWD+')').attr('name','on');
	olSWD.find('li:nth-child('+lichildSWD+')').attr('index','on'+indexON);
 	});

	if(arr == '1'){ arr = arr1};
	jQuery.each( arr, function(i, v){
	indexBOL++;
	olSWD.find('li[index=on'+indexBOL+']').addClass(v);
 	});

	

	
	};
	
function shutdown(aB) {
  	$(aB).parents('.bararrow').find('.barUpdate').removeClass('barUpdate_on');
	$(aB).parents('.bararrow').find('.barUpdate').attr('name', 'update_off');
	$(aB).parents('.bararrow').find('.window').addClass('window_off');
	$(aB).parents('.bararrow').addClass('barOP').removeClass('bar');
	
	};
	
function turniton(aB) {
  	$(aB).parents('.barOP').find('.barUpdate').addClass('barUpdate_on');
	$(aB).parents('.barOP').find('.barUpdate').attr('name', 'update_on');
	$(aB).parents('.barOP').find('.window_off').removeClass('window_off');
	$(aB).parents('.barOP').addClass('bar').removeClass('barOP');
	};
	
	//periodicity function
	$('.leftarrowbt, .rightarrowbt').click(function() {
	
	var aBper = $(this).parents('.bar').find('.window');
	var confir = $(this);
		
		if(($(this).attr('name')==='leftarrow')&&(parseW(aBper)>=2)){
		$(aBper).text(parseW(aBper)-1);
		WW(confir);
			}else{
		
				if((parseW(aBper)<= 5)&&($(this).attr('name')!='leftarrow')){
				$(aBper).text(parseW(aBper)+1);
				WW(confir);
				};
	
		};
	});
	
	//add quantity function
	$('.pluss, .minuss').click(function() {
	
	var qts = $(this).parents('.bar').find('.addquantity');
	var confir = $(this);
	
		
		if(($(this).attr('name')==='lows')&&(parseWW(qts)>=2)){
		$(qts).val(parseWW(qts)-1);
		WW(confir);
			}else{
				if($(this).attr('name')!='lows'){
				$(qts).val(parseWW(qts)+1);
				WW(confir);
				};	
		};
	});
	
	//startweek function
	$('.plus, .minus').click(function() {
	
	var qts = $(this).parents('.bar').find('.startweek');
	var confir = $(this);
	
		if(($(this).attr('name')==='low')&&(parseWW(qts)>=33)){
		$(qts).val(parseWW(qts)-1);
		WW(confir);
			}else{
				if(($(this).attr('name')!='low')&&(parseWW(qts)<=36)){
				$(qts).val(parseWW(qts)+1);
				WW(confir);
				};
		};
	});
	
	
	//lasttweek function
	$('.pluslw, .minuslw').click(function() {
	
	var qts = $(this).parents('.bar').find('input.lastweek');
	var confir = $(this);
		
		if(($(this).attr('name')==='lowlw')&&(parseWW(qts)>=2)){
		$(qts).val(parseWW(qts)-1);
		WW(confir);
			}else{
		
				if($(this).attr('name')!='lowlw'){
				$(qts).val(parseWW(qts)+1);
				WW(confir);
				};
	
		};
	});
	
			//confirmation and call shutdown temp
	$('.confirmation').click(function() {
	
	var confir = $(this);
	
		if($(this).attr('name')==='conf_off'){
				$(this).removeClass('confirmation_off');
				$(this).attr('name', 'conf_on');
				turniton(confir);
				}else{
					  shutdown(confir);
					  $(this).addClass('confirmation_off');
					  $(this).attr('name', 'conf_off');
					 };
		});
	
	
		//upDate
	$('.barUpdate').click(function() {
	
	var thisbar = $(this);
		if($(this).attr('name')==='update_on'){
				$(this).removeClass('barUpdate_on');
				WWstartweek(thisbar);
				};
	});
	
	//eliminar temp
	$('.eliminar').click(function() {
	
	$(this).parents('.bar').remove();
	$(this).parents('.bararrow').remove();
	
	});
	
			//addProduct to list
	$('.barAdd').click(function() {
			$(this).addClass('barAdd_off');		 
		});
	
	
});	
