$(document).ready(function()
{	
	init();
	contact_init();
});

this.init = function() 
{
	pngfix();
	
	$("#dropDown").dropdown('');
	
	$(window).resize(function() 
	{
    positionDropdown();
  });
  
  $("#nav ul li").each(function() 
  { 
    $(this).mouseover(function()
    {
      $(this).addClass("highlight"); 
    });
    
    $(this).mouseout(function()
    {
      $(this).removeClass("highlight"); 
    });      
  });

};

function contact_init() 
{

  // set the widths for the nav items
  $('#home').css("width","70px");        
  $('#approach').css("width","110px");
  $('#team').css("width","90px");
  $('#clients').css("width","90px");
  $('#contact').css("width","90px");

  // change the background of nav items
  var thispage = jQuery.url.attr("path");

  if (thispage == "/")
  {
    $('#home').addClass("selected");
    $("#home_img").attr("src","/images/nav_home_over.png");
  }
  else if (thispage == "/approach/")
  {
    $('#approach').addClass("selected");
    $("#approach_img").attr("src","/images/nav_approach_over.png");
  }
  else if (thispage == "/team/")
  {
    $('#team').addClass("selected");
    $("#team_img").attr("src","/images/nav_team_over.png");
  }
  else 
  {
    $('#clients').addClass("selected");
    $("#clients_img").attr("src","/images/nav_clients_over.png");
  }

  $('#form_div').show();
  
  // hide the email address from bots
  var email_form_address = $('#email_form_to');
  email_form_address[0].value = email_form_address[0].getAttribute('to') + '@' + email_form_address[0].getAttribute('at');
          
  var options = { 
      beforeSubmit:  validate,          // pre-submit callback 
      success:       showResponse,      // post-submit callback 
      clearForm:     true               // clear all form fields after successful submit   
  };

  // bind 'email_form' using 'ajaxForm'
  $('#email_form').ajaxForm(options);  

}

function positionDropdown()  
{
  
  var dropDownDiv = $("#dropDown");
  
  // calculate center for the dropdown
  var winWidth  = $(window).width();
  var divWidth = dropDownDiv.width();
  var winDiff  = winWidth - divWidth;
  var leftPos = 0;  
    
  if(winDiff/2 > 0)
  {
    leftPos = winDiff/2;
  }
  
  dropDownDiv.css("left", leftPos + "px");
  dropDownDiv.css("top", "0");
}

jQuery.fn.dropdown = function(dropText)
{
  var dropdown_obj = $(this);
  var dropped = false;
  
  dropdown_obj.css("overflow","hidden");
  
  // dynamically create a new div to 'wrap' the contact info div
  var container = document.createElement("div");
  container.className = "dropDownContainer";
  
  // dynamically add styles to the container div
  $(container).css("position", "absolute");
  $(container).css("top", "24px");
  $(container).css("z-index", 200);
  
  // insert the container div and append the contact info div inside it
  $(container).insertBefore(dropdown_obj);
  $(container).append(dropdown_obj);
  
  dropdown_obj.hide();
  
  $(".drop a").click(function()
  {   
    if(dropped == false)
    {
      dropped = true;
      dropContact();
    }
    else
    {
      dropped = false;
      undropContact();
    }  
  });
  
  // unset the select on the contact nav
  // reset the current page nav to selected
  $(".closeButton").click(function()
  {   
    undropContact();
  });
  
  // center the dropdown to the window
  positionDropdown();
  
};

this.pngfix = function() 
{
	var ie55 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 5.5") != -1);
	var ie6 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 6.0") != -1);
	if (jQuery.browser.msie && (ie55 || ie6)) 
	{		
		$("*").each(function(){
			var bgIMG = $(this).css('background-image');
			if(bgIMG.indexOf(".png")!=-1){
				var iebg = bgIMG.split('url("')[1].split('")')[0];
				$(this).css('background-image', 'none');
				$(this).get(0).runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + iebg + "',sizingMethod='scale')";
			};
		});
	};		
};

function undropContact()
{
	$("#dropDown").slideToggle("slow");
	if (jQuery.browser.msie)
	{
  		$('#darken').css("display", "none");
	}
	else
	{
  		$('#darken').fadeOut('400');
	}
	$("#contact_img").attr("src","/images/nav_contact.png");
	clearNavStyles();
	contact_init();
}

function dropContact()
{
	$("#dropDown").slideToggle("300");
	if (jQuery.browser.msie)
	{
  		$('#darken').css("display", "block");
	}
	else
	{
		$('#darken').fadeIn('400');
	}
	$("#response_div").hide();
	$("#form_div").show();
	clearNavStyles();
	$("#contact").addClass("selected"); 
	$("#contact_img").attr("src","/images/nav_contact_over.png");
}

function clearNavStyles()
{
  $("#nav ul li").each(function () 
  { 
    $(this).removeClass("selected");
  });

  $("#home_img").attr("src","/images/nav_home.png");
  $("#approach_img").attr("src","/images/nav_approach.png");
  $("#team_img").attr("src","/images/nav_team.png");
  $("#clients_img").attr("src","/images/nav_clients.png");
}

function validate(formData, jqForm, options) 
{ 
  for (var i=0; i < formData.length; i++) 
  { 
    if (!formData[i].value) 
    { 
      alert('Please enter a value for all fields'); 
      return false; 
    } 
  } 
}



function showResponse(responseText, statusText)  
{ 
  // for normal html responses, the first argument to the success callback 
  // is the XMLHttpRequest object's responseText property 

  // if the ajaxForm 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 ajaxForm 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(statusText == "success" && responseText == "true")
  {
    $("#response_div").show();
    $("#form_div").hide();
  }
  else
  {
      alert('There was a problem delivering your email. Please ensure that all fields have valid information.'); 
  }
}


