
var lastParallel = null;
var curvyCornersVerbose = false; 

/* Stop Parallel effects running */
function stopParallelEffects(Paralleleffect) {
  if(Paralleleffect != null) {
    Paralleleffect.cancel();
  }
}

function movePhoto(direction) {
  var w = $('photo_container').down().getWidth();
  var photo_counter = parseInt($('photo_counter').innerHTML);
  var dist_photo = 0;
  var dist_li = 0;
  
  var effects = new Array();
  if(direction == 'next') {
    
    if( photo_counter < parseInt($('photo_total').innerHTML)) {
      
      $('photo_counter').innerHTML = photo_counter + 1;
      dist_photo = -w * photo_counter - 14 * photo_counter;
      var actual_photo = parseInt($('photo_counter').innerHTML) - 1;
      
      var list_item = $('photos_descriptions_list').down();
      dist_li    += list_item.getHeight();                

      for(var i = 0; i < actual_photo - 1; i++) {
        list_item = list_item.next();
      dist_li    += list_item.getHeight();        
      }      
      
//      dist_li    = list_item.getHeight();
      effects.push(new Effect.Move('photo_container', {x: dist_photo, y: 0, mode:'absolute', sync: true}));
      effects.push(new Effect.Move('photos_descriptions_list', {x: 0, y: -dist_li, mode:'absolute', sync: true}));
    }
    
  } else {
    
    if(photo_counter > 1) {    
      photo_counter--;
      $('photo_counter').innerHTML = photo_counter;
      dist_photo = (w * photo_counter + 14 * (photo_counter-1)) - w;
      
      if(photo_counter == 1) {
        dist_li = 0;
      } else {
        var list_item = $('photos_descriptions_list').down();
        dist_li = 0;        
        dist_li   += list_item.getHeight();                
        
        for(var i = 1; i < photo_counter - 1; i++) {
          list_item = list_item.next();
          dist_li   += list_item.getHeight();                
        }      
      }     
      effects.push(new Effect.Move('photo_container', {x: -dist_photo, y: 0, mode:'absolute', sync: true}));
      effects.push(new Effect.Move('photos_descriptions_list', {x: 0, y: -dist_li, mode:'absolute', sync: true}));
    }
    
  }
    var container;

    if($('photo_descr_strech')) {
       container = $('photo_descr_strech');
    } else if($('photo_descr_breed')) {
       container = $('photo_descr_breed');
    }

    var actual_photo = parseInt($('photo_counter').innerHTML);
    var list_item = $('photos_descriptions_list').down();

    for(var i = 0; i < actual_photo - 1; i++) {
      list_item = list_item.next();
    }

    container.setStyle({"height": list_item.getHeight()+"px"});  
  //  effects.push(new Effect.Morph(container, {style: "height:"+list_item.getHeight()+"px;"}));  
  
  if(effects.length > 0) {
    stopParallelEffects(lastParallel);
    lastParallel = new Effect.Parallel(effects, {duration: 0.2});
  }
}

function resizeDescription() {
  var container;
  
  if($('photo_descr_strech')) {
     container = $('photo_descr_strech');
  } else if($('photo_descr_breed')) {
     container = $('photo_descr_breed');
  }
  var actual_photo;
	if($('photo_counter'))
  	actual_photo = parseInt($('photo_counter').innerHTML);
  var list_item = $('photos_descriptions_list').down();
  
  for(var i = 0; i < actual_photo - 1; i++) {
    list_item = list_item.next();
  }
  
//  container.setStyle({"height": list_item.getHeight()+"px"});  
  new Effect.Morph(container, {style: "height:"+list_item.getHeight()+"px;", duration: 0.1});
}

function disable_neigbours_input(el) {
  
  var id = return_el_id_in_class(el);
  if(id == false)
    return;
  var values = id.split("-");
  // 0 week 
  // 1 type
  // 2 year

  var ul_conts = $$('ul.'+values[0]+'-'+values[2]+'-ul');
  ul_conts.each(function(li_cont) {
    var li_cont = el.up().up();  

    if(el.hasClassName('midweek')) {
        li_cont.previous().down().down().checked = false;
        li_cont.next().down().down().checked = false;
      } else if(el.hasClassName('week')) {
        li_cont.next().down().down().checked = false;
        li_cont.next().next().down().down().checked = false;      
      } else { // it is weekend
        li_cont.previous().down().down().checked = false;
        li_cont.previous().previous().down().down().checked = false;      
      }
    
  });
  
     
}

