$(function() {
  //used in header
  setTopNav();
  //used on home page
  setSlider();
  //no more IE6!
  ie6nomore()
  if($.facybox) {
    $('a[rel*=facybox]').facybox();
    funkshun();
    flash();
  }
  $('#toplink a').click(function() {
    $('html, body').animate({scrollTop: 0}, 1000);
    return false;
  });
  setPageClick();
  buttonPush('.service a');
  buttonPush('#signup');
  buttonPush('#submit-btn');
  $('.seemore').mousedown(function() {
    $(this).css({'background-position': '0px -25px'});
  });
  //used for forms
  setForm();
  bloglink();
});
function ie6nomore() {
  //Thanks to David at http://www.ie6nomore.com/
  var msg = "<div style='border: 1px solid #F7941D; background: #FEEFDA; text-align: center; clear: both; height: 75px; position: relative;'><div style='position: absolute; right: 3px; top: 3px; font-family: courier new; font-weight: bold;'><a href='#' onclick='javascript:this.parentNode.parentNode.style.display=\"none\"; return false;'><img src='http://www.ie6nomore.com/files/theme/ie6nomore-cornerx.jpg' style='border: none;' alt='Close this notice'/></a></div><div style='width: 640px; margin: 0 auto; text-align: left; padding: 0; overflow: hidden; color: black;'><div style='width: 75px; float: left;'><img src='http://www.ie6nomore.com/files/theme/ie6nomore-warning.jpg' alt='Warning!'/></div><div style='width: 275px; float: left; font-family: Arial, sans-serif;'><div style='font-size: 14px; font-weight: bold; margin-top: 12px;'>You are using an outdated browser</div><div style='font-size: 12px; margin-top: 6px; line-height: 12px;'>For a better experience using this site, please upgrade to a modern web browser.</div></div><div style='width: 75px; float: left;'><a href='http://www.firefox.com' target='_blank'><img src='http://www.ie6nomore.com/files/theme/ie6nomore-firefox.jpg' style='border: none;' alt='Get Firefox 3.5'/></a></div><div style='width: 75px; float: left;'><a href='http://www.browserforthebetter.com/download.html' target='_blank'><img src='http://www.ie6nomore.com/files/theme/ie6nomore-ie8.jpg' style='border: none;' alt='Get Internet Explorer 8'/></a></div><div style='width: 73px; float: left;'><a href='http://www.apple.com/safari/download/' target='_blank'><img src='http://www.ie6nomore.com/files/theme/ie6nomore-safari.jpg' style='border: none;' alt='Get Safari 4'/></a></div><div style='float: left;'><a href='http://www.google.com/chrome' target='_blank'><img src='http://www.ie6nomore.com/files/theme/ie6nomore-chrome.jpg' style='border: none;' alt='Get Google Chrome'/></a></div></div></div>";
  if($.browser.msie && $.browser.version < 7) {
    $('body').prepend(msg);
  }
}
function setTopNav() {
  $('#active-arrow').css({
    'left': $('#topnav a.current').position().left + ($('#topnav a.current').width() + 11) / 2
  });
  $('#nav > li > a').mouseover(function() {
    var offset = $(this).position().left + ($(this).width() + 11) / 2
    $('#active-arrow').stop().animate({left: offset}, 200);
  });
  $('#nav > li > a').mouseout(function() {
    var offset = $(this).position().left + ($(this).width() + 11) / 2
    $('#active-arrow').stop().animate({left: offset}, 200);
  });
  $(".drop").hover(
    function(){ $("ul", this).stop(true, true).slideDown('slow'); }, 
    function(){ $("ul", this).slideUp("slow"); } 
  );
  $('#topnav').mouseleave(function() {
    $('#active-arrow').stop().animate({left: $('#topnav a.current').position().left + ($('#topnav a.current').width() + 11) / 2}, 100, function() {
      $('.drop ul').slideUp('fast');
    });
    return false;
  });
}
function setSlider() {
  if($('#slide-images').length > 0) {
    $('#slide-images').cycle({
        fx:'fade',
        speed: 'fast',
        timeout: 6000,
        pause:1,
        pager: '#pager',
        pagerAnchorBuilder: function(idx, slide) {
  				return '#pager a:eq(' + idx + ')';
  			}
    });
  }
}
function setForm() {
  if($('#contactform').length > 0) {
    $('#contactform').validate();
  	$('#key').val(generateKey());
  	$('#signup').click(function() {
  		if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
  			var $target = $(this.hash);
  			$target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
  			if ($target.length) {
  				var targetOffset = $target.offset().top -16;
  				$('html,body').animate({scrollTop: targetOffset}, 1000, 'easeOutBack', function() {
  					$('#contactform p').animate({opacity: 0.3}, 600).animate({opacity: 1}, 600);
  				});
  				return false;
  			}
  		}
  	});
  }
}
function bloglink() {
  $('#bloglink').bind("mouseenter", function() {
    $(this).append('<a rel="close" href="javascript:hideBlog()" style="display:block;color:red;">[hide]</a>');
  });
  $('#bloglink').bind("mouseleave", function() {
    $('a[rel*=close]', this).remove();
  });
}
function hideBlog() {
  $('#bloglink').animate({left: "-100px"}, 3000);
}
function setPageClick() {
  $('#portclick').click(function() {
    var targetOffset = $('#portfoliowrap').position().top - 10;
    $('html,body').animate({scrollTop: targetOffset}, 1000, 'easeOutBack');
  });
  $('#servclick').click(function() {
    var targetOffset = $('#service-wrap').position().top - 10;
    $('html,body').animate({scrollTop: targetOffset}, 1000, 'easeOutBack');
  });
  $('#aboutclick').click(function() {
    var targetOffset = $('#team').position().top - 10;
    $('html,body').animate({scrollTop: targetOffset}, 1000, 'easeOutBack');
  });
}
function buttonPush(element) {
  $(element).mousedown(function() {
    $(this).css({'background-position': '0px 0px'});
  });
  $(element).mouseup(function() {
    $(this).css({'background-position': '0px -39px'});
  });
}
function switchTabs() {
  $('#port-links-nav span a').bind('click', function() {
    var link = $(this).parents('span:first');
    var target = link.attr("rel");
    $('#port-links-nav span').show();
    $(link).hide();
    $('.portfolio').hide();
    $('#' + target).show();
    $('div[rel='+target+']').show();
    $('#port-header h1 span').text($(this).text());
    return false;
  });
}
function play() {
  $.facybox({
    ajax: 'winmine/index.html'
  });
  $(this).remove();
}
function end() {
  $('.gametime').remove();
}
function funkshun() {
  if(window.addEventListener) {
    var kkeys = [], code = "38,38,40,40,37,39,37,39,66,65";
    window.addEventListener("keydown", function(e){
      kkeys.push(e.keyCode);
      if(kkeys.toString().indexOf(code) >= 0) {
        $('body').append('<div class="gametime"><a href="#" rel="facybox" onclick="play();">Play Game</a> or <a href="#" onclick="end()">Quit</a></div>');
        kkeys = [];
      }
    }, true);
  }
}
function generateKey() {
  var key = '';
  for(var i=0;i<10;i++) {
    key += Math.floor(Math.random()*101) + 10;
    key += String.fromCharCode(97 + Math.round(Math.random() * 25));
  }
  return key;
}
function flash() {
  if(window.location.href.indexOf("?") != -1) {
    if(window.location.search == "?success") {
      $.facybox("Thank you! We will contact you soon.");
    } else if(window.location.search == "?fail") {
      $.facybox("Seems there was an error submitting your request...Do you like green eggs and spam?");
    } else {
      $.facybox();
    }
  } else {
    '';// don't do anything
  }
}
