var Lightbox = (function() {
        var locked = 0;
	var showing = false,
		newwidth = 490;
	var $lightbox, $inner, $ajax;
	
	$(document).ready(function() {	

		$lightbox = $('#lightbox');
		$inner = $('#lightbox_inner');
		$ajax = $('#lightbox_ajax');
		
		$('#lightbox_x, #lightbox').click(hide);
		$inner.click(function(e) {e.stopPropagation();});
		
		$('a.lightbox').click(linkClicked);
    /*$('input.lightbox').click(function(e) {
      runPost($(this).parent().attr('action'),$(this).parent().serialize());
      return false;
    });*/
		$(window).scroll(function(e) {
			if(showing && $inner.outerHeight() < $(window).height() - 75)
		    	$inner.stop().animate({ marginTop : $(window).scrollTop() + 75 });
		});

	});
	function linkClicked(e) {
		$stopSlideShow = 1;
		var $this = $(this);
		run($this.attr('href'));
		if(matches = $this.attr('class').match(/width[0-9]+/))
			newwidth = parseInt(matches[0].replace('width',''));
		else
			newwidth = 490;
		return false;
	}

	function run(url) {
		$('#lightbox_ajax').load(url, function(response, status, xhr) {
			if(status == 'success') {
				$('a.lightbox').unbind('click').click(linkClicked);
				show();
			} else
				alert(status);
		});
	}
	function runPost(url,values) {
		/*
		 *  TO WHOM IT MAY CONCERN
		 *  Hey broseph!  You need to return this in the object
		 *  at the bottom to expose this function to the outside world!
		 *
		 *  Then call it with "Lightbox.runPost(...)"
		 */
		$.post(url, values, function(data) {
			  //$inner.empty().append( data );
			  show();
		});
	}
	function formSubmit(form) {
		newwidth = 490;
		queryString = $(form).serialize();		
		$.ajax({
			url : form.action,
			data : queryString,
			success : function(data) {
				form.reset();
				$('.submitted').removeClass('submitted');
				$('a.lightbox').unbind('click').click(linkClicked);
				$('#lightbox_ajax').html(data);
				show();
			},
			cache : false,
			type : form.method
		});
		return false;
	}
	function show() {
                setDynamicBackgroundHeight();
                $lightbox.css({opacity:0});
                $('#lightboxbg').css({opacity:0});
                $('#lightboxbg').stop().css({
			display : 'block'
		}).animate({ opacity : 0.5 }, {
			queue : false,
			duration : 1000
		});

    		$inner.css({
			marginTop : $(window).scrollTop() + 75,
			width : newwidth
		});
		$lightbox.stop().css({
			display : 'block'
		}).animate({ opacity : 1.0 }, {
			queue : false,
			duration : 1000,
			complete : function() {
				$inner.find('.youtube_frame').css('visibility', 'visible');
				styleInput();
                                setDynamicHeight();
			}
		});
		$inner.find('.youtube_frame').css('visibility', 'hidden');
		showing = true;

        ///////////////////////////////////////////////////

        var options = { 
          success:       showResponse  // post-submit callback 
        }; 
                
        $('.careerltr').blur(function() {
          var x = $(this).html();
          $('#coverltr').val(x);
        });

        $('#careerform').ajaxForm(function(){});
        $('#careerbutton').click(function() { 
          if(locked==1) return;
          locked = 1;
          $(this).css({ opacity: 0.5 });
          var first_name = $("#careerfname").val();
          var last_name = $("#careerlname").val();  
          var email = $("#careeremail").val();  
          var phone = $("#careerphone").val(); 
          var cover_letter = $(".careerltr").html();  
          var job_title = $("#career").val();
          var file = $("#file").val();

	  if(first_name != "First name *")
	    makeCGrey('.careerfname');
	  if(last_name != "Last name *")
	    makeCGrey('.careerlname');
	  if(email != "Email *")
	    makeCGrey('.careeremail');
	  if(phone != "Phone number *")
	    makeCGrey('.careerphone');
	  if(file != "")
	    makeCustomFileGrey('.customfile');
	  if(cover_letter != "Your cover letter *")
	    makeCGrey('.careerltr');

	  if(first_name == "First name *"){
            makeCRed('.careerfname');
            $('#mce-error-response3').html("Please enter your first name.");
            locked = 0;
    	    $(this).css({ opacity: 1.0 });
    	    return;
  	  }
	  else if(last_name == "Last name *"){
            makeCRed('.careerlname');
   	    $('#mce-error-response3').html("Please enter your last name.");
            locked = 0;
    	    $(this).css({ opacity: 1.0 });
    	    return;
  	  }
	  else if(email == "Email *"){
            makeCRed('.careeremail');
   	    $('#mce-error-response3').html("Please enter your email.");
            locked = 0;
    	    $(this).css({ opacity: 1.0 });
    	    return;
  	  }
	  else if(phone == "Phone number *"){
            makeCRed('.careerphone');
   	    $('#mce-error-response3').html("Please enter your phone number.");
            locked = 0;
    	    $(this).css({ opacity: 1.0 });
    	    return;
  	  }
	  else if(file == ""){
            makeCustomFileRed('.customfile');
   	    $('#mce-error-response3').html("Please select your resume.");
            locked = 0;
    	    $(this).css({ opacity: 1.0 });
    	    return;
  	  }
	  else if(cover_letter == "Your cover letter *"){
            makeCRed('.careerltr');
   	    $('#mce-error-response3').html("Please enter your cover letter.");
            locked = 0;
    	    $(this).css({ opacity: 1.0 });
    	    return;
  	  }
          else{
            $('#mce-error-response3').html("");
            // submit the form 
            $('#careerform').ajaxSubmit(options);

            $('#careerbutton').addClass("disabled");
            // return false to prevent normal browser submit and page navigation
            return false;
          }
});
function showResponse(responseText, statusText, xhr, $form)  { 
    // for normal html responses, the first argument to the success callback 
    // is the XMLHttpRequest object's responseText property 
 
    // if the ajaxSubmit method was passed an Options Object with the dataType 
    // property set to 'xml' then the first argument to the success callback 
    // is the XMLHttpRequest object's responseXML property 
 
    // if the ajaxSubmit method was passed an Options Object with the dataType 
    // property set to 'json' then the first argument to the success callback 
    // is the json data object returned by the server 

    if(responseText == '1'){
      $('#careerbutton').hide();
      locked = 0;
      $('#mce-success-response3').show();
      $('#mce-success-response3').html("Thank you for your submission");
      hideslow();
    }
    else{
      $('#careerbutton').css({ opacity: 1.0 });
      $('#careerbutton').removeClass("disabled");
      locked = 0;
      $('#mce-error-response3').html("An error occurred when sending email (" + responseText  + ")");
    }
} 
	}
  
  function setDynamicHeight(){
    if(($('#lightbox_ajax').height() + ($(window).scrollTop() + 75)) > $('body').height()){
      $lightbox.css({height : $('body').height() + ( ($('#lightbox_ajax').height() + ($(window).scrollTop() + 75)) - $('body').height() ) + 30});
      $('#lightboxbg').css({height : $('body').height() + ( ($('#lightbox_ajax').height() + ($(window).scrollTop() + 75)) - $('body').height() ) + 30});
    }
    else{
      $lightbox.css({height : $('body').height() + 30});
      $('#lightboxbg').css({height : $('body').height() + 30});
    }
  }
  function setDynamicBackgroundHeight(){
    if(($('#lightbox_ajax').height() + ($(window).scrollTop() + 75)) > $('body').height()){
      $('#lightboxbg').css({height : $('body').height() + ( ($('#lightbox_ajax').height() + ($(window).scrollTop() + 75)) - $('body').height() ) + 30});
    }
    else{
      $('#lightboxbg').css({height : $('body').height() + 30});
    }
  }
  
	function hide() {
                $('#lightboxbg').stop().animate({ opacity : 0 }, {
			queue : false,
			duration : 1000,
			complete : function() {
				$('#lightboxbg').css('display', 'none');
			}
		});

		$lightbox.stop().animate({ opacity : 0 }, {
			queue : false,
			duration : 1000,
			complete : function() {
				$lightbox.css('display', 'none');

			}
		});
		$inner.find('.youtube_frame').css('visibility', 'hidden');
		showing = false;
	}
	return {
		'show' : show,
		'hide' : hide,
		'formSubmit' : formSubmit
	};
	function hideslow() {
                $('#lightboxbg').stop().animate({ opacity : 0 }, {
			queue : false,
			duration : 4000,
			complete : function() {
				$('#lightboxbg').css('display', 'none');
			}
		});
		$lightbox.stop().animate({ opacity : 0 }, {
			queue : false,
			duration : 4000,
			complete : function() {
				$lightbox.css('display', 'none');
			}
		});
		$inner.find('.youtube_frame').css('visibility', 'hidden');
		showing = false;
	}
	return {
		'show' : show,
		'hide' : hide,
		'formSubmit' : formSubmit
	};
})();



function styleInput(){
				if($('#file').length == 1){
      	       			  enhance({
	        		  loadScripts: [
				    'http://avinger.com/index.php/incl/custom_file_input_js_fileinput',
				    'http://avinger.com/index.php/incl/custom_file_input_js_function',
				    'http://avinger.com/index.php/incl/jquery_form'
				  ],
				  loadStyles: ['http://avinger.com/index.php/incl/custom_file_input_css']	
      				  });
				}
}