/* el is the input box self, where is 'week', 'midweek', or 'weekend' */
function get_input_prev_week(el, where) {
  
  var id = return_el_id_in_class(el);
  if(id == false)
    return;
  var values = id.split("-");
  // 0 week 
  // 1 type
  // 2 year

  var ul_conts = $$('ul.'+values[0]+'-'+values[2]+'-ul');
  var ul_prevs = new Array();
  var lis;
  var els_return = new Array();
  
  ul_conts.each(function(ul_cont) {
    if(ul_cont.previous() && ul_cont.previous().hasClassName('days')) {
      ul_prevs.push(ul_cont.previous());
    }    
  });

  ul_prevs.each(function(ul_prev) {
    lis = ul_prev.down().nextSiblings();
    lis.each(function(li) {
      if(!li.hasClassName('descr'))
        $continue;

      var input_el;
      if(li.down() && li.down().down()) {
        input_el = li.down().down();
        if(where == 'week' && input_el.hasClassName('week')) {
          els_return.push(input_el);
          $break;
        } else if(where == 'midweek' && input_el.hasClassName('midweek')) {
          els_return.push(input_el);
          $break;        
        } else if(where == 'weekend' && input_el.hasClassName('weekend')) {
          els_return.push(input_el);
          $break;        
        }      
      }
      });    
  });

    return els_return;
}

function return_el_id_in_class(el) {
  var classes = el.classNames();
  
  var id = false;
  classes.each(function(cl) {
    if(parseInt(cl[0]) < 9)
    id = cl;
  })
  
  return id;
  
}

/* el is the input box self, where is 'week', 'midweek', or 'weekend' */
function get_input_next_week(el, where) {
  
  var id = return_el_id_in_class(el);
  if(id == false)
    return;
    
  var values = id.split("-");
  // 0 week 
  // 1 type
  // 2 year
  
  var ul_conts = $$('ul.'+values[0]+'-'+values[2]+'-ul');
  var ul_next = new Array();
  var el_return = new Array();  
  var lis;
  
  ul_conts.each(function(ul_cont) {
    if(ul_cont.next() && ul_cont.next().hasClassName('days')) {
      ul_next.push(ul_cont.next());
    } 
  });
  
  ul_next.each(function(ul) {
    lis = ul.down().nextSiblings();
    
    lis.each(function(li) {
      if(!li.hasClassName('descr') && !li.hasClassName('descrLW') && !li.hasClassName('descrL'))
        $continue;
    
      var input_el;
      if(li.down() && li.down().down()) {
        input_el = li.down().down();
        if(where == 'week' && input_el.hasClassName('week')) {
          el_return.push(input_el);
          $break;
        } else if(where == 'midweek' && input_el.hasClassName('midweek')) {
          el_return.push(input_el);
          $break;        
        } else if(where == 'weekend' && input_el.hasClassName('weekend')) {
          el_return.push(input_el);
          $break;        
        }        
      }
    });  
  });
    return el_return;
}

