var deltaLeftConstantInt = 1536
var deltaTopConstantInt	 = -36
var animationInProgress  = false
var slideCount           = 0

// http://railscasts.com/episodes/136-jquery
jQuery.fn.submitWithAjax = function() {
  this.live("submit", function() {
    $.post(this.action, $(this).serialize(), null, "script");
    return false;
  })
  return this;
};

$(function(){
	if (WebKitDetect.isMobile()) {
		initPhone();
	} else {
		initScreen();
	}
});

function initScreen() {
	
  $("#drop-navigation").live("click",function(){
    $("#drop-navigation ul").slideToggle();
    return false;
  });
	
	////////////////////////////////////////// TODO:
	// Find the equivalent page-navigation link and set active
	// on it too.
  $("#drop-navigation ul a").live("click", function(){
    $(this).parents("div > ul").find("a").removeClass("active");
    $(this).addClass("active");
    $("#page-navigation").find("a").removeClass("active");
    $("#page-navigation").find("a:contains('" + $(this).html() + "')").addClass("active");
    $.getScript(this.href);
    return false;
  })
	
  $("#legal").css({ top: "777px"})
  
  $("a.legal").click(function(){
    if ($("#legal").hasClass("visible")) {
      $("#legal").animate({ top: "777px"},"slow").removeClass("visible")
    } else {
      $("#legal").animate({ top: "595px"},"slow").addClass("visible")
    }
    return false;
  });
	
  $("#navigation a:not(.blog)").live("click", function(){
    if (!animationInProgress) {
      animationInProgress = true
      try { getFlashMovie('ddandf-logo').playClickSound(); } catch(err) {}
      $(this).parents("div > ul").find("a").removeClass("active");
      $(this).addClass("active");
      $.getScript(this.href);
    }
    return false;
  })
  
  $("#page-navigation a, #footer a.ajax").live("click", function(){
    $(this).parents("div > ul").find("a").removeClass("active");
    $(this).addClass("active");
    $.getScript(this.href);
    return false;
  })
	
  $(".thumbnails a").live("mouseover", function(){
   if (!$(this).hasClass("active")) {
     $img = $(this).children("img");
     $img
       .attr("src", $img.attr("data-rollover"))
       .addClass("hover");
       // .css('{ border : "1px solid red" }');
   }
  })
	
  $(".thumbnails a").live("mouseout",function(){
   if (!$(this).hasClass("active")) {
     $img = $(this).children("img");
     $img
       .attr("src", $img.attr("data-src"))
       .removeClass('hover');
       // .css({border : "1px solid black"});
   }
  })
  
  $(".thumbnails a").live("click", function(){
   $link = $(this)
   
   $link.parents(".thumbnails").find("a").each(function(index,object){
     $img = $(object).children("img")
     $img
       .attr("src", $img.attr("data-src"))
       // .css({ border : "1px solid black" })
       .removeClass('active')
       .removeClass('hover')
   })
   
   // .removeClass("active")
   $link.addClass("active")
   var $img = $link.children("img");
   $img
     .attr("src", $img.attr("data-rollover"))
     // .css({ border : "1px solid red" });
     .addClass('active')
     .removeClass("hover")
   $.getScript(this.href);
   return false;
  })
  
  $(".panel-tab a").live("click", function(){
   $.getScript(this.href);
   return false;
  });

  $('#flash-logo').click(function(){
   $.getScript('/pages/home');
  })
  
  $(".region:not(#region_1, #page-navigation)").scrollerish();
	
  $("li").addClass("last");
  
  $("form").submitWithAjax();
}

function initPhone() {
  window.addEventListener('load', function(){
    setTimeout(scrollTo, 0, 0, 1);
  }, false);
  $('div[style]').removeAttr('style')
}

/****************************************************************************/

function toPx(string) {
	return string + "px"
}

function getFlashMovie(movieName) {
	var isIE = navigator.appName.indexOf("Microsoft") != -1;
	return (isIE) ? window[movieName] : document[movieName];
}

function navigationIsAtTop() {
	return parseInt($("#navigation").css("top")) <= 0
}

function setNavigationPosition(positionString, onComplete) {
	
	if (positionString == "top") {
		cssHash = { left: "100pxpx", top: "-35px" };
	} else {
		
		if (positionString == "top-hidden") {
			cssHash = { left: "100px", top: "-100px" };
		} else {
			cssHash = { left: "192px", top: "362px" };
		}
		
	}
	
	$("#navigation").css( cssHash, onComplete );
	
}

function prepareToSlideIn(selector, onComplete) {
	
	var $element = $(selector);
	
	originalLeft = parseInt($element.css("left"))
	originalTop = parseInt($element.css("top"))
	
	destinationPositionHash = {
		left: toPx(originalLeft + deltaLeftConstantInt),
		top:  toPx(originalTop + deltaTopConstantInt)
	}
	
	$element
		.css( destinationPositionHash )
		.show()
		.animate( destinationPositionHash, 10, onComplete );
	
}

function slide(selector, onComplete) {
	
	var $element = $(selector);
	
	leftPositionInt = parseInt($element.css("left"));
	topPositionInt  = parseInt($element.css("top"));

	destinationPositionHash = {
		left: toPx(leftPositionInt - deltaLeftConstantInt),
		top:  toPx(topPositionInt - deltaTopConstantInt)
	}
	
	$element
		.animate( { opacity: 1.0}, 1000, function(){
		  if (slideCount % 2 == 0) {
		    try { getFlashMovie('ddandf-logo').playRollingSound(); } catch(err) {}
		  }
		  slideCount = slideCount + 1
		})
		.animate( destinationPositionHash, 1800, onComplete );
	
}

function revealContent() {
	$(".region, #page-navigation").fadeIn()
	$(".region:not(.no-scroll, #page-navigation)").scrollerish()
}

function enterTheFist(selector) {
	prepareToSlideIn(selector, function(){
		slide(selector, function(){
			revealContent();
			animationInProgress = false;
		});
	});
}

var WebKitDetect = {};

WebKitDetect.isWebKit = function isWebKit()
{
	return RegExp(" AppleWebKit/").test(navigator.userAgent);
}
WebKitDetect.isMobile = function isMobile()
{
	return WebKitDetect.isWebKit() && RegExp(" Mobile/").
		test(navigator.userAgent);
}