$( document ).ready( function()
{
	$( "#main-nav li" ).hover( 
		function() { $( this ).addClass( "hover" ); },
		function() { $( this ).removeClass( "hover" ); }
	);

	$( "a.enlarge" ).click( function( e )
	{
		Enlarge( $( this ).attr( "href" ), $( this ).siblings( "img.enlarge" ).attr( "alt" ), $( this ).siblings( "img.enlarge" ) );
		e.preventDefault();
	});

	$( "img.enlarge" ).click( function( e )
	{
		Enlarge( $( this ).siblings( "a.enlarge" ).attr( "href" ), $( this ).attr( "alt" ), $( this ) );
		e.preventDefault();
	});

	$( "a.download" ).click( function( e )
	{
		Download( $( this ).attr( "href" ), $( this ).attr( "title" ) );
		e.preventDefault();
	});

	$( ".lightbox a:not( .doc )" ).click( function( e )
	{
		e.preventDefault();

		//window.parent.OnCloseClick( $( "div.pop_box", window.parent.document ) );
		window.parent.location = $( this ).attr( "href" );
	});
	
	$( "#footer select" ).change( function ( e )
	{
		$( "#site-list > option:selected" ).each( function ()
		{
			document.location.href = $( this ).attr( "value" );
		});
	});

	$( "*[rel=external]" ).each( function()
	{
		$( this ).attr( "target", "external" );
	});
});

function Enlarge( imgSrc, imgAlt, source )
{
	var inner = CreatePopup( source.offset().left, ( source.offset().top - $( document ).scrollTop() ), "enlargebox" );

	// Setup popup inside elements

	var container = $( "<div class=\"enlarge-box lightbox\" />" );

	var title = $( "<h1 />" );
	title.html( imgAlt );

	var img = new Image();
	img.onload = function()
	{
		$( this ).fadeIn( "slow" );
		PositionPopup( $( "#enlargebox" ), "auto", "auto", this.width, this.height + title.height(), true );
	};
	img.src = imgSrc;
	
	container.append( title );
	container.append( $( img ) );

	inner.append( container );
}

function Download( link, title )
{
	// Generate data

	var data = urlencode( base64_encode( link + "----" + title + "----" ) ).replace( "%", "_" );

	// Setup base for lightbox

	var inner = CreatePopup( "auto", "auto", "downloadbox" );

	// Setup popup inside elements

	var iframe = $( "<iframe />" );

	iframe.attr( "id",			"downloadbox-iframe" );
	iframe.attr( "src",			"/download/form/" + data );
	iframe.attr( "frameBorder",	"0" );		// for IE's benefit, and capital B for IE6
	iframe.css( "display",		"block" );
	iframe.css( "position",		"absolute" );

	inner.append( iframe );
}

function OnSubmit( form, success )
{
	if( success )
	{
		switch( form.attr( "name" ) )
		{
			case "contact-us":
				Contact( "../xml/xmlCorporateFormPost.aspx", "thanks/index.html", "TelaPoint Contact Form" );
			break;
			case "download":
				Contact( "../xml/xmlCorporateFormPost.aspx", "thanks/" + form.find( "input#doc-encoded-data[type='hidden']" ).val(), "TelaPoint Contact Form"  );
			break;
		}
	}

	return( false );
}

function Contact( sendURL, doneURL, subject )
{
	var xml = "";

	xml += "<info>\n";
	xml += "	<first-name><![CDATA[" + $( "input#form-first-name" ).val() + "]]></first-name>\n";
	xml += "	<last-name><![CDATA[" + $( "input#form-last-name" ).val() + "]]></last-name>\n";
	xml += "	<city><![CDATA[" + $( "input#form-city" ).val() + "]]></city>\n";
	xml += "	<stateprovince><![CDATA[" + $( "select#form-stateprovince" ).val() + "]]></stateprovince>\n";
	xml += "	<company><![CDATA[" + $( "input#form-company" ).val() + "]]></company>\n";
	xml += "	<title><![CDATA[" + $( "input#form-title" ).val() + "]]></title>\n";
	xml += "	<phone><![CDATA[" + $( "input#form-phone" ).val() + "]]></phone>\n";
	xml += "	<fax><![CDATA[" + $( "input#form-fax" ).val() + "]]></fax>\n";
	xml += "	<email><![CDATA[" + $( "input#form-email" ).val() + "]]></email>\n";
	xml += "	<retail-sites><![CDATA[" + $( "input#form-retail-sites" ).val() + "]]></retail-sites>\n";
	xml += "	<bulk-sites><![CDATA[" + $( "input#form-bulk-sites" ).val() + "]]></bulk-sites>\n";
	xml += "	<help><![CDATA[" + $( "select#form-help" ).val() + "]]></help>\n";
	xml += "	<interest><![CDATA[" + $( "select#form-interest" ).val() + "]]></interest>\n";
	xml += "	<type><![CDATA[" + $( "select#form-type" ).val() + "]]></type>\n";
	xml += "	<promo-code><![CDATA[" + $( "input#form-promo-code" ).val() + "]]></promo-code>\n";

	if( $( "input#title[type='hidden']" ).length > 0 )
	{
		xml += "	<document-title><![CDATA[" + $( "input#title[type='hidden']" ).val() + "]]></document-title>\n";
	}

	if( $( "input#link[type='hidden']" ).length > 0 )
	{
		xml += "	<document-link><![CDATA[" + $( "input#link[type='hidden']" ).val() + "]]></document-link>\n";
	}

  xml += "	<subject><![CDATA[" + subject + "]]></subject>\n";

	xml += "</info>\n";

	$( "input#form-clear" ).attr( "disabled", "true" );
	$( "input#form-submit" ).attr( "disabled", "true" );

	$.ajax(
	{
		type: "POST",
		url: sendURL,
		dataType: "xml",
		processData: false,
		data: "d=" + urlencode( ( xml.length > 0 ? base64_encode( xml ) : "" ) ),
		success: function( xmlResponse ) { ContactContinued( xmlResponse, doneURL ); }
	});
}

function ContactContinued( xmlResponse, doneURL )
{
	var success = $( "success", xmlResponse ).text();
  if (success == "1")
  {
	  window.location = doneURL;
  }
  else
  {
	var error = $( "error", xmlResponse ).text();
    alert(error);
  }
}
