(function($) {

  var selection = null,
  baseHeight = 360,
  expandedHeight = 600,
  leftOffset = 0,
  outerContainer = null,
  innerContainer = null,
  elements = null,
  nextButton = null,
  previousButton = null,
  plusButton = null,
  minusButton = null,
  currentEndPosition = 0,
  bottomButtonRowHeight = 70,
  expanded = false,


  elementslider_setup = function () {
    outerContainer.css({left: -leftOffset, overflow: "hidden", width: $(window).width() , height: baseHeight+bottomButtonRowHeight});
    innerContainer.css({position: 'absolute', top: 0, left: leftOffset, height: baseHeight, width: 19999});


    if (previousButton) {
      if (elements.length>1) {
        previousButton.show();
        previousButton.click(function (e) {
          elementslider_previous();
          e.preventDefault();
        });
      }
    }

    if (nextButton) {
      if (elements.length>1) {
        nextButton.show();
        nextButton.click(function (e) {
          elementslider_next();
          e.preventDefault();
        });
      }
    }

    if (plusButton) {
      plusButton.show();
      plusButton.click(function (e) {
        elementslider_expand();
        e.preventDefault();
      });
    }

    if (minusButton) {
      minusButton.show();
      minusButton.click(function (e) {
        elementslider_contract();
        e.preventDefault();
      });
    }

    elementslider_positionbuttons();

    // resize inner container to width of all elements. ready and load to account for caching. Buttering the bacon.
    $(innerContainer).children('img').ready(function() { elementslider_sizecontainer(); });
    $(innerContainer).children('img').load(function() { elementslider_sizecontainer(); });

    $(window).resize(elementslider_resize);
  },

  elementslider_previous = function () {
    var nextPos = 0;
    var lastOkPos = 0;
    for (i=0; i<elements.length-1; i++) {
      nextPos += $(elements[i]).outerWidth() + parseInt($(elements[i]).css('marginRight'))  + parseInt($(elements[i]).css('marginLeft')) + parseInt($(elements[i]).css('paddingRight')) + parseInt($(elements[i]).css('paddingLeft'));
      if (nextPos<currentEndPosition) {lastOkPos = nextPos;} else {break;}
      
    }
    currentEndPosition = lastOkPos;
    elementslider_animate_to_endpos();
  },

  elementslider_next = function () {
    var nextPos = 0;
    for (i=0; i<elements.length-1; i++) {
      nextPos += $(elements[i]).outerWidth() + parseInt($(elements[i]).css('marginRight'))  + parseInt($(elements[i]).css('marginLeft')) + parseInt($(elements[i]).css('paddingRight')) + parseInt($(elements[i]).css('paddingLeft'));
      if (nextPos>currentEndPosition) {break;}
    }
    currentEndPosition = nextPos;
    elementslider_animate_to_endpos();
  },

  elementslider_animate_to_endpos = function () {
    innerContainer.stop().animate({left: leftOffset-currentEndPosition}, 600, 'easeOutExpo');
  },

  elementslider_expand = function () {
    if (!expanded) {
      currentEndPosition = Math.round(currentEndPosition*(expandedHeight/baseHeight));
      innerContainer.css({width: Math.round(innerContainer.width()*(expandedHeight/baseHeight))});
      outerContainer.stop().animate({height: expandedHeight+bottomButtonRowHeight}, 600, 'easeOutExpo');
      innerContainer.stop().animate({height: expandedHeight, left: leftOffset-currentEndPosition}, 600, 'easeOutExpo');
      innerContainer.children('div').stop().animate({height: expandedHeight, width: (expandedHeight/9)*16}, 600, 'easeOutExpo');
      innerContainer.children('img').stop().animate({height: expandedHeight}, 600, 'easeOutExpo');

      if (previousButton) {
        previousButton.stop().animate({top: (expandedHeight/2)-(previousButton.height()/2), left: 30}, 600, 'easeOutExpo');
      }
      if (nextButton) {
        nextButton.stop().animate({top: (expandedHeight/2)-(nextButton.height()/2), left: $(window).width() - 80}, 600, 'easeOutExpo');
      }
      if (plusButton) {
        plusButton.stop().animate({top: expandedHeight+10, left: leftOffset}, 600, 'easeOutExpo');
      }
      if (minusButton) {
        minusButton.stop().animate({top: expandedHeight+10, left: leftOffset + 46}, 600, 'easeOutExpo');
      }

      expanded = true;
    }
  },

  elementslider_contract = function () {
    if (expanded) {
      currentEndPosition = Math.round(currentEndPosition/(expandedHeight/baseHeight));
      innerContainer.css({width: Math.round(innerContainer.width()/(expandedHeight/baseHeight))});
      outerContainer.stop().animate({height: baseHeight+bottomButtonRowHeight}, 600, 'easeOutExpo');
      innerContainer.stop().animate({height: baseHeight, left: leftOffset-currentEndPosition}, 600, 'easeOutExpo');
      innerContainer.children('div').stop().animate({height: baseHeight, width: (baseHeight/9)*16}, 600, 'easeOutExpo');
      innerContainer.children('img').stop().animate({height: baseHeight}, 600, 'easeOutExpo');

      if (previousButton) {
        previousButton.stop().animate({top: (baseHeight/2)-(previousButton.height()/2), left: 30}, 600, 'easeOutExpo');
      }
      if (nextButton) {
        nextButton.stop().animate({top: (baseHeight/2)-(nextButton.height()/2), left: $(window).width() - 80}, 600, 'easeOutExpo');
      }
      if (plusButton) {
        plusButton.stop().animate({top: baseHeight+10, left: leftOffset}, 600, 'easeOutExpo');
      }
      if (minusButton) {
        minusButton.stop().animate({top: baseHeight+10, left: leftOffset + 46}, 600, 'easeOutExpo');
      }

      expanded = false;
    }
  },

  elementslider_resize = function () {
    outerContainer.css({width: $(window).width()});
    elementslider_sizecontainer();
    elementslider_positionbuttons();
  },

  elementslider_sizecontainer = function () {
    var totalWidth = 0;
    for (i=0; i<elements.length; i++) {
      totalWidth += $(elements[i]).outerWidth() + parseInt($(elements[i]).css('marginRight'))  + parseInt($(elements[i]).css('marginLeft')) + parseInt($(elements[i]).css('paddingRight')) + parseInt($(elements[i]).css('paddingLeft'));
    }
    totalWidth += 10;
    innerContainer.css({width: totalWidth});
  },

  elementslider_positionbuttons = function () {
    if (previousButton) {
      previousButton.css({top: ((expanded?expandedHeight:baseHeight)/2)-(previousButton.height()/2), left: 30});
    }
    if (nextButton) {
      nextButton.css({top: ((expanded?expandedHeight:baseHeight)/2)-(nextButton.height()/2), left: $(window).width() - 80});
    }
    if (plusButton) {
      plusButton.css({top: (expanded?expandedHeight:baseHeight)+10, left: leftOffset});
    }
    if (minusButton) {
      minusButton.css({top: (expanded?expandedHeight:baseHeight)+10, left: leftOffset + 46});
    }
  };

	$.fn.elementslider = function(obj) {
    leftOffset = (obj.leftOffset) ? obj.leftOffset : leftOffset;
    outerContainer = $(this);
    previousButton = outerContainer.children('#previous-button');
    nextButton = outerContainer.children('#next-button');
    plusButton = outerContainer.children('#plus-button');
    minusButton = outerContainer.children('#minus-button');

    innerContainer = $(this).children('.container');
    if (innerContainer) {
      elements = $(this).children('.container').children();

      if (elements.length>0) {
        elementslider_setup();
      }
    }
	}

})(jQuery);