function click_input(event) {

  var el                = event.element();
  if(el.tagName != 'INPUT') {
//    alert("trigger is nat an input checkbox...");    
    return;
  }
  
  var li_conts                = new Array();
  var ul_conts                = new Array();
  var first_day_li_conts      = new Array();
  var first_day_li_next_conts = new Array();  

  var id = return_el_id_in_class(el);

  if(id == false)
    return;
    
  $$('input.'+id).each(function(inp) {
    li_conts.push(inp.up().up());
  });  

  li_conts.each(function(li) {
    ul_conts.push(li.up());
  });

  ul_conts.each(function(ul) {
    first_day_li_conts.push(ul.down().next());
  });
  
  ul_conts.each(function(ul_cont) {
      if(ul_cont.next() && ul_cont.next().hasClassName('days')) {
        first_day_li_next_conts.push(ul_cont.next().down().next());    
      } 
  });
  
  // Is midweek?
  if(el.hasClassName('midweek')) {
    
    if(el.checked) {
      
      // Go and check the other weeks indicating the same midweek
      $$('input.'+id).each(function(inp) {
        inp.checked = true;
      });      
      
      // If prev week selected unselect
      var prev_weeks = get_input_prev_week(el, 'week');
//      alert("hi"+prev_week.value);

      prev_weeks.each(function(prev_week) {
        if(prev_week.checked) {
          prev_week.checked = false; 
          var we = prev_week.up().up();
          for(var i = 0; i < 3; i++) {
            we = we.previous();
            we.removeClassName('selected');
            we.removeClassName('selected_1');
            we.removeClassName('selected_2');          
            we.removeClassName('selected_3');          
            we.addClassName('available');
          }
        }        
      });

      // If this week is selected, unselect the first four days of the next week      
/*      $$('input.'+id).each(function(inp) {
        var this_week = inp.up().up().previous().down().down();

        if(this_week.checked)   {
          var next_weeks = get_input_next_week(inp, 'week');

          next_weeks.each(function(next_week) {
            var d = next_week.up().up();
            for(var j = 0; j < 7; j++) {
              d = d.previous();
              if(j < 3)
                continue;
                d.removeClassName('selected');
                d.removeClassName('selected_1');
                d.removeClassName('selected_2');
                d.removeClassName('selected_3');          
                d.addClassName('available');
            }          
          });          
        }
      }); */      
      
    // If this week is selected only unchek the weekend
     $$('input.'+id).each(function(inp) {
        var this_week = inp.up().up().previous().down().down();

        if(this_week.checked)   {
//          var next_weeks = get_input_next_week(inp, 'week');
          inp.up().up().next().down().down().checked = false;
          var next_weeks = get_input_next_week(inp, 'weekend');
            next_weeks.each(function(nw) {
              nw.checked = false;
            });
        }
      });       
      
      // If this week is selected, only deselect the weekend of this week(s)      
      var inp = $$('input.'+id).pop();                        
      for(var i = 0; i < 7; i++) {  
        first_day_li_conts.each(function(fd) {
          var first_day_li_cont = fd;
          
          if(i >= 1) {
            for(var k=0; k < i; k++) {
              first_day_li_cont = first_day_li_cont.next();                        
            }
          }
          
          if(!first_day_li_cont.hasClassName('disabled')) {          
            if(i >= 4) {      
              if(!(inp.up().up().next().down().down().checked || inp.up().up().previous().down().down().checked)) {
                first_day_li_cont.addClassName('available');   
                first_day_li_cont.removeClassName('selected');             
                first_day_li_cont.removeClassName('selected_1');             
                first_day_li_cont.removeClassName('selected_2');             
                first_day_li_cont.removeClassName('selected_3');                                         
              }              
              
            } else {
              first_day_li_cont.addClassName('selected');
              if(i == 0)
                first_day_li_cont.addClassName('selected_1');
              if(i == 1 || i == 2)
                first_day_li_cont.addClassName('selected_2');
              if(i == 3)            
                first_day_li_cont.addClassName('selected_3');
              first_day_li_cont.removeClassName('available');
            }             
          }
        });
      }
    } else {
      
      // Go and uncheck the other weeks 
      $$('input.'+id).each(function(inp) {
        inp.checked = false;
      });      
      
      for(var i = 0; i < 4; i++) {
        first_day_li_conts.each(function(fd) {        
          var first_day_li_cont = fd;
          
          if(i >= 1) {
            for(var k=0; k < i; k++) {
              first_day_li_cont = first_day_li_cont.next();                        
            }
          }
        
          if(!first_day_li_cont.hasClassName('disabled')) {     
            first_day_li_cont.addClassName('available');
            first_day_li_cont.removeClassName('selected');
            first_day_li_cont.removeClassName('selected_1');
            first_day_li_cont.removeClassName('selected_2');
            first_day_li_cont.removeClassName('selected_3');
          }
//          first_day_li_cont = first_day_li_cont.next();
      });
      }
    }

  // is it week?  
  } else if(el.hasClassName('week')) {
    
    if(el.checked) {
      // Go and check the other weeks 
      $$('input.'+id).each(function(inp) {
        inp.checked = true;
      });      
      
      // Uncheck next midweek if checked
      var next_midweeks = get_input_next_week(el, 'midweek');
      
      next_midweeks.each(function(next_midweek) {
        if(next_midweek.checked) 
          next_midweek.checked = false;
      });
      
      // If this midweek is selected only unchek the weekend
       $$('input.'+id).each(function(inp) {
          inp.up().up().next().next().down().down().checked = false;
        });
    
      for(var i = 0; i < 7; i++) {
        first_day_li_conts.each(function(fd) {
          
          var first_day_li_cont = fd;

          if(i >= 1) {
            for(var k=0; k < i; k++) {
              first_day_li_cont = first_day_li_cont.next();                        
            }
          }
          
          if(!first_day_li_cont.hasClassName('disabled') && i > 3) {              
            first_day_li_cont.removeClassName('available');
            first_day_li_cont.addClassName('selected');
            if(i == 4)
              first_day_li_cont.addClassName('selected_1');
            if(i == 5)
              first_day_li_cont.addClassName('selected_2');
            if(i == 6)
              first_day_li_cont.addClassName('selected_2');            
          } 
          
        });
        first_day_li_next_conts.each(function(fd) {
          var first_day_li_next_cont = fd;

          if(i >= 1) {
            for(var k=0; k < i; k++) {
              first_day_li_next_cont = first_day_li_next_cont.next();                        
            }
          }          
          
          if(!first_day_li_next_cont.hasClassName('disabled') && i < 4) {
            first_day_li_next_cont.removeClassName('available');
            first_day_li_next_cont.addClassName('selected');              
            if(i != 3)
              first_day_li_next_cont.addClassName('selected_2');
            if(i == 3)
              first_day_li_next_cont.addClassName('selected_3');          
          } 
        });
      }
      
    } else {  // Not checked

      // Go and uncheck the other weeks 
      $$('input.'+id).each(function(inp) {
        inp.checked = false;
      });
      
      for(var i = 0; i < 7; i++) {
        first_day_li_conts.each(function(fd) {
          
          var first_day_li_cont = fd;

          if(i >= 1) {
            for(var k=0; k < i; k++) {
              first_day_li_cont = first_day_li_cont.next();                        
            }
          }          
          
          if(!first_day_li_cont.hasClassName('disabled') && i > 3) {              
            first_day_li_cont.addClassName('available');
            first_day_li_cont.removeClassName('selected');
            first_day_li_cont.removeClassName('selected_1');              
            first_day_li_cont.removeClassName('selected_2');              
            first_day_li_cont.removeClassName('selected_3');
          }
        });

        first_day_li_next_conts.each(function(fd) {
          
          var first_day_li_next_cont = fd;

          if(i >= 1) {
            for(var k=0; k < i; k++) {
              first_day_li_next_cont = first_day_li_next_cont.next();                        
            } 
          }         
          if(!first_day_li_next_cont.hasClassName('disabled') && i < 4) {              
            first_day_li_next_cont.addClassName('available');
            first_day_li_next_cont.removeClassName('selected');              
            first_day_li_next_cont.removeClassName('selected_1');              
            first_day_li_next_cont.removeClassName('selected_2');              
            first_day_li_next_cont.removeClassName('selected_3');                                  
          }
        });        
      }
    }
  // Otherwise it is weekend  
  } else {
    
    if(el.checked) {
      // Go and check the other weeks 
      $$('input.'+id).each(function(inp) {
        inp.checked = true;
      });      
      
    // If this week is selected, unselect the first four days of the next week
    var this_week = el.up().up().previous().previous().down().down();
    var next_weeks = get_input_next_week(el, 'week');
    var prev_weeks = get_input_prev_week(el, 'week');

    if(this_week.checked) {
      next_weeks.each(function(next_week) {
        var d = next_week.up().up();
        for(var j = 0; j < 7; j++) {
          d = d.previous();
          if(j < 3)
            continue;
          d.removeClassName('selected');
          d.removeClassName('selected_1');
          d.removeClassName('selected_2');
          d.removeClassName('selected_3');                        
          d.addClassName('available');
        }        
      });
    }


// If this week is selected, unselect input and unselect first four days of next week
  if(this_week.checked) {
    
     $$('input.'+id).each(function(inp) {
        var tw = inp.up().up().previous().previous().down().down();
        if(tw.checked)   {
          tw.checked = false;
        }
      });
  }

//    disable_neigbours_input(el);

      for(var i = 0; i < 7; i++) {              
        first_day_li_conts.each(function(fd) {      
                    
          var first_day_li_cont = fd;
          if(i >= 1) {
            for(var k=0; k < i; k++) {
              first_day_li_cont = first_day_li_cont.next();                        
            } 
          }
          
          if(i < 4) {

            if(!first_day_li_cont.hasClassName('disabled')) {   

/*              prev_weeks.each(function(prev_week) {
              // If the previous week was selected, do not unselect the first days                
                if(!prev_week.checked) {
                  first_day_li_cont.addClassName('available');                
                  first_day_li_cont.removeClassName('selected');
                  first_day_li_cont.removeClassName('selected_1');
                  first_day_li_cont.removeClassName('selected_2');
                  first_day_li_cont.removeClassName('selected_3');
                  $break;
                }
              });
*/                
            } 
          } else {

          if(!first_day_li_cont.hasClassName('disabled')) {
            first_day_li_cont.addClassName('selected');            
            if(i == 4) {
              first_day_li_cont.addClassName('selected_1');
            } else if(i == 6) {
              first_day_li_cont.addClassName('selected_3');
              first_day_li_cont.removeClassName('selected_2');
              
            } else {
              first_day_li_cont.addClassName('selected_2');              
            }
                          
            first_day_li_cont.removeClassName('available');
          }
        }             
        });        
      }
    } else {
      
      // Go and check the other weeks 
      $$('input.'+id).each(function(inp) {
        inp.checked = false;
      });      
      for(var i = 0; i < 7; i++) {
        
        if(i < 4)
          continue;

        first_day_li_conts.each(function(fd) {
          var first_day_li_cont = fd;
          
          if(i >= 1) {
            for(var k=0; k < i; k++) {
              first_day_li_cont = first_day_li_cont.next();                        
            } 
          }

          if(!first_day_li_cont.hasClassName('disabled')) {                              
            first_day_li_cont.addClassName('available');
            first_day_li_cont.removeClassName('selected');
            first_day_li_cont.removeClassName('selected_1');
            first_day_li_cont.removeClassName('selected_2');
            first_day_li_cont.removeClassName('selected_3');                                    
          }
          first_day_li_cont = first_day_li_cont.next();                
        });
      }
    }          
  } 
}

