/* KEEP YOUR JQUERY DOM TRAVELS OUT OF MAH FUNCTIONS - NO DOLLAR SIGNS DAMNIT */

function append_effects (container, effects) {
  for(_slide in effects)  { //checking all the slides to add to the system
    for(_item in effects[_slide]['items']) {  //checking all the items in the slides
      var element = effects[_slide]['items'][_item]['element']; //assign html/jquery element to element
      element.hide();
      container.append(element); //append element to container
      element.css(effects[_slide]['items'][_item]['start']); //apply the element's css
    }
  }
}

function show_effects(slide, stopper) {
  for (_item in slide['items']) { //for each item in the slide
    var element = slide['items'][_item]['element']; //assign item to element
    element.show(); //show the element
    if(typeof slide['items'][_item]['callback_function'] !== 'function') {
      slide['items'][_item]['callback_function'] = null;
    }
    if(typeof slide['items'][_item]['stopper'] != 'undefined' && stopper && slide['items'][_item]['stopper']) {
      break;
    }
    element.delay(slide['items'][_item]['animate_wait']).animate(  //animate the element
      slide['items'][_item]['animate'],
      slide['items'][_item]['animate_time'],
      slide['items'][_item]['callback_function']
    );
  }
}

function hide_effects(effects, animation_cell) {
  if(typeof animation_cell != 'undefined' && animation_cell) {
    animation_cell.stop(0,0);
  }
  for (_slide in effects) { //for each slide in the system
    for (_item in effects[_slide]['items']) { //for each item in each slide
      var resetvars = effects[_slide]['items'][_item]['start'];  //get the css to reset to
      var resetelem = effects[_slide]['items'][_item]['element'];  //get the item's element to reset
      resetelem.hide();
      resetelem.stop(1,1); //finish any animation happening with item
      resetelem.css(resetvars); //reset the element
    }
  }
}

function tween_hover(menu_element, animation_cell, effects, tween_slide, link_from, link_target, delay) {
  
  if(menu_element) { animation_cell.css(effects[menu_element.attr('id')]['background']); } //we're depending on the calling id matching an element in the slides
  hide_effects(effects, animation_cell); //hide all effects happening
  if(link_from && link_target) { //if we're given some link objects, we'll shove the href from one to the other, and check target too
    link_target.href = link_from.href;
    if(link_from.target) {
      link_target.target = link_from.target;
    } else {
      link_target.target = "";
    }
  }
  show_effects(tween_slide); //run the tweenslide effects
}

function menu_flash(menu_container, background_image, x_coord_to, y_coord_to, x_coord_from, y_coord_from, speed) {
  menu_container.css({
    'background-image': 'url('+background_image+')',
    'background-repeat': 'repeat-x',
    'background-position':  x_coord_from.toString()+'px '+y_coord_from.toString()+'px'
  });
  menu_container.animate(
    {'backgroundPosition': x_coord_to.toString()+'px '+y_coord_to.toString()+'px'},
    speed, function() {}
  );
}

/** AND NOW OTHER GUY'S STUFF: **/
/**
 * @author Alexander Farkas
 * v. 1.02
 */
(function($) {
	$.extend($.fx.step,{
    backgroundPosition: function(fx) {
      if (fx.state === 0 && typeof fx.end == 'string') {
        var start = $.curCSS(fx.elem,'backgroundPosition');
        if(typeof start == 'undefined') { start = '0px 0px'; }  /* Internet Exploder 8 */
        start = toArray(start);
        fx.start = [start[0],start[2]];
        var end = toArray(fx.end);
        fx.end = [end[0],end[2]];
        fx.unit = [end[1],end[3]];
      }
      var nowPosX = [];
      nowPosX[0] = ((fx.end[0] - fx.start[0]) * fx.pos) + fx.start[0] + fx.unit[0];
      nowPosX[1] = ((fx.end[1] - fx.start[1]) * fx.pos) + fx.start[1] + fx.unit[1];
      fx.elem.style.backgroundPosition = nowPosX[0]+' '+nowPosX[1];
      function toArray(strg){
        strg = strg.replace(/left|top/g,'0px');
        strg = strg.replace(/right|bottom/g,'100%');
        strg = strg.replace(/([0-9\.]+)(\s|\)|$)/g,"$1px$2");
        var res = strg.match(/(-?[0-9\.]+)(px|\%|em|pt)\s(-?[0-9\.]+)(px|\%|em|pt)/);
        return [parseFloat(res[1],10),res[2],parseFloat(res[3],10),res[4]];
      }
    }
	});
})(jQuery);