var App = function (name) {
  this.initialize();
}

App.prototype.initialize = function () {
  
}

App.prototype.start = function () {
  // element slider
  if ($(".elements").length>0) {
    $(".elements").elementslider({leftOffset: 220});
  }

    
  // comment form validation
  if ($("#comment_form").length>0) {
    $("#comment_form").validate();
  }

  // comment form validation
  if ($("#search-box form").length>0) {
    $("#search-box form").validate();
  }

  // juggle news list
  if ($("#news-expanded").length>0) {
    $.each($("#news-expanded ul li"), function(index, value) {
      $(value).css({left: -20 + (Math.random()*40)});
    });
  }

  // juggle project list
  if ($("#projects-expanded").length>0) {
    $.each($("#projects-expanded ul li"), function(index, value) {
      $(value).css({left: -20 + (Math.random()*40)});
    });

    $("#projects-expanded ul li a div").css({ display: 'block', top: 203, height: 'auto' });

    $("#projects-expanded ul li a").mouseover(function () {
      var topPos = 203 - ($(this).children('div').height() + parseInt($(this).children('div').css('marginBottom'))  + parseInt($(this).children('div').css('marginTop')) + parseInt($(this).children('div').css('paddingBottom')) + parseInt($(this).children('div').css('paddingTop')));
      $(this).children('div').stop().animate({ top: topPos }, 200, 'easeOutExpo');
    });
    $("#projects-expanded ul li a").mouseout(function () {
      $(this).children('div').stop().animate({ top: 203 }, 200, 'easeOutExpo');
    });
  }

}

App.prototype.navigateToContent = function () {

}

var site = new App();

// main app init
$(document).ready(function() {
  site.start();

  /*
  // Address handler
  $.address.init(function(event) {

  }).change(function(event) {
      site.navigateToContent(event.value);
  }).externalChange(function(event) {

  }).history(true);
  */

  var rotation = 0;

  var mouseMoveRotate = function (e) {
    var position = $("#logo-image").position();
    var half_width = $("#logo-image").width() * 0.5;
    var half_height = $("#logo-image").height() * 0.5;
    var radians = Math.atan2(e.pageY-(position.top+half_height), e.pageX-(position.left+half_width));
    var degrees = (radians/Math.PI)*180;
    rotation = degrees-90;

    $("#logo-image").css({
      '-webkit-transform': 'rotateZ(' + rotation + 'deg)',
      '-moz-transform': 'rotate(' + rotation + 'deg)',
      '-o-transform': 'rotate(' + rotation + 'deg)',
      'transform': 'rotateZ(' + rotation + 'deg)' });
  };


  $("body").mousemove($.throttle( 50, mouseMoveRotate ));


  // IE embed tag fix
  var embedTag;
  $("embed").each(function(i) {
      embedTag = $(this).attr("outerHTML");
      if ((embedTag != null) && (embedTag.length > 0)) {
          embedTag = embedTag.replace(/embed /gi, "embed wmode=\"transparent\" ");
          $(this).attr("outerHTML", embedTag);
      }
      else {
          //$(this).wrap("<div></div>");
      }
  });

});

