//
// Epic Games Javascript
//
// Author: nGen Works (http://ngenworks.com)
//

function stringLinks(text) {
  var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
  return text.replace(exp,"<a href='$1'>$1</a>");
}

function encodeQuotes(str) {
  str=str.replace(/\'/g,'&apos;');
  str=str.replace(/\"/g,'&quot;');
  return str;
}

function twitterUserLinks(text) {
  var exp = /(^|[^a-z0-9_])@([a-z0-9_]+)/ig;
  return text.replace(exp,"$1<a href=\'http://twitter.com/$2\'>\@$2</a>");
}

//
// Hide and show the text in a form field on focus on blur, respectivley.
function clear_it(object){
  object.each(function(){
    $(this).bind("focus", function(){
      var orig_text = $(this).val();
      $(this).val('');
      $(this).bind("blur", function(){
        orig_text = ($(this).val() !== "") ? $(this).val() : orig_text ;
        $(this).val(orig_text);
      });
    });
  });
}

// Add/remove the windowshade
function windowshade(show_hide) {
    if(show_hide) {
        $(".wrapper-home").prepend("<div id='windowshade'></div>");
    $("#windowshade").width( $(document).width() );
    $("#windowshade").height( $(document).height() );
    $("#windowshade").show();
    } else {
        $("#windowshade").hide();
        $("#windowshade").remove();
    }
}
//

//
// Show an item's video
function show_video(video_id) {
  hide_videos();
  pause_video();
  
  $(video_id).show();
  
  play_video(video_id);
}
//

//
function play_video(video_id) {
  video_id = video_id.substring(1);
  video = document.getElementById(video_id);
  //alert('playing: ' + video_id);
  if(typeof video.play == 'function') {
    video.play();
  }
}
//

//
function pause_video() {
  //video_id = video_id.substring(1);
  videos = $('.video-sd, .video-hd');
  videos.each(function() { video = document.getElementById( $(this).attr('id') ); if(typeof video.pause == 'function') { video.pause(); } } );
}
//

//
function hide_videos() {
  $('.video-sd, .video-hd').children('object').each(function() { 
        //alert( $(this).attr('id') );
        vidplayer = document.getElementById( $(this).attr('id') );
        if(typeof vidplayer.sendEvent == 'function') {
          vidplayer.sendEvent("STOP");
        }
    });
  
  $('.video-sd, .video-hd').hide();
}
//

// Move HD videos up in the DOM
function move_hd() {
  $('.video-hd').each(function() { video = $(this).detach(); $('.wrapper-home').prepend(video); });
}
//

var player;
function playerReadyCallback(obj) {
    //alert('the videoplayer '+obj['id']+' has been instantiated');
    player = document.getElementById(obj['id']);
};

$(document).ready(function() {
  
  // Clear it on contact fields
  //clear_it( $("body#contact form input, body#contact form textarea") );
  
  // Dropdown footer centering
  $(".drpdwn").each(function() {
    d_width = $(this).width()/2;
    
    $(this).css('margin-left', '-' + d_width + 'px');
  });
  
  // Hide video windowshade on click
  $('#windowshade').live('click', function(e) { pause_video(); hide_videos(); windowshade(false); });
 


	// Job Category Selector
	$("#job-categories a").click(function() {
		$("#job-categories .current").removeClass('current');
		$(this).parent().addClass('current');
		
		$('.job-full').hide();		
		
		

		var pCat = $(this).parent().attr('id');
		
		
		$('li.jobs-divide').show();
		$('li.jobs-divide:not(.' + pCat + ')').hide();  		
		
		return false;
	});
	
	// Job Description Slider
	$(".job-title a").click(function(){
		$(this).parent().next('.job-full').slideToggle('fast');
		return false;
	});
 

	//Hero Galleries
	$("#gallery-nav").fadeOut();
	$("#gallery").hover(function() {
		$("#gallery-nav").show();
		$("#caption").fadeIn();
		},
		function() {
		$("#gallery-nav").fadeOut('fast');
		$("#caption").fadeOut();
	});

	if(jQuery().cycle) {
  	$("#slides").cycle({
  		fx: 'fade',
  		pause: 5,
  		prev: '#prev',
  		next: '#next',
  		before: function() {
  					var caption = $(this).children().attr('alt');
  		            $('#caption').html(caption).fadeIn();
  		        }
  	});
	};
	
	// lightbox using Colorbox
	if(jQuery().colorbox) {
  	$('.tech-gallery li a').colorbox();
	$('.enlarged a').colorbox();
  }

});
