// Parallax scrolling 
// Thanks to Brett Taylor for the providing the basis of this script.
// http://inner.geek.nz/javascript/parallax/ 

function calcParallax(tileheight, speedratio, scrollposition)
{
  return (-(Math.floor(scrollposition / speedratio) + (tileheight+1)));
}

window.onload = function()
{ 
  var widthX = (window.innerWidth) ? window.innerWidth : document.body.offsetWidth;
  var positionX = -850 + Math.floor(widthX / 3);    
    var posY = 0;
  
    var bgBackFieldparallax = calcParallax(-600, 8, posY);
  var bgBackField = document.getElementById('bgBackField');
    var bgMiddleField = document.getElementById('bgMiddleField');
    var bgMiddleFieldparallax = calcParallax(-300, 11, posY);
    var bgForegroundField = document.getElementById('bgForegroundField');
    var bgForegroundFieldparallax = calcParallax(-700, 6, posY);

    bgBackField.style.backgroundPosition = positionX + "px " + bgBackFieldparallax + "px";
    bgMiddleField.style.backgroundPosition = "0 " + bgMiddleFieldparallax + "px"; 
    bgForegroundField.style.backgroundPosition = "0 " + bgForegroundFieldparallax + "px";
    
    window.onresize = function()
    {
      widthX = (window.innerWidth) ? window.innerWidth : document.body.offsetWidth;
    positionX = -850 + Math.floor(widthX / 3);
      posY = (document.documentElement.scrollTop) ? document.documentElement.scrollTop : window.pageYOffset;
      bgBackField.style.backgroundPosition = positionX + "px " + bgBackFieldparallax + "px";
    }
  
  window.onscroll = function() 
  {
    widthX = (window.innerWidth) ? window.innerWidth : document.body.offsetWidth;
    positionX = -850 + Math.floor(widthX / 3);
      posY = (document.documentElement.scrollTop) ? document.documentElement.scrollTop : window.pageYOffset;
      if(!posY)
      {
        posY = 0;
      } 
      bgBackField = document.getElementById('bgBackField');
      bgBackFieldparallax = calcParallax(-600, 8, posY);
      bgBackField.style.backgroundPosition = positionX + "px " + bgBackFieldparallax + "px";

      bgMiddleField = document.getElementById('bgMiddleField');
      bgMiddleFieldparallax = calcParallax(-300, 11, posY);
      bgMiddleField.style.backgroundPosition = "0 " + bgMiddleFieldparallax + "px"; 
    
      bgForegroundField = document.getElementById('bgForegroundField');
      bgForegroundFieldparallax = calcParallax(-700, 6, posY);
      bgForegroundField.style.backgroundPosition = "0 " + bgForegroundFieldparallax + "px";    
    }
}
//jQuery Stuff
$(document).ready(function()
{
  //scroll when menu clicked
  $(".menulink").click(function(e)
  {
    e.preventDefault();
    var full_url = this.href;
    var parts = full_url.split("#");
    var trgt = parts[1];
    var target_offset = $("#"+trgt).offset();
    var target_top = target_offset.top;
    
	var loadwinner = "";
	var temp = $(this).attr('class');
	if ($(this).hasClass('loadwinner')){
	  loadwinner = $(this).attr('rel');
	}
	 
    $('html, body').animate({scrollTop:target_top}, 1000,function(){
	  if (loadwinner){
		var url = "winners/"+loadwinner;
        $("#winnerBox").show().load(url);
      }	  
	});
  });
  

  
  //scroll when inline lines clicked
  $(".scroll").click(function(e)
  {
    e.preventDefault();
    var full_url = this.href;
    var parts = full_url.split("#");
    var trgt = parts[1];
    var target_offset = $("#"+trgt).offset();
    var target_top = target_offset.top;
    
    $('html, body').animate({scrollTop:target_top }, 1000);
  });

  //ghetto hover states
  $("a").mouseover(function(){
    $(this).find("span").addClass("hovering");
  });
  $("a").mouseout(function(){
    $(this).find("span").removeClass("hovering");
  });

  //ghetto active states
  $("a").click(function(e)
  {
    $("span").removeClass("active");
    $(this).find("span").addClass("active");
  });
    
  //remove borders from clicked links in firefox
  $("a").livequery('click', function(event)
  {
    this.blur();
  });
  
  //Endorsements.
  $("#endorse a").click(function(event)
  {
    event.preventDefault();
    var href = $(this).attr('href');

    $("#endorse div.leftColumn").hide();
    $("#endorse " + href).show();

  });

  
	//toggle judges - could be cleaned up
    $("#jtitle01").click(function(e)
	{
		e.preventDefault();
    	if ($("#judge01").is(":hidden"))
    	{
       		$("#judgeColumn").fadeIn("fast");
       		$("#judge01").fadeIn("fast");
	    	$("#judge02").fadeOut("fast");
    		$("#judge03").fadeOut("fast");
    		$("#judge04").fadeOut("fast");
    		$("#judge05").fadeOut("fast");
	    }
  	});
    	
    $("#jtitle02").click(function(e){
	e.preventDefault();
    if ($("#judge02").is(":hidden") ) 
    	{
       		$("#judgeColumn").fadeIn("fast");
       		$("#judge02").fadeIn("fast");
	    	$("#judge01").fadeOut("fast");
    		$("#judge03").fadeOut("fast");
    		$("#judge04").fadeOut("fast");
    		$("#judge05").fadeOut("fast");
	    }
  	});
  	
  	$("#jtitle03").click(function(e){
	e.preventDefault();
    if ($("#judge03").is(":hidden") ) 
    	{
       		$("#judgeColumn").fadeIn("fast");
       		$("#judge03").fadeIn("fast");
	    	$("#judge01").fadeOut("fast");
    		$("#judge02").fadeOut("fast");
    		$("#judge04").fadeOut("fast");
    		$("#judge05").fadeOut("fast");

	    }
  	});

  	$("#jtitle04").click(function(e){
	e.preventDefault();
    if ($("#judge04").is(":hidden") ) 
    	{
       		$("#judgeColumn").fadeIn("fast");
       		$("#judge04").fadeIn("fast");
	    	$("#judge01").fadeOut("fast");
    		$("#judge02").fadeOut("fast");
	    	$("#judge03").fadeOut("fast");
	    	$("#judge05").fadeOut("fast");
	    }
  	});

  	$("#jtitle05").click(function(e){
	e.preventDefault();
    if ($("#judge05").is(":hidden") ) 
    	{
       		$("#judgeColumn").fadeIn("fast");
       		$("#judge05").fadeIn("fast");
	    	$("#judge01").fadeOut("fast");
    		$("#judge02").fadeOut("fast");
	    	$("#judge03").fadeOut("fast");
    		$("#judge04").fadeOut("fast");
	    }
  	});
    
    
    //waypoint nav states - could be cleaned up
            $('div.biaItemHeader').waypoint(function() 
            {
                        if (navigator.userAgent.match(/iPad/) || navigator.userAgent.match(/iPhone/) || navigator.userAgent.match(/Android/) || navigator.userAgent.match(/iPod/) ) { }
                        else {

                                    $("a.menulink").removeClass("currentNav");
                                    var biaWaypoint = $(this).parent().attr('id');
                                    $("#menu" + biaWaypoint).addClass("currentNav");
                                    if (biaWaypoint.match("intro")) {
                                                $(".footerSM").fadeOut(500);
                                    }

                                    else {
                                                $(".footerSM").fadeIn(500);
                                    }

                        }
                        
            });
            
            $('div.biaItemFooter').waypoint(function() 
            {
                        if (navigator.userAgent.match(/iPad/) || navigator.userAgent.match(/iPhone/) || navigator.userAgent.match(/Android/) || navigator.userAgent.match(/iPod/) ) { }
                        else {
                                    $("a.menulink").removeClass("currentNav");
                                    var biaWaypoint = $(this).parent().attr('id');
                                    $("#menu" + biaWaypoint).addClass("currentNav");
                                    if (biaWaypoint.match("intro")) {
                                                $(".footerSM").fadeOut(500);
                                    }

                                    else {
                                                $(".footerSM").fadeIn(500);
                                    }
                        }
            });

  
  
  
  
  //toggle registration form nav
    $(".registerLinks a").click(function(e)
  {
      e.preventDefault();
      var href = $(this).attr('href');

      if ($(href).is(":hidden")) 
    {
      $(".visibles").hide();
      $(href).fadeIn("fast");
      $("a").removeClass("current");
      $(href).addClass("visibles");
      $(this).addClass("current")
    }
    });

  //register form
    $("#regh1").click(function(e)
  {
      e.preventDefault();     
      if ($("#instructions").is(":hidden") ) 
    {
      $(".visibles").fadeOut("fast");
      $("#instructions").fadeIn("fast");
      $("#instructions").addClass("visibles");      
      $("a").removeClass("current");
      $(this).addClass("current")
    }
  });
  
  //terms
  $(".tclink").click(function(e)
  {
      e.preventDefault();
    
      var href = $(this).attr('href');
      if ($(href).is(":hidden") ) 
      {
        $(".visibles").hide();
        $(href).fadeIn("fast");
        $("a").removeClass("current");
        $(href).addClass("visibles");
        $(this).addClass("current")
      }
      else{}
  });
 
  //submit form?  
  $("#step3 a").click(function(e){
      e.preventDefault();
      
      var href = $(this).attr('href');
      if ($(href).is(":hidden") ) ;
      {
         $("#stuEntry").hide();
         $("#proEntry").hide();
        $(href).show();
      }
    }); 

  //previous winners
  
      $("#header2011").click(function(e)
    {
      e.preventDefault();
        if ($("#2011entries").is(":hidden") ) 
          {
            $(".entries").hide();
            $("#header2011 .winnerButton .plus").hide();
            $("#header2011 .winnerButton .minus").show();
            
              $("#2011entries").show();
              $(".winnerButton").addClass(".winnerButtonDown");
          }
      else 
        { 
            $(".winnerButton .minus").hide();
            $(".winnerButton .plus").show();
  
          $(".entries").hide(); 
              $(".winnerButton").removeClass(".winnerButtonDown");
        }
    });
    
    $("#header2010").click(function(e)
  {
    e.preventDefault();
      if ($("#2010entries").is(":hidden") ) 
        {
          $(".entries").hide();
          $("#header2010 .winnerButton .plus").hide();
          $("#header2010 .winnerButton .minus").show();
          
            $("#2010entries").show();
            $(".winnerButton").addClass(".winnerButtonDown");
        }
    else 
      { 
          $(".winnerButton .minus").hide();
          $(".winnerButton .plus").show();

        $(".entries").hide(); 
            $(".winnerButton").removeClass(".winnerButtonDown");
      }
    });
      
    $("#header2008").click(function(e){
    e.preventDefault();
      if ($("#2008entries").is(":hidden") ) 
        {
          $(".entries").hide();
          $("#header2008 .winnerButton .plus").hide();
          $("#header2008 .winnerButton .minus").show();
            $("#2008entries").show();
            $(".winnerButton").addClass(".winnerButtonDown");
        }
    else 
      { 
        $(".entries").hide(); 
          $(".winnerButton .minus").hide();
          $(".winnerButton .plus").show();
            $(".winnerButton").removeClass(".winnerButtonDown");
      }
      });
    
    $("#header2007").click(function(e){
    e.preventDefault();
      if ($("#2007entries").is(":hidden") ) 
        {
          $("#header2007 .winnerButton .plus").hide();
          $("#header2007 .winnerButton .minus").show();

          $(".entries").hide();
            $("#2007entries").show();
            $(".winnerButton").addClass(".winnerButtonDown");
        }
    else 
      { 
          $(".winnerButton .minus").hide();
          $(".winnerButton .plus").show();
        $(".entries").hide(); 
            $(".winnerButton").removeClass(".winnerButtonDown");
      }
      });
    
    $("#header2006").click(function(e){
    e.preventDefault();
      if ($("#2006entries").is(":hidden") ) 
        {
          $("#header2006 .winnerButton .plus").hide();
          $("#header2006 .winnerButton .minus").show();

          $(".entries").hide();
            $("#2006entries").show();
            $(".winnerButton").addClass(".winnerButtonDown");
        }
    else 
      { 
          $(".winnerButton .minus").hide();
          $(".winnerButton .plus").show();

        $(".entries").hide(); 
            $(".winnerButton").removeClass(".winnerButtonDown");
      }
      });
    
    $("#header2005").click(function(e){
    e.preventDefault();
      if ($("#2005entries").is(":hidden") ) 
        {
          $("#header2005 .winnerButton .plus").hide();
          $("#header2005 .winnerButton .minus").show();

          $(".entries").hide();
            $("#2005entries").show();
            $(".winnerButton").addClass(".winnerButtonDown");
        }
    else 
      { 
          $(".winnerButton .minus").hide();
          $(".winnerButton .plus").show();
        $(".entries").hide(); 
            $(".winnerButton").removeClass(".winnerButtonDown");
      }
      });
    
  //ajax load for past winners
    $(".winnerThumbWrap a").click(function(e){
    e.preventDefault();

    formatLink = function()
    {
      //give first image link active state
      $(".winnerImageSelector a:first").addClass("active");
    }; 

    $("#winnerBox").show().load(this.href,formatLink);
    });

  //manual slideshow for previous winner galleries
  $(".winnerImageSelector a").livequery('click', function(event)
  {
    //dont go anywhere
    event.preventDefault();   

    //find the selected image number
    newImage = $(this).attr("id").replace("image-","");   
    //replace the _X.jpg in the filename with the new number
    newImagePath = $(".winnerImage img").attr("src").replace(/\d.jpg/g,newImage+".jpg");    
    //swap paths
    $(".winnerImage img").attr("src",newImagePath);
    
    //toggle highlighting
    $(".winnerImageSelector a").not($(this)).removeClass("active");
    $(this).addClass("active");   
    });

  //colorbox slideshow for media galleries
  $("a[rel='mediaLocal']").colorbox();
  $("a[rel='mediaInt']").colorbox();
  
  //contact form
    var contactOptions =
  {
        success:   showContactResponse
  }
  function showContactResponse(data)
  {
    if (data){
      $("#contactResponse").html(data);
      $("#contForm").hide();
      $("#contactResponse").show();
    }
  };
  $("#contactform").ajaxForm(contactOptions);
  
  //register form
  $(".trigger-pro").click(function(event) 
  {
    $("#instructions").hide();
    $("#proEntry").show();
    $("#stuEntry").hide();    
    $(".trigger-pro").addClass("active");
    $(".trigger-stu").removeClass("active");
    $("#bi_type").val("P");
    $("#reg").addClass("visibles");   
    $("#reg").show();
  });
  
  $(".trigger-stu").click(function(event) 
  {
    $("#instructions").hide();
    $("#proEntry").hide();
    $("#stuEntry").show();    
    $(".trigger-stu").addClass("active");
    $(".trigger-pro").removeClass("active");    
    $("#bi_type").val("S"); 
    $("#reg").addClass("visibles");         
    $("#reg").show();
  }); 

  //enter form
    var enterOptions =
  {
        success:   showEnterResponse
  }
  function showEnterResponse(data)
  {
    if (data){
      $("#reg").hide();
      var target_offset = $("#register").offset();
      var target_top = target_offset.top;    
      $('html, body').animate({scrollTop:target_top }, 1000);        
      $("#instructions").html(data);
      $("#instructions").show();
    }
  };
  $("#enter").ajaxForm(enterOptions); 
  
  $(".trigger-print").click(function(event) 
  {
    event.preventDefault();
    $("#instructions").load($(this).attr("href"));    
  });
  
  $(".trigger-lost").livequery('click', function(event)
  {
    event.preventDefault();
    $("#instructions").load($(this).attr("href"));
  });
});