// Observe some elements in the page
document.observe('dom:loaded', function() {
        
  if($('photo_descr_strech') || $('photo_descr_breed')) {
    resizeDescription();
  }
  
  if($('cr')) {
    $('cr').absolutize();
  
    if(document.viewport.getHeight() > ($('cr').cumulativeOffset().top + $('cr').getHeight())) {
      $('cr').setStyle({'top': document.viewport.getHeight() - 3 - $('cr').getHeight() + "px"});
    }
  }    
  
  if($('photo_nav'))
    $('photo_nav').writeAttribute({"unselectable": "on"});

  if($('photo_volgende')) {
    $('photo_volgende').observe('click', function(event) {
    	event.stop();
    	movePhoto('next');
    	});

			$('photo_container').observe('click', function(event) {
				event.stop();
				movePhoto('next');
			});
  }
  
  if($('photo_vorige')) {
    $('photo_vorige').observe('click', function(event) {
      event.stop();
      movePhoto('prev');
    });  
  }
  
  $$('input.week_selection').each( function(el) {
    if(!el.disabled) {
      el.observe('click', click_input);
    }
  });

  $$('ul#cal_control li').each(function(el) {
    
    el.observe('click', function(event) {
      event.stop();
      stopParallelEffects(lastParallel);
      pos         = 1 * ($('months_container').getStyle('left').gsub('px', ''));
      width       = 1 * $('months_container').getWidth();
      month_width = 1 * $('months_container').down().getWidth();
                
                
      if(el.id == 'volgende' && !el.hasClassName('disabled')) {
        
        if(pos % month_width != 0) {
          move = month_width + (pos % month_width);
        } else {
          move = month_width;
        }
        
        lastParallel = new Effect.Move($('months_container'), {x: -move, 
                                               transition: Effect.Transitions.sinoidal, 
                                               mode: 'relative', 
                                               duration: 0.2,
                                               afterFinish: function() {
                                                 $('vorige').removeClassName('disabled');
                                                 if( ((pos*-1) == 5440)) {
                                                   $('volgende').addClassName('disabled');
                                                 }                                                 
                                               }
                                               });
      } else if(el.id == 'vorige' && !el.hasClassName('disabled')) {

        if(parseInt($('months_container').getStyle('left').gsub('px', '')) >= 0) {
          move = 0;
          $('months_container').setStyle({'left': '0px'});          
          $('vorige').addClassName('disabled');
          $('volgende').removeClassName('disabled');          
        } else if(pos % month_width != 0) {
          move = month_width - (pos % month_width);
        } else {
          move = month_width;                  
        }
        
        lastParallel = new Effect.Move($('months_container'), {x: move, 
                                             transition: Effect.Transitions.sinoidal, 
                                             mode: 'relative', 
                                             afterFinish: function() {
                                               $('volgende').removeClassName('disabled');
                                               if(parseInt($('months_container').getStyle('left').gsub('px', '')) >= 0) {
                                                 $('vorige').addClassName('disabled');
                                                 $('months_container').setStyle({'left': '0px'});
                                               }
                                             },
                                             duration: 0.2});
      }
    });
  });
});

